def getHitCount(self): """get the number of retrieved pages @return int the number of retrieved pages """ hits = False if self.__engine == "tsubaki": url = "http://tsubaki.ixnlp.nii.ac.jp/api.cgi?query=%s&only_hitcount=1" % self.__query force_dpnd = self.getParameter("force_dpnd") if force_dpnd: url += "&force_dpnd=" + str(force_dpnd) print url if self.is_available_caching(): res = self.runCacheFunc(url) else: res = urllib.urlopen(url).read() if not re.match("^[0-9]+$", res): return "error" hits = res.rstrip() elif self.__engine == "yahoo" or self.__engine == "yahoo2": if self.is_available_caching(): xmlstr = self.runCacheFunc(self.getResultURI(1, 1)) print xmlstr doc = ElementTree(fromstring(xmlstr)) else: url = self.getResultURI(1, 1) fd = file(url, "rb") doc = ElementTree(file=fd) e = doc.getroot() hits = e.attrib["totalResultsAvailable"] return hits
def search(self, start, max): """run searching Web""" engine = self.getEngine() result = {} rank = 1 if engine == "tsubaki": url = self.getResultURI(start, max) if self.is_available_caching(): xmlstr = self.runCacheFunc(url) doc = ElementTree(fromstring(xmlstr)) else: fd = file(url, "rb") doc = ElementTree(file=fd) for e in doc.findall("//Result"): tmp = { "title": e.find("Title").text, "url": e.find("Url").text, "snippet": e.find("Snippet").text, "cache": "http://tsubaki.ixnlp.nii.ac.jp/api.cgi?id=" + e.attrib["Id"] + "&format=html" } result[rank] = tmp rank += 1 return result
def getTree(self): "распарсить дерево" #parser = ET.XMLParser(encoding="windows-1251") #self.tree = ET.parse(self.filename, parser=parser) if self.xmlstring: self.tree = ET.ElementTree(ET.XML(self.xmlstring)) elif self.filename: self.tree = ET.parse(self.filename) else: self.tree = ET.ElementTree(ET.XML('<root></root>'))
def __init__(self, xmlstate=None, validatestate=True): self._model = FieldModel(self.modeltext) if xmlstate is not None: self._state = ET.ElementTree(ET.XML(xmlstate)) # If state is being pre-supplied, then it ought to validate, # unless the caller has specifically asserted that it does not: if validatestate: self.state_validate() else: self._state = ET.ElementTree(ET.Element('none'))
def __get__(self, document, owner): if document is None: return self if hasattr(document, '_element'): return document._element nodes = {} root = None for node in document._nodes: if node.parent_id is not None: parent = nodes[node.parent_id] elem = ElementTree.SubElement(parent, node.tag) nodes[node.element_id] = elem else: parent = None elem = root = ElementTree.Element(node.tag) nodes[node.element_id] = root for attr in node.attributes: elem.attrib[attr.name] = attr.value elem.text = node.text elem.tail = node.tail document._element = ElementTree.ElementTree(root) return document._element
def ppxml(self, xmlstring): # built in minidom's toprettyxml has an annoying habit of inserting newlines in text elements. # workaround sourced from http://stackoverflow.com/questions/749796/pretty-printing-xml-in-python def indent(elem, level=0): i = "\n" + level*" " if len(elem): if not elem.text or not elem.text.strip(): elem.text = i + " " if not elem.tail or not elem.tail.strip(): elem.tail = i for elem in elem: indent(elem, level+1) if not elem.tail or not elem.tail.strip(): elem.tail = i else: if level and (not elem.tail or not elem.tail.strip()): elem.tail = i root = ElementTree.parse(StringIO.StringIO(xmlstring)).getroot() indent(root) tree = ElementTree.ElementTree(root) pretty = StringIO.StringIO() tree.write(pretty) return pretty.getvalue()
def shows(): url = 'http://www.usanetwork.com/globalNav.xml' showsxml = getHTML(url) xml = ElementTree(fromstring(showsxml)) shows = [] for item in xml.getroot().findall('menu/item')[5]: show = [] show.append(item.get('url')) name = item.get('name').title() show.append(name) if name == 'Burn Notice': thumb = 'http://www.usanetwork.com/fullepisodes/images/bn.gif' elif name == 'Monk': thumb = 'http://www.usanetwork.com/fullepisodes/images/monk.gif' elif name == 'Psych': thumb = 'http://www.usanetwork.com/fullepisodes/images/psych.gif' elif name == 'In Plain Sight': thumb = 'http://www.usanetwork.com/fullepisodes/images/ips.gif' else: thumb = '' show.append(thumb) shows.append(show) #Need better way to get show list missing Starter Wife and couldn't find a feed for Dr Steve-O #show = [] #show.append('http://video.usanetwork.com/player/feeds/?level=743701&type=placement&showall=1') #show.append('Starter Wife') #show.append('http://www.usanetwork.com/fullepisodes/images/sw.gif') #shows.append('show') return shows
def loadAccount(self, email): accview = None self.createAccountFileTree(email) accpath = os.path.join(self.account_dir, "account.xml") accfile = file(accpath, "r") root_tree = ElementTree(file=accfile) accfile.close() account = root_tree.getroot() if account.tag == "aMSNAccount": #email emailElmt = account.find("email") if emailElmt is None: return None accview = AccountView(self._core, emailElmt.text) #nick nickElmt = account.find("nick") if nickElmt is None: return None if nickElmt.text: accview.nick.appendText(nickElmt.text) #TODO: parse... #psm psmElmt = account.find("psm") if psmElmt is None: return None if psmElmt.text: accview.psm.appendText(psmElmt.text) #presence presenceElmt = account.find("presence") if presenceElmt is None: return None accview.presence = presenceElmt.text #password passwordElmt = account.find("password") if passwordElmt is None: accview.password = None else: accview.password = passwordElmt.text #save_password savePassElmt = account.find("save_password") if savePassElmt.text == "False": accview.save_password = False else: accview.save_password = True #autoconnect saveAutoConnect = account.find("autoconnect") if saveAutoConnect.text == "False": accview.autologin = False else: accview.autologin = True #TODO: use backend & all #dp dpElmt = account.find("dp") #TODO #TODO: preferred_ui ? accview.save = True return accview
def loadStanzas(self, stanzaFiles, progressCB=None): # If files is really a single file, turn it into a list. if types.StringType == type(stanzaFiles): stanzaFiles = [ stanzaFiles, ] if not progressCB: progressCB = null_progress_cb num_files = len(stanzaFiles) for (i, f) in zip(xrange(num_files), stanzaFiles): file_name = os.path.abspath(f) if os.path.exists( file_name) and not self.mStanzas.has_key(file_name): progressCB(i / num_files, "Loading file: %s" % file_name) stanza_elm = ET.ElementTree(file=file_name).getroot() self.mStanzas[file_name] = stanza_elm # Store a digest to ensure we save changes. stanza_str = ET.tostring(stanza_elm) stanza_digest = md5.new(stanza_str).digest() self.mStanzaDigests[file_name] = stanza_digest self._expandCmdLine()
def updateConfig(self, gui): if(not os.path.isfile(self.configFile)): return False, util.localize(35003) tree = ElementTree().parse(self.configFile) if(tree == None): Logutil.log('Could not read config.xml', util.LOG_LEVEL_ERROR) return False, util.localize(35004) self.tree = tree configVersion = tree.attrib.get('version') Logutil.log('Reading config version from config.xml: ' +str(configVersion), util.LOG_LEVEL_INFO) if(configVersion == None): #set to previous version configVersion = '0.7.4' #nothing to do if(configVersion == util.CURRENT_CONFIG_VERSION): Logutil.log('Config file is up to date', util.LOG_LEVEL_INFO) return True, '' Logutil.log('Config file is out of date. Start update', util.LOG_LEVEL_INFO) #backup config.xml newFileName = self.configFile +'.backup ' +configVersion if not os.path.isfile(newFileName): try: shutil.copy(str(self.configFile), str(newFileName)) except Exception, (exc): return False, util.localize(35007) +": " +str(exc)
def parse(self, encoding=None, errors='strict', **kwargs): """Parses a settings file using ElementTree. @param encoding: encoding used by settings file @type encoding: string @param errors: Error handling scheme for codec. Same as C{.decode} inbuilt method. @type errors: string @param kwargs: Keyword arguments passed to L{StandardFormat.fields()} @type kwargs: keyword arguments dictionary @rtype: ElementTree._ElementInterface @return: contents of toolbox settings file with a nested structure """ builder = ElementTree.TreeBuilder() for mkr, value in self.fields(encoding=encoding, errors=errors, **kwargs): # Check whether the first char of the field marker # indicates a block start (+) or end (-) block = mkr[0] if block in ("+", "-"): mkr = mkr[1:] else: block = None # Build tree on the basis of block char if block == "+": builder.start(mkr, {}) builder.data(value) elif block == '-': builder.end(mkr) else: builder.start(mkr, {}) builder.data(value) builder.end(mkr) return ElementTree.ElementTree(builder.close())
def _assertSerialized(self, field, value, expected): element = utils.valueToElement(field, value, 'value') sio = StringIO() etree.ElementTree(element).write(sio) self.assertEquals(sio.getvalue(), expected) unserialized = utils.elementToValue(field, element) self.assertEquals(value, unserialized)
def load_config(self, account): """ @type account: L{amsn2.core.account_manager.aMSNAccount} """ c = aMSNConfig() c.set_key("ns_server", "messenger.hotmail.com") c.set_key("ns_port", 1863) configpath = os.path.join(self.accounts_dir, self._get_dir(account.view.email), "config.xml") configfile = None try: configfile = file(configpath, "r") except IOError: return c root_tree = ElementTree(file=configfile) configfile.close() config = root_tree.getroot() if config.tag == "aMSNConfig": lst = config.findall("entry") for elmt in lst: if elmt.attrib['type'] == 'int': c.set_key(elmt.attrib['name'], int(elmt.text)) else: c.set_key(elmt.attrib['name'], elmt.text) return c
def test_simple(self): div = Element('DIV') el = Element('P') div.append(el) el.text = u"Some contents" tree = ElementTree(div) tree.write(open('test.xml', 'w'))
def generate_xml(self, xml_out): id = 1 root = ET.Element("comentarios") if self.__type: for i in self.__vector_tagged_comments: if i[0] != "None": comentario = ET.SubElement(root, "comentario") id_comentario = ET.SubElement(comentario, "id") id_comentario.text = str(id) texto = ET.SubElement(comentario, "contenido") texto.text = i[0] polaridad = ET.SubElement(comentario, "polaridad") polaridad.text = i[1] id += 1 id = 1 if self.__type == False: for i in self.__vector_comments: if i != "None": comentario = ET.SubElement(root, "comentario") id_comentario = ET.SubElement(comentario, "id") id_comentario.text = str(id) texto = ET.SubElement(comentario, "contenido") texto.text = i id += 1 tree = ET.ElementTree(root) tree.write(xml_out)
def merge(file, langs): input_helper_name = os.path.join(work_dir, os.path.dirname(file), ".helper", os.path.basename(file)) input_file_name = os.path.join(work_dir, file) helper_document = ET.parse(input_helper_name).getroot() translated_document = ET.parse(input_file_name).getroot() merge_docs(helper_document, translated_document, langs) document = helper_document remove_ids(document) if not "no-format" in options: create_node_order_helper(document) format_xml(document) all_document = ET.ElementTree(document) all_document.write(file, "utf-8") #write copy with IDs and hash, so that when this file is #next splited, changed tags get marked with "changed" create_new_ids(document) all_document.write(input_helper_name, "utf-8") if not "no-delete-rip" in options: os.unlink(input_file_name)
def main(): """ """ advset = 'special://userdata/advancedsettings.xml' source = os.path.join(rootdir, 'resources', 'advancedsettings.xml') try: if os.path.isfile(advset): settings = get_screensaver_settings(sys.argv[2]) tree = ET.parse(advset) root = tree.getroot() if tree.findtext('screensaver'): newroot = update_screensaver_settings(root, settings) else: newroot = insert_screensaver_settings(root, settings) # wrap it in an ElementTree instance, and save as XML tree = ET.ElementTree(newroot) tree.write(advset, ENCODING) else: copy_advanced_settings(source, advset) main() #xbmc.executebuiltin('XBMC.ReloadSkin()') #xbmcgui.Dialog().ok( _(30013), _(30014),slideshowpath) if xbmcgui.Dialog().yesno(_(30013), _(30014), _(30015)): xbmc.executebuiltin('XBMC.RestartApp') except: return
def parseapiArtists(response): if response == False: return False tree1 = ElementTree(fromstring(response)) artists = [] for entry in tree1.getroot().findall('{%s}entry' % atom): artist = [] artist.append(entry.find('{%s}id' % atom).text) artist.append(entry.find('{%s}title' % atom).text) turl = [0, 'url'] for t in entry.findall('{%s}thumbnail' % media): url = t.get('url') width = t.get('width') height = t.get('height') w = int(width) h = int(height) pixels = w * h if pixels > turl[0]: turl[0] = pixels turl[1] = url if turl[1] <> 'url': artist.append(turl[1]) else: artist.append('') artists.append(artist) return artists
def parseapiVideos(response): if response == False: return False tree1 = ElementTree(fromstring(response)) videos = [] for entry in tree1.getroot().findall('{%s}entry' % atom): video = [] m = entry.find('{%s}content' % media) video.append(m.get('url').split('/')[-1]) name = entry.find('{%s}description' % media).text name = name.split('|')[0] + '-' + name.split('|')[1] video.append(name) turl = [0, 'url'] for t in entry.findall('{%s}thumbnail' % media): url = t.get('url') width = t.get('width') height = t.get('height') w = int(width) h = int(height) pixels = w * h if pixels > turl[0]: turl[0] = pixels turl[1] = url if turl[1] <> 'url': video.append(turl[1]) else: video.append('') videos.append(video) return videos
def exportTopicTreeSpecXml(moduleName=None, rootTopic=None, bak='bak', moduleDoc=None): """ If rootTopic is None, then pub.getDefaultTopicTreeRoot() is assumed. """ if rootTopic is None: from .. import pub rootTopic = pub.getDefaultTopicTreeRoot() elif py2and3.isstring(rootTopic): from .. import pub rootTopic = pub.getTopic(rootTopic) tree = ET.Element('topicdefntree') if moduleDoc: mod_desc = ET.SubElement(tree, 'description') mod_desc.text = ' '.join(moduleDoc.split()) traverser = pub.TopicTreeTraverser(XmlVisitor(tree)) traverser.traverse(rootTopic) indent(tree) if moduleName: filename = '%s.xml' % moduleName if bak: pub._backupIfExists(filename, bak) fulltree = ET.ElementTree(tree) fulltree.write(filename, "utf-8", True) return ET.tostring(tree)
def writeKML(earth, counties, party): print 'Writing ' + party kml = ET.Element('kml', {'xmlns': 'http://earth.google.com/kml/2.0'}) kmlDocument = ET.SubElement(kml, 'Document') kmlDocumentLookAt = ET.SubElement(kmlDocument, 'LookAt') kmlDocumentLookAtLatitude = ET.SubElement(kmlDocumentLookAt, 'latitude') kmlDocumentLookAtLatitude.text = '43.5' kmlDocumentLookAtLongitude = ET.SubElement(kmlDocumentLookAt, 'longitude') kmlDocumentLookAtLongitude.text = '-71.7' kmlDocumentLookAtRange = ET.SubElement(kmlDocumentLookAt, 'range') kmlDocumentLookAtRange.text = '200000' kmlDocumentLookAtTilt = ET.SubElement(kmlDocumentLookAt, 'tilt') kmlDocumentLookAtTilt.text = '55' kmlDocumentName = ET.SubElement(kmlDocument, 'name') kmlDocumentName.text = 'New Hampshire ' + partyName(party) + ' Primary' kmlFolder = ET.SubElement(kmlDocument, 'Folder') kmlFolderName = ET.SubElement(kmlFolder, 'name') kmlFolderName.text = 'New Hampshire Towns' for name, county in counties.iteritems(): kmlPlacemark = ET.SubElement(kmlFolder, 'Placemark') #kmlPlaceName = ET.SubElement( kmlPlacemark, 'name' ) #kmlPlaceName.text = name kmlMultiGeometry = ET.SubElement(kmlPlacemark, 'MultiGeometry') if earth: kmlPoint = ET.SubElement(kmlMultiGeometry, 'Point') kmlPointCoordinates = ET.SubElement(kmlPoint, 'coordinates') kmlPointCoordinates.text = coord(county['centroid']) kmlPolygon = ET.SubElement(kmlMultiGeometry, 'Polygon') kmlOuterBoundaryIs = ET.SubElement(kmlPolygon, 'outerBoundaryIs') kmlLinearRing = ET.SubElement(kmlOuterBoundaryIs, 'LinearRing') kmlCoordinates = ET.SubElement(kmlLinearRing, 'coordinates') kmlCoordinates.text = ' '.join( [coord(point) for point in county['points']]) kmlStyle = ET.SubElement(kmlPlacemark, 'Style') if earth: kmlIconStyle = ET.SubElement(kmlStyle, 'IconStyle') kmlIcon = ET.SubElement(kmlIconStyle, 'Icon') kmlIconHref = ET.SubElement(kmlIcon, 'href') leader = getLeader(county, party) or {'name': 'generic'} kmlIconHref.text = iconBaseUrl + leader['name'] + '-border.png' kmlBalloonStyle = ET.SubElement(kmlStyle, 'BalloonStyle') kmlBalloonText = ET.SubElement(kmlBalloonStyle, 'text') kmlBalloonText.text = htmlBalloon(county, party) kmlLineStyle = ET.SubElement(kmlStyle, 'LineStyle') kmlLineStyleColor = ET.SubElement(kmlLineStyle, 'color') kmlLineStyleColor.text = '40000000' kmlLineStyleWidth = ET.SubElement(kmlLineStyle, 'width') kmlLineStyleWidth.text = '1' kmlPolyStyle = ET.SubElement(kmlStyle, 'PolyStyle') kmlPolyStyleColor = ET.SubElement(kmlPolyStyle, 'color') kmlPolyStyleColor.text = getColor(county, party) kmlTree = ET.ElementTree(kml) kmlfile = open( private.targetKML + ['maps', 'earth'][earth] + '-nh-' + party + '.kml', 'w') kmlfile.write('<?xml version="1.0" encoding="utf-8" ?>\n') kmlTree.write(kmlfile) kmlfile.close()
def main(bgffile,xbgffile): seq = ET.Element(slpsns.xbgf_('sequence')) rr = ET.SubElement(seq,slpsns.xbgf_('reroot')) for r in ET.XML('<list>'+''.join(open(bgffile,'r').readlines()).strip()+'</list>').findall('*'): x = ET.SubElement(rr,'root') x.text = r.text ET.ElementTree(seq).write(xbgffile) return
def createHistoryFile(self): """ Method to create the history file """ rootEl = ET.Element('histories') tree = ET.ElementTree(rootEl) # ___ Write the file tree.write(self.__history_file__)
def serialize(elem, encoding=None): import cStringIO file = cStringIO.StringIO() tree = ElementTree.ElementTree(elem) if encoding: tree.write(file, encoding) else: tree.write(file) return file.getvalue()
def WriteCurrentPlaylist(self): """ This function WRITES the current playlist changes to the playlist file. It's mainly used when building a new playlist, to write the complete tree, or when updating the rank of a file. Path is absolute. """ #print "Writing playlist to "+self.current_file # debug # wrap it in an ElementTree instance, and save as XML tree = ET.ElementTree(self.playlistROOT) tree.write(self.current_file)
def __init__(self, source): self.source_wiki = source self.etroot = etree.Element(u"mediawiki") self.etroot.set(u"xml:lang", u"en") self.etdoc = etree.ElementTree(self.etroot) self.timestr = mx.DateTime.ISO.strUTC(mx.DateTime.utc()) self.timestr = self.timestr.replace(u" ", u"T") self.timestr = self.timestr.replace(u"+0000", u"Z")
def __init__(self, filename, datatype): self.lstData = [] self.parsefile = { "Point": self.GetPoint, "Polyline": self.GetLines, "Polygon": self.getPolygons, "Cartography": self.getCartography } self.tree = ET.ElementTree(element=None, file=filename) self.rootElem = self.tree.getroot()
def fetchSong(url, viewCount): try: #Get song info from url songInfo = {} _get = url.split('?')[1] tokens = _get.split('&') for token in tokens: toks = token.split('=') songInfo[toks[0]] = int(toks[1]) #fetch the html lyricsWeb = urllib2.urlopen(url) webContent = lyricsWeb.read() lyricsWeb.close() soup = BeautifulSoup(webContent) lyrics = soup.findAll(id="mylrc")[0].contents author = soup.findAll(attrs={'class' : 'link_hb'})[0].contents[0] album = soup.findAll(attrs={'class' : 'link_hb'})[1].contents[0] title = soup.findAll(attrs={'class' : 'link_hb'})[2].contents[0] #print lyrics lyricsText = '' for line in lyrics: for t in line: lyricsText += t #Construct the xml root = ET.Element("xml") doc = ET.SubElement(root, "doc") sidNode = ET.SubElement(doc, "sid") sidNode.text = str(songInfo[u'sid']) aidNode = ET.SubElement(doc, "aid") aidNode.text = str(songInfo[u'aid']) lidNode = ET.SubElement(doc, "lid") lidNode.text = str(songInfo[u'lid']) titleNode = ET.SubElement(doc, "title") titleNode.text = title authorNode = ET.SubElement(doc, "author") authorNode.text = author viewCountNode = ET.SubElement(doc, "viewCount") viewCountNode.text = str(viewCount) lyricsNode = ET.SubElement(doc, "lyrics") lyricsNode.text = lyricsText #Construct the tree tree = ET.ElementTree(root) filename = lyricsDbPath + str(songInfo['lid']) + ".txt" tree.write(filename, "utf-8") except: pass
def xldf_transformGrammar(localpath,cmd,tree): root = ET.Element(slpsns.xbgf_('sequence'),{}) cx0 = 0 for rule in cmd.findall('*')[1:]: if rule.tag != 'context': root.append(rule) cx0 += 1 ET.ElementTree(root).write('xldf-tmp.xbgf') found = findnode(tree,cmd.findtext('target')) if not found: print '[----] xldf:transform failed: target id',cmd.findtext('target'),'not found' return realprods = [] contextprods = [] for p in found.findall('*/*/'+slpsns.bgf_('production')): realprods.append(p) for p in found.findall('*/'+slpsns.bgf_('production')): realprods.append(p) for c in cmd.findall('context'): f = findnode(tree,c.text) if not f: print '[----] xldf:transform failed: context target id',c.text,'not found' return for p in f.findall('*/*/'+slpsns.bgf_('production')): contextprods.append(p) root = ET.Element(slpsns.bgf_('grammar'),{}) for p in realprods: root.append(p) for p in contextprods: root.append(p) #print '[====]',len(realprods),'+',len(contextprods),'productions' ET.ElementTree(root).write('xldf-tmp.bgf') if os.system('xbgf xldf-tmp.xbgf xldf-tmp.bgf xldf-tmp-result.bgf | grep -v Loading | grep -v Saving'): print '[----] xldf:transform failed: error in XBGF' return try: gtree = ET.parse('xldf-tmp-result.bgf') except IOError,e: print '[----] xldf:transform failed: XBGF result file not found' sys.exit(3) return
def OnExportEntity(self, event): exportDialog = wx.FileDialog(self.entityPanel, message="Save file", wildcard="*.xml", style=wx.SAVE | wx.OVERWRITE_PROMPT) if exportDialog.ShowModal() == wx.ID_OK: root = ET.Element("entity", attrib={"name": self.Entity.name}) if not len(self.Entity.Transform) == 0: transformobject = ET.SubElement(root, "transformobject") for transObject in self.Entity.Transform: node = ET.SubElement(transformobject, "node", attrib={ "name": transObject.name, "parent": transObject.node }) ET.SubElement(node, "position", attrib={ "x": str(transObject.position[0]), "y": str(transObject.position[1]), "z": str(transObject.position[2]) }) ET.SubElement(node, "rotation", attrib={ "x": str(transObject.rotation[0]), "y": str(transObject.rotation[1]), "z": str(transObject.rotation[2]) }) ET.SubElement(node, "scale", attrib={ "x": str(transObject.scale[0]), "y": str(transObject.scale[1]), "z": str(transObject.scale[2]) }) if not len(self.Entity.Graphic) == 0: graphicobject = ET.SubElement(root, "graphicobject") for grapObject in self.Entity.Graphic: mesh = ET.SubElement(graphicobject, "mesh", attrib={ "name": grapObject.name, "model": grapObject.model, "material": grapObject.material, "node": grapObject.node }) ET.ElementTree(root).write(exportDialog.GetPath(), 'utf-8') exportDialog.Destroy() pass