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 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 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 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)
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) print s.getvalue()
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)
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) #ElementTree(module).write(filename, encoding="UTF-8", xml_declaration=True)
def rec2format (context, recs, output_format='mods'): try: config = context.config source_catalog = context.get_source_catalog() ctm = source_catalog.get_complete_mapping() root = Element('modsCollection') root.set('xmlns', 'http://www.loc.gov/mods/v3') root.set('xmlns:xlink', 'http://www.w3.org/1999/xlink') root.set('version', '3.0') root.set('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance') root.set('xsi:schemaLocation', 'http://www.loc.gov/mods/v3 http://www.loc.gov/standards/mods/v3/mods-3-0.xsd') for rec in recs: try: mods = SubElement(root, 'mods') mm = rec.get_mapped_metadata(ctm) if mm.get('title', ''): titleInfo = SubElement(mods, 'titleInfo') title = SubElement(titleInfo, 'title') title.text = mm['title'] if mm.get('author', []): for au in mm['author']: name = SubElement(mods, 'name') name.set('type', 'personal') # Note: bibutils looks for family/given split if ' ' in au: family, given = au.split(' ') namePart = SubElement(name, 'namePart') namePart.set('type', 'given') namePart.text = given namePart = SubElement(name, 'namePart') namePart.set('type', 'family') namePart.text = family else: namePart = SubElement(name, 'namePart') namePart.set('type', 'family') namePart.text = au role = SubElement(name, 'role') roleTerm = SubElement(role, 'roleTerm') roleTerm.set('type', 'text') roleTerm.text = 'author' typeOfResource = SubElement(mods, 'typeOfResource') typeOfResource.text = 'text' # The following block is about the "host" journal in which this # article appeared relatedItem = SubElement(mods, 'relatedItem') relatedItem.set('type', 'host') titleInfo = SubElement(relatedItem, 'titleInfo') title = SubElement(titleInfo, 'title') title.text = mm['journal'] if mm.get('issn', ''): identifier = SubElement(relatedItem, 'identifier') identifier.set('type', 'issn') identifier.text = mm['issn'] originInfo = SubElement(relatedItem, 'originInfo') issuance = SubElement(originInfo, 'issuance') issuance.text = 'continuing' part = SubElement(relatedItem, 'part') if mm.get('volume', ''): detail = SubElement(part, 'detail') detail.set('type', 'volume') number = SubElement(detail, 'number') number.text = mm['volume'] if mm.get('issue', ''): detail = SubElement(part, 'detail') detail.set('type', 'issue') number = SubElement(detail, 'number') number.text = mm['issue'] if mm.get('pages', ''): extent = SubElement(part, 'extent') extent.set('unit', 'page') if '-' in mm['pages']: start, end = mm['pages'].split('-') st = SubElement(extent, 'start') st.text = start en = SubElement(extent, 'end') en.text = end else: st = SubElement(extent, 'start') st.text = mm['pages'] if mm.get('pubdate', ''): date = SubElement(part, 'date') date.text = mm['pubdate'][:4] for subtype in ['subject', 'keyword']: for sub in mm.get(subtype, []): subject = SubElement(mods, 'subject') if subtype == 'subject': subject.set('authority', 'mesh') topic = SubElement(subject, 'topic') topic.text = sub except: print traceback.print_exc() mods_out = etree.tostring(root) if output_format == 'mods': return mods_out elif output_format in FORMATS.keys(): bibutils_format_name = FORMATS[output_format] filename = '%s/records-%s.mods' % (config.temp_image_dir, time.time()) fp = open(filename, 'w') fp.write(mods_out) fp.close() bibutils_call = '/usr/local/bin/xml2%s %s > %s.%s' % (bibutils_format_name, filename, filename, output_format) os.system(bibutils_call) time.sleep(3) return open('%s.%s' % (filename, output_format)).read() except: print traceback.print_exc()