def toXML(self): '''Generate the xml representation of the letter matrix''' # <control type="panel"> control = Element('control', type='panel', id=str(CONTROL_PANEL)) if True: SubElement(control, 'posx').text = str(self.posx) SubElement(control, 'posy').text = str(self.posy) SubElement(control, 'width').text = str(self.width) SubElement(control, 'height').text = str(self.height) SubElement(control, 'onleft').text = '-' SubElement(control, 'onright').text = '-' SubElement(control, 'onup').text = '-' SubElement(control, 'ondown').text = '-' SubElement(control, 'viewtype', label='').text = 'panel' SubElement(control, 'pagecontrol').text = '-' SubElement(control, 'scrolltime').text = '-' SubElement(control, 'hitrect', x=str(-10), y=str(-10), w=str(1), h=str(1)) # <itemlayout> itemlayout = SubElement(control, 'itemlayout', height=str(self.letterHeight), width=str(self.letterWidth)) if True: self.addItemLayout(itemlayout, self.theme.inactive, 1) self.addItemLayout(itemlayout, self.theme.active, 2) # </itemlayout> SubElement(control, 'focusedlayout', height=str(self.height), width=str(self.width)) # <content> content = SubElement(control, 'content') if True: # <item> for letter in self.letters: content.append(letter.toXML()) # </item> # </content> # </control> return control
def TextElement(root, name, detail_value): if detail_value: te = SubElement(root, name) if detail_value.type == "text/html": detail_value.type = "html" if detail_value.type == "text/plain": detail_value.type = "text" te.attrib["type"] = detail_value.type te.text = detail_value.value
def CreateSourceElement(ee, feed): """Create an atom:source element in the provided entry element, based on the provided feed metadata. """ if not feed: return root = SubElement(ee, "source") TextElement(root, "title", feed.get("title_detail")) if feed.has_key("links"): for link in feed.links: LinkElement(root, "link", link) TextElement(root, "subtitle", feed.get("subtitle_detail")) TextElement(root, "rights", feed.get("rights_detail")) SubElement(root, "generator").text = "feedarchive" SubElement(root, "updated").text = rfc3339(time.time()) SubElementIf(root, "id", feed.get("id")) if feed.has_key("image"): SubElement(root, "icon").text = feed.image.href if feed.has_key("tags"): for tag in feed.tags: te = SubElement(root, "category") if tag.get("term"): te.attrib["term"] = tag.term if tag.get("scheme"): te.attrib["scheme"] = tag.scheme if tag.get("label"): te.attrib["label"] = tag.label PersonElement(root, "author", feed.get("author_detail"))
def __call__ (self) : self.processed = {} self.pending = {} tree = Element ("project", id = "SW-Tasks") SubElement (tree, "label").text = ("Task(s) %s." % self.toplevel) SubElement (tree, "import-resources", file = "workers.xml") if self.VERBOSE > 1 : self.debug ("Writing `part_of` tree ...") for issue_id in self.toplevel : if self.VERBOSE > 2 : self.debug \ ("Issue %d is top-level. Processing it now ..." % issue_id) issue = self.db.issue.getnode (issue_id) self.process_issue (issue, tree) if self.VERBOSE > 1 : self.debug ("\nWriting issues due to `depends_on` or `needs` ...") while self.pending : (issue_id, issue) = self.pending.popitem () if issue_id not in self.processed : if self.VERBOSE > 2 : self.debug ("Adding %s ... " % issue_id) self.process_issue (issue, tree) else : if self.VERBOSE > 3 : self.debug ("%s already included." % issue_id) file = open (self.oname, "w") ElementTree (tree).write (file, encoding = "utf-8") if self.VERBOSE > 1 : self.debug ("Done.")
def dump(self, output): tempo = {} root = Element('resources') root.tail = '\n' tempo[()] = root for key in self._resources_order: for i in range(1, len(key) + 1): if key[0:i] not in tempo: parent = tempo[key[0:i - 1]] value = self._resources.get(key[0:i], None) if value is None: elem = SubElement(parent, 'node', name=key[i - 1]) else: fullKey = key[0] for j in range(1, i): fullKey += '/' + key[j] newValue = self._dictionary[fullKey] elem = SubElement(parent, 'node', name=key[i - 1], value=newValue) parent.text = elem.tail = '\n' + i * SPACES tempo[key[0:i]] = elem fix_it(root) print >> output, '<?xml version="1.0" encoding="UTF-8"?>' ElementTree(root).write(output, 'ascii')
def build_request_xml(self, data, root_tag): root = Element(root_tag, USERID=self.user_id) for i, address in enumerate(data): address_element = SubElement(root, 'Address', ID=str(i)) for field in self.address_fields: SubElement( address_element, field).text = address.get(field.lower()) return tostring(root)
def LinkElement(root, name, link): if link: le = SubElement(root, name) if (link.has_key("rel")): le.attrib["rel"] = link.rel if (link.has_key("title")): le.attrib["title"] = link.title if (link.has_key("type")): le.attrib["type"] = link.type if (link.has_key("href")): le.attrib["href"] = link.href
def addRing(self, objRing, strType): if strType == 'Inner': elemBnd = Element('innerBoundaryIs') else: elemBnd = Element('outerBoundaryIs') elemRing = SubElement(elemBnd, 'LinearRing') elemCoords = SubElement(elemRing, "coordinates") elemCoords.text = self.addCoordinates(objRing[1]) return elemBnd
def toXML(self): # <window> window = Element('window', id=str(WINDOW_ID)) if True: # Fade in for 1.0 second (but not in screensaver mode) if not self.screensaverMode: SubElement(window, 'animation', effect='fade', time=str(1000)).text = 'WindowOpen' # <controls> controls = SubElement(window, 'controls') if True: # <control type="image"> color = Image(1280, 720, BACKGROUND_IMAGE, self.theme.background) controls.append(color.toXML()) # </control> # <control type="image"> if self.theme.image: image = Image(self.theme.imageWidth, self.theme.imageHeight, self.theme.image) else: # Placeholder for if we change the theme by pressing T image = Image(1280, 720, '-') controls.append(image.toXML()) # </control> #sprites = Sprites(self.layout, self.theme) #controls.append(sprites.toXML()) # <control type="panel"> matrix = Matrix(self.letters, self.layout, self.theme) controls.append(matrix.toXML()) # </control> # </controls> # </window> return window
def formatNetworkAdapterConfig(parent_node, adapter): adapter_node = SubElement(parent_node, 'NetworkAdapter') textChild(adapter_node, 'Mac', adapter['mac']) configs_node = SubElement(adapter_node, 'IpConfigurations') for config in adapter['configurations']: config_node = SubElement(configs_node, 'IpConfiguration') textChild(config_node, 'Ip', config['ip']) textChild(config_node, 'SubnetMask', config['netmask']) if 'gateway' in config: textChild(config_node, 'Gateway', config['gateway'])
def GetFeedElement(feed): """Create an atom:feed element for the provided feed. The provided feed must be in the format described at http://feedparser.org. """ rss = Element("rss") rss.attrib["version"] = "2.0" root = SubElement(rss, "channel") TextElement(root, "title", feed.feed.get("title_detail")) if feed.feed.has_key("links"): for link in feed.feed.links: if link.rel != "self": continue SubElementIf(root, "link", link.href) TextElement(root, "description", feed.feed.get("subtitle_detail")) TextElement(root, "copyright", feed.feed.get("rights_detail")) SubElement(root, "generator").text = "feedarchive" if feed.feed.has_key("image"): im = feed.feed.image ie = SubElement(root, "image") SubElementIf(ie, "url", im.get("href")) SubElementIf(ie, "title", im.get("title")) SubElementIf(ie, "link", im.get("link")) if feed.feed.has_key("tags"): for tag in feed.feed.tags: te = SubElement(root, "category") if (tag.has_key("scheme")): te.attrib["domain"] = tag.scheme te.text = tag.term for entry in feed.entries: ee = SubElement(root, "item") TextElement(ee, "title", entry.get("title_detail")) SubElementIf(ee, "link", entry.get("link")) TextElement(ee, "description", entry.get("summary_detail")) SubElementIf(ee, "guid", entry.get("id")) DateTimeElement(ee, "pubDate", entry, "published") PersonElement(ee, "author", entry.get("author_detail")) if entry.has_key("links"): for link in entry.links: if link.rel != "enclosure": continue ence = SubElement(ee, "enclosure") AttribIf(ence, "url", link.get("url")) AttribIf(ence, "length", link.get("length")) AttribIf(ence, "type", link.get("type")) return rss
def toXML(self): '''Generate the xml representation of this Letter''' # <item> item = Element('item') if True: # <label>$INFO[Window(Home).Property(Unqlocked.0.Background)]</label> SubElement(item, 'label').text = '$INFO[Window(Home).Property(%s)]' % (PROPERTY_INACTIVE % self.index) # <label2>$INFO[Window(Home).Property(Unqlocked.0.Highlight)]</label2> SubElement(item, 'label2').text = '$INFO[Window(Home).Property(%s)]' % (PROPERTY_ACTIVE % self.index) # <onclick>-</onclick> SubElement(item, 'onclick').text = '-' # </item> return item
def gencix(major, minor): # First generate first pass at the CILE over all of the lib tree cixfile = "activeperl-%d.%d.cix" % (major, minor) command = "python ../../../ci2.py scan -n -r -p -l Perl -T /tmp/ActivePerl-%d.%d/perl/lib -i \"*.pm\"> %s" % ( major, minor, cixfile) retval = os.system(command) if retval != 0: print "Error scanning ActivePerl library" sys.exit(retval) # # Grab the output of that scan root = parse(cixfile).getroot() newroot = Element("codeintel", version="2.0") cixfile = SubElement(newroot, "file", lang="Perl", mtime=str(int(time.time())), path=os.path.basename('perl.cix')) for file in root.getiterator('file'): print >> sys.stderr, "Processing", file.get('path') for blob in file: if blob.get("src"): # Don't want the src string. del blob.attrib["src"] cixfile.append(blob) cix = genPerlStdCIX( cixfile, "/tmp/ActivePerl-%d.%d/perl/lib/pod/perlfunc.pod" % (major, minor)) parent_map = dict((c, p) for p in cixfile.getiterator() for c in p) for variable in newroot.getiterator('variable'): attributes = variable.get('attributes') if attributes and '__local__' in variable.get('attributes'): parent_map[variable].remove(variable) # Generate the CIX. print >> sys.stderr, "Prettying" prettify(newroot) tree = ElementTree(newroot) #fname = '../../../lib/codeintel2/stdlibs/perl-%d.%d.cix' % (major, minor) fname = 'perl-%d.%d.cix' % (major, minor) #os.system('p4 edit %s' % fname) stream = open(fname, "w") print >> sys.stderr, "Writing" stream.write('<?xml version="1.0" encoding="UTF-8"?>\n') tree.write(stream) stream.close()
def write_task (self, issue, tree) : user = self.db.user.getnode (issue.responsible) SubElement (tree, "label").text = \ ("%s" % ( escape (issue.title [:50]) + ("","...")[len (issue.title) > 50] ) ) SubElement (tree, "priority").text = \ ("%d" % (issue.effective_prio or issue.priority)) ### XXX still not taking candidates into account SubElement \ ( tree , "use-resource" , idref = "%s" % (user.nickname or user.username) , type = "worker" ) if not issue.composed_of : effort = issue.numeric_effort or (5, 1)[issue.status == self.s_test] effort = int (effort + .5) SubElement (tree, "duration", unit = "days").text = ("%s" % effort) else : # fill containers with a dummy length of 0 days SubElement (tree, "duration", unit = "days").text = "0" for dep_id in issue.depends or [] : if self.db.issue.getnode (dep_id).status != self.s_closed : SubElement \ ( tree , "constraint" , type = "begin-after-end" ).text = ("%s" % dep_id) for dep_id in issue.needs or [] : if self.db.issue.getnode (dep_id).status != self.s_closed : SubElement \ ( tree , "constraint" , type = "end-after-end" ).text = ("%s" % dep_id) if issue.deadline : SubElement \ ( tree , "constraint" , type = "end-before-date" ).text = ("%s" % self.reduce_date (issue.deadline)) if issue.earliest_start : SubElement \ ( tree , "constraint" , type = "begin-after-date" ).text = ("%s" % self.reduce_date (issue.earliest_start))
def GetFeedElement(feed): """Create an atom:feed element for the provided feed. The provided feed must be in the format described at http://feedparser.org. """ root = Element("feed") root.attrib["xmlns"] = "http://www.w3.org/2005/Atom" TextElement(root, "title", feed.feed.get("title_detail")) if feed.feed.has_key("links"): for link in feed.feed.links: LinkElement(root, "link", link) TextElement(root, "subtitle", feed.feed.get("subtitle_detail")) TextElement(root, "rights", feed.feed.get("rights_detail")) SubElement(root, "generator").text = "feedarchive" SubElement(root, "updated").text = rfc3339(time.time()) SubElementIf(root, "id", feed.feed.get("id")) if feed.feed.has_key("image"): SubElement(root, "icon").text = feed.feed.image.href if feed.feed.has_key("tags"): for tag in feed.feed.tags: te = SubElement(root, "category") if tag.get("term"): te.attrib["term"] = tag.term if tag.get("scheme"): te.attrib["scheme"] = tag.scheme if tag.get("label"): te.attrib["label"] = tag.label PersonElement(root, "author", feed.feed.get("author_detail")) for entry in feed.entries: ee = SubElement(root, "entry") TextElement(ee, "title", entry.get("title_detail")) if entry.has_key("links"): for link in entry.links: LinkElement(ee, "link", link) TextElement(ee, "summary", entry.get("summary_detail")) TextElement(ee, "content", entry.get("content_detail")) DateTimeElement(ee, "published", entry, "published") DateTimeElement(ee, "updated", entry, "updated") SubElementIf(ee, "id", entry.get("id")) PersonElement(ee, "author", entry.get("author_detail")) PersonElement(ee, "publisher", entry.get("publisher_detail")) if entry.has_key("contributors"): for contributor in entry.contributors: PersonElement(ee, "contributor", contributor) CreateSourceElement(ee, entry.get("source")) return root
def toelem(pattern, elem): from elementtree.ElementTree import SubElement patt = SubElement(elem, 'pattern') patt.attrib['name'] = str(pattern.name) patt.attrib['beats'] = str(pattern.beats) for note in pattern: sub = SubElement(patt, 'note') sub.attrib['start'] = str(note.start) sub.attrib['stop'] = str(note.stop) sub.attrib['pitch'] = str(note.pitch)
def dump(self, output, lang): tempo = {} root = Element('resources') root.tail = '\n' tempo[()] = root for key in self._resources_order: for i in range(1, len(key) + 1): if key[0:i] not in tempo: parent = tempo[key[0:i - 1]] value = self._resources.get(key[0:i], None) if value is None: elem = SubElement(parent, 'node', name=key[i - 1]) else: localized = value.get(lang, None) english = value.get('en', None) if english is None: print >> sys.stderr, 'English file does not have the string for', key[ 0:i] print >> sys.stderr, ' entry is marked as obosolete.' elem = SubElement(parent, 'node', name=key[i - 1], value=localized, obsolete='true') elif localized is not None: elem = SubElement(parent, 'node', name=key[i - 1], value=localized) else: elem = SubElement(parent, 'node', name=key[i - 1], value=english, toBeTranslated='true') parent.text = elem.tail = '\n' + i * SPACES tempo[key[0:i]] = elem fix_it(root) print >> output, '<?xml version="1.0" encoding="UTF-8"?>' ElementTree(root).write(output, 'utf-8')
def createFilePodcast(mediaFilePath, title, description=''): """ create the xml file using utf """ mediaItem = Element("media") mediaItem.attrib["version"] = VERSION titleNode = SubElement(mediaItem, "title") titleNode.text = title descrNode = SubElement(mediaItem, "description") descrNode.text = description createXmlFile(mediaFilePath + '.xml', mediaItem) mediaItem.clear()
def main(): top_node = Element('osinfo') if sys.platform == 'win32': info = windowsInfo() elif sys.platform == 'linux2': info = linuxInfo() elif sys.platform.startswith('freebsd'): info = bsdInfo() SubElement(top_node, 'name').text = info['name'] SubElement(top_node, 'version').text = info['version'] SubElement(top_node, 'details').text = info['details'] print tostring(top_node) return 0
def ToElement(self): """ Gets an ElementTree.Element representation. """ peer = self.peer elt = Element("entity", id=peer.id_) SubElement(elt, "address").text = peer.address.ToString() SubElement(elt, "version").text = str(peer.protocol_version) hist = SubElement(elt, "history") keys = self.intervals.keys() keys.sort() for k in keys: c = SubElement(hist, "connected") i = self.intervals[k] c.text = "%s-%s" % (i.start, i.end) return elt
def changeSyntaticToPhonetic(xml): lexicon = xml.getroot() for lemma in lexicon.getiterator('lemma'): if lemma.get('special'): continue phon = lemma.find('phon') if phon is not None: phon = phon.text.split() phon.append('#1') synt = lemma.find('synt') if synt is None: synt = SubElement(lemma, 'synt') else: synt.clear() synt.tail = '\n ' if phon: for ph in phon: SubElement(synt, 'tok').text = ph
def gencix(major, minor): # First generate first pass at the CILE over all of the lib tree cixfile = "activeperl-%d.%d.cix" % (major, minor) command = "python ../../../ci2.py scan -n -r -p -l Perl -T /tmp/ActivePerl-%d.%d/perl/lib -i \"*.pm\"> %s" % (major, minor, cixfile) retval = os.system(command) if retval != 0: print "Error scanning ActivePerl library" sys.exit(retval) # # Grab the output of that scan root = parse(cixfile).getroot() newroot = Element("codeintel", version="2.0") cixfile = SubElement(newroot, "file", lang="Perl", mtime=str(int(time.time())), path=os.path.basename('perl.cix')) for file in root.getiterator('file'): print >> sys.stderr, "Processing", file.get('path') for blob in file: if blob.get("src"): # Don't want the src string. del blob.attrib["src"] cixfile.append(blob) cix = genPerlStdCIX(cixfile, "/tmp/ActivePerl-%d.%d/perl/lib/pod/perlfunc.pod" % (major, minor)) parent_map = dict((c, p) for p in cixfile.getiterator() for c in p) for variable in newroot.getiterator('variable'): attributes = variable.get('attributes') if attributes and '__local__' in variable.get('attributes'): parent_map[variable].remove(variable) # Generate the CIX. print >>sys.stderr, "Prettying" prettify(newroot) tree = ElementTree(newroot) #fname = '../../../lib/codeintel2/stdlibs/perl-%d.%d.cix' % (major, minor) fname = 'perl-%d.%d.cix' % (major, minor) #os.system('p4 edit %s' % fname) stream = open(fname, "w") print >>sys.stderr, "Writing" stream.write('<?xml version="1.0" encoding="UTF-8"?>\n') tree.write(stream) stream.close()
def add_work_unit_to_task(tree_root, task_name_or_id): """Adds a work unit to the specified task. If the task is not active, the operation throws Active_error.""" # Obtain the task id. node = find_task(tree_root, task_name_or_id) task_id = node.find("id").text # If the task is killed, report it. if node.get("killed") == "yes": raise Active_error("Task %s is dead, aborting " "operation." % task_name_or_id) # Create a work-unit for the found task. work_units = tree_root.find("work-unit-list") work_unit = SubElement(work_units, "work-unit") work_unit.set("id", task_id) work_unit.text = get_today()
def process_issue (self, issue, tree) : """Process an issue: Write the XML output, add the `issue` to `processed` and continue recursively calling `collect_parts`. """ task = SubElement (tree, "task", id = ("%s" % issue.id)) self.write_task (issue, task) self.processed [issue.id] = issue self.collect_parts (issue, task)
def FormattedContent(self, report_obj, chart_style, request): from atrinsic.base.models import AqWidget,UserAqWidget x = self.widget_id widget = AqWidget.objects.get(pk=self.widget_id) var_columns = None if self.user_widget_id != None: user_widget = UserAqWidget.objects.get(pk=self.user_widget_id) if user_widget.custom_columns != None: var_columns = user_widget.custom_columns if (var_columns == None) & (self.data_columns != None): var_columns = self.data_columns else: var_columns = widget.data_columns if var_columns.find(",") > 0: col1,col2 = var_columns.split(",") else: col1 = var_columns col2 = None columns = { 'var1': col1, 'var2':col2 } if chart_style == "table": html = self.getReportHTML(report_obj) return html elif chart_style == "json-array": import cjson return cjson.encode(report_obj.RenderContents()) elif chart_style == "json": import cjson json = self.get_json(report_obj) return cjson.encode(json) elif chart_style == "xml": from elementtree import ElementTree from elementtree.ElementTree import Element,tostring,SubElement,ElementTree,dump xmldict = {} xmldict["xml"] = self.get_json(report_obj) root = Element("xml") for row in xmldict["xml"]: item_element = SubElement(root,"item") for field in row: SubElement(item_element,field).text = row[field] return tostring(root) else: return self.getAQChart(request=request,chart_style=chart_style,report_obj=report_obj,columns=columns)
def add_task(tree_root, task_name): """Adds (stripped) task_name to the tree_root.""" assert task_name == task_name.strip() assert len(task_name) > 1 # Get a new task id, higher than all previous ones. id_num = highest_task_id(tree_root) + 1 assert id_num >= 0 # Create the task. tasklist = tree_root.find("task-list") task = SubElement(tasklist, "task") SubElement(task, "id").text = "%d" % id_num SubElement(task, "name").text = task_name.strip() date_today = get_today() SubElement(task, "starting-day").text = date_today SubElement(task, "note").text = "" last_unit = SubElement(task, "last-unit", attrib = {"amount": "0"}) last_unit.text = date_today
def recurse_for_children(current_node, parent_node, active_cat, show_empty=True): child_count = current_node.child.count() if show_empty or child_count > 0 or current_node.product_set.count() > 0: temp_parent = SubElement(parent_node, 'li') attrs = {'href': current_node.get_absolute_url()} if current_node == active_cat: attrs["class"] = "current" if current_node.parent: link = SubElement(temp_parent, 'a', attrs) else: link = SubElement(temp_parent, 'span', {}) link.text = current_node.name if child_count > 0: new_parent = SubElement(temp_parent, 'ul') children = current_node.child.all() for child in children: recurse_for_children(child, new_parent, active_cat)
def ConvertDialogToElement(dialog): # build a tree structure root = Element("DIALOG") for ctrl in dialog.AllControls(): ctrlElem = SubElement(root, "CONTROL") for name, value in ctrl.properties.items(): AddElement(ctrlElem, name, value) return root
def AddElement(element, name, value): # if it is a ctypes structure if isinstance(value, ctypes.Structure): # create an element for the structure structElem = SubElement(element, name) # iterate over the fields in the structure for propName in value._fields_: propName = propName[0] itemVal = getattr(value, propName) if isinstance(itemVal, (int, long)): propName += "_LONG" itemVal = unicode(itemVal) structElem.set(propName, EscapeSpecials(itemVal))#.encode('utf8')) elif isinstance(value, (list, tuple)): # add the element to hold the values #listElem = SubElement(element, name) # remove the s at the end (if there) name = name.rstrip('s') for i, attrVal in enumerate(value): AddElement(element, "%s_%05d"%(name, i), attrVal) elif isinstance(value, dict): dictElem = SubElement(element, name) for n, val in value.items(): AddElement(dictElem, n, val) else: if isinstance(value, (int, long)): name += "_LONG" element.set(name, EscapeSpecials(value))#.encode('utf8', 'backslashreplace'))
def create_empty_configuration_file(file_name): """Creates a default empty file with today's date as starting day.""" root = Element("frequent-task-reminder") # Create the configuration element. config = SubElement(root, "configuration") # Set the first day to track as today. start = SubElement(config, "starting-day") start.text = time.asctime() # Create a default empty task. tasklist = SubElement(root, "tasklist") task = SubElement(tasklist, "task") SubElement(task, "id").text = 0 SubElement(task, "name").text = "Default name" SubElement(task, "notes").text = "Some random notes you can modify." # Finally write the xml file. ElementTree(root).write(file_name, encoding="latin1") print "Created empty configuration file '%s'." % file_name
def _set_link_rel_edit(self, etree, id): reledit = [ e for e in etree.findall(ATOM_LINK) if e.get('rel', '') == 'edit' ] if not reledit: new_lin = SubElement(etree.getroot(), ATOM_LINK) new_lin.set('rel', 'edit') new_lin.set('href', self.EDIT_URI_TEMPLATE % id) else: reledit[0].set('href', self.EDIT_URI_TEMPLATE % id)
def write(pattern, fpath): from elementtree.ElementTree import Element, SubElement, ElementTree from elementtree import TidyTools root = Element('xml') patt = SubElement(root, 'pattern') patt.attrib['name'] = str(pattern.name) patt.attrib['beats'] = str(pattern.beats) for note in pattern: sub = SubElement(patt, 'note') sub.attrib['start'] = str(note.start) sub.attrib['stop'] = str(note.stop) sub.attrib['pitch'] = str(note.pitch) ElementTree(root).write(fpath) os.system('tidy -xml -iqm \"%s\"' % fpath)
def toElementTree(self, details): """ Return an Element node with the XML rappresentation of this module """ c = Element('module') n = SubElement(c, 'name') n.text = self._alias if details: # version v = SubElement(c, 'version') v.text = str(self._version) # type t = SubElement(c, 'type') t.text = self._type # lock u = SubElement(c, 'upgradable') u.text = repr(self._upgradable).lower() return c
def __init__(self, lstData, GeoType, strPath, strFilename, strLayername): dctWriteKML = {'Point': self.writePoint, 'Polyline': self.writeLine, 'Polygon': self.writePolygon} #Create new element tree with a root of KML... objRoot = Element("{http://earth.google.com/kml/2.1}kml") objTree = ElementTree(element=objRoot) elemDoc = SubElement(objRoot, 'Document') elemDocName = SubElement(elemDoc, 'name') #According the KML spec, default Polystyle stuff goes here... elemDocName.text = strLayername #Add a document name element here...populate from supplied parameters for objRow in lstData: elemPlace = SubElement(elemDoc, 'Placemark') elemName =SubElement(elemPlace,'name') elemName.text = objRow['Name'] #Add support for the description tag... elemDesc = SubElement(elemPlace, 'description') elemDesc.text = objRow['Description'] elemGeo = dctWriteKML.get(GeoType, self.errHandler)(objRow['Geometry'], elemPlace) elemPlace.append(elemGeo) self.Write(objTree, strPath, strFilename)
def toXML(self): # <control> control = Element('control', type='image', id=str(self.id)) if True: SubElement(control, 'posx').text = str((1280 - self.width) / 2) SubElement(control, 'posy').text = str((720 - self.height) / 2) SubElement(control, 'width').text = str(self.width) SubElement(control, 'height').text = str(self.height) SubElement(control, 'texture').text = self.image if self.diffuse: SubElement(control, 'colordiffuse').text = self.diffuse # AARRGGBB # </control> return control
def add_task(self, taskid, tasktype, folder=None): """Add a task to the default xml task sheet Args: taskid: unique ID for new task for tracking individual tasks tasktype: Identifier to track purpose of task folder: Name of output folder if necessary Returns: None Raises: None """ if tasktype == "processSolr": if folder == None: folder = self.solrFolder root = self.tree.getroot() newTask = SubElement(root, "task") newNode = Element("status") newNode.text = "pending" newTask.append(newNode) newNode = Element("taskid") newNode.text = str(taskid) newTask.append(newNode) newNode = Element("directory") newNode.text = str(folder) newTask.append(newNode) self.tree.write("outfile.xml") elif tasktype == "processTopic": print "Processing topic modeling data" if folder == None: folder = self.topicFolder else: print "Unknown task" + tasktype
def changeSyntaticToPhonetic(xml): lexicon = xml.getroot() for lemma in lexicon.getiterator("lemma"): if lemma.get("special"): continue phon = lemma.find("phon") if phon is not None: phon = phon.text.split() phon.append("#1") synt = lemma.find("synt") if synt is None: synt = SubElement(lemma, "synt") else: synt.clear() synt.tail = "\n " if phon: for ph in phon: SubElement(synt, "tok").text = ph
def formatSystemStatus(info): top_node = Element('systemstatus') SubElement(top_node, 'datetime', {'value': time.strftime(TIME_FORMAT, info['datetime'])}) for cpu in info['cpus']: cpu_node = SubElement(top_node, 'cpu', { 'value': cpu['value'], 'used': cpu['used'], }) cores_node = SubElement(cpu_node, 'cores') for core in cpu['cores']: SubElement(cores_node, 'core', core) SubElement(top_node, 'ram', info['ram']) disks = SubElement(top_node, 'disks') for disk in info['disks']: SubElement(disks, 'disk', disk) return tostring(top_node)
def output_workers (self, file = sys.stdout) : week = [] tree = Element ("resources-list", type = "worker") tt = SubElement (tree, "timetable", id = "weekend") SubElement (tt, "dayoff", type = "weekday").text = "saturday" SubElement (tt, "dayoff", type = "weekday").text = "sunday" for day in ("monday", "tuesday", "wednesday", "thursday", "friday") : tt = SubElement (tree, "timetable", id = day) SubElement (tt, "dayoff", type = "weekday").text = day week.append (day) stati = [self.db.user_status.lookup (i) for i in ("valid", "obsolete", "system") ] for uid in self.db.user.filter (None, dict (status = stati)) : dyn = self.get_user_dynamic (self.db, uid, self.now) if not dyn : dyn = self.last_user_dynamic (self.db, uid) user = self.db.user.getnode (uid) if not user.nickname and not user.username : continue r = SubElement \ ( tree , "resource" , id = (user.nickname or user.username).decode ("utf-8") , fullname = (user.realname or user.username).decode ("utf-8") ) SubElement (r, "use-timetable", idref = "weekend") wh = 38.5 if dyn : wh = self.weekly_hours (dyn) or 38.5 wh *= 4 wh += 7.75 * 4 - 1 wh = int (wh) wh = int (wh / (7.75 * 4)) for i in range (wh, 5) : SubElement (r, "use-timetable", idref = week [i]) ElementTree (tree).write (file, encoding = "utf-8")
def _ConvertModelPerformance(perf,modelPerf): if len(modelPerf)>3: confMat = modelPerf[3] accum = 0 for row in confMat: for entry in row: accum += entry accum = str(accum) else: confMat = None accum = 'N/A' if len(modelPerf)>4: elem = SubElement(perf,"ScreenThreshold") elem.text=str(modelPerf[4]) elem = SubElement(perf,"NumScreened") elem.text=accum if len(modelPerf)>4: elem = SubElement(perf,"NumSkipped") elem.text=str(modelPerf[6]) elem = SubElement(perf,"Accuracy") elem.text=str(modelPerf[0]) elem = SubElement(perf,"AvgCorrectConf") elem.text=str(modelPerf[1]) elem = SubElement(perf,"AvgIncorrectConf") elem.text=str(modelPerf[2]) if len(modelPerf)>4: elem = SubElement(perf,"AvgSkipConf") elem.text=str(modelPerf[5]) if confMat: elem = SubElement(perf,"ConfusionMatrix") elem.text = str(confMat)
def textChild(parent, name, text): n = SubElement(parent, name) n.text = text return n
def TextElement(root, name, detail_value): if detail_value: te = SubElement(root, name) te.text = detail_value.value
def addGraphonesToLexicon(xml, graphones): lexicon = xml.getroot() for letters, phonemes in graphones: lemma = SubElement(lexicon, 'lemma') lemma.text = '\n ' orth = SubElement(lemma, 'orth') orth.text = '_' + ''.join(letters) + '_' orth.tail = '\n ' phon = SubElement(lemma, 'phon') phon.text = ' '.join(phonemes) phon.tail = '\n ' synt = SubElement(lemma, 'synt') SubElement(synt, 'tok').text = lmToken(letters, phonemes) synt.tail = '\n' # synt.tail = '\n ' # eval = SubElement(lemma, 'eval') # SubElement(eval, 'tok').text = '[UNKNOWN]' # eval.tail = '\n' lemma.tail = '\n'
def genPerlStdCIX(filename, stream): log.debug("genPerlStdCIX(filename=%r, stream=%r)", filename, stream) root = Element("codeintel", version="2.0") cixfile = SubElement(root, "file", lang="Perl", mtime=str(int(time.time())), path=os.path.basename(filename)) # Process Perl's built-ins out of perlfunc.pod. if 1: p4path = "//depot/main/Apps/Gecko/src/Core/pod/perlfunc.pod" cmd = "p4 print -q %s" % p4path i,o,e = os.popen3(cmd) lines = o.read().splitlines(0) i.close(); o.close(); retval = e.close() if retval: raise Error("error running: %s" % cmd) else: lines = open("perlfunc.pod", 'r').read().splitlines(0) # Parse the "Alphabetical Listing of Perl Functions" into a list of # 'blocks' where each block is one command-"=item" block. start = lines.index("=head2 Alphabetical Listing of Perl Functions") blocks = [] block = None level = 0 def parseItem(line): sig = line.split(None, 1)[1] name = re.split("[ \t\n(/]", sig, 1)[0] return name, sig for i, line in enumerate(lines[start:]): if line.startswith("=over"): level += 1 if line.startswith("=back"): level -= 1 if level == 0: # done the 'Alphabetical Listing' section if block: blocks.append(block) break if level > 1: if block: block["lines"].append(line) elif block is None and not line.startswith("=item"): continue elif block is None and line.startswith("=item"): block = {} name, sig = parseItem(line) block = { "name": name, "sigs": [sig], "lines": [] } elif line.startswith("=item"): name, sig = parseItem(line) if name == block["name"]: block["sigs"].append(sig) else: blocks.append(block) block = { "name": name, "sigs": [sig], "lines": [] } else: if not block["lines"] and not line.strip(): pass # drop leading empty lines elif not line.strip() and block["lines"] and \ not block["lines"][-1].strip(): pass # collapse multiple blank lines else: block["lines"].append(line) #pprint(blocks) # Process the blocks into a list of command info dicts. def podrender(pod): rendered = pod rendered = re.sub("F<(.*?)>", r"\1", rendered) rendered = re.sub("I<(.*?)>", r"*\1*", rendered) def quoteifspaced(match): if ' ' in match.group(1): return "'%s'" % match.group(1) else: return match.group(1) rendered = re.sub("C<(.*?)>", quoteifspaced, rendered) def linkrepl(match): content = match.group(1) if content.startswith("/"): content = content[1:] if "/" in content: page, section = content.split("/", 1) content = "%s in '%s'" % (section, page) else: content = "'%s'" % content return content rendered = re.sub("L<(.*?)>", linkrepl, rendered) return rendered # These perl built-ins are grouped in perlfunc.pod. commands = [] WIDTH = 60 # desc field width syscalls = """ getpwnam getgrnam gethostbyname getnetbyname getprotobyname getpwuid getgrgid getservbyname gethostbyaddr getnetbyaddr getprotobynumber getservbyport getpwent getgrent gethostent getnetent getprotoent getservent setpwent setgrent sethostent setnetent setprotoent setservent endpwent endgrent endhostent endnetent endprotoent endservent """.split() calltip_skips = "sub use require".split() for block in blocks: name, sigs, lines = block["name"], block["sigs"], block["lines"] if name == "-X": # template for -r, -w, -f, ... pattern = re.compile(r"^ (-\w)\t(.*)$") tlines = [line for line in lines if pattern.match(line)] for tline in tlines: tname, tdesc = pattern.match(tline).groups() tsigs = [s.replace("-X", tname) for s in sigs] command = {"name": tname, "sigs": tsigs, "desc": textwrap.fill(tdesc, WIDTH)} commands.append(command) elif name in ("m", "q", "qq", "qr", "qx", "qw", "s", "tr", "y"): operators = { "m": """\ m/PATTERN/cgimosx /PATTERN/cgimosx Searches a string for a pattern match, and in scalar context returns true if it succeeds, false if it fails. """, "q": """\ q/STRING/ 'STRING' A single-quoted, literal string. """, "qq": """\ qq/STRING/ "STRING" A double-quoted, interpolated string. """, "qr": """\ qr/STRING/imosx Quotes (and possibly compiles) STRING as a regular expression. """, "qx": """\ qx/STRING/ `STRING` A string which is (possibly) interpolated and then executed as a system command. """, "qw": """\ qw/STRING/ Evaluates to a list of the words extracted out of STRING, using embedded whitespace as the word delimiters. """, "s": """\ s/PATTERN/REPLACEMENT/egimosx Searches a string for a pattern, and if found, replaces that pattern with the replacement text and returns the number of substitutions made. Otherwise it returns the empty string. """, "tr": """\ tr/SEARCHLIST/REPLACEMENTLIST/cds y/SEARCHLIST/REPLACEMENTLIST/cds Transliterates all occurrences of the characters found in the search list with the corresponding character in the replacement list. It returns the number of characters replaced or deleted. """, "y": """\ tr/SEARCHLIST/REPLACEMENTLIST/cds y/SEARCHLIST/REPLACEMENTLIST/cds Transliterates all occurrences of the characters found in the search list with the corresponding character in the replacement list. It returns the number of characters replaced or deleted. """, } sigs = [] desclines = None for line in operators[name].splitlines(0): if desclines is not None: desclines.append(line.strip()) elif not line.strip(): desclines = [] else: sigs.append(line.strip()) command = {"name": name, "sigs": sigs, "desc": textwrap.fill(' '.join(desclines), WIDTH)} commands.append(command) elif name in syscalls: desc = "Performs the same function as the '%s' system call." % name desc = textwrap.fill(desc, WIDTH) getterListContext = { "getpw": "\n" " ($name,$passwd,$uid,$gid,$quota,$comment,\n" " $gcos,$dir,$shell,$expire) = %s", "getgr": "\n ($name,$passwd,$gid,$members) = %s", "gethost": "\n ($name,$aliases,$addrtype,$length,@addrs) = %s", "getnet": "\n ($name,$aliases,$addrtype,$net) = %s", "getproto": "\n ($name,$aliases,$proto) = %s", "getserv": "\n ($name,$aliases,$port,$proto) = %s", } getterScalarContext = { "getgrent": "$name = %s", "getgrgid": "$name = %s", "getgrnam": "$gid = %s", "gethostbyaddr": "$name = %s", "gethostbyname": "$addr = %s", "gethostent": "$name = %s", "getnetbyaddr": "$name = %s", "getnetbyname": "$net = %s", "getnetent": "$name = %s", "getprotobyname": "$num = %s", "getprotobynumber": "$name = %s", "getprotoent": "$name = %s", "getpwent": "$name = %s", "getpwnam": "$uid = %s", "getpwuid": "$name = %s", "getservbyname": "$num = %s", "getservbyport": "$name = %s", "getservent": "$name = %s", } for prefix, template in getterListContext.items(): if name.startswith(prefix): desc += template % sigs[0] if name in getterScalarContext: desc += "\nin list context or:\n "\ + getterScalarContext[name] % sigs[0] command = {"name": name, "desc": desc, "sigs": sigs} commands.append(command) elif name == "shmread": desc = """\ Reads the System V shared memory segment ID starting at position POS for size SIZE by attaching to it, copying out, and detaching from it. """ desc = ' '.join([ln.strip() for ln in desc.splitlines(0)]) command = {"name": name, "sigs": sigs, "desc": textwrap.fill(desc, WIDTH)} commands.append(command) elif name == "shmwrite": desc = """\ Writes the System V shared memory segment ID starting at position POS for size SIZE by attaching to it, copying in, and detaching from it. """ desc = ' '.join([ln.strip() for ln in desc.splitlines(0)]) command = {"name": name, "sigs": sigs, "desc": textwrap.fill(desc, WIDTH)} commands.append(command) elif name in calltip_skips: continue # just drop the sub calltip: annoying else: # Parsing the description from the full description: # Pull out the first sentence up to a maximum of three lines # and one paragraph. If the first *two* sentences fit on the # first line, then use both. desc = "" sentencePat = re.compile(r"([^\.]+(?:\. |\.$))") if name in ("dbmclose", "dbmopen"): # Skip the first paragraph: "[This function...superceded by" lines = lines[lines.index('')+1:] elif name == "do": # Skip the first sentence: "Not really a function." end = sentencePat.match(lines[0]).span()[1] lines[0] = lines[0][end:].lstrip() for i, line in enumerate(lines): if not line.strip(): break sentences = sentencePat.findall(line) if not sentences: desc += line + ' ' continue elif i == 0 and len(sentences) > 1: desc += ' '.join([s.strip() for s in sentences[:2]]) else: desc += sentences[0].strip() break command = {"name": name, "sigs": sigs, "desc": textwrap.fill(podrender(desc), WIDTH)} commands.append(command) #for command in commands: # print # print banner(command["name"], '-') # print '\n'.join(command["sigs"]) # print # print command["desc"] # Generate the CIX for each function. module_elt = SubElement(cixfile, "scope", ilk="blob", name="*") # "built-ins" module for command in commands: name, sigs, desc = command["name"], command["sigs"], command["desc"] func_elt = SubElement(module_elt, "scope", ilk="function", name=name) if sigs: func_elt.set("signature", '\n'.join(sigs)) if desc: doclines = desc.split('\n')[:3] #doclines = parseDocSummary(doclines) doc = '\n'.join(doclines) func_elt.set("doc", doc) # Generate the CIX. prettify(root) tree = ElementTree(root) stream.write('<?xml version="1.0" encoding="UTF-8"?>\n') tree.write(stream)
def _make_mods(self, qdc, PID): """Create a mods xml string for use with bibutils.""" mods = Element("mods") # Metadata from the Journal try: ISSN = getattr(self, 'ISSN') except: ISSN = self.portal_properties.dipp_properties.ISSN.strip() if ISSN == "": fedora = getToolByName(self, "fedora") jqdc = fedora.getQualifiedDCMetadata(fedora.PID) ISSN = jqdc['identifierISSN'] # Metadata stored only in Plone try: startpage = self.startpage except: startpage = None try: endpage = self.endpage except: endpage = None # Qualified Dublin Core Metadata (qdc) stored in Fedora # title qTitles = qdc['title'] titleInfo = SubElement(mods, "titleInfo") title = SubElement(titleInfo, "title") title.text = qTitles[0]['value'] # authors for author in qdc['creatorPerson']: name = SubElement(mods, "name", type="personal") namePart = SubElement(name, "namePart", type="family") namePart.text = author['lastName'] namePart = SubElement(name, "namePart", type="given") namePart.text = author['firstName'] # abstract qAbstracts = qdc['DCTermsAbstract'] abstract = SubElement(mods, "abstract") abstract.text = qAbstracts[0]['value'] # subjects subject = SubElement(mods, "subject") for qSubject in qdc['subject']: topic = SubElement(subject, "topic") topic.text = qSubject # ddc for qDDC in qdc['DDC']: ddc = SubElement(mods, "classification", authority="ddc") ddc.text = qDDC # genre relatedItem = SubElement(mods, "relatedItem", type="host") genre = SubElement(relatedItem, "genre", authority="marcgt") genre.text = "periodical" genre = SubElement(relatedItem, "genre") genre.text = "academic journal" # Bibliographic Data bc = qdc['bibliographicCitation'][0] # Journaltitle titleInfo = SubElement(relatedItem, "titleInfo") title = SubElement(titleInfo, "title") title.text = bc['journalTitle'] # Volume/Issue/Pagenumbers part = SubElement(relatedItem, "part") volume = SubElement(part, "detail", type="volume") number = SubElement(volume, "number") number.text = bc["journalVolume"] issue = SubElement(part, "detail", type="issue") number = SubElement(issue, "number") number.text = bc["journalIssueNumber"] date = SubElement(part, "date") try: year = DateTime(bc["journalIssueDate"]).strftime('%Y') except: year = "????" date.text = year if startpage: extent = SubElement(part, "extent", unit="page") start = SubElement(extent, "start") start.text = str(startpage) end = SubElement(extent, "end") end.text = str(endpage) # identifier id = qdc['creatorPerson'][0]["lastName"].lower() + str(year) if ISSN: issn = SubElement(mods, "identifier", type="issn") issn.text = ISSN urn = SubElement(mods, "identifier", type="urn") urn.text = qdc['identifierURN'] if qdc['identifierDOI']: doi = SubElement(mods, "identifier", type="doi") doi.text = qdc['identifierDOI'] uri = SubElement(mods, "identifier", type="uri") uri.text = "http://nbn-resolving.de/" + qdc['identifierURN'] citekey = SubElement(mods, "identifier", type="citekey") citekey.text = id return mods
def genPerlStdCIX(filename, stream): log.debug("genPerlStdCIX(filename=%r, stream=%r)", filename, stream) root = Element("codeintel", version="2.0") cixfile = SubElement(root, "file", lang="Perl", mtime=str(int(time.time())), path=os.path.basename(filename)) # Process Perl's built-ins out of perlfunc.pod. if 1: p4path = "//depot/main/Apps/Gecko/src/Core/pod/perlfunc.pod" cmd = "p4 print -q %s" % p4path i, o, e = os.popen3(cmd) lines = o.read().splitlines(0) i.close() o.close() retval = e.close() if retval: raise Error("error running: %s" % cmd) else: lines = open("perlfunc.pod", 'r').read().splitlines(0) # Parse the "Alphabetical Listing of Perl Functions" into a list of # 'blocks' where each block is one command-"=item" block. start = lines.index("=head2 Alphabetical Listing of Perl Functions") blocks = [] block = None level = 0 def parseItem(line): sig = line.split(None, 1)[1] name = re.split("[ \t\n(/]", sig, 1)[0] return name, sig for i, line in enumerate(lines[start:]): if line.startswith("=over"): level += 1 if line.startswith("=back"): level -= 1 if level == 0: # done the 'Alphabetical Listing' section if block: blocks.append(block) break if level > 1: if block: block["lines"].append(line) elif block is None and not line.startswith("=item"): continue elif block is None and line.startswith("=item"): block = {} name, sig = parseItem(line) block = {"name": name, "sigs": [sig], "lines": []} elif line.startswith("=item"): name, sig = parseItem(line) if name == block["name"]: block["sigs"].append(sig) else: blocks.append(block) block = {"name": name, "sigs": [sig], "lines": []} else: if not block["lines"] and not line.strip(): pass # drop leading empty lines elif not line.strip() and block["lines"] and \ not block["lines"][-1].strip(): pass # collapse multiple blank lines else: block["lines"].append(line) # pprint(blocks) # Process the blocks into a list of command info dicts. def podrender(pod): rendered = pod rendered = re.sub("F<(.*?)>", r"\1", rendered) rendered = re.sub("I<(.*?)>", r"*\1*", rendered) def quoteifspaced(match): if ' ' in match.group(1): return "'%s'" % match.group(1) else: return match.group(1) rendered = re.sub("C<(.*?)>", quoteifspaced, rendered) def linkrepl(match): content = match.group(1) if content.startswith("/"): content = content[1:] if "/" in content: page, section = content.split("/", 1) content = "%s in '%s'" % (section, page) else: content = "'%s'" % content return content rendered = re.sub("L<(.*?)>", linkrepl, rendered) return rendered # These perl built-ins are grouped in perlfunc.pod. commands = [] WIDTH = 60 # desc field width syscalls = """ getpwnam getgrnam gethostbyname getnetbyname getprotobyname getpwuid getgrgid getservbyname gethostbyaddr getnetbyaddr getprotobynumber getservbyport getpwent getgrent gethostent getnetent getprotoent getservent setpwent setgrent sethostent setnetent setprotoent setservent endpwent endgrent endhostent endnetent endprotoent endservent """.split() calltip_skips = "sub use require".split() for block in blocks: name, sigs, lines = block["name"], block["sigs"], block["lines"] if name == "-X": # template for -r, -w, -f, ... pattern = re.compile(r"^ (-\w)\t(.*)$") tlines = [line for line in lines if pattern.match(line)] for tline in tlines: tname, tdesc = pattern.match(tline).groups() tsigs = [s.replace("-X", tname) for s in sigs] command = { "name": tname, "sigs": tsigs, "desc": textwrap.fill(tdesc, WIDTH) } commands.append(command) elif name in ("m", "q", "qq", "qr", "qx", "qw", "s", "tr", "y"): operators = { "m": """\ m/PATTERN/cgimosx /PATTERN/cgimosx Searches a string for a pattern match, and in scalar context returns true if it succeeds, false if it fails. """, "q": """\ q/STRING/ 'STRING' A single-quoted, literal string. """, "qq": """\ qq/STRING/ "STRING" A double-quoted, interpolated string. """, "qr": """\ qr/STRING/imosx Quotes (and possibly compiles) STRING as a regular expression. """, "qx": """\ qx/STRING/ `STRING` A string which is (possibly) interpolated and then executed as a system command. """, "qw": """\ qw/STRING/ Evaluates to a list of the words extracted out of STRING, using embedded whitespace as the word delimiters. """, "s": """\ s/PATTERN/REPLACEMENT/egimosx Searches a string for a pattern, and if found, replaces that pattern with the replacement text and returns the number of substitutions made. Otherwise it returns the empty string. """, "tr": """\ tr/SEARCHLIST/REPLACEMENTLIST/cds y/SEARCHLIST/REPLACEMENTLIST/cds Transliterates all occurrences of the characters found in the search list with the corresponding character in the replacement list. It returns the number of characters replaced or deleted. """, "y": """\ tr/SEARCHLIST/REPLACEMENTLIST/cds y/SEARCHLIST/REPLACEMENTLIST/cds Transliterates all occurrences of the characters found in the search list with the corresponding character in the replacement list. It returns the number of characters replaced or deleted. """, } sigs = [] desclines = None for line in operators[name].splitlines(0): if desclines is not None: desclines.append(line.strip()) elif not line.strip(): desclines = [] else: sigs.append(line.strip()) command = { "name": name, "sigs": sigs, "desc": textwrap.fill(' '.join(desclines), WIDTH) } commands.append(command) elif name in syscalls: desc = "Performs the same function as the '%s' system call." % name desc = textwrap.fill(desc, WIDTH) getterListContext = { "getpw": "\n" " ($name,$passwd,$uid,$gid,$quota,$comment,\n" " $gcos,$dir,$shell,$expire) = %s", "getgr": "\n ($name,$passwd,$gid,$members) = %s", "gethost": "\n ($name,$aliases,$addrtype,$length,@addrs) = %s", "getnet": "\n ($name,$aliases,$addrtype,$net) = %s", "getproto": "\n ($name,$aliases,$proto) = %s", "getserv": "\n ($name,$aliases,$port,$proto) = %s", } getterScalarContext = { "getgrent": "$name = %s", "getgrgid": "$name = %s", "getgrnam": "$gid = %s", "gethostbyaddr": "$name = %s", "gethostbyname": "$addr = %s", "gethostent": "$name = %s", "getnetbyaddr": "$name = %s", "getnetbyname": "$net = %s", "getnetent": "$name = %s", "getprotobyname": "$num = %s", "getprotobynumber": "$name = %s", "getprotoent": "$name = %s", "getpwent": "$name = %s", "getpwnam": "$uid = %s", "getpwuid": "$name = %s", "getservbyname": "$num = %s", "getservbyport": "$name = %s", "getservent": "$name = %s", } for prefix, template in getterListContext.items(): if name.startswith(prefix): desc += template % sigs[0] if name in getterScalarContext: desc += "\nin list context or:\n "\ + getterScalarContext[name] % sigs[0] command = {"name": name, "desc": desc, "sigs": sigs} commands.append(command) elif name == "shmread": desc = """\ Reads the System V shared memory segment ID starting at position POS for size SIZE by attaching to it, copying out, and detaching from it. """ desc = ' '.join([ln.strip() for ln in desc.splitlines(0)]) command = { "name": name, "sigs": sigs, "desc": textwrap.fill(desc, WIDTH) } commands.append(command) elif name == "shmwrite": desc = """\ Writes the System V shared memory segment ID starting at position POS for size SIZE by attaching to it, copying in, and detaching from it. """ desc = ' '.join([ln.strip() for ln in desc.splitlines(0)]) command = { "name": name, "sigs": sigs, "desc": textwrap.fill(desc, WIDTH) } commands.append(command) elif name in calltip_skips: continue # just drop the sub calltip: annoying else: # Parsing the description from the full description: # Pull out the first sentence up to a maximum of three lines # and one paragraph. If the first *two* sentences fit on the # first line, then use both. desc = "" sentencePat = re.compile(r"([^\.]+(?:\. |\.$))") if name in ("dbmclose", "dbmopen"): # Skip the first paragraph: "[This function...superceded by" lines = lines[lines.index('') + 1:] elif name == "do": # Skip the first sentence: "Not really a function." end = sentencePat.match(lines[0]).span()[1] lines[0] = lines[0][end:].lstrip() for i, line in enumerate(lines): if not line.strip(): break sentences = sentencePat.findall(line) if not sentences: desc += line + ' ' continue elif i == 0 and len(sentences) > 1: desc += ' '.join([s.strip() for s in sentences[:2]]) else: desc += sentences[0].strip() break command = { "name": name, "sigs": sigs, "desc": textwrap.fill(podrender(desc), WIDTH) } commands.append(command) # for command in commands: # print # print banner(command["name"], '-') # print '\n'.join(command["sigs"]) # print # print command["desc"] # Generate the CIX for each function. module_elt = SubElement(cixfile, "scope", ilk="blob", name="*") # "built-ins" module for command in commands: name, sigs, desc = command["name"], command["sigs"], command["desc"] func_elt = SubElement(module_elt, "scope", ilk="function", name=name) if sigs: func_elt.set("signature", '\n'.join(sigs)) if desc: doclines = desc.split('\n')[:3] # doclines = parseDocSummary(doclines) doc = '\n'.join(doclines) func_elt.set("doc", doc) # Generate the CIX. prettify(root) tree = ElementTree(root) stream.write('<?xml version="1.0" encoding="UTF-8"?>\n') tree.write(stream)
</feed>""" etree = ElementTree(file=StringIO.StringIO(content)) feed = XML(content) print etree print feed #print len(feed) #print feed[0] #print feed.keys() ATOM = "http://www.w3.org/2005/Atom" entry = etree.getiterator('{%s}entry'%ATOM)[0] new_lin = SubElement(entry, '{%s}link'%ATOM) new_lin.set('rel', 'source') new_lin.set('href', 'http://somthing.org') title = etree.findall('{%s}title'%ATOM)[0] print tostring(title) missing = etree.findall('{%s}missing'%ATOM) print missing for e in etree.findall('//{%s}link'%ATOM): print e.get('rel', 'alternate') s = StringIO.StringIO() etree.write(s) s.seek(0)
import httplib2 import time BASE = "http://localhost:3000/" client = httplib2.Http(".cache") def showNotes(): headers, xml = client.request(BASE + "notes.xml") doc = ElementTree.fromstring(xml) for note in doc.findall('note'): print "%s: %s" % (note.find('date').text, note.find('body').text) newNote = Element("note") date = SubElement(newNote, "date") date.attrib['type'] = "date" date.text = time.strftime("%Y-%m-%d", time.localtime()) body = SubElement(newNote, "body") body.text = "A test note" headers, ignore = client.request(BASE + "notes.xml", "POST", body=tostring(newNote), headers={'content-type': 'application/xml'}) newURI = headers['location'] modifiedBody = Element("note") body = SubElement(modifiedBody, "body") body.text = "This note has been modified"
def PackageToXml(pkg,summary="N/A",trainingDataId='N/A', dataPerformance=[], recommendedThreshold=None, classDescriptions=[], modelType=None, modelOrganism=None): """ generates XML for a package that follows the RD_Model.dtd If provided, dataPerformance should be a sequence of 2-tuples: ( note, performance ) where performance is of the form: ( accuracy, avgCorrectConf, avgIncorrectConf, confusionMatrix, thresh, avgSkipConf, nSkipped ) the last four elements are optional """ head = Element("RDModelInfo") name = SubElement(head,"ModelName") notes = pkg.GetNotes() if not notes: notes = "Unnamed model" name.text = notes summ = SubElement(head,"ModelSummary") summ.text = summary calc = pkg.GetCalculator() descrs = SubElement(head,"ModelDescriptors") for name,summary,func in zip(calc.GetDescriptorNames(),calc.GetDescriptorSummaries(),calc.GetDescriptorFuncs()): descr = SubElement(descrs,"Descriptor") elem = SubElement(descr,"DescriptorName") elem.text = name elem = SubElement(descr,"DescriptorDetail") elem.text = summary if hasattr(func,'version'): vers = SubElement(descr,"DescriptorVersion") major,minor,patch = func.version.split('.') elem = SubElement(vers,"VersionMajor") elem.text = major elem = SubElement(vers,"VersionMinor") elem.text = minor elem = SubElement(vers,"VersionPatch") elem.text = patch elem = SubElement(head,"TrainingDataId") elem.text = trainingDataId for description,perfData in dataPerformance: dataNode = SubElement(head,"ValidationData") note = SubElement(dataNode,'ScreenNote') note.text = description perf = SubElement(dataNode,"PerformanceData") _ConvertModelPerformance(perf,perfData) if recommendedThreshold: elem = SubElement(head,"RecommendedThreshold") elem.text=str(recommendedThreshold) if classDescriptions: elem = SubElement(head,"ClassDescriptions") for val,text in classDescriptions: descr = SubElement(elem,'ClassDescription') valElem = SubElement(descr,'ClassVal') valElem.text = str(val) valText = SubElement(descr,'ClassText') valText.text = str(text) if modelType: elem = SubElement(head,"ModelType") elem.text=modelType if modelOrganism: elem = SubElement(head,"ModelOrganism") elem.text=modelOrganism hist = SubElement(head,"ModelHistory") revision = SubElement(hist,"Revision") tm = time.localtime() date = SubElement(revision,"RevisionDate") elem = SubElement(date,"Year") elem.text=str(tm[0]) elem = SubElement(date,"Month") elem.text=str(tm[1]) elem = SubElement(date,"Day") elem.text=str(tm[2]) note = SubElement(revision,"RevisionNote") note.text = "Created" return ElementTree(head)
fieldset = SubElement(module, "fieldset") f_legend = SubElement(fieldset, "legend") f_element = SubElement(fieldset, "element") e_type = SubElement(f_element, "type") e_name = SubElement(f_element, "name") e_value = SubElement(f_element, "value") e_desc = SubElement(f_element, "description") e_input = SubElement(f_element, "input") e_output = SubElement(f_element, "output") #e_required = SubElement(f_element, "required") #e_default = SubElement(f_element, "default") #e_selected = SubElement(f_element, "selected") permissions = SubElement(module, "permissions") permissions.set("clearance","") p_owner = SubElement(permissions, "owner").text = "7" p_everyone = SubElement(permissions, "everyone") p_group = SubElement(permissions, "group") p_groupname = SubElement(permissions, "groupname") methodName = SubElement(module, "methodName").text = mod_name createdBy = SubElement(module, "createdBy").text = uname dateCreated = SubElement(module, "dateCreated").text = runtime data = SubElement(module, "data").text = data lab = SubElement(module, "lab"). text = labname.split(".")[1] indent(module) #dump(module) mod_str = dump(module)
def writeLine(self, objGeo, elemPlace): elemGeo = Element('LineString') elemCoords = SubElement(elemGeo, "coordinates") elemCoords.text = self.addCoordinates(objGeo) return elemGeo