def read_superputty_xml(self):
style = wx.FD_OPEN | wx.FD_FILE_MUST_EXIST
dialog = wx.FileDialog(self, message='Open Sessions.XML', wildcard='(*.XML)|*.XML', style=style)
if dialog.ShowModal() == wx.ID_OK:
file = dialog.GetPath()
else:
return False
dialog.Destroy()
try:
tree = ET.parse(file)
for item in tree.iter():
if item.tag == "SessionData":
sessionPath = item.attrib.get('SessionId')
list = sessionPath.encode('utf-8').split('/')
tmp = self.root
res = Resolver('name')
counter = 1
for i in list:
pathB64 = base64.b64encode(i)
try:
if res.get(tmp, pathB64):
tmp = res.get(tmp, pathB64)
if counter >= len(list):
print pathB64
self.saveSessionData(
node=tmp,
name=str(item.attrib.get('SessionName').encode('utf-8')),
username=str(item.attrib.get('Username').encode('utf-8')),
privateKey='',
hostname=str(item.attrib.get('Host').encode('utf-8')),
port=str(item.attrib.get('Port').encode('utf-8'))
)
print pathB64
except ChildResolverError as e:
if counter < len(list):
tmp = Node(pathB64, parent=tmp, type="Container")
if counter >= len(list):
self.saveSessionData(
node=tmp,
name=str(item.attrib.get('SessionName').encode('utf-8')),
username=str(item.attrib.get('Username').encode('utf-8')),
privateKey='',
hostname=str(item.attrib.get('Host').encode('utf-8')),
port=str(item.attrib.get('Port').encode('utf-8'))
)
counter = counter + 1
return True
except Exception as e:
return False
python类FD_OPEN的实例源码
def read_mobaxterm_ini(self):
style = wx.FD_OPEN | wx.FD_FILE_MUST_EXIST
dialog = wx.FileDialog(self, message='Open MobaXterm.ini', wildcard='(*.ini)|*.ini', style=style)
if dialog.ShowModal() == wx.ID_OK:
file = dialog.GetPath()
else:
return False
dialog.Destroy()
try:
config = configparser.RawConfigParser()
config.optionxform = str
config.read(file)
res = Resolver('name')
for s in config.sections():
if s.startswith('Bookmarks'):
if config[s]['SubRep'] == 'PuTTY sessions':
continue
tmp = self.root
for (key,val) in config.items(s):
if key == 'ImgNum':
continue
if key == 'SubRep' and val:
sessionPath = config[s]['SubRep']
list = sessionPath.encode('utf-8').split('\\')
counter = 1
for i in list:
pathB64 = base64.b64encode(i)
try:
if res.get(tmp, pathB64):
tmp = res.get(tmp, pathB64)
except ChildResolverError as e:
node = Node(pathB64, parent=tmp, type='Container')
tmp = node
counter = counter + 1
break
for (key,val) in config.items(s):
if key == 'ImgNum' or key == 'SubRep':
continue
sessionData = val.encode('utf-8').split('%')
if sessionData[0] == '#109#0':
self.saveSessionData(tmp, key, sessionData[3], '', sessionData[1], sessionData[2])
return True
except Exception as e:
return False