Example #1
0
    def copy_shape(self, shape: Element, page: ET, page_path: str) -> ET:
        """Insert shape into first Shapes tag in destination page, and return the copy.

        If destination page does not have a Shapes tag yet, create it.

        Parameters:
            shape (Element): The source shape to be copied. Use Shape.xml
            page (ElementTree): The page where the new Shape will be placed. Use Page.xml
            page_path (str): The filename of the page where the new Shape will be placed. Use Page.filename

        Returns:
            ElementTree: The new shape ElementTree

        """

        new_shape = ET.fromstring(ET.tostring(shape))

        self.set_page_max_id(page_path)

        # find or create Shapes tag
        shapes_tag = page.find(f"{namespace}Shapes")
        if shapes_tag is None:
            shapes_tag = Element(f"{namespace}Shapes")
            page.getroot().append(shapes_tag)

        id_map = self.increment_shape_ids(new_shape, page_path)
        self.update_ids(new_shape, id_map)
        shapes_tag.append(new_shape)

        self.pages[page_path] = page
        return new_shape
Example #2
0
def gatherSplitTimeStepXDMFfilesOpt(size,
                                    filename,
                                    dataDir='.',
                                    addname="_all",
                                    nStepsOnly=None,
                                    stride=1):
    """
    in case archiving failed to collect results from various processors in parallel simulation

    size -- nprocessors

    """
    xmlFile = open(filename + str(0) + ".xmf", "r")
    tree = ElementTree(file=xmlFile)
    xmlFile.close()
    nSteps = len(tree.getroot()[-1][-1])
    if nStepsOnly != None:
        nSteps = nStepsOnly
    print "nSteps", nSteps
    stepsToGather = [i * stride for i in range(nSteps / stride)]
    for tn in stepsToGather:
        fAll = open(
            os.path.join(
                dataDir,
                filename + "_t" + str(tn) + addname + str(size) + ".xmf"), "w")
        fAll.write(r"""<?xml version="1.0" ?>
<!DOCTYPE Xdmf SYSTEM "Xdmf.dtd" []>
<Xdmf Version="2.0" xmlns:xi="http://www.w3.org/2001/XInclude">
  <Domain>
    <Grid CollectionType="Temporal" GridType="Collection" Name="Mesh Spatial_Domain">
""")

        print "time step", tn
        print "subdomain", 0
        fAll.write(
            r"""      <Grid CollectionType="Spatial" GridType="Collection">
        """)
        xmlFile = open(os.path.join(dataDir, filename + str(0) + ".xmf"), "r")
        tree = ElementTree(file=xmlFile)
        xmlFile.close()
        Grid = tree.getroot()[-1][-1][tn]
        fAll.write(tostring(Grid[0]))
        del Grid[0]
        fAll.write(tostring(Grid))
        for i in range(1, size):
            print "subdomain", i
            xmlFile = open(os.path.join(dataDir, filename + str(i) + ".xmf"),
                           "r")
            tree = ElementTree(file=xmlFile)
            xmlFile.close()
            Grid = tree.getroot()[-1][-1][tn]
            del Grid[0]
            fAll.write(tostring(Grid))
        fAll.write(r"""      </Grid>
""")
        fAll.write(r"""    </Grid>
  </Domain>
</Xdmf>
""")
        fAll.close()
Example #3
0
    def copy_shape(self, shape: Element, page: ET, page_path: str) -> ET:
        '''Insert shape into first Shapes tag in destination page, and return the copy.

        If destination page does not have a Shapes tag yet, create it.

        Parameters:
            shape (Element): The source shape to be copied. Use Shape.xml
            page (ElementTree): The page where the new Shape will be placed. Use Page.xml
            page_path (str): The filename of the page where the new Shape will be placed. Use Page.filename

        Returns:
            ElementTree: The new shape
        '''

        new_shape = ET.fromstring(ET.tostring(shape))

        self.set_page_max_id(page_path)

        # find or create Shapes tag
        for shapes_tag in page.getroot():  # type: Element
            if 'Shapes' in shapes_tag.tag:
                break
        else:  #no break
            shapes_tag = Element(f'{namespace}Shapes')
            page.getroot().append(shapes_tag)

        id_map = self.increment_shape_ids(new_shape, page_path)
        self.update_ids(new_shape, id_map)
        shapes_tag.append(new_shape)

        return new_shape
    def collect_news(self, rss):
        print('Diario: {} Sección: {} URL: {}'.format(rss[0], rss[1], rss[2]))
        file_name = "../resources/rss/" + rss[0] + "/" + rss[1] + ".xml"
        header = {
            'User-Agent':
            "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)"
        }
        proxies_dict = {
            'http': 'http: // 10.10.10.10: 8000',
            'https': 'http: // 10.10.10.10: 8000',
        }
        result = request('get', rss[2], proxies=[])
        element = fromstring(result.text.strip())
        try:
            xml_parsed = ET.parse(file_name)
        except FileNotFoundError:
            root = Element("news")
            xml_parsed = ElementTree(root)
            if not os.path.isdir('../resources/rss/' + rss[0]):
                os.makedirs("../resources/rss/" + rss[0])
            open(file_name, 'w+', encoding='utf-8')

        for item in element.findall(".//item"):
            news_item = Element("item")
            for tag in item:
                if tag.tag == "title" or tag.tag == "description" or tag.tag == "pubDate":
                    news_item.append(tag)
            if not self.find_item(news_item, file_name):
                xml_parsed.getroot().append(news_item)
        xml_parsed.write(file_name, encoding='UTF-8')
def gatherTimes(filename,dataDir='.',addname="_complete", tCount=None):
    """
    in case archiving failed to collect results from all times
    """
    import h5py
    xmlFile = open(filename+".xmf","r")
    h5File = h5py.File(filename+".h5","r")
    tree = ElementTree(file=xmlFile)
    xmlFile.close()
    XDMF = tree.getroot()
    Domain = XDMF[0]
    for TemporalGridCollection in Domain:
        if tCount == -1:
            SpatialCollection = TemporalGridCollection[-1]
            Grids = SpatialCollection[:]
            tCount = int(Grids[0].attrib['Name'])+1
            del TemporalGridCollection[:]
        for i in range(tCount):
            dataset_name = TemporalGridCollection.attrib['Name']+"_"+`i`
            dataset_name = dataset_name.replace(" ","_")
            grid_array = h5File["/"+dataset_name]
            size = grid_array.shape[0]
            SpatialCollection=SubElement(TemporalGridCollection,"Grid",{"GridType":"Collection",
                                                                        "CollectionType":"Spatial"})
            time = SubElement(SpatialCollection,"Time",{"Value":grid_array.attrs['Time'],"Name":str(i)})
            for j in range(size):
                Grid = fromstring(grid_array[j])
                SpatialCollection.append(Grid)
    xmlFile = open(filename+addname+".xmf","w")
    indentXML(tree.getroot())
    tree.write(xmlFile)
    xmlFile.close()
Example #6
0
def _compare_node(mod_xml: ElementTree, ori_xml: ElementTree):
    if mod_xml.getroot() and ori_xml.getroot():
        mod_node_list = list(mod_xml.getroot())
        ori_node_list = list(ori_xml.getroot())
        if mod_node_list and ori_node_list:
            for mode_node in mod_node_list:
                if not mode_node in ori_node_list:
                    pass
    pass
def gatherXDMFfilesOpt(size,filename,dataDir='.',addname="_all",nStepsOnly=None,stride=1):
    """
    in case archiving failed to collect results from various processors in parallel simulation

    size -- nprocessors

    """

    xmlFile = open(filename+str(0)+".xmf","r")
    tree = ElementTree(file=xmlFile)
    xmlFile.close()
    nSteps = len(tree.getroot()[-1][-1])
    if nStepsOnly != None:
        nSteps = nStepsOnly
    print "nSteps",nSteps
    #stepsToGather=[i*stride for i in range(nSteps/stride)]
    stepsToGather = range(0,nSteps,stride)
    fAll = open(os.path.join(dataDir,filename+addname+str(size)+".xmf"),"w")
    fAll.write(r"""<?xml version="1.0" ?>
<!DOCTYPE Xdmf SYSTEM "Xdmf.dtd" []>
<Xdmf Version="2.0" xmlns:xi="http://www.w3.org/2001/XInclude">
  <Domain>
    <Grid CollectionType="Temporal" GridType="Collection" Name="Mesh Spatial_Domain">
""")
    for tn  in stepsToGather:
        print "time step",tn
        print "subdomain",0
        fAll.write(r"""      <Grid CollectionType="Spatial" GridType="Collection">
        """)
        xmlFile = open(os.path.join(dataDir,filename+str(0)+".xmf"),"r")
        tree = ElementTree(file=xmlFile)
        xmlFile.close()
        Grid=tree.getroot()[-1][-1][tn]
        fAll.write(tostring(Grid[0]))
        del Grid[0]
        fAll.write(tostring(Grid))
        for i in range(1,size):
            print "subdomain",i
            xmlFile = open(os.path.join(dataDir,filename+str(i)+".xmf"),"r")
            tree = ElementTree(file=xmlFile)
            xmlFile.close()
            Grid=tree.getroot()[-1][-1][tn]
            del Grid[0]
            fAll.write(tostring(Grid))
        fAll.write(r"""      </Grid>
""")
    fAll.write(r"""    </Grid>
  </Domain>
</Xdmf>
""")
    fAll.close()
Example #8
0
def gatherTimes(filename,
                dataDir='.',
                addname="_complete",
                tCount=None,
                global_sync=True):
    """
    in case archiving failed to collect results from all times
    """
    import h5py
    xmlFile = open(filename + ".xmf", "r")
    h5File = h5py.File(filename + ".h5", "r")
    tree = ElementTree(file=xmlFile)
    xmlFile.close()
    XDMF = tree.getroot()
    Domain = XDMF[0]
    for TemporalGridCollection in Domain:
        if tCount == -1:
            if global_sync:
                tCount = int(TemporalGridCollection[0][0].attrib['Name']) + 1
            else:
                SpatialCollection = TemporalGridCollection[-1]
                Grids = SpatialCollection[:]
                tCount = int(Grids[0].attrib['Name']) + 1
            del TemporalGridCollection[:]
        for i in range(tCount):
            dataset_name = TemporalGridCollection.attrib['Name'] + "_" + repr(
                i)
            dataset_name = dataset_name.replace(" ", "_")
            grid_array = h5File["/" + dataset_name]
            if global_sync:
                Grid = fromstring(grid_array[0])
                TemporalGridCollection.append(Grid)
            else:
                size = grid_array.shape[0]
                SpatialCollection = SubElement(TemporalGridCollection, "Grid",
                                               {
                                                   "GridType": "Collection",
                                                   "CollectionType": "Spatial"
                                               })
                time = SubElement(SpatialCollection, "Time", {
                    "Value": grid_array.attrs['Time'],
                    "Name": str(i)
                })
                for j in range(size):
                    Grid = fromstring(grid_array[j])
                    SpatialCollection.append(Grid)
    xmlFile = open(filename + addname + ".xmf", "w")
    indentXML(tree.getroot())
    tree.write(xmlFile)
    xmlFile.close()
Example #9
0
def shuffle_tree(input_document: Xml, filter_include: str,
                 seed: Optional[int]) -> Xml:
    """
    Copy an input document and shuffle its pages.
    :param input_document: The input document.
    :param filter_include: A filter expression shuffling only those pages where the id matches the pattern.
    :param seed: An optional seed for the random generator.
    :return: A shuffled copy.
    """
    output_document = copy.deepcopy(input_document)
    input_root = input_document.getroot()
    output_root = output_document.getroot()

    indices = [
        i for i, child in enumerate(input_root.findall('*'))
        if child.tag == "page"
        and fnmatch(child.attrib.get("id", ""), filter_include)
    ]
    new_indices = indices[:]

    rng = Random(seed)
    rng.shuffle(new_indices)
    for old, new in zip(indices, new_indices):
        output_root[new] = input_root[old]

    return output_document
Example #10
0
def parse_london_terminals(xml: ElementTree) -> dict:
    """Parses the London Terminals Feed XML element tree."""

    data = {}

    for station in xml.getroot().find('Stations'):

        name = station.find('CrsCode').text
        data[name] = {}

        for LondonTerminalsMapping in station.find('LondonTerminalsMappings'):

            fare_route = LondonTerminalsMapping.find('FareRoute').text
            start_date = LondonTerminalsMapping.find('StartDate').text
            end_date = LondonTerminalsMapping.find('EndDate').text
            terminals = [
                t.text for t in LondonTerminalsMapping.find('LondonTerminals')
            ]

            data[name][fare_route] = {
                'StartDate': start_date,
                'EndDate': end_date,
                'Terminals': terminals
            }

    return data
Example #11
0
def strip_namespaces(ET):
    try:
        for el in ET.getroot().iter():
            el.tag = el.tag.split('}', 1)[-1]
    except AttributeError:
        for _, el in ET:
            el.tag = el.tag.split('}', 1)[-1]
Example #12
0
    def getTeiBodyText(self, xTree: ElementTree, elemToEmpty: str = None):
        """
        Uses Element.itertext() on the body element of given TEI file. Then retrieves
        the text and concatinates it to one string WITHOUT string refining. (=whitespaces
        etc. will be included if in given file.) Method is quite unreliable --- each tag's text
        content will be seperated by a single whitespace ...> will split the word e.g. at
        Elep<char>h</char>hant.
        :param xTree: The xml-file parsed as ElementTree (etree library)
        :return: concatStr: The concatenated text WITHOUT normalization / stripping etc.
        = As it is in the file.
        """
        root: ElementTree.Element = xTree.getroot()
        body: ElementTree.Element = root.find(
            ".//{http://www.tei-c.org/ns/1.0}body")

        if elemToEmpty is not None:
            body = self._emptyElem(body, elemToEmpty)

        # needs to be done that way ...> if accessing mulitple
        # elems via xpath and then looping through elem's and their
        # texts ...> problem at elems with text AND other elems nested
        # inside. Going around that bug via .itertext().
        concatStr = ""
        for txt in body.itertext():
            concatStr += (txt + " ")

        return concatStr
Example #13
0
def convertTiddlyExport(inputFilepath, outputFilepath):
    inTree = ElementTree()
    inTree.parse(inputFilepath)
    outTree = ElementTree(Element('entries'))
    outRoot = outTree.getroot()    
    for item in inTree.getroot().findall('channel/item'):
        newEntry = SubElement(outRoot, 'entry')
        newEntryDate = SubElement(newEntry, 'date')
        newDate = datetime.datetime.strptime(item.find('pubDate').text, tiddlyDateFormat)
        newEntryDate.text = newDate.strftime(settings.DATE_FORMAT) 
        newEntryBody = SubElement(newEntry, 'body')
        htmlText = item.find('description').text
        bodyText = htmlText.replace('<br>', '\n');            
        bodyText = re.sub(r'\<.*?\>', "", bodyText)
        newEntryBody.text = bodyText
    outTree.write(outputFilepath)
def getPitchRefListsOfTriadTypeFromFile(filepath, triadType, musicXml, type="MusicXml"):
	pitchLists = []
	if type == "MusicXml":
		fileBase = basename(filepath)
		basenameIndex = filepath.index(fileBase)

		harmonyFilePath = filepath[:basenameIndex] + "HM-" + basename(filepath)

		ET = ElementTree()
		ET.parse(harmonyFilePath)
		regionXml = ET.getroot()

		key = getKeyFromMusicXml(musicXml)

		while regionXml:
			for chord in regionXml.findall("chord-span"):
				originalChordDegree = chord.attrib['deg']
				print(originalChordDegree)
				chordRootPitchClass = 0
				curChordType = getTypeFromChordDegree(originalChordDegree, key)
				pitchList = []
				if curChordType == triadType:
					for pitchRef in chord.findall("note"):
						pitchRefId = pitchRef.attrib['id']
						pitchList.append(pitchRefId)
					pitchLists.append([originalChordDegree, pitchList])
			regionXml = regionXml.find("region")
	return pitchLists
 def dump(elem):
     if not isinstance(elem, ElementTree):
         elem = ElementTree(elem)
     elem.write(sys.stdout, encoding="unicode")
     tail = elem.getroot().tail
     if not tail or tail[-1] != "\n":
         sys.stdout.write("\n")
Example #16
0
def _do_demangle(document: ElementTree):
    for package in document.getroot().find('packages'):
        for class_ in package.find('classes'):
            for method in class_.find('methods'):
                attrib = method.attrib

                attrib['name'] = _demangle(attrib['name'])
Example #17
0
    def makeXML(self, work_directory):
        # </Artifact>
        TheArtifact = Element( 'Artifact')
        TheArtifact.attrib['xmlns']="http://geni.net/schema"
        TheArtifact.attrib['xmlns:xsi']="http://www.w3.org/2001/XMLSchema-instance"
        TheArtifact.attrib['xsi:schemaLocation']="http://geni.net/schema GENIObject.xsd"

        # <Artifact><Type/>
        Type = SubElement( TheArtifact, 'Type')

        # <Artifact><Type><Primary/>
        Primary = SubElement( Type, 'Primary')
        Primary.text = self.art_type_prime

        # <Artifact><Interpretation/>
        Interpretation = SubElement( TheArtifact, 'Interpretation')
    
        # <Artifact><Interpretation><Interpretation_read_me_text/>
        Read_me_text = SubElement( Interpretation, 'Interpretation_read_me_text')
        Read_me_text.text = self.interpretation_read_me

        #To print to file
        Test = ElementTree()
        Test._setroot(TheArtifact)
        root = Test.getroot()
        self.indent(root)
        Test.write(work_directory+'/artifact.xml')
Example #18
0
    def update(self, xml_path):
        file = open(xml_path)

        try:
            tree = ElementTree()
            tree.parse(file)
        finally:
            file.close()

        root = tree.getroot()
        channel = root.find("channel")

        conn = sqlite3.connect(self.path)

        try:
            cursor = conn.cursor()

            for elem in channel.findall("item"):
                record = TicketRecord()
                record.parse(elem)
                record.insert(cursor)

                conn.commit()
        finally:
            conn.close()
Example #19
0
 def dump(elem):
     if not isinstance(elem, ElementTree):
         elem = ElementTree(elem)
     elem.write(sys.stdout, encoding="unicode")
     tail = elem.getroot().tail
     if not tail or tail[-1] != "\n":
         sys.stdout.write("\n")
Example #20
0
def extract(tree: et.ElementTree):
    # Fetch all paragraph ids, we will later use these as anchors
    body = tree.getroot()[0]
    speakers = defaultdict(list)
    speaker = None
    for paragraph in body:
        if is_subject_bookmark(paragraph):
            speaker = None
            continue

        if is_speaker(paragraph):
            speaker = extract_text(paragraph)
            continue

        if speaker is None:
            continue

        if is_speaker_remark(paragraph):
            continue

        if is_styled(paragraph):
            speaker = None
            continue

        if len(extract_text(paragraph)) == 0:
            continue

        speakers[speaker].append(paragraph)

    return speakers
Example #21
0
def setup_streets(net: sumolib.net.Net, xml: ElementTree, pop_noise: NoiseSampler, work_noise: NoiseSampler):
    """
    Create a street for each edge in the network and calculate its population and workplaces based on
    modified Perlin noise from NoiseSamplers
    :param net: the SUMO network
    :param xml: the statistics XML for the network
    :param pop_noise: NoiseSampler to use for population
    :param work_noise: NoiseSample to use for workplaces
    """

    streets = xml.find("streets")
    if streets is None:
        streets = ET.SubElement(xml.getroot(), "streets")

    # Some edges might already have a street, so we want to ignore those
    known_streets = {street.attrib["edge"]: street for street in streets.findall("street")}

    for edge in net.getEdges():
        eid = edge.getID()
        if eid not in known_streets:
            # This edge is missing a street entry. Find population and industry for this edge
            pos = get_edge_pair_centroid(edge.getShape())
            population = pop_noise.sample(pos)
            industry = work_noise.sample(pos)

            logging.debug(
                f"[perlin] Adding street with eid: {eid},\t population: {population:.4f}, industry: {industry:.4f}")
            ET.SubElement(streets, "street", {
                "edge": eid,
                "population": str(population),
                "workPosition": str(industry)
            })
Example #22
0
class xml_document(object):
    """
    ElementTree requires that the complete XML be loaded in memory
    before working with it, which may be discouraging when dealing
    with large files. By abstracting it this way, if we want to
    implement a more efficient approach later, we will avoid the need
    for messing with other sections of code.
    """
    def __init__(self, element=None, file_obj=None):
        """
        Initializes reference to the ElementTree parser, passing it
        the a file descripter object to be read and parsed or the
        ElemenTree.Element object to be used as the root element.
        """
        self.etree = ElementTree.ElementTree(element=element, file=file_obj)

    def parse_string(self, source):
        "Loads an XML document from an XML string, source."
        root = ElementTree.fromstring(source)
        self.etree = ElementTree(element=root)

    def parse_file(self, source):
        """
        Loads an XML document from source, which can either be a
        filepath string or a file object.
        """
        root = ElementTree.parse(source=source)
        self.etree = ElementTree(element=root)

    def getiterator(self, tag):
        """
        Returns an iterator over all top-level elements from the root element
        that have the matching tag.
        """
        i = self.etree.getroot().getiterator(tag)
        if not i:
            i = self.etree.getroot().getiterator("{http://www.nexml.org/1.0}" +
                                                 tag)
            if i:
                diagnose_namespace(tag, "decorated with NeXML namespace")
            else:
                diagnose_namespace(tag, "NOT FOUND")
        else:
            diagnose_namespace(tag, "no namespace decoration")
        return utils.RecastingIterator(i, XmlElement)
Example #23
0
def get_root(tree: ElementTree) -> Element:
    root = tree.getroot()

    root_tag = "module"
    assert (
        root.tag == root_tag
    ), f"unsupported IML file: malformed structure: root tag != <{root_tag}>"

    return root
Example #24
0
 def copy_shape(self, shape: Element, page: ET, page_path: str) -> ET:
     # insert shape into first Shapes tag in page
     new_shape = ET.fromstring(ET.tostring(shape))
     for shapes_tag in page.getroot():  # type: Element
         if 'Shapes' in shapes_tag.tag:
             id_map = self.increment_shape_ids(shape, page_path)
             self.update_ids(new_shape, id_map)
             shapes_tag.append(new_shape)
     return new_shape
Example #25
0
	def do_PROPFIND(self):
		print 'PROPFIND ' + self.path
		for key in self.headers.keys():
			print key + '\t' + self.headers[key]
		req = self.parseinputxml()
		req = ElementTree(req)
		res = ElementTree(Element(QName("DAV:", 'multistatus')))
		self.addresponse('/', res.getroot(), 0)

		self.writeresponse(res)
Example #26
0
    def getRootTag(self, xTree: ElementTree):
        """
        Reads given xml (see param), accesses the root element
        and calcs the it's tagname.
        :param xTree: ElementTree instance of the XML. (from etree library)
        :return: string value of the root-tag
        """

        rootTag: str = xTree.getroot().tag
        return rootTag
Example #27
0
def get_node_by_line_col(tree: ET, line: int, col: int) -> ET.Element:
    """
    Get node from tree by its line and col.
    Tree needs position.
    If there's no such node return None.
    """
    for node in tree.getroot().iter():
        if LINEKEY in node.attrib:
            if line == int(node.attrib[LINEKEY]) and col == int(node.attrib[COLUMNKEY]):
                return node
    return None
Example #28
0
def get_bfs_node_list(tree: ET) -> [ET.Element]:
    root = tree.getroot()
    node_list = []
    q = queue.Queue()
    q.put(root)
    while not q.empty():
        node = q.get()
        node_list.append(node)
        for child in node:
            q.put(child)
    return node_list
Example #29
0
def overwrite_prop( doc: ElementTree, prop_name: str, value: str ):
    for prop in doc.findall( "property" ):
        if prop.find( "name" ).text == prop_name:
            print( f'replacing {prop_name} : {prop.find( "value" ).text} with {value}' )
            prop.find( "value" ).text = value  ## careful, this modifies the doc
            break
    else:
        print( f'adding {prop_name} : {value}' )
        new_prop = ElementTree.SubElement( doc.getroot(), 'property' )
        ElementTree.SubElement( new_prop, 'name' ).text = prop_name
        ElementTree.SubElement( new_prop, 'value' ).text = value
    def scan_dir(self, database, dir):
        if not isinstance(dir, unicode):
            dir = dir.decode(sys.getfilesystemencoding())
        if not os.path.exists(dir):
            print("does not exist")
            return
        dir = get_real_case(dir)

        for name in os.listdir(dir):
            if self.stop_check():
                return
            if name in [".git"]:
                continue
            path = os.path.join(dir, name)
            if os.path.isdir(path):
                self.scan_dir(database, path)
                continue
            dummy, ext = os.path.splitext(path)
            ext = ext.lower()
            #if ext not in self.extensions:
            #    continue
            if ext != ".xml":
                continue

            self.scan_count += 1
            self.set_status(
                    _("Scanning configurations ({count} scanned)").format(
                    count=self.scan_count), name)

            print("scan", name)
            result = None
            try:
                tree = ElementTree()
                tree.parse(path)
                root = tree.getroot()
                if root.tag == "config":
                    result = self.scan_configuration(database, tree)
                elif root.tag == "game":
                    self.scan_game(database, tree, path)
            except Exception:
                traceback.print_exc()
            if result is not None:
                if "name" in result:
                    name = result["name"]
                else:
                    name, ext = os.path.splitext(name)
                print("found", name)
                search = self.create_configuration_search(name)
                #name = self.create_configuration_name_from_path(path)
                name = self.create_configuration_name(name)
                database.add_configuration(path=path, uuid=result["uuid"],
                        name=name, scan=self.scan_version, search=search)
Example #31
0
class ItemTree:
    def __init__(self):
        self.tree = ElementTree(file="data/sale.xml")
        self.root = self.tree.getroot()
        self.u_id = set()

        for elem in self.root:
            self.u_id.add(int(elem.get("uid")))

    def getId(self):
        id_itter = 1
        while id_itter in self.u_id:
            id_itter += 1
        self.u_id.add(id_itter)
        return id_itter

    def remove_id(self, uid):
        # Free up used id's.
        self.u_id.remove(uid)

    def add_item(self, name, item_id, amount, price):
        user = SubElement(self.root, "item")
        user.set("name", name)
        user.set("itemId", str(item_id))
        user.set("price", str(price))
        user.set("add_time", str(time.time()))
        user.set("relisted", str(0))
        user.set("amount", str(amount))
        user.set("uid", str(self.getId()))
        self.save()

    def get_uid(self, uid):
        for elem in self.root:
            if elem.get("uid") == str(uid):
                return elem
        return -10

    def remove_item_uid(self, uid):
        for elem in self.root:
            if elem.get("uid") == str(uid):
                self.root.remove(elem)
                self.remove_id(uid)
                self.save()
                return 1
        return -10

    def save(self):
        # Be sure to call save() after any changes to the tree.
        f = open('data/sale.xml', 'w')
        dom = xml.dom.minidom.parseString(clean_xml(tostring(self.root)))
        f.write(dom.toprettyxml('    '))
        f.close()
Example #32
0
class ItemTree:
    def __init__(self):
        self.tree = ElementTree(file="data/sale.xml")
        self.root = self.tree.getroot()
        self.u_id = set()

        for elem in self.root:
            self.u_id.add(int(elem.get("uid")))

    def getId(self):
        id_itter = 1
        while id_itter in self.u_id:
            id_itter += 1
        self.u_id.add(id_itter)
        return id_itter

    def remove_id(self, uid):
        # Free up used id's.
        self.u_id.remove(uid)

    def add_item(self, name, item_id, amount, price):
        user = SubElement(self.root, "item")
        user.set("name", name)
        user.set("itemId", str(item_id))
        user.set("price", str(price))
        user.set("add_time", str(time.time()))
        user.set("relisted", str(0))
        user.set("amount", str(amount))
        user.set("uid", str(self.getId()))
        self.save()

    def get_uid(self, uid):
        for elem in self.root:
            if elem.get("uid") == str(uid):
                return elem
        return -10

    def remove_item_uid(self, uid):
        for elem in self.root:
            if elem.get("uid") == str(uid):
                self.root.remove(elem)
                self.remove_id(uid)
                self.save()
                return 1
        return -10

    def save(self):
        # Be sure to call save() after any changes to the tree.
        f = open("data/sale.xml", "w")
        dom = xml.dom.minidom.parseString(clean_xml(tostring(self.root)))
        f.write(dom.toprettyxml("    "))
        f.close()
Example #33
0
def get_id_set(url):
    xml = ElementTree(file = urlopen(url))
    root = xml.getroot()
    
    ids = root.findall(".//{http://www.w3.org/2005/Atom}id")
    ids.pop(0)
    
    list = []
    for i in ids:
        #print i.get("{http://itunes.apple.com/rss}id")
        list.append(i.get("{http://itunes.apple.com/rss}id"))
        
    return set(list)
 def do_PROPFIND(self):
     """Respond to a PROPFIND request."""
     print 'PROPFIND ' + self.path
     abs_path = re.sub("\\?auth", "", os.path.join(os.getcwd(), self.path[1:]))
     rc = self.get_rc(abs_path)
     #for key in self.headers.keys():
     #    print key + '\t' + self.headers[key]
     req = self.parseinputxml()
     req = ElementTree(req)
     res = ElementTree(Element(QName("DAV:", 'multistatus')))
     if rc == 200:
         self.addresponse('/', res.getroot(), 0)
     self.writeresponse(res, rc)
Example #35
0
    def __init__(self, document: ElementTree) -> None:
        super().__init__()

        self.types = []

        root = document.getroot()

        remove_namespace(
            root, "http://schemas.microsoft.com/vstudio/debugger/natvis/2010")

        for child in root:
            if child.tag == "Type":
                self.types.append(NatvisType(child))
Example #36
0
def loadConfigFile(fileName): #TODO: error handling etc.
    """Loads a config file.  If its an xml file, inheritances are automatically resolved."""
    try:
        if '.params' in fileName:
            return fileToDict(fileName)
        if '.xml' in fileName:
            config = ElementTree() 
            config.parse(fileName)
            resolveDocumentInheritances(config.getroot())
            return config
    except Exception as inst:
        main_log.info('Error loading config file ' + fileName)#, inst) TODO: log exception too
        main_log.info(str(inst))
        return None
Example #37
0
 def __init__(self, tipo):
     from xml.etree import ElementTree as et
     self.tipo = tipo
     et = et.parse('../con.xml')
     root = et.getroot()
     for child in root.iter(tipo):
         self.con_Data = {}
         for cada in child:
             d = cada.tag
             self.d = cada.text
             if cada.text == 'True' or cada.text == 'False':
                 self.con_Data[cada.tag] = bool(cada.text)
             else:
                 self.con_Data[cada.tag] = cada.text
     self.connect()
Example #38
0
 def __init__(self, tipo):
     from xml.etree import ElementTree as et
     self.tipo = tipo
     et = et.parse('../con.xml')
     root = et.getroot()
     for child in root.iter(tipo):
         self.con_Data = {}
         for cada in child:
             d = cada.tag
             self.d = cada.text
             if cada.text == 'True' or cada.text == 'False':
                 self.con_Data[cada.tag] = bool(cada.text)
             else:
                 self.con_Data[cada.tag] = cada.text
     self.connect()
Example #39
0
def clearh5(filename,
            dataDir='.',
            addname="_clean",
            tCount=None,
            global_sync=True):
    """
    in case archiving failed to collect results from all times
    """
    import h5py
    xmlFile = open(filename + ".xmf", "r")
    h5File = h5py.File(filename + ".h5", "r")
    newh5File = h5py.File(filename + "_clean" + ".h5", 'w')

    tree = ElementTree(file=xmlFile)
    xmlFile.close()
    XDMF = tree.getroot()
    Domain = XDMF[0]
    for TemporalGridCollection in Domain:
        for gridChild in TemporalGridCollection.findall("Grid"):
            counter = int(gridChild.find('Time').attrib['Name'])
            if (counter >= tCount):
                print counter
                Domain[0].remove(gridChild)

    import re
    for fieldNames in h5File.keys():
        result = int(re.search(r'\d+', fieldNames).group())
        if (result < tCount):
            newh5File.copy(h5File["/" + fieldNames], "/" + fieldNames)

    xmlFile = open(filename + addname + ".xmf", "w")
    indentXML(tree.getroot())
    tree.write(xmlFile)
    xmlFile.close()
    h5File.close()
    newh5File.close()
Example #40
0
    def fillin_mainB(db_b, nodePath):
      nonlocal indent_level
      nodeXMLfile=".basket"
      nodeTree = ElementTree(file=(nodePath + nodeXMLfile))
      xml_rawroot = nodeTree.getroot()

      xml_root = xml_rawroot.getchildren()[1]

      def traverseBranch(xml_parent_b, db_parent_b, fillin_Parent = False):
        nonlocal indent_level
        for xml_b in xml_parent_b:
          if xml_b.tag == "note":
            type = xml_b.get("type")
            content = xml_b.find("content").text

            data = ""
            if type == "html":
              with open(nodePath + content) as nodeContFile:
                data = nodeContFile.read()
            elif type == "link" or type == "cross_reference" or type == "file" or type == "image":
              data = content
            else:
              data = content
              #print("\n Error: Unrecognized type of the note! \n")
            """
            elif type == "cross_reference":
            elif type == "file":
            elif type == "image":
              #<p><img alt="" data-cke-saved-src="www.ya.ru" src="www.ya.ru"><br></p>
            """
            if ( db_parent_b.text == "" and fillin_Parent ):
              print(indent(indent_level + 1) + str(content))
              db_parent_b.text = data
            else:
              print(indent(indent_level) + "subBranch: " + str(content))
              db_b = Branch(tree=curtree, main=False, folded=False, parent=db_parent_b)
              db_b.text = data
          if xml_b.tag == "group":
            # it is first level branches and has one groupe only
            if ( xml_root == xml_parent_b and len(xml_parent_b.getchildren()) == 1):
              traverseBranch(xml_b, db_parent_b, fillin_Parent = False)
            else:
              db_b = Branch(tree=curtree, main=False, folded=xml_b.get("folded"), text="", parent=db_parent_b)
              print(indent(indent_level) + "subBranch: ")
              indent_level += 1
              traverseBranch(xml_b, db_b, fillin_Parent = True)
              indent_level -= 1
      traverseBranch(xml_root, db_b)
Example #41
0
def parseXml(filename):
    filename = os.path.abspath(filename);
    if not os.path.exists(filename):
        raise IOError("File not found!")

    etree = ElementTree(file=filename)
    eroot = etree.getroot() # chemical_element_list
    for elem in eroot.getchildren():
        # we care only about single chemical_elements
        if elem.tag != "chemical_element": continue
        finalElem = update(elem)
        if finalElem == None: continue
        print stringifyElement(finalElem, 0)

#    print tem.elemDefinition()
    print tem.definition()
Example #42
0
def parseXml(filename):
    filename = os.path.abspath(filename)
    if not os.path.exists(filename):
        raise IOError("File not found!")

    etree = ElementTree(file=filename)
    eroot = etree.getroot()  # chemical_element_list
    for elem in eroot.getchildren():
        # we care only about single chemical_elements
        if elem.tag != "chemical_element": continue
        finalElem = update(elem)
        if finalElem == None: continue
        print stringifyElement(finalElem, 0)

#    print tem.elemDefinition()
    print tem.definition()
Example #43
0
def loadConfigFile(fileName):  #TODO: error handling etc.
    """Loads a config file.  If its an xml file, inheritances are automatically resolved."""
    try:
        if '.params' in fileName:
            return fileToDict(fileName)
        if '.xml' in fileName:
            config = ElementTree()
            config.parse(fileName)
            resolveDocumentInheritances(config.getroot())
            return config
    except Exception as inst:
        if '.xml' in fileName:
            main_log.error('Error loading config file ' +
                           fileName)  #, inst) TODO: log exception too
            main_log.error(str(inst))
        return None
Example #44
0
class StackTree(ItemTree):
    def __init__(self):
        self.save_file = 'data/stack.xml'
        self.tree = ElementTree(file=self.save_file)
        self.root = self.tree.getroot()
        self.u_id = set()
        self.id_itter = 101
        self.next_id = self.id_itter

        for elem in self.root:
            self.u_id.add(int(elem.get("uid")))

    def add_item(self, name, item_id, amount, price):
        user = SubElement(self.root, "item")
        user.set("name", name)
        user.set("itemId", str(item_id))
        user.set("price", str(price))
        user.set("amount", str(amount))
        user.set("uid", str(self.getId()))
        self.save()

    # Here we take the oldest id put into stack.xml so we have FIFO
    def get_next_id(self):
        if len(self.u_id) == 0:
            self.next_id = self.id_itter
            return
        else:
            # TODO Here I'm handling rotation (gets a high number then
            # back to 101). Any better manners to do this?
            if min(self.u_id) - self.next_id < 0:
               next_uid = self.next_id - min(self.u_id)
               self.next_id = self.id_itter + next_uid + 1
            else:
               next_uid = min(self.u_id) - self.next_id
               self.next_id += next_uid
            return
        

    def remove_item_uid(self, uid):
        for elem in self.root:
            if elem.get("uid") == str(uid):
                self.root.remove(elem)
                self.remove_id(uid)
                self.save()
                return 1
        self.get_next_id()
        return -10
Example #45
0
def findLoosePages(pages,files,indir):
        
    if debugMode():
        print("findLoosePages:",indir)

    # get all the directory information
    entries = os.walk(indir)

    # look for files not already in the lists
    for entry in entries:
        if debugMode():
            print(entry)
        dir = entry[0]
        rdir = os.path.relpath(dir,indir)
        
        efiles = entry[2]
        for file in efiles:
            fpath = rdir+os.sep+file
            fpath = fpath.replace('\\','/')
            # look for an html file not in the toc
            if isHTML(fpath) and (not fpath in files):
              # create a pages and files entry for it
              print(" add file not in toc:",fpath)
              pe = {"node": None, "level":0,"parent":None,"in_index":False}
              pe["href"] = fpath
              pe["tag"] = None
              # read the title from the file
              ff = os.path.normpath(indir + os.sep + fpath)
              tree = ElementTree()
              try:
                  tree.parse(ff)
              except:
                  print("Error parsing",ff)
                  continue
              troot = tree.getroot()
              title = troot.find("head/title")
              if (not title==None) and (not title.text == None):
                  pe["title"] = title.text
              else:
                  pe["title"] = ""
              pages.append(pe)
              pageno = len(pages)-1
              # collect file data
              if not fpath in files:
                 fdata = {'pageno':pageno}
                 files[fpath] = fdata
Example #46
0
def addEmoticon(xmlPath,files):
	ID=int(time.time())
	print  xmlPath, files
	fs=files#os.listdir(ROOT_DIR)
	mainXML=ElementTree(file=xmlPath)
	mainRoot=mainXML.getroot()
	haveOn=False
	mainData=mainRoot.find("Catalog")
	'''
	for s in mainRoot.findall("Catalog"):
		if s.get("Title")=="fengx":
			haveOn=True
			#mainData=s
			mainData=mainRoot.find("Catalog")
	if haveOn==False:
		mainData=Element("Catalog", {'id' : "ID_fengx", 'Path' : ' ', "Title":"fengx"})
	
	print haveOn
	'''
	if len(fs)!=0:
		i=1
		for f in fs:
			fileType=os.path.splitext(os.path.split(f)[1])[1] 
			name=os.path.splitext(os.path.split(f)[1])[0]
			if fileType==".jpg" or fileType==".png" or fileType==".bmp" or fileType==".JPG" or fileType==".PNG" or fileType==".GIF" or fileType==".gif":  
				n=name+str(i)
				print n
				print ID
				ID+=1
				print ID
				fileName=n+fileType

				tPath=os.path.split(xmlPath)[0]+"\\"+fileName 		
				shutil.copy(f,  tPath)

				#copyFiles(f, tPath)

				#os.system ("xcopy /s %s %s" % (f, tPath))
				item=emotionElement(str(ID),n,fileName)
				i+=1
				mainData.append(item)
		#if haveOn==False:
		#	mainRoot.append(mainData)
		mainXML._setroot(indent(mainRoot))
		mainXML.write(xmlPath,"utf-8")
Example #47
0
class DelistedTree(ItemTree):
    def __init__(self):
        self.save_file = 'data/delisted.xml'
        self.tree = ElementTree(file=self.save_file)
        self.root = self.tree.getroot()
        self.u_id = set()
        self.id_itter = 301

        for elem in self.root:
            self.u_id.add(int(elem.get("uid")))

    def add_item(self, name, item_id, amount):
        user = SubElement(self.root, "item")
        user.set("name", name)
        user.set("itemId", str(item_id))
        user.set("amount", str(amount))
        user.set("uid", str(self.getId()))
        self.save()
def update_keymap(keymap: ElementTree, replacements: Dict[str, Replacement]):
    print("updating keymap")
    actions = keymap.getroot()
    updated_shortcuts_cnt = 0
    for action in actions:
        for shortcut in action:
            if shortcut.tag != "keyboard-shortcut":
                continue

            should_update = get_first_key(shortcut) in replacements
            if has_second_keystroke(shortcut):
                should_update = should_update or (get_second_key(shortcut)
                                                  in replacements)

            if should_update:
                updated_shortcuts_cnt += 1
                update(shortcut, replacements)

    print(updated_shortcuts_cnt, "shortcuts were updated")
Example #49
0
 def restore(self):
   " Restore tree structure from xml file "
   def restore_fromXML(tree_curI, xml_curE):
     elements = [elem for elem in xml_curE]
     if xml_curE.tag == 'root':
       elements = [elem for elem in elements[0]]
     for curE in elements:
       if curE.tag == 'Branch':
         curI = tree_curI.add(Branch(name=curE.get('name')))
         restore_fromXML(curI, curE)
       if curE.tag == 'Leaf':
         curI = tree_curI.add(Leaf(name=curE.get('name'), desc=curE.get('desc'), path=curE.get('path')))
   try:
     xml_tree = ElementTree(file=structureFile)
     xml_root = xml_tree.getroot()
     tree = self.__root
     restore_fromXML(tree, xml_root)
   except IOError:
     raise XMLfileNotfound
     pass
Example #50
0
def filter_articles(tree: ElementTree, issue_num: str) -> List[Article]:
    """Given an ElementTree parsed from an XML dump, returns a list 
    of Article instances containing all the articles tagged with issue_num.
    """
    root = tree.getroot()
    articles: List[Article] = []
    article_tags = root.findall('.//item')
    for article_tag in article_tags:
        if not is_for_issue(article_tag, issue_num):
            continue
        article = Article()
        #possible optimization, instead of calling find several times,
        #loop through tag children once and parse out data as we run into it
        article.title = article_tag.find('title').text
        #we will post process this later
        article.author = 'UNKNOWN AUTHOR'
        article_text_content = article_tag.find('content:encoded', XML_NS).text
        article.content = BeautifulSoup(article_text_content, 'html.parser')
        articles.append(article)
    return articles
Example #51
0
def main():
    tree = ElementTree()
    tree.parse(argv[1])
    root = tree.getroot()

    test_results = Element('TestResults')
    #test_results.text = "\n\t"
    homes = []
    i = 0
    for child in root:
        home = Home(i, root)
        homes.append(home)
        test = SubElement(test_results, 'Test')
        test.set('addr', child.get('addr'))
        test.text = str(home.largest_sight_line())
        print()
        i += 1

    tree = ElementTree(test_results)
    tree.write(argv[2])
Example #52
0
class XmlRecordset():
    def __init__(self):
        self.__doc = None
        self.__savePath = None
        
    def open(self, path):
        if not os.path.exists(path):
            self.__createNew()
        else:
            self.__doc = ElementTree(path)
        self.__savePath = path
    
    def save(self, path = None):
        savePath = path or self.__savePath
        if None == savePath:
            return False
        self.__doc.write(self.__savePath, encoding = 'utf-8')
    
    def __createNew(self):
        root = Element(u'photoAlbums')
        doc = ElementTree(root)
        self.__doc = doc
        
    def getAlbumNode(self, path):
        def _getNode(albumsNode):
            if albumsNode == None:
                return None
            for albumNode in albumsNode:
                tmpPath = albumNode.attrib['path']
                if path == tmpPath:
                    return albumNode
                if len(path) > len(tmpPath):
                    if path.startswith(tmpPath):
                        subAlbumsNode = albumNode.find('subAlbums')
                        return _getNode(subAlbumsNode)
                
            return None
        
        ret = _getNode(self.__doc.getroot())
        return ret
Example #53
0
def mediaList(media,files,f,tdir):
    if debugMode():
        print("mediaList",f,tdir)

    cnt=0 # count of media files found

    # parse the html file
    ff = os.path.normpath(tdir + os.sep + f)
    subtree = ElementTree()
    subtree.parse(ff)
    root = subtree.getroot()
    body = root.find("body")
    
    # get all the img elements
    imgs = body.iterfind("*//img")
                        
    # populate information for all the images
    for img in imgs:
        # compute the image normalized path
        spath=img.get("src")
        if not isURL(spath):
            npath = os.path.normpath(ff+os.sep+".."+os.sep+spath)
            npath = os.path.abspath(npath)
            if debugMode():
                print(" image:",npath)
            if not npath in media:
                # remember this media reference
                media[npath]={"alt":img.get("alt")}
                cnt=cnt+1
                if "media" in files[f]:
                    files[f]["media"].append(npath)
                else:
                    files[f]["media"]=[npath]
        else:
            print("skipping img",spath)

    if debugMode():
        print(cnt,"images found in",f)
        
    return cnt
Example #54
0
class xml_document(object):
    """
    ElementTree requires that the complete XML be loaded in memory
    before working with it, which may be discouraging when dealing
    with large files. By abstracting it this way, if we want to
    implement a more efficient approach later, we will avoid the need
    for messing with other sections of code.
    """

    def __init__(self, element=None, file_obj=None, namespace_list=()):
        """
        __init__ initializes a reference to the ElementTree parser, passing it
        the a file descripter object to be read and parsed or the
        ElemenTree.Element object to be used as the root element.
        """
        self.namespace_list = list(namespace_list)
        self.etree = ElementTree.ElementTree(element=element, file=file_obj)

    def parse_string(self, source):
        "Loads an XML document from an XML string, source."
        root = ElementTree.fromstring(source)
        self.etree = ElementTree(element=root)

    def parse_file(self, source):
        """
        Loads an XML document from source, which can either be a
        filepath string or a file object.
        """
        root = ElementTree.parse(source=source)
        self.etree = ElementTree(element=root)

    def getiterator(self, tag):
        """
        Returns an iterator over all top-level elements from the root element
        that have the matching tag.
        """
        return _getiterator(self.etree.getroot(), tag, self.namespace_list)
Example #55
0
class UserTree:
    def __init__(self):
        self.tree = ElementTree(file="data/user.xml")
        self.root = self.tree.getroot()

    def add_user(self, name, stalls, al):
        if self.get_user(name) == -10:
            user = SubElement(self.root, "user")
            user.set("name", name)
            user.set("stalls", str(stalls))
            user.set("used_stalls", str(0))
            user.set("money", str(0))
            user.set("id", str(0))
            user.set("accesslevel", str(al))
            self.save()

    def get_user(self, name):
        for elem in self.root:
            if elem.get("name") == name:
                return elem
        return -10

    def remove_user(self, name):
        for elem in self.root:
            if elem.get("name") == name:
                self.root.remove(elem)
                self.save()
                return 1
        return -10

    def save(self):
        # Be sure to call save() after any changes to the tree.
        f = open("data/user.xml", "w")
        dom = xml.dom.minidom.parseString(clean_xml(tostring(self.root)))
        f.write(dom.toprettyxml("    "))
        f.close()
Example #56
0
 def fromStandaloneFile(path):
     """For a file that contains a single OVAL XML element as the root element, instantiate the appropriate OvalElement sublcass for that element
     @type path: string
     @param path: the path to the file
     
     @rtype OvalElement
     @return None on error, or an object of the appropriate OvalElement subclass
     """
     
     if not path or path is None:
         return None
     
     if not os.path.exists(path):
         return None
     
     try:
         tree = ElementTree()
         tree.parse(path)
         
         root = tree.getroot()
         return OvalElement.asOvalElement(root)
     
     except Exception:
         return None
Example #57
0
    def test_element_tree(self):
        from xml.etree import ElementTree

        # 3$$$as ET #, getiterator
        from xml.etree.ElementTree import ElementTree, Element, SubElement, dump

        handle, file_name = tempfile.mkstemp(".xml", __name__ + "_")
        os.close(handle)
        handle = open(file_name, "w")

        sample = """<source_model_zone magnitude_type="Mw">
  <zone 
  area = "5054.035" 
  name = "bad zone">
    
    <geometry 
       azimuth= "6" 
       delta_azimuth= "2" 
       dip= "15"
       delta_dip = "5"
       depth_top_seismogenic = "7"
       depth_bottom_seismogenic = "30">
      <boundary>
	  -32.4000 151.1500   
	  -32.7500 152.1700 
	  -33.4500 151.4300   
	  -32.4000 151.1500 
      </boundary>
      <excludes>
	  -32.4000 151.1500     
	  -32.7500 152.1700    
	  -33.4500 151.4300  
      </excludes>
    </geometry>
    
    <recurrence_model
      distribution = "bounded_gutenberg_richter"
      recurrence_min_mag = "3.3" 
      recurrence_max_mag = "5.4" 
      A_min= "0.568" 
      b = "1">
      <event_generation 
      generation_min_mag = "3.3"
	  number_of_mag_sample_bins = "15" 
	  number_of_events = "1000" />
    </recurrence_model>
    
    <ground_motion_models 
       fault_type = "normal" 
       ground_motion_selection = "crustal fault" />   
  </zone>
</source_model_zone>
"""
        handle.write(sample)
        handle.close()

        tree = ElementTree(file=file_name)
        elem = tree.getroot()
        # works
        # dump(elem)
        for sub in elem:  # .getiterator():
            pass
            # dump(sub)
        os.remove(file_name)
Example #58
0
def main():
    usage = "usage: %prog [options] dumpFile [URL]"
    parser = OptionParser(usage, version="%prog 0.1")
    parser.add_option("-v", "--verbose", action="store_true", dest="verbose")
    parser.add_option("-p", "--password", dest="password", help="HTTP Digest password", default="123456")
    url = "http://www.fluffyhome.com"
    url = "http://localhost:8088"

    (options, args) = parser.parse_args()
    if len(args) > 2:
        parser.error("incorrect number of arguments")
    if len(args) < 1:
        parser.error("incorrect number of arguments")

    fileName = args[0]
    if len(args) == 2:
        url = args[1]

    print "Uploading %s to %s" % (fileName, url)

    tree = ElementTree(file=fileName)
    root = tree.getroot()
    assert root is not None

    user = root.find("user")
    assert user is not None

    userName = user.attrib.get("userName")

    # create the user
    u = url + "/admin/user/" + userName + "/"
    post(None, u, "application/x-www-form-urlencoded", "admin", "none")

    # update the user record
    email = user.attrib.get("email")
    attributes = user.items()
    data = urlencode(attributes)
    u = url + "/user/" + userName + "/prefs/"
    post(data, u, "application/x-www-form-urlencoded", userName, options.password)

    # create the sensors
    sensors = tree.findall("sensor")
    for sensor in sensors:
        sensorName = sensor.attrib.get("sensorName")

        attributes = sensor.items()
        data = urlencode(attributes)
        u = url + "/sensor/" + userName + "/" + sensorName + "/create/"

        post(data, u, "application/x-www-form-urlencoded", userName, options.password)

    # create the meassurement data
    count = 0
    data = ""
    measurements = tree.findall("measurement")
    for measurement in measurements:
        sensorName = str(measurement.attrib.get("sensor"))
        time = int(measurement.attrib.get("time"))

        # print sensorName,time,value,integral

        if count == 0:
            data = "[\n"
        else:
            data += ", \n"

        data += '  { "n":"%s", "t":%d' % (sensorName, time)

        value = measurement.attrib.get("value")
        if value is not None:
            data += ', "v":%f' % float(value)

        integral = measurement.attrib.get("integral")
        if integral is not None:
            data += ', "s":%f' % float(integral)

        joules = measurement.attrib.get("joules")
        if joules is not None:
            data += ', "j":%f' % float(joules)

        patchLevel = measurement.attrib.get("patchLevel")
        if patchLevel is None:
            patchLevel = 0
        data += ', "pl":%d' % int(patchLevel)

        data += " }"

        count = count + 1

        if count > 20:
            data += "\n]\n"
            u = url + "/sensorValues/"
            post(data, u, "application/json", userName, options.password)
            count = 0
            data = ""
    if count > 0:
        data += "\n]\n"
        u = url + "/sensorValues/"
        post(data, u, "application/json", userName, options.password)
        count = 0
        data = ""
Example #59
0
image_count = 0

node_data = {}

###################################
#
# MAIN PROCESSING SECTION
#
###################################

# build a tree from the manifest XML
intree = ElementTree()
intree.parse(input_file)

# get the root element
root = intree.getroot()
if debugMode():
    print("XML root:",root.tag)
indir = root.get("dir")

# determine CMS
incmse = root.find("CMS")
if not incmse == None:
    cms = incmse.text
else:
    cms = DRUPAL

# display parameters
print("settings:")
print("input file:",input_file)
print("CMS:",cms)
Example #60
0
class OvalDocument(object):
    """
    For working with OVAL documents.  That interaction will entail the use of the other classes.
    Can be used to find certain elements within the document, update the document, and save the changes to a file
    """
        
    
    # A time format to match what OVAL expects
    TIME_FORMAT = "%Y-%m-%dT%H:%M:%S%z"
    
    NS_DEFAULT  = {"def": "http://oval.mitre.org/XMLSchema/oval-definitions-5"}
    NS_OVAL     = {"oval": "http://oval.mitre.org/XMLSchema/oval-common-5"}
    NS_XSI      = {"xsi": "http://www.w3.org/2001/XMLSchema-instance"}
    
#     xmlns:oval="http://oval.mitre.org/XMLSchema/oval-common-5" 
#     xmlns:oval-def="http://oval.mitre.org/XMLSchema/oval-definitions-5" 
#     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
#     xsi:schemaLocation="http://oval.mitre.org/XMLSchema/oval-definitions-5 oval-definitions-schema.xsd
#     http://oval.mitre.org/XMLSchema/oval-definitions-5#independent independent-definitions-schema.xsd 
#     http://oval.mitre.org/XMLSchema/oval-definitions-5#solaris solaris-definitions-schema.xsd 
#     http://oval.mitre.org/XMLSchema/oval-common-5 oval-common-schema.xsd 
#     http://oval.mitre.org/XMLSchema/oval-definitions-5#unix unix-definitions-schema.xsd">^M
    
    
    @staticmethod
    def indent(elem, level=0):
        i = "\n" + level*"  "
        if len(elem):
            if not elem.text or not elem.text.strip():
                elem.text = i + "  "
            if not elem.tail or not elem.tail.strip():
                elem.tail = i
            for elem in elem:
                OvalDocument.indent(elem, level+1)
            if not elem.tail or not elem.tail.strip():
                elem.tail = i
        else:
            if level and (not elem.tail or not elem.tail.strip()):
                elem.tail = i    
    
    
    @staticmethod            
    def getOvalTimestamp(timestamp=None):
        """Renders a datetime to a string formatted according to the OVAL specification.
        if the timestamp argument is None (which it is by default) or is not of type datetime,
        this function will return a string using the current datetime.
        
        @type timestamp: datetime
        @param timestamp: A datetime to be formatted as an OVAL timestamp, or None to use the current time.
        
        @rtype: string
        @return: a string formatted as per OVAL
        """
        if timestamp is None or not isinstance(timestamp, datetime):
            now = datetime.date.today() 
            return now.strftime(OvalDocument.TIME_FORMAT)
        else:
            return timestamp.strftime(OvalDocument.TIME_FORMAT)
 
    
    
    def __init__(self, tree):
#         if not tree or not isinstance(tree, ElementTree):
        if not tree:
            root = Element("oval_definitions")
            self.tree = ElementTree.ElementTree(root)
            element = Element("{" + OvalDocument.NS_DEFAULT.get("def") + "}generator")
            gen = OvalGenerator(element)
            gen.setProduct("The CIS OVAL Repository")
            gen.setTimestamp(None)
            gen.setSchemaVersion("5.10.1")
            root.append(gen.get_element())
            return        
        
        self.tree = tree
        
        
    def parseFromFile(self, filename):
        """
        Load an OVAL document from a filename and parse that into an ElementTree
        Returns False if the filename is empty or there is an error parsing the XML document
        @type filename: string
        @param filename: The path to the OVAL XML document to parse
        
        @rtype:    boolean
        @return:    True on success, otherwise False
        """
        try: 
            if not filename:
                self.tree = None
                return False
            else:
                self.tree = ElementTree.parse(filename)
                return True
        except Exception:
            return False
        
        
    def parseFromText(self, xmltext):
        """
        Initializes the ElementTree by parsing the xmltext as XML
        Returns False if the string could not be parsed as XML
        
        @rtype:    boolean
        @return:    True on success, otherwise False
        """
        try:
            if not xmltext:
                return False
            else:
                root = ElementTree.fromstring(xmltext)
                self.tree = ElementTree(root)
                return True
        except Exception:
            return False
            
            
    def writeToFile(self, filename):
        """
        Writes the internal representation of the XmlTree to the given file name
        Returns False on error
        
        @rtype:    boolean
        @return:    True on success, otherwise False
        """
        try:
            if not filename:
                return False
            if not self.tree:
                return False
            
            ### TODO:  Add all necessary namespaces
            self.tree.write(filename, "UTF-8", True, OvalDocument.NS_DEFAULT, "xml")
                
        except Exception:
            return False
        
        
    def to_string(self):
        
        if not self.tree:
            return None
        
        root = self.tree.getroot()
        if root is None:
            return ""
        OvalDocument.indent(root)
        return ElementTree.tostring(root, "UTF-8", "xml").decode("utf-8")
    
        
    def getDocumentRoot(self):
        """
        Returns the root element of the XML tree if one has been loaded.
        Otherwise, returns None
        
        @rtype:    Element
        @return:    The root Element of the OVAL document, or None
        """
        if not self.tree:
            return None
        
        return self.tree.getroot()
    
    
    def getGenerator(self, create=False):
        """
        Gets the generator for this OVAL document as an OvalGenerator object.
        If the generator element does not exist, the default behavior is to
        return none.  However, setting the optional parameter to True will cause
        a default generate element to be created, added to the document, and that will be returned.
        A value of None may also be returned if this OvalDocument is empty
        
        @rtype:    OvalGenerator
        @return:    An OvalGenerator object, or None if it does not exist and create was not set to True
        """
        if not self.tree:
            return None
        
        root = self.getDocumentRoot()
        if not root:
            return None
        
        gen_element = root.find("def:generator", OvalDocument.NS_DEFAULT)
        
        if gen_element is not None:
            return OvalGenerator(gen_element)
        
        if not create:
            return None
        else:
            element = Element("{" + OvalDocument.NS_DEFAULT.get("def") + "}generator")
            gen = OvalGenerator(element)
            gen.setProduct("The CIS OVAL Repository")
            gen.setTimestamp(None)
            gen.setSchemaVersion("5.10.1")
            return gen
            
    
    
                
    def getDefinitions(self):
        """
        Returns a list of all definitions found in this OvalDocment where each item in the list is of type OvalDefinition
        Returns None if no definitions could be found
        
        @rtype:    List
        @return:    All definitions in the OVAL document or None if none were found
        """
        root = self.getDocumentRoot()
        if not root:
            return None
        
        defroot = root.find("def:definitions", OvalDocument.NS_DEFAULT)
        
        if defroot is None:
            return None
        
        element_list = list(defroot)
        if not element_list:
            return None
        
        return [OvalDefinition(element) for element in element_list]
        
        

        
    def getTests(self):
        """
        Returns a list of all tests in this OvalDocument where each list item is of type OvalTest
        Returns None if no tests could be found
        
        @rtype:    List
        @return:    All tests in the OVAL document or None if none were found
        """
        root = self.getDocumentRoot()
        if not root:
            return None
        
        testroot = root.find("def:tests", OvalDocument.NS_DEFAULT)

        if testroot is None:
            return None
        
        element_list = list(testroot)
        if not element_list:
            return None
        
        return [OvalTest(element) for element in element_list]
        
        
        
    def getObjects(self):
        """
        Returns a list of all objects in this OvalDocument where each list item is of type OvalObject
        Returns None if no objects could be found
        
        @rtype:    List
        @return:    All objects in the OVAL document or None if none were found
        """
        root = self.getDocumentRoot()
        if not root:
            return None
        
        objectroot = root.find("def:objects", OvalDocument.NS_DEFAULT)
        
        if objectroot is None:
            return None
        
        element_list = list(objectroot)
        if not element_list:
            return None
        
        return [OvalObject(element) for element in element_list]
    
        
        
    def getStates(self):
        """
        Returns a list of all states in this OvalDocument where each list item is of type OvalState
        Returns None if no states could be found
        
        @rtype:    List
        @return:    All states in the OVAL document or None if none were found
        """
        root = self.getDocumentRoot()
        if not root:
            return None
        
        stateroot = root.find("def:states", OvalDocument.NS_DEFAULT)
        
        if stateroot is None:
            return None
        
        element_list = list(stateroot)
        if not element_list:
            return None
        
        return [OvalState(element) for element in element_list]
        
        
        
        
    def getVariables(self):
        """
        Returns a list of all variables in this OvalDocument where each list item is of type OvalVariable
        Returns None if no variables could be found
        
        @rtype:    List
        @return:    All variables in the OVAL document or None if none were found
        """
        root = self.getDocumentRoot()
        if not root:
            return None
        
        varroot = root.find("def:variables", OvalDocument.NS_DEFAULT)
        
        if varroot is None:
            return None
        
        element_list = list(varroot)
        if not element_list:
            return None
        
        return [OvalVariable(element) for element in element_list]
        
        
        
        
    def getElementByID(self, ovalid):
        """
        Uses the ovalid argument to determine what type of element is being referenced and locate that element
        in the OVAL ElementTree.
        Returns an OvalElement of the appropriate class (OvalDefinition, OvalTest, ...) 
        or None if there is no ElementTree or if a matching item could not be found
        
        @rtype:    OvalElement
        @return:    The located element as the appropriate OvalElement subclass, or None if no matching element was found.
        """
        if not ovalid:
            return None
        
        root = self.getDocumentRoot()
        if not root:
            return None
        
        try:
            oval_type = OvalElement.getElementTypeFromOvalID(ovalid)
        except Exception:
            return None
        
        if oval_type == OvalDefinition.DEFINITION:
            elist = self.getDefinitions()
        elif oval_type == OvalDefinition.TEST:
            elist = self.getTests()
        elif oval_type == OvalDefinition.OBJECT:
            elist = self.getObjects()
        elif oval_type == OvalDefinition.STATE:
            elist = self.getStates()
        elif oval_type == OvalDefinition.VARIABLE:
            elist = self.getVariables()
        else:
            return None
            
        if not elist:
            return None
        
        for element in elist:
            defid = element.getId()
            if defid and defid == ovalid:
                return element
            
                    
        
    
    def addElement(self, element, replace=True):
        """
        Adds the element to the ElementTree for this OVAL document
        The element argument must be of type OvalElement
        This method uses the OVALID of the element to determine what type of element it is
        and if an existing element with that OVALID already exists.
        This method will also create the necessary structure (id est, adding <definitions>, <tests>, etc)
        if the ElementTree does not already contain it.
        By default this method will replace an existing item with the same OVALID, but this behavior can
        be overridden by changing the option second argument to a value of "False"
        Returns True on success, otherwise False
        
        @rtype:    boolean
        @return:    True if the element was added to the document, otherwise False
        """
        if not element or element is None:
            return False
        if not self.tree or self.tree is None:
            return False
        
        ovalid = element.getId()
        if not ovalid:
            return False
        
        root = self.tree.getroot()
        if not root:
            root = Element("{" + OvalDocument.NS_DEFAULT.get("def") + "}oval_definitions")
            self.tree._setroot(root)
            
        # If replace has been set to False, then we want to exit with no changes
        #  when an element with this OVALID already appears in the document            
        if not replace:
            existing = self.getElementByID(ovalid)
            if existing:
                return False;
            
        
        try:
            oval_type = OvalElement.getElementTypeFromOvalID(ovalid)
        except Exception:
            return False
                
        # Depending on the ID type, find the parent for it or create that parent if it doesn't exist
        # Then append the current element
        if oval_type == OvalDefinition.DEFINITION:
            parent = root.find("def:definitions", OvalDocument.NS_DEFAULT)
            if parent is None:
                parent = Element("{" + OvalDocument.NS_DEFAULT.get("def") + "}definitions")
                root.append(parent)
                
            parent.append(element.getElement())
            return True
        
        elif oval_type == OvalDefinition.TEST:
            parent = root.find("def:tests", OvalDocument.NS_DEFAULT)
            if parent is None:
                parent = Element("{" + OvalDocument.NS_DEFAULT.get("def") + "}tests")
                root.append(parent)
                
            parent.append(element.getElement())
            return True
        
        elif oval_type == OvalDefinition.OBJECT:
            parent = root.find("def:objects", OvalDocument.NS_DEFAULT)
            if parent is None:
                parent = Element("{" + OvalDocument.NS_DEFAULT.get("def") + "}objects")
                root.append(parent)
                
            parent.append(element.getElement())
            return True
        
        elif oval_type == OvalDefinition.STATE:
            parent = root.find("def:states", OvalDocument.NS_DEFAULT)
            if parent is None:
                parent = Element("{" + OvalDocument.NS_DEFAULT.get("def") + "}states")
                root.append(parent)
                
            parent.append(element.getElement())
            return True
        
        elif oval_type == OvalDefinition.VARIABLE:
            parent = root.find("def:variables", OvalDocument.NS_DEFAULT)
            if parent is None:
                parent = Element("{" + OvalDocument.NS_DEFAULT.get("def") + "}variables")
                root.append(parent)
                
            parent.append(element.getElement())
            return True
        
        else:
            return False