Ejemplo n.º 1
0
def ETFromObj(obj):
    """obj can be
    1) a string that ends with .xml -> the file is parsed and the resulting ElementTree returned
    2) a string that ends with .xml.gz -> the file is unzipped, parsed, and the resulting ElementTree is returned
    3) an open input stream -> the input is parsed and the resulting ElementTree is returned
    4) an ElementTree or an Element -> obj is returned as-is, nothing is done"""
    if isinstance(obj, str) or isinstance(obj, unicode):
        if obj.endswith(".xml.gz"):
            fStream = GzipFile(obj, "rt")
            #fStream = codecs.getreader("utf-8")(GzipFile(obj,"rt"))
        elif obj.endswith(".xml") or obj.endswith(".svg") or obj.endswith(
                ".nxml") or obj.endswith(".csml"):
            fStream = open(obj, "rt")
            #fStream=codecs.open(obj, "rt", "utf-8")
        else:
            raise ValueError(
                "%s: File format not recognized (expected .xml or .xml.gz)" %
                obj)
        return ElementTree.parse(fStream)
    elif isinstance(obj,
                    ElementTree.ElementTree) or ElementTree.iselement(obj):
        return obj
    else:
        #not a string, not a tree, not an element, should be a stream
        #let's parse it
        return ElementTree.parse(obj)
Ejemplo n.º 2
0
def track_chart(username, start, end):
    """Retrieves the track chart for a single week.
	The data is returned as an ordered list of (trackname, artist, plays) tuples.
	Implements caching of the XML."""

    # Get the XML if it doesn't already exist
    filename = os.path.join(cachedir,
                            "trackchart-%s-%s-%s.xml" % (username, start, end))
    if not os.path.exists(filename):
        fo = fetch("user/%s/weeklytrackchart.xml?from=%i&to=%i" %
                   (username, start, end))
        import shutil
        shutil.copyfileobj(fo, open(filename, "w"))

    # Load and parse the XML
    tree = ET.parse(filename)
    root = tree.getroot()

    # Check the type
    assert root.tag == "weeklytrackchart", "This is not a Weekly Chart List"

    # Now, loop over the tracks
    tracks = []
    for tag in root.findall("track"):
        artist_tag = tag.find("artist")
        artist_name = artist_tag.text

        name = tag.find("name").text
        plays = int(tag.find("playcount").text)

        tracks.append((name, artist_name, plays))

    return tracks
Ejemplo n.º 3
0
def track_chart(username, start, end):
	"""Retrieves the track chart for a single week.
	The data is returned as an ordered list of (trackname, artist, plays) tuples.
	Implements caching of the XML."""
	
	# Get the XML if it doesn't already exist
	filename = os.path.join(cachedir, "trackchart-%s-%s-%s.xml" % (username, start, end))
	if not os.path.exists(filename):
		fo = fetch("user/%s/weeklytrackchart.xml?from=%i&to=%i" % (username, start, end))
		import shutil
		shutil.copyfileobj(fo, open(filename, "w"))
	
	# Load and parse the XML
	tree = ET.parse(filename)
	root = tree.getroot()
	
	# Check the type
	assert root.tag == "weeklytrackchart", "This is not a Weekly Chart List"
	
	# Now, loop over the tracks
	tracks = []
	for tag in root.findall("track"):
		artist_tag = tag.find("artist")
		artist_name = artist_tag.text
		
		name = tag.find("name").text
		plays = int(tag.find("playcount").text)
		
		tracks.append((name, artist_name, plays))
	
	return tracks
Ejemplo n.º 4
0
def expand_toolbox(toolboxFile, outdir, toolboxDirName=None, force=0):
    """ Write out the contents of toolboxFile to outdir.
        Callers should wrap this in a try/except block to allow unexpected input.
    """
    tree = ET.parse(toolboxFile)
    root = tree.getroot()
    global _kpf_version
    tmp_ver = root.get('kpf_version')
    if tmp_ver is not None:
        _kpf_version = int(tmp_ver)
    prefSets = root.findall("preference-set")
    for ps in prefSets:
        root.remove(ps)
    dirTree = TreeBuilder().reassembleTree(tree)

    if not os.path.exists(outdir):
        # Allow an exception here
        os.makedirs(outdir)
    elif not os.path.isdir(outdir):
        raise ExpandToolboxException(
            "outdir %s is not a directory, not expanding" % outdir)
    os.chdir(outdir)
    if toolboxDirName is None:
        toolboxDirName = koToolbox2.DEFAULT_TARGET_DIRECTORY
    if not os.path.exists(toolboxDirName):
        os.makedirs(toolboxDirName)
    os.chdir(toolboxDirName)
    obsoleteItems = []
    TreeWalker(obsoleteItems, force).expandTree(dirTree)
    #todo: Write out the misc parts
    if obsoleteItems:
        log.warn("The following items weren't converted: %s\n", obsoleteItems)
        # "\n".join(["%s: %s" % (x[0], x[1]) for x in obsoleteItems]))
    return 0
Ejemplo n.º 5
0
def expand_toolbox(toolboxFile, outdir, toolboxDirName=None, force=0):
    """ Write out the contents of toolboxFile to outdir.
        Callers should wrap this in a try/except block to allow unexpected input.
    """
    tree = ET.parse(toolboxFile)
    root = tree.getroot()
    global _kpf_version
    tmp_ver = root.get("kpf_version")
    if tmp_ver is not None:
        _kpf_version = int(tmp_ver)
    prefSets = root.findall("preference-set")
    for ps in prefSets:
        root.remove(ps)
    dirTree = TreeBuilder().reassembleTree(tree)

    if not os.path.exists(outdir):
        # Allow an exception here
        os.makedirs(outdir)
    elif not os.path.isdir(outdir):
        raise ExpandToolboxException("outdir %s is not a directory, not expanding" % outdir)
    os.chdir(outdir)
    if toolboxDirName is None:
        toolboxDirName = koToolbox2.DEFAULT_TARGET_DIRECTORY
    if not os.path.exists(toolboxDirName):
        os.makedirs(toolboxDirName)
    os.chdir(toolboxDirName)
    obsoleteItems = []
    TreeWalker(obsoleteItems, force).expandTree(dirTree)
    # todo: Write out the misc parts
    if obsoleteItems:
        log.warn("The following items weren't converted: %s\n", obsoleteItems)
        # "\n".join(["%s: %s" % (x[0], x[1]) for x in obsoleteItems]))
    return 0
Ejemplo n.º 6
0
    def handle(self, *args, **options):
        if len(args) < 1:
            raise CommandError("No filename specify")

        for filename in args:
            logs.note("=== Parse file " + filename + " ===")
            try:
                dom = ET.parse(open(filename, "r"))
                root = dom.getroot()
            except:
                raise CommandError("Unable to open and parse file: " +
                                   filename)

            for data in root:
                t = data.tag
                if t == "El":  #Atoms
                    self.parse_atom(data)
                elif t == "AA":  #animo acids
                    self.parse_amino_acid(data)
                elif t == "Mod":  #PTM
                    self.parse_modification(data)
                elif t == "CAgt":  #Enzyme
                    self.parse_enzyme(data)
                elif t == "AIn":  #ions
                    self.parse_ion(data)
                else:
                    raise CommandError("Unknow data type")
    def handle(self, *args, **options):
        if len(args)<1:
            raise CommandError("No filename specify")

        for filename in args:
            logs.note("=== Parse file "+filename+" ===")
            try:
                dom = ET.parse(open(filename, "r"))
                root = dom.getroot()
            except:
                raise CommandError("Unable to open and parse file: " + filename)

            for data in root:
                t = data.tag
                if t == "El": #Atoms
                    self.parse_atom(data)
                elif t == "AA" : #animo acids
                    self.parse_amino_acid(data)
                elif t =="Mod": #PTM
                    self.parse_modification(data)
                elif t == "CAgt": #Enzyme
                    self.parse_enzyme(data)
                elif t == "AIn": #ions
                    self.parse_ion(data)
                else:
                    raise CommandError("Unknow data type")
Ejemplo n.º 8
0
def rss(xml, instance, author):
    """Imports from RSS Feed into Everything!"""
    from django.template.defaultfilters import slugify

    xml = ET.parse(StringIO(xml["content"]))
    root = xml.getroot().find("channel")

    for i in root.findall("item"):
        title = i.findtext("title")
        slug = slugify(title)
        try:
            e = Entry.objects.get(slug=slug)
        except:
            tags = []
            try:
                for cat in i.findall("category"):
                    tags.append(cat.text)
                tagstring = string.join(tags, ", ")
            except:
                tagstring = ""

            e = Entry(
                title=title,
                content=i.findtext("description"),
                tags=tagstring,
                slug=slug,
                status="public",
                app=instance,
                author_id=int(author),
            )
            e.save()
            e.created = dateutil.parser.parse(i.findtext("pubDate"))
            e.save()
Ejemplo n.º 9
0
def getDocument(s):
    xmlDocResults=''
    try:
        getSettings()
        IOStreamer=StringIO(s)
        doc = ElementTree.parse(IOStreamer).getroot()
        if(debugProg["flag"]==1):
            debug_Info= "\nXML file execution command:" + str(doc.get('command')) + "\n"
            if(debugProg["print"]==1): print debug_Info
            writefile(debug_Info,debugProg["file"])
            debug_Info=""
        if(doc.get('command')):
            if(doc.get('command')=="INSERT"):
                xmlDocResults=goInsert(doc)
            if(doc.get('command')=="SELECT"):
                xmlDocResults=goSelect(doc)
            if(doc.get('command')=="JOIN"):
                xmlDocResults=goJoin(doc)
        else:
            xmlDocResults="<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
            xmlDocResults=xmlDocResults + "<RELATIONS name=\"ERROR\">\n\t<REL name=\"ERROR_1\">\n\t\t<ATT name=\"error_message\">Document command missing:Either SELECT or INSERT."
            xmlDocResults=xmlDocResults + "</ATT>\n\t</REL>\n</RELATIONS>\n"
    except:
        typ, value = sys.exc_info()[:2]
        if(debugProg["print"]==1): print "Error:", typ, "->", value
        if(debugProg["flag"]==1):
                debug_Info= "\nError:" + str(typ) + " -> "+ str(value) +"\n"
                writefile(debug_Info,debugProg["file"])
                debug_Info=""
        xmlDocResults="<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
        xmlDocResults=xmlDocResults + "<RELATIONS name=\"ERROR\">\n\t<REL name=\"ERROR_1\">\n\t\t<ATT name=\"error_message\">" + str(typ) + " -> " + str(value)
        xmlDocResults=xmlDocResults + "</ATT>\n\t</REL>\n</RELATIONS>\n"
	
    return xmlDocResults
Ejemplo n.º 10
0
def getRevision(filename):
    from subprocess import Popen, PIPE
    import cElementTree as et

    f = Popen(['svn', 'info', filename, '--xml'], stdout=PIPE).stdout
    tree = et.parse(f)
    return int(tree.find('entry').get('revision'))
Ejemplo n.º 11
0
 def handle(self, ontology_file=settings.ONTOLOGY_FILE, *args, **options):
     try:
         file = open(ontology_file)
         tree = ElementTree.parse(file) 
     except Exception, e:
         raise CommandError(e.message)
         sys.exit(0)
Ejemplo n.º 12
0
def load(project, filename):
    f = open(filename)
    header = f.read(10)

    data = f.read()
    if header == "GRAPHITEGZ":
        data = zlib.decompress(data)
    elif header == "GRAPHITEBZ":
        data = bz2.decompress(data)

    sio = StringIO.StringIO(data)
    try:
        tree = xml.parse(sio)
    finally:
        f.close()
        sio.close()

    root = tree.getroot()

    for welem in root.findall("Worksheet"):
        wsheet = project.new(Worksheet, welem.get("name"))

        for celem in welem:
            if celem.tag == "CalcColumn":
                setattr(wsheet, celem.get("name"), [])
                wsheet[celem.get("name")]._expr = celem.text
            elif celem.tag == "Column":
                safedict = {"__builtins__": {"True": True, "False": False, "None": None}}
                setattr(wsheet, celem.get("name"), pickle.loads(eval(celem.text, safedict)))

    for w in project.top.contents():
        for c in w:
            if hasattr(c, "_expr"):
                c.expr = c._expr
                del c._expr

    for gelem in root.findall("Graph"):
        graph = project.new(Graph, eval(gelem.get("name")))

        # axes
        for aelem in gelem.findall("Axis"):
            axisid = pyget(aelem, "id")
            if axisid == 2:  # QwtPlot.xBottom
                xmin, xmax = pyget(aelem, "limits")
                graph.xtype = ["linear", "log"][pyget(aelem, "logscale")]
                graph.xtitle = pyget(aelem, "title")
            elif axisid == 0:  # QwtPlot.yLeft
                ymin, ymax = pyget(aelem, "limits")
                graph.ytype = ["linear", "log"][pyget(aelem, "logscale")]
                graph.ytitle = pyget(aelem, "title")
        graph.zoom(xmin, xmax, ymin, ymax)

        # datasets
        for delem in gelem.findall("Dataset"):
            # data
            wsheet = project.top[pyget(delem, "worksheet")]
            colx = pyget(delem, "xcolumn")
            coly = pyget(delem, "ycolumn")
            rangemin, rangemax = pyget(delem, "range")
            ds = graph.add(wsheet[colx], wsheet[coly])
Ejemplo n.º 13
0
def getRevision(filename):
    from subprocess import Popen, PIPE
    import cElementTree as et

    f = Popen(['svn', 'info', filename, '--xml'], stdout=PIPE).stdout
    tree = et.parse(f)
    return int(tree.find('entry').get('revision'))
Ejemplo n.º 14
0
 def parse(self):
     # XXX support HTTP URI's
     self.tree = ElementTree.parse(self.uri, NamespaceParser())
     self.root = self.tree.getroot()
     if self.root.tagName != "catalog":
         raise "Invalid catalog file [%s] root tag [%s]" % (self.uri, self.root.tagName)
     self.parent_map = dict((c, p) for p in self.tree.getiterator() for c in p)
     self._parseNode(self.root)
Ejemplo n.º 15
0
 def parse(self):
     self.tree = ElementTree.parse(self.filename, NamespaceParser())
     self.root = self.tree.getroot()
     if self.root.tagName != "grammar":
         raise "Invalid RNG file [%s] root tag [%s]" % (self.filename, self.root.tagName)
     self.parent_map = dict((c, p) for p in self.tree.getiterator() for c in p)
     self.parseNode(self.root)
     self.dataset.resolveRefs()
Ejemplo n.º 16
0
 def run(self, connect, report_name,
         ext='.html'):
     design = ET.parse((self._templates / (report_name + ext)).fp())
     # TODO: return design as a value from parse_design;
     # pass it to start_page.
     self._parse_design(design)
     self._init_page(self._orientation, self._report_header)
     self._detail(self._data(connect, self._breaks, self._sql))
Ejemplo n.º 17
0
 def __init__(self, url, _xmlns, root=None):
     self.url = url
     dom = ET.parse(urllib2.urlopen(url))
     self.xmlns = _xmlns
     if root is None:
         self.root = dom.getroot()
     else:
         self.root = root
     self.process()
Ejemplo n.º 18
0
 def read_object_defs(self):
     self.logger.debug("==============================================================")
     objects = []
     for config in self.config_location:
         self.logger.debug("* Parsing %s" % config)
         components = etree.parse(config).getroot()
         objects.extend([self._convert_component(component) for component in components])
     self.logger.debug("==============================================================")
     return objects
Ejemplo n.º 19
0
 def __init__(self, url, _xmlns, root=None):
     self.url = url
     dom = ET.parse(urllib2.urlopen(url))
     self.xmlns = _xmlns
     if root is None:
         self.root = dom.getroot()
     else:
         self.root = root
     self.process()
Ejemplo n.º 20
0
def main(argv=[]):
    if len(argv) != 4:
        print("Usage:", argv[0], "IN-XML OUT-TEXT OUT-SO", file=sys.stderr)
        return -1

    in_fn, out_txt_fn, out_so_fn = argv[1:]

    # "-" for STDIN / STDOUT
    if in_fn == "-":
        in_fn = "/dev/stdin"
    if out_txt_fn == "-":
        out_txt_fn = "/dev/stdout"
    if out_so_fn == "-":
        out_so_fn = "/dev/stdout"

    tree = ET.parse(in_fn)
    root = tree.getroot()

    # normalize space in target elements
    normalize_space(root, ['segment'])
    add_newlines(root)

    text, standoffs = text_and_standoffs(root)

    # eliminate extra space
    for s in standoffs:
        s.strip()

    # filter
    standoffs = [s for s in standoffs if not s.tag() in EXCLUDED_TAG]

    # convert selected elements
    converted = []
    for s in standoffs:
        if s.tag() in convert_function:
            converted.extend(convert_function[s.tag()](s))
        else:
            converted.append(s)
    standoffs = converted

    for so in standoffs:
        try:
            so.compress_text(MAXIMUM_TEXT_DISPLAY_LENGTH)
        except AttributeError:
            pass

    # open output files
    out_txt = open(out_txt_fn, "wt")
    out_so = open(out_so_fn, "wt")

    out_txt.write(text.encode("utf-8"))
    for so in standoffs:
        print(so, file=out_so)

    out_txt.close()
    out_so.close()
Ejemplo n.º 21
0
def main(argv=[]):
    if len(argv) != 4:
        print >> sys.stderr, "Usage:", argv[0], "IN-XML OUT-TEXT OUT-SO"
        return -1

    in_fn, out_txt_fn, out_so_fn = argv[1:]

    # "-" for STDIN / STDOUT
    if in_fn == "-":
        in_fn = "/dev/stdin"
    if out_txt_fn == "-":
        out_txt_fn = "/dev/stdout"
    if out_so_fn == "-":
        out_so_fn = "/dev/stdout"

    tree = ET.parse(in_fn)
    root = tree.getroot()

    # normalize space in target elements
    normalize_space(root, ['segment'])
    add_newlines(root)

    text, standoffs = text_and_standoffs(root)

    # eliminate extra space
    for s in standoffs:
        s.strip()

    # filter
    standoffs = [s for s in standoffs if not s.tag() in EXCLUDED_TAG]

    # convert selected elements
    converted = []
    for s in standoffs:
        if s.tag() in convert_function:
            converted.extend(convert_function[s.tag()](s))
        else:
            converted.append(s)
    standoffs = converted

    for so in standoffs:
        try:
            so.compress_text(MAXIMUM_TEXT_DISPLAY_LENGTH)
        except AttributeError:
            pass

    # open output files
    out_txt = open(out_txt_fn, "wt")
    out_so = open(out_so_fn, "wt")

    out_txt.write(text.encode("utf-8"))
    for so in standoffs:
        print >> out_so, so

    out_txt.close()
    out_so.close()
Ejemplo n.º 22
0
 def _open(self):
     try:
         return self._repo
     except AttributeError:
         try:
             r = self.xml_path +self.repo
             self._repo =   cElementTree.parse(r).getroot()
             return self._repo
         except:
             self.pk.error(ERROR_REPO_CONFIGURATION_ERROR," The file %s not parsed submit a issue at http://issues.foresightlinux.org" % self.repo )
Ejemplo n.º 23
0
 def read_object_defs(self):
     self.logger.debug("==============================================================")
     # Reset, in case the file is re-read
     self.objects = []
     for config in self.config_location:
         self.logger.debug("* Parsing %s" % config)
         beans = etree.parse(config).getroot()
         self.objects.extend([self._convert_bean(bean) for bean in beans])
     self.logger.debug("==============================================================")
     return self.objects
Ejemplo n.º 24
0
 def setUp(self):
     #This uses a canned file so that im not dealing with changed values to
     #check two parsers against each other...
     print '\nIsbndbBookTest:',
     elem = ElementTree.parse('tests/xml/book_test.xml')
     elem = elem.getroot()
     elem = elem.find('BookList')
     elem = elem.find('BookData')
     self.book = IsbndbBook(elem)
     self.comparer = Comparer()
Ejemplo n.º 25
0
    def getXmlTree(self):
         """
         Sends the xml file into the ElementTree library's parser. Allows for the program to get the schemaLocation before parsing the xml against the schema.

         No parameters.
         """

         try:

              tree =  ET.parse(self.xmlFileInput)
              if self.verbose:
                   print "XML file parsed by the ElementTree library Suceessfully..." 
         except Exception,e:
              print
              print "Program Error: The ElementTree library's parse function was unable to read your"
              print  "XML File correctly. The following are its errors (the program will halt):"
              print
              
              tree =  ET.parse(self.xmlFileInput)
Ejemplo n.º 26
0
 def setUp(self):
     #This uses a canned file so that im not dealing with changed values to
     #check two parsers against each other...
     print '\nIsbndbBookTest:',
     elem = ElementTree.parse('tests/xml/book_test.xml')
     elem = elem.getroot()
     elem = elem.find('BookList')
     elem = elem.find('BookData')
     self.book = IsbndbBook(elem)
     self.comparer = Comparer()
Ejemplo n.º 27
0
    def getXmlTree(self):
        """
         Sends the xml file into the ElementTree library's parser. Allows for the program to get the schemaLocation before parsing the xml against the schema.

         No parameters.
         """

        try:

            tree = ET.parse(self.xmlFileInput)
            if self.verbose:
                print "XML file parsed by the ElementTree library Suceessfully..."
        except Exception, e:
            print
            print "Program Error: The ElementTree library's parse function was unable to read your"
            print "XML File correctly. The following are its errors (the program will halt):"
            print

            tree = ET.parse(self.xmlFileInput)
Ejemplo n.º 28
0
 def _open(self):
     try:
         return self._repo
     except AttributeError:
         try:
             self._repo = cElementTree.parse(self.xml_file).getroot()
             return self._repo
         except SyntaxError as e:
             self.pk.error(ERROR_REPO_CONFIGURATION_ERROR, "Failed to parse %s: %s. A cache refresh should fix this." %
                     (self.xml_file, str(e)))
Ejemplo n.º 29
0
 def parse(self):
     self.tree = ElementTree.parse(self.filename, NamespaceParser())
     self.root = self.tree.getroot()
     if self.root.tagName != "grammar":
         raise "Invalid RNG file [%s] root tag [%s]" % (self.filename,
                                                        self.root.tagName)
     self.parent_map = dict(
         (c, p) for p in self.tree.getiterator() for c in p)
     self.parseNode(self.root)
     self.dataset.resolveRefs()
Ejemplo n.º 30
0
 def parse(self):
     # XXX support HTTP URI's
     self.tree = ElementTree.parse(self.uri, NamespaceParser())
     self.root = self.tree.getroot()
     if self.root.tagName != "catalog":
         raise "Invalid catalog file [%s] root tag [%s]" % (
             self.uri, self.root.tagName)
     self.parent_map = dict((
         c, p) for p in self.tree.getiterator() for c in p)
     self._parseNode(self.root)
Ejemplo n.º 31
0
def main(argv=[]):
    if len(argv) != 4:
        print >> sys.stderr, "Usage:", argv[0], "IN-XML OUT-TEXT OUT-SO"
        return -1

    in_fn, out_txt_fn, out_so_fn = argv[1:]

    if in_fn == "-":
        in_fn = "/dev/stdin"
    if out_txt_fn == "-":
        out_txt_fn = "/dev/stdout"
    if out_so_fn == "-":
        out_so_fn = "/dev/stdout"

    tree = ET.parse(in_fn)
    root = tree.getroot()

    empty_elements(
        root,
        set([
            'article-categories', 'copyright-statement', 'license',
            'copyright-holder', 'copyright-year', 'journal-meta', 'article-id',
            'back', 'fig', 'table-wrap', 'contrib-group', 'aff',
            'author-notes', 'pub-date', 'volume', 'issue', 'fpage', 'lpage',
            'history'
        ]))

    add_space(root)

    text, standoffs = text_and_standoffs(root)

    standoffs = [s for s in standoffs if not s.tag() in EXCLUDED_TAG]

    converted = []
    for s in standoffs:
        if s.tag() in convert_function:
            converted.extend(convert_function[s.tag()](s))

    standoffs = converted

    for so in standoffs:
        try:
            so.compress_text(MAXIMUM_TEXT_DISPLAY_LENGTH)
        except AttributeError:
            pass

    out_txt = open(out_txt_fn, "wt")
    out_so = open(out_so_fn, "wt")

    out_txt.write(text.encode("utf-8"))
    for so in standoffs:
        print >> out_so, so

    out_txt.close()
    out_so.close()
Ejemplo n.º 32
0
 def readTemplate(self, file):
   """This reads the template in and returns a dict with the trigger as key and
   the template as value"""
   templates = {}
   doc = ElementTree.parse(file)
   entries = doc.findall("entry")
   for entry in entries:
     templates[entry.find("trigger").text] = [entry.find("description").text,
     entry.find("template").text]
   self._convertTabs(templates)
   return templates
Ejemplo n.º 33
0
def ETFromObj(obj):
    """obj can be
    1) a string that ends with .xml -> the file is parsed and the resulting ElementTree returned
    2) a string that ends with .xml.gz -> the file is unzipped, parsed, and the resulting ElementTree is returned
    3) an open input stream -> the input is parsed and the resulting ElementTree is returned
    4) an ElementTree or an Element -> obj is returned as-is, nothing is done"""
    if isinstance(obj,str) or isinstance(obj,unicode):
        if obj.endswith(".xml.gz"):
            fStream=GzipFile(obj,"rt")
        elif obj.endswith(".xml"):
            fStream=open(obj,"rt")
        else:
            raise ValueError("%s: File format not recognized (expected .xml or .xml.gz)"%obj)
        return ElementTree.parse(fStream)
    elif isinstance(obj,ElementTree.ElementTree) or ElementTree.iselement(obj):
        return obj
    else:
        #not a string, not a tree, not an element, should be a stream
        #let's parse it
        return ElementTree.parse(obj)
Ejemplo n.º 34
0
    def _make_printable(self, tutdir, up_to_root=2):
        endpath = tutdir
        tutdir = os.path.join(self.srcdirs[0], tutdir)
        import cElementTree as elementtree
        masterdoc = """<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<?python import printable ?>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:py="http://purl.org/kid/ns#" py:extends="printable">
<head>
 <meta content="text/html; charset=UTF-8" http-equiv="content-type" />
 <link rel="stylesheet" type="text/css" href="../../default.css" py:attrs="href=root+'default.css'"/>
 <link type="text/css" rel="stylesheet" href="../../sh/SyntaxHighlighter.css" py:attrs="href=root+'sh/SyntaxHighlighter.css'"></link> 
 <title>TurboGears: 20 Minute Wiki Tutorial</title>
</head>
<body>
"""
        docs = os.listdir(tutdir)
        docs.sort()
        for doc in docs:
            if not doc.endswith(".html"):
                continue
            log.info("combining %s" % doc)
            tree = elementtree.parse(os.path.join(tutdir, doc))
            body = tree.find("{http://www.w3.org/1999/xhtml}body")
            map(body.remove, body.findall("{http://www.w3.org/1999/xhtml}script"))
            bodytext = elementtree.tostring(body)
            bodytext = bodytext.replace("</html:body>", "")
            bodytext = bodytext.replace('<html:body xmlns:html="http://www.w3.org/1999/xhtml">', "")
            masterdoc += bodytext
            
        masterdoc += """<script src="../../sh/shCore.js" py:attrs="src=root+'sh/shCore.js'"></script>
<script src="../../sh/shBrushPython.js" py:attrs="src=root+'sh/shBrushPython.js'"></script>
<script src="../../sh/shBrushXml.js" py:attrs="src=root+'sh/shBrushXml.js'"></script>
<script src="../../sh/shBrushJScript.js" py:attrs="src=root+'sh/shBrushJScript.js'"></script>
<script language="javascript">
	dp.SyntaxHighlighter.HighlightAll('code');
</script>
</body></html>"""
        masterdoc = masterdoc.replace("html:", "")
        template = kid.Template(source=masterdoc, root="../" * up_to_root)
        template.serializer = self.serializer
        
        destend = os.path.join(self.destdir, endpath)
        if not os.path.exists(destend):
            os.makedirs(destend)
        outfn = os.path.join(destend, "printable.html")
        print "combined output: %s" % outfn
        outfile = open(outfn, "w")
        masterdoc = template.serialize(encoding=self.encoding)
        masterdoc = masterdoc.replace("$${", "${")
        outfile.write(masterdoc)
        outfile.close()
        self.currentfiles.add(outfn)
        
Ejemplo n.º 35
0
def getSettings():
    global debugProg
    global db_Settings
    global Soap_Server_Settings

    if not debugProg:
        debugProg={}
        debugProg["flag"]=0
        debugProg["file"]=""
        debugProg["print"]=0
        db_Settings={}
        Soap_Server_Settings={}
        
    ##    aXMLfile='XSM-configuration.xml'
        aXMLfile=CONF_FILE
        
        doc = ElementTree.parse(aXMLfile).getroot()
    
    # Go through the CONF and SET for the settings
        for node in doc.findall('CONF'):
            for nodeSet in node.findall('SET'): 
                if(nodeSet.get('name')=="debugProg" and nodeSet.get('value')=="true" and nodeSet.get('file')!=""):
                    debugProg["flag"]=1
                    debugProg["file"]=nodeSet.get('file')
                    if(nodeSet.get('print_out')=="true"):
                        debugProg["print"]=1
                    else:
                        debugProg["print"]=0
                    
                if(nodeSet.get('name')=="debugProg" and nodeSet.get('value')=="false"):
                    debugProg["flag"]=0
                    debugProg["file"]=""
                    debugProg["print"]=0
                    
                if(nodeSet.get('name')=="dbip" and nodeSet.get('value')!=""): db_Settings["dbip"]=nodeSet.get('value')
                if(nodeSet.get('name')=="dbnm" and nodeSet.get('value')!=""): db_Settings["dbnm"]=nodeSet.get('value')
                if(nodeSet.get('name')=="dbuser" and nodeSet.get('value')!=""): db_Settings["dbuser"]=nodeSet.get('value')
                if(nodeSet.get('name')=="dbpass" and nodeSet.get('value')!=""): db_Settings["dbpass"]=nodeSet.get('value')
                if(nodeSet.get('name')=="dbencod" and nodeSet.get('value')!=""): db_Settings["dbencod"]=nodeSet.get('value')
                if(nodeSet.get('name')=="dbunicod" and nodeSet.get('value')!=""): db_Settings["dbunicod"]=nodeSet.get('value')
                
                if(nodeSet.get('name')=="databaseSchema" and nodeSet.get('value')!=""): db_Settings["databaseSchema"]=nodeSet.get('value')
    
                if(nodeSet.get('name')=="Soap_Server_IP" and nodeSet.get('value')!=""): Soap_Server_Settings["Soap_Server_IP"]=nodeSet.get('value')
                if(nodeSet.get('name')=="Soap_Server_Port" and nodeSet.get('value')!=""): Soap_Server_Settings["Soap_Server_Port"]=nodeSet.get('value')
    
        if(debugProg["flag"]==1):
            debug_Info= "Getting settings from configuration file... [%s]\n" %(aXMLfile) 
            debug_Info= debug_Info + "The settings document is " + ElementTree.tostring(doc) + "\n"
            debug_Info= debug_Info +  "Settings accepted -\n\tSoap Server: " + str(Soap_Server_Settings) + "\n\t Database setting: " + str(db_Settings) + " \n\tDebug Flag: " + str(debugProg) + "\n"
            if(debugProg["print"]==1): print debug_Info
            writefile(debug_Info,debugProg["file"])
            debug_Info=""
Ejemplo n.º 36
0
 def _open(self):
     try:
         return self._repo
     except AttributeError:
         try:
             self._repo = cElementTree.parse(self.xml_file).getroot()
             return self._repo
         except SyntaxError as e:
             self.pk.error(
                 ERROR_REPO_CONFIGURATION_ERROR,
                 "Failed to parse %s: %s. A cache refresh should fix this."
                 % (self.xml_file, str(e)))
Ejemplo n.º 37
0
 def _open(self):
     try:
         return self._repo
     except AttributeError:
         try:
             r = self.xml_path + self.repo
             self._repo = cElementTree.parse(r).getroot()
             return self._repo
         except:
             self.pk.error(
                 ERROR_REPO_CONFIGURATION_ERROR,
                 " The file %s not parsed submit a issue at http://issues.foresightlinux.org"
                 % self.repo)
Ejemplo n.º 38
0
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()
Ejemplo n.º 39
0
def loadGpx(fn, postprocess=True):
  gpx = ET.parse(fn)
  root = gpx.getroot()
  if root.get("creator").startswith("GpsLog"):
    root = extractMarks(root)
  else:
    killNs(root)
    root.set("xmlns",            GPX_NS)
    root.set("{%s}dummy"%GPX_NS, "true") # dummy to force ns insertion
    root.set("{%s}dummy"%MY_NS,  "true") # dummy to force ns insertion
    if postprocess:
      addCenters(root)
      synthesizeNames(root)
  return root
Ejemplo n.º 40
0
def extractMarks(srcGpx, dstGpx=None):
  if ET.iselement(srcGpx):
    sroot = srcGpx
  else:
    src   = ET.parse(srcGpx)
    sroot = src.getroot()

  
  root = ET.Element("gpx")
  root.attrib = sroot.attrib.copy()
  root.set("xmlns",            GPX_NS)
  root.set("{%s}dummy"%GPX_NS, "true") # dummy to force ns insertion
  root.set("{%s}dummy"%MY_NS,  "true") # dummy to force ns insertion
  root.text = "\n  "
  all    = sroot.findall(mkpath("trk/trkseg/trkpt"))
  
  trk = seg = None
  
  ctr  = []
  open = None
  
  for tpt in all:
    mark = tpt.find(mkpath("extensions/gpslog:mark")) # extensions/gpslog:mark"))
    if mark != None and mark.get("in") == "true":
      trk  = ET.SubElement(root, "trk")
      tnm  = ET.SubElement(trk,  "name")
      tdsc = ET.SubElement(trk,  "desc")
      seg  = ET.SubElement(trk,  "trkseg")
      tnm.text  = mark.text
      tdsc.text = mark.text + " - " + tpt.findtext(mkpath("time"))
      # Formatting in DOM :-(
      tnm.tail = "\n    "; seg.text = "\n      "; seg.tail = "\n    ";
      trk.text = "\n    "; trk.tail = "\n  "
      open = tpt

    if seg != None:
      seg.append(killNs(tpt))

    if mark != None and mark.get("in") != "true":
      assert open != None, "Found closing trackpoint without opener"
      ctr += [ center(open, tpt) ]
      trk = seg = open = None
      

  if open != None:
    ctr += [ center(open, tpt) ]
  
  addCenterpoints(root, ctr)

  return root
Ejemplo n.º 41
0
def main(argv=[]):
    if len(argv) != 4:
        print >> sys.stderr, "Usage:", argv[0], "IN-XML OUT-TEXT OUT-SO"
        return -1

    in_fn, out_txt_fn, out_so_fn = argv[1:]

    if in_fn == "-":
        in_fn = "/dev/stdin"
    if out_txt_fn == "-":
        out_txt_fn = "/dev/stdout"
    if out_so_fn == "-":
        out_so_fn = "/dev/stdout"

    tree = ET.parse(in_fn)
    root = tree.getroot()

    normalize_space(root, ['S', 'A-S'])

    text, standoffs = text_and_standoffs(root)

    for s in standoffs:
        s.strip()

    standoffs = [s for s in standoffs if not s.tag() in EXCLUDED_TAG]

    converted = []
    for s in standoffs:
        if s.tag() in convert_function:
            converted.extend(convert_function[s.tag()](s))
        else:
            converted.append(s)
    standoffs = converted

    for so in standoffs:
        try:
            so.compress_text(MAXIMUM_TEXT_DISPLAY_LENGTH)
        except AttributeError:
            pass

    out_txt = open(out_txt_fn, "wt")
    out_so = open(out_so_fn, "wt")

    out_txt.write(text.encode("utf-8"))
    for so in standoffs:
        print >> out_so, so

    out_txt.close()
    out_so.close()
Ejemplo n.º 42
0
def main(argv=None):
    if argv is None:
        argv = sys.argv

    args = argparser().parse_args(argv[1:])

    # Ugly hack for quick testing: allow "-" for standard in/out
    in_fn, out_txt_fn, out_so_fn = args.in_xml, args.out_text, args.out_so
    if in_fn == "-":
        in_fn = "/dev/stdin"
    if out_txt_fn == "-":
        out_txt_fn = "/dev/stdout"
    if out_so_fn == "-":
        out_so_fn = "/dev/stdout"

    try:
        tree = ET.parse(in_fn)
    except Exception:
        print >> sys.stderr, "%s: Error parsing %s" % (argv[0], in_fn)
        return 1

    root = tree.getroot()

    text, standoffs = text_and_standoffs(root)

    # filter standoffs by tag
    if args.filter is None:
        filtered = set()
    else:
        filtered = set(args.filter.split(','))
    standoffs = [s for s in standoffs if s.tag() not in filtered]

    # set ID prefixes
    if args.prefix is not None:
        for s in standoffs:
            s.set_prefix(args.prefix)

    # open output files 
    out_txt = open(out_txt_fn, "wt")
    out_so  = open(out_so_fn, "wt")

    out_txt.write(text.encode("utf-8"))
    for so in standoffs:
        so.compress_text(MAXIMUM_TEXT_DISPLAY_LENGTH)
        print >> out_so, so

    out_txt.close()
    out_so.close()
Ejemplo n.º 43
0
def fauxFetch(params):
    """This is a simple fetch for testing cursor functionality"""
    page = os.path.join(prependdir, 'xml', '%s.xml' % (params.pageNum, ))
    sys.stdout.write('OPENING PAGE: %s\n' % (page, ))
    #f = open(page)
    #a = f.read()
    #r =  ElementTree.fromstring(unicode(a,'ascii',errors='ignore'))
    try:
        r = ElementTree.parse(page)
    except:
        raise SyntaxError, 'page was: %s' % (page, )
    r = r.find('BookList')
    if ElementTree.iselement(r):
        return r
    else:
        raise Exception, 'not getting a page...'
Ejemplo n.º 44
0
def dbSchemaChecker(XMLFile):

    aDbSchema=db_Settings["databaseSchema"]

    if(debugProg["flag"]==1):
        debug_Info= "The Database schema XML that will be processed... [%s]\n" %(aDbSchema) + "\n"
        if(debugProg["print"]==1): print debug_Info
        writefile(debug_Info,debugProg["file"])
        debug_Info=""

    dbSchema = ElementTree.parse(aDbSchema).getroot()

    if(dbSchema.get('datatype') and XMLFile.get('datatype')):
       if (dbSchema.get('datatype')==XMLFile.get('datatype')):
          return 1
    return 0
Ejemplo n.º 45
0
def fauxFetch(params):
    """This is a simple fetch for testing cursor functionality"""
    page = os.path.join(prependdir,'xml','%s.xml'%(params.pageNum,))
    sys.stdout.write('OPENING PAGE: %s\n' % (page,))
    #f = open(page)
    #a = f.read()
    #r =  ElementTree.fromstring(unicode(a,'ascii',errors='ignore'))
    try:
        r = ElementTree.parse(page)
    except:
        raise SyntaxError, 'page was: %s' % (page,)
    r = r.find('BookList')
    if ElementTree.iselement(r):
        return r
    else:
        raise Exception, 'not getting a page...'
Ejemplo n.º 46
0
def main():
    gui.xml.merge('grafit.mgx')
    win = gui.xml.build('mainwin')

    tree = win.find('tree')
    r = ElementTreeNode(et.parse('gui.xml').getroot())
    tree.append(r)

    def hello():
        print 'hello'

    gui.commands['file-new'].connect('activated', hello)
    win.find('bouton').connect('clicked', hello)
    win.find('html').LoadPage('test.html')

    gui.run(win)
Ejemplo n.º 47
0
def fetch_files(xml_file, file_dir):
    """ Fetch files referenced in the xml_file into the dir file_dir. """
    root = ElementTree.parse(xml_file).getroot()
    to_fetch = set()
    deleted = set()
    for artifact in root.find('artifacts'):
        for field in artifact.findall('field'):
            if field.get('name') == 'artifact_id':
                aid = field.text
        for field in artifact.findall('field'):
            if field.get('name') != 'artifact_history': continue
            for event in field.findall('history'):
                d = {}
                for field in event.findall('field'):
                    d[field.get('name')] = field.text
                if d['field_name'] == 'File Added':
                    fid = d['old_value'].split(':')[0]
                    to_fetch.add((aid, fid))
                if d['field_name'] == 'File Deleted':
                    fid = d['old_value'].split(':')[0]
                    deleted.add((aid, fid))
    to_fetch = to_fetch - deleted

    got = set(os.listdir(file_dir))
    to_fetch = to_fetch - got

    # load cached urls (sigh)
    urls = {}
    if os.path.exists(os.path.join(file_dir, 'urls.txt')):
        for line in open(os.path.join(file_dir, 'urls.txt')):
            aid, url = line.strip().split()
            urls[aid] = url

    for aid, fid in support.Progress('Fetching files', list(to_fetch)):
        if fid in got: continue
        if not urls.has_key(aid):
            urls[aid] = get_url(aid)
            f = open(os.path.join(file_dir, 'urls.txt'), 'a')
            f.write('%s %s\n'%(aid, urls[aid]))
            f.close()
        url = urls[aid] + '&file_id=' + fid
        f = urllib2.urlopen(url)
        data = f.read()
        n = open(os.path.join(file_dir, fid), 'w')
        n.write(data)
        f.close()
        n.close()
Ejemplo n.º 48
0
def dtdcollect(xmlfile, dtdfile):
    global nowrite
    log.info("dtdcollect(xmlfile=%r, dtdfile=%r)", xmlfile, dtdfile)

    entities = {}

    # read the xml text so we can do replacements
    fin = open(xmlfile, 'r')
    xmlContent = fin.read()
    fin.close()
    
    # first, parse the xml file
    tree = ET.parse(xmlfile)
    
    # iterate through the nodes, looking for attributes we care about
    for node in tree.getiterator():
        id = node.attrib.get("id", None)
        if not id:
            id = node.attrib.get("label", None)
        if id in specialNames:
            id = specialNames[id]
        for attr in attrs:
            entityVal = node.attrib.get(attr, None)
            if not entityVal:
                continue
            if attr == "accesskey" and id:
                try:
                    words = [w for w in re.split("[\W_]+", id) if w]
                    if len(words[0]) > 1 and not words[0][1].isupper():
                        # lowercase the first letter
                        words[0] = words[0][0].lower()+words[0][1:]
                    entityName = words[0] + "".join([w[0].upper()+w[1:] for w in words[1:]])
                except IndexError, e:
                    print "failure on name %s" % entityVal
                    raise
            elif entityVal in specialNames:
                entityName = specialNames[entityVal]
            else:
                try:
                    words = [w for w in re.split("[\W_]+", entityVal) if w]
                    if len(words[0]) > 1 and not words[0][1].isupper():
                        # lowercase the first letter
                        words[0] = words[0][0].lower()+words[0][1:]
                    entityName = words[0] + "".join([w[0].upper()+w[1:] for w in words[1:]])
                except IndexError, e:
                    print "failure on name %s" % entityVal
                    raise
Ejemplo n.º 49
0
def fetch_files(xml_file, file_dir):
    """ Fetch files referenced in the xml_file into the dir file_dir. """
    root = ElementTree.parse(xml_file).getroot()
    to_fetch = set()
    deleted = set()
    for artifact in root.find('artifacts'):
        for field in artifact.findall('field'):
            if field.get('name') == 'artifact_id':
                aid = field.text
        for field in artifact.findall('field'):
            if field.get('name') != 'artifact_history': continue
            for event in field.findall('history'):
                d = {}
                for field in event.findall('field'):
                    d[field.get('name')] = field.text
                if d['field_name'] == 'File Added':
                    fid = d['old_value'].split(':')[0]
                    to_fetch.add((aid, fid))
                if d['field_name'] == 'File Deleted':
                    fid = d['old_value'].split(':')[0]
                    deleted.add((aid, fid))
    to_fetch = to_fetch - deleted

    got = set(os.listdir(file_dir))
    to_fetch = to_fetch - got

    # load cached urls (sigh)
    urls = {}
    if os.path.exists(os.path.join(file_dir, 'urls.txt')):
        for line in open(os.path.join(file_dir, 'urls.txt')):
            aid, url = line.strip().split()
            urls[aid] = url

    for aid, fid in support.Progress('Fetching files', list(to_fetch)):
        if fid in got: continue
        if aid not in urls:
            urls[aid] = get_url(aid)
            f = open(os.path.join(file_dir, 'urls.txt'), 'a')
            f.write('%s %s\n'%(aid, urls[aid]))
            f.close()
        url = urls[aid] + '&file_id=' + fid
        f = urllib_.urlopen(url)
        data = f.read()
        n = open(os.path.join(file_dir, fid), 'w')
        n.write(data)
        f.close()
        n.close()
Ejemplo n.º 50
0
def get_config_root_node(config_file_name=None, config_file_data=None):
    try:
        # get root
        if config_file_data is None:
            config_file_content = open(config_file_name, "r")
            config = ET.parse(config_file_content)
            root_node = config.getroot()
        else:
            root_node = ET.fromstring(config_file_data)

        # get root data
        root_data = root_node.get("name")
        root_name = np.array(root_data.split(), dtype=str)
    except:
        quit("ERROR: Unable to process config file %s" % config_file_name)

    return root_node, root_name
Ejemplo n.º 51
0
def loadKeys(fname=None, force_=False):
    global _KEYS, _KEYFILE
    if not fname:
        fname = os.path.expanduser('~/.isbndbkeys')
    if not os.path.exists(fname):
        a = ElementTree.Element('Keys')
        ElementTree.ElementTree(a).write(fname)
    _KEYFILE = fname
    _KEYS = dict()
    tree = ElementTree.parse(fname)
    for x in tree.findall('key'):
        try:
            _KEYS[x.get("name")] = Key(x.text, x.get("name"))
        except:
            if force_:
                _KEYS[x.get("name")] = Key(x.text, x.get("name"), force_=True)
            else:
                pass
Ejemplo n.º 52
0
    def __init__(self,api_config_file='config.xml' ):
        try:
            self.config = ET.parse(open(api_config_file, "r"))
            self.root = self.config.getroot()
            self.child = [child.tag for child in self.root]
            #for child in self.root:
            #    child.append(child.tag)
            #print(self.root.tag)
            self.nameValuePair = {}
            for k in  self.child:   
                element = self.root.find(k)
                for e in element.iter('level'):
                    name=element.tag+'.'+str(e.get('name'))
                    self.nameValuePair[name]=e.get('value')
                    #print (e.attrib['name'],e.get('name'),element.tag)

        except:
            sys.exit("Unable to open or parse input definition file: " + api_config_file)
Ejemplo n.º 53
0
    def read_object_defs(self):
        self.logger.debug("==============================================================")
        # Reset, in case the file is re-read
        self.objects = []
        for config in self.config_location:
            self.logger.debug("* Parsing %s" % config)

            # A flat list of objects, as found in the XML document.
            objects = etree.parse(config).getroot()

            # We need to handle both 1.0 and 1.1 XSD schemata *and* we may be
            # passed a list of config locations of different XSD versions so we
            # must find out here which one is used in the current config file
            # and pass the correct namespace down to other parts of XMLConfig.
            ns = objects.tag[:objects.tag.find("}") + 1]

            # A dictionary of abstract objects, keyed by their IDs, used in
            # traversing the hierarchies of parents; built upfront here for
            # convenience.
            abstract_objects = {}
            for obj in objects:
                if obj.get("abstract"):
                    abstract_objects[obj.get("id")] = obj

            for obj in objects:
                if obj.get("class") is None and not obj.get("parent"):
                    self._map_custom_class(obj, xml_mappings, ns)

                elif obj.get("parent"):
                    # Children are added to self.objects during the children->abstract parents traversal.
                    pos_constr = self._get_pos_constr(obj, ns)
                    named_constr = self._get_named_constr(obj, ns)
                    props = self._get_props(obj, ns)
                    self._traverse_parents(obj, obj, ns, pos_constr, named_constr, props, abstract_objects)
                    continue

                self.objects.append(self._convert_object(obj, ns=ns))

        self.logger.debug("==============================================================")
        for object in self.objects:
            self.logger.debug("Parsed %s" % object)
        return self.objects
Ejemplo n.º 54
0
def extract_songs(playlist, copy_to):
    num_songs = 0

    pl = ElementTree.parse(playlist)
    dicts = pl.findall('dict')
    for d in dicts:
        for k in d.findall('key'):
            if k.text == 'Tracks':
                for trk_d in d.findall('dict/dict'):
                    look_for_loc = False

                    for child in trk_d.getchildren():
                        if child.tag == 'key' and child.text == 'Location':
                            look_for_loc = True
                        if child.tag == 'string' and look_for_loc:
                            copy_song(copy_to, child.text)
                            num_songs += 1
                            look_for_loc = False
                            break
    if dry_run:
        print "DRY RUN:",
    print "copied %d song(s) to %s" % (num_songs, copy_to)
Ejemplo n.º 55
0
def merge(filename):
    root = parse(filename).getroot()
    for elem in root:
        if elem.tag == 'Commands':
            for e in elem:
                comm = _from_element(e)
                commands[comm.id] = comm
        elif elem.tag == 'Images':
            for e in elem:
                if e.tag == 'Image':
                    if 'path' in e.keys():
                        img = Image.open(_attr(e.get('path')))
                    else:
                        img = Image.open(
                            StringIO.StringIO(e.text.strip().decode('base64')))
                    images.register(_attr(e.get('id')), img)
                elif e.tag == 'DirImageProvider':
                    images.register_dir(_attr(e.get('path')))
        elif 'name' in elem.keys():
            registry[eval(elem.get('name'), {})] = elem
        else:
            print >> sys.stderr, "cannot use element", elem
Ejemplo n.º 56
0
    def parseXSD(self):
        """
        Reads the given xsd file and creates a set of classes that corespond to the complex and simple type definitions.

        No parameters.
        """
        if self.verbose:
            print "Sending the schema file to the ElementTree Parser..."

        tree = ET.parse(self.xsdFile)

        root = tree.getroot()

        if self.verbose:
            print "Sending the schema ElementTree to the ElementRepresntative module..."

        schemaER = ElementRepresentative.factory(root, None)

        for simpleType in schemaER.simpleTypes.values():

            cls = simpleType.clsFor(self)

            self.classes[simpleType.name] = cls

            if self.verbose:
                print "Class created for the %s type..." % simpleType.name

        for complexType in schemaER.complexTypes.values():

            cls = complexType.clsFor(self)

            self.classes[complexType.name] = cls

            if self.verbose:
                print "Class created for the %s type..." % complexType.name

        return