예제 #1
0
파일: igd.py 프로젝트: fatpat/igd-exporter
def probe_metric(service_url, metric):
    '''
    Query the service at the given URL for the given metric value.

    Assumptions are made about the name of the method and output parameters
    which are only valid for the WanCommonInterfaceConfig service.
    '''
    envelope = E(
        QName(ns['s'], 'Envelope'), {
            QName(ns['s'], 'encodingStyle'):
            'http://schemas.xmlsoap.org/soap/encoding/'
        })
    body = sE(envelope, QName(ns['s'], 'Body'))
    method = sE(body, QName(ns['i'], 'Get{}'.format(metric)))
    request_tree = ET(envelope)
    with io.BytesIO() as out:
        out.write(b'<?xml version="1.0"?>')
        request_tree.write(out, encoding='utf-8')
        out.write(b'\r\n')  # or else my Belkin F5D8236-4 never responds...
        req = urllib.request.Request(service_url, out.getvalue())

    req.add_header('Content-Type', 'text/xml')
    req.add_header('SOAPAction', '"{}#{}"'.format(ns['i'],
                                                  'Get{}'.format(metric)))

    with urllib.request.urlopen(req) as result:
        result_tree = ElementTree.parse(result)
        return int(
            result_tree.findtext('.//New{}'.format(metric), namespaces=ns))
예제 #2
0
    def vm_read_config(self):
        """
        This method parses the libvirt xml config file and fills the 
        cfg_details dictionary. This method returns the dictionary or 
        raise exception if xml is not valid.
        """

        domain = ET().parse(self.cfg_file)
        vm_type = domain.get('type')
        self.cfg_details['vm_type'] = HVM_LIBVIRT_NAMEMAP[vm_type]
        self.cfg_details['vm_type_str'] = vm_type
        self.cfg_details['displayName'] = domain.find('name').text
        self.cfg_details['memsize'] = int(domain.find('memory').text) >> 10

        primary_disk_list = []
        for disk in domain.findall('devices/disk'):
            disk_details = self.get_disk_details(disk)
            if disk.get('type') == 'file' and \
               disk.get('device') == 'disk' and \
               disk_details['dev'] in ('sda', 'hda', 'vda') and \
               disk_details['bus'] in ('ide', 'scsi', 'virtio'):
                primary_disk_list.append(os.path.basename(
                    disk_details['file']))
                break

        self.cfg_details['primary_disk'] = primary_disk_list
        self.cfg_details['primary_disk_str'] = ','.join(primary_disk_list)

        if not self.cfg_details:
            raise config_file_invalid()
        else:
            return self.cfg_details
def get_xmlroot(filepath):
    """Return ElementTree root of xml file. You can pass in the path to the actual
    xml file or a path to a tif with the same name as the xml file."""
    filepath = get_xml_filename(filepath)
    tree = ET()
    tree.parse(filepath)
    return tree
예제 #4
0
파일: YoUtil.py 프로젝트: yoramo1/Ecat
def get_devices_desc(path):
    pass
    tree = ET()
    tree.parse(path)
    lst = tree.findall('./Descriptions/Devices/Device')
    all_devices = list()
    for device_node in lst:
        device_name = None
        last_name = None
        if device_node != None:
            type_node = device_node.find('Type')
            if type_node != None:
                prod = type_node.attrib['ProductCode']
                rev = type_node.attrib['RevisionNo']

                for name_node in device_node.findall('Name'):
                    if name_node != None:
                        last_name = name_node.text
                        #print('>> ',name_node.text)
                        if 'LcId' in name_node.attrib.keys():
                            lcid = name_node.attrib['LcId']
                            if lcid == '1031':
                                device_name = name_node.text
                if device_name is None:
                    device_name = last_name
                all_devices.append((prod, rev, device_name))
    return all_devices
예제 #5
0
def esummary(idlist, db, start=0, batchsize=100):
    #specifically used for Pubmed and Pubmed Central
    #will probably break with other databases
    if type(idlist) not in (list, tuple): sorted_ids = sorted(idlist)
    else: sorted_ids = idlist

    baseurl = 'https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esummary.fcgi'
    postdata = {'db': db}

    summaries = {}

    for i in range(start, start + batchsize, batchsize):
        postdata.update({'id': ','.join(sorted_ids[i:i + batchsize])})
        #f = urllib.urlopen(baseurl, data=urllib.urlencode(postdata))
        f = _curl(baseurl, urllib.urlencode(postdata))
        et = ET()
        esumresult = et.parse(f)
        curid = None
        curobj = {}
        for docsum in esumresult:
            for prop in docsum:
                if prop.tag == 'Id':
                    if curid: summaries[curid] = curobj
                    curid = prop.text
                    curobj = {}
                elif prop.tag == 'Item':
                    if prop.attrib['Name'] == 'AuthorList':
                        authorlist = [author.text for author in prop]
                        curobj['AuthorList'] = authorlist
                    else:
                        curobj[prop.attrib['Name']] = prop.text
        if curid: summaries[curid] = curobj

        time.sleep(0.35)
    return summaries
예제 #6
0
def parseRootElement():
    if not xml_doc:
        return
    else:
        tree = ET()
        root = tree.parse(xml_doc)
        return tree, root
예제 #7
0
    def parse(self):
        """
            Use the xml definition of the field positions, names and lengths to
            parse a CEOS data file
        """
        xmlFP = open(self.xml, 'r')

        self.root = ET(file=xmlFP).getroot()
        for z in self.root:
            # If the tag name is 'rec', this is a plain old record
            if z.tag == 'rec':
                (key, data) = self.decodeNode(z)
                self.metadata[key] = data
            # If the tag name is 'struct', we need to loop over some other
            #records
            if z.tag == "struct":
                try:
                    loopCounterName = z.attrib['loop']
                    loopCount = self.metadata[loopCounterName]
                except KeyError:
                    loopCount = int(z.attrib['nloop'])

                key = z.attrib['name']
                self.metadata[key] = [None] * loopCount
                for i in range(loopCount):
                    struct = {}
                    for node in z:
                        (subkey, data) = self.decodeNode(node)
                        struct[subkey] = data
                    self.metadata[key][i] = struct

        xmlFP.close()
        self.recordLength = self.metadata['Record Length']
예제 #8
0
def lookupTable():
    tree = ET()
    root = tree.parse(xml_doc)
    table = []
    for elem in root.findall('lookup'):
        table.append(elem.get('name'))
    return table
예제 #9
0
def parseRootElement():
    if xml_doc == None:
        return
    else:
        tree = ET()
        root = tree.parse(xml_doc)
        return tree, root
예제 #10
0
def XMLTableElement():
    tree = ET()
    root = tree.parse(xml_doc)
    table = []
    for elem in root.findall('table'):
        table.append(elem.get('name'))
    return table
예제 #11
0
	def __ladePreise(self, dateiname):
		'''Lädt Preise aus einer XML-Datei (daten/preisliste.xml)'''
		#if not os.path.exists(dateiname):
		#	# Wenn die Preisliste nicht da ist, soll man diese vom USB-Stick importieren können
		#	from gui.self_update import showUpdateDialog
		#	showUpdateDialog()
		tree = ET()
		try:
			tree.parse(dateiname)
		except:
			print('Die Preisliste wurde nicht gefunden. Bitte daten/preisliste.xml.example nach preisliste.xml kopieren und anpassen!')
			raise Exception("Could not open pricelist file")
		for item in tree.findall('item'):
			if not 'key' in item.attrib:
				raise ValueError("Invalid Pricelist-Entry!")
			key = item.attrib['key']
			self.preise[key] = {}
			einheit = 'Stk'
			if 'unit' in item.attrib:
				einheit = item.attrib['unit']
			self.einheiten[key] = einheit
			if 'rebategroup' in item.attrib:
				rg = item.attrib['rebategroup']
				if rg in self.rabattGruppen.keys():
					self.rabattGruppen[rg].append(key)
				else:
					self.rabattGruppen[rg] = [key, ]
			if 'vat' in item.attrib:
				self.steuersatz[key] = float(item.attrib['vat'])
			if key == 'minprice':
				minpricekeys = []
				for k in item.findall('key'):
					minpricekeys.append(k.text)
				price = item.find('price').text
				self.mindestpreis['keys'] = minpricekeys
				self.mindestpreis['gesamtpreis'] = float(price)
			for c in item.findall('price'):
				value = float(c.text)
				stufe = 0
				if 'liter' in c.attrib:
					if self.rabattArt(key) != 'liter':
						raise ValueError('Fehler in der Preisliste: %s: Liter-Rabatt nicht vorgesehen' % key)
					stufe = int(c.attrib['liter'])
					self._rabattStufeSetzen(key, stufe, value)
				elif 'count' in c.attrib:
					if self.rabattArt(key) == 'liter' or not self.rabattArt(key):
						raise ValueError('Fehler in der Preisliste: %s: Stückzahl-Rabatt nicht vorgesehen' % key)
					stufe = float(c.attrib['count'])
					self._rabattStufeSetzen(key, stufe, value)
				else:
					self._rabattStufeSetzen(key, 0, value)
			if 'desc' in item.attrib:
				self.beschreibungen[key] = item.attrib['desc']
			else:
				self.beschreibungen[key] = key
			if 'liter' in item.attrib:
				self.liter[key] = int(item.attrib['liter'])
			else:
				self.liter[key] = 0
def validate_xml_input(xml_document):
    """Validate a given XML file used as input."""
    parser = ET()
    try:
        parser.parse(source=xml_document)
        return True
    except ParseError:
        return False
예제 #13
0
def parseKML(kmlFile):

    # get kml file data into element tree
    tree = ET()
    tree.parse(kmlFile)

    # initialize placemark dictionary for holding kml data
    placemarkDict = {}

    for Document in tree.findall('{http://www.opengis.net/kml/2.2}Document'):
        documentName = Document.find('{http://www.opengis.net/kml/2.2}name').text

        for Folder in Document.findall('{http://www.opengis.net/kml/2.2}Folder'):
            folderName = Folder.find('{http://www.opengis.net/kml/2.2}name').text

            # Add dictionary to placemark Dictionary
            placemarkDict[folderName] = {}

            for Placemark in Folder.findall('{http://www.opengis.net/kml/2.2}Placemark'):
                placemarkName = Placemark.find('{http://www.opengis.net/kml/2.2}name').text

                # Get placemark type and coordinates. Each placemark can only be one type, so
                # only one of the following loops will execute for each placemark.
                for LineString in Placemark.findall('{http://www.opengis.net/kml/2.2}LineString'):
                    coordinates = LineString.find('{http://www.opengis.net/kml/2.2}coordinates').text
                    placemarkType = 'LineString'

                for Polygon in Placemark.findall('{http://www.opengis.net/kml/2.2}Polygon'):
                    for outerBoundaryIs in Polygon.findall('{http://www.opengis.net/kml/2.2}outerBoundaryIs'):
                        for LinearRing in outerBoundaryIs.findall('{http://www.opengis.net/kml/2.2}LinearRing'):
                            coordinates = LinearRing.find('{http://www.opengis.net/kml/2.2}coordinates').text
                            placemarkType = 'Polygon'

                for Point in Placemark.findall('{http://www.opengis.net/kml/2.2}Point'):
                    coordinates = Point.find('{http://www.opengis.net/kml/2.2}coordinates').text
                    placemarkType = 'Point'

                # remove commas from coordinates string and split into list
                coordinates = coordinates.replace(',',' ')
                coordinates = coordinates.split()

                # organize the coordinates list - it's one single list at this point, so take
                # each set of coordinates and add them to their own list.
                llaDegList = []
                coordinateIndex = 0

                while coordinateIndex <= len(coordinates)-3:
                    longitude = float(coordinates[coordinateIndex])        # longitude in degrees
                    latitude = float(coordinates[coordinateIndex+1])       # latitude in degrees
                    altitude = float(coordinates[coordinateIndex+2])       # altitude in meters
                    llaDegList.extend([[latitude, longitude, altitude]])
                    coordinateIndex +=3

                # Add data to the dictionary
                placemarkDict[folderName][placemarkName]={'placemarkType' : placemarkType, 'llaDeg': llaDegList}

    return placemarkDict
예제 #14
0
파일: YoUtil.py 프로젝트: yoramo1/Ecat
def get_esi_vendor(path):
    #print('>> ',path)
    tree = ET()
    tree.parse(path)
    vNode = tree.find('Vendor')
    if vNode != None:
        name_node = vNode.find('Name')
        if name_node != None:
            return name_node.text
    return None
예제 #15
0
def xml_tree_parse(xml_file):
	""" load xml index in memory """
	
	# load and parse xml using ElementTree
	try:
		tree = ET()
		tree.parse(xml_file)
	except Exception, inst:
		print >> stderr, 'error opening file: %s' % (xml_file)
		return
예제 #16
0
def columns(tableName):
    #scan the xml file and return all the table defined columns information
    tree = ET()
    root = tree.parse(xml_doc)
    tabCols = {}
    bool = False
    for elem in root.findall('table'):
        if elem.get('name') == tableName:
            for child in elem[0]:
                tabCols = child.attrib
    return tabCols
예제 #17
0
파일: hp-msa.py 프로젝트: ujin77/hp-msa
 def _request(self, api):
     # print(self._request_url(api))
     if self.sessionKey:
         req = urllib2.Request(url=self._request_url(api))
         req.add_header('dataType', 'api-brief')
         req.add_header('sessionKey', self.sessionKey)
         xml = urllib2.urlopen(
             req, context=ssl._create_unverified_context()).read()
         if len(xml) > 10:
             return ET(fromstring(xml)).getroot()
     return None
예제 #18
0
def update_simulation_state(session_path, simulation_name, new_state):
    """Updates the state of the simulation."""
    tree = ET().parse(session_path + '.xml')
    for simulation in tree.findall('simulation'):
        if (simulation.find('name').text == simulation_name):
            simulation.attrib['in_batch'] = str(new_state)

    indent(tree)

    with open(session_path + '.xml', 'wb') as f:
        f.write(tostring(tree))
예제 #19
0
def efetch2(webenv, query_key, db, **kwargs):
    baseurl = 'https://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi'
    postdata = {'db': db, 'WebEnv': webenv, 'query_key': query_key}
    postdata.update(kwargs)
    f = _curl(baseurl, urllib.urlencode(postdata))

    et = ET()
    #efetchresult = et.parse(f)
    efetchresult = f.read()
    time.sleep(DELAY)
    return efetchresult
예제 #20
0
파일: workspace.py 프로젝트: yoramo1/Ecat
	def load(self):
		YoUtil.debug_print('loading ->', self.path)
		self.tree = ET()
		self.tree.parse(self.path)
		root = self.tree.getroot()	
		xml_list = root.findall('Workspace/Targets/item')
		if  xml_list != None:
			#YoUtil.debug_print('lst',xml_list)
			for xml_node in xml_list:
				tm = target_model(xml_node)
				self.target_list.append(tm)
예제 #21
0
def XMLTableElement(profile):
    tree = ET()
    root = tree.parse(xml_doc)
    tables = []
    level = (".//*[@name='%s']/table") % profile
    for elem in root.findall(level):
        if elem.get('name') in non_editable_tables:
            continue
        else:
            tables.append(elem.get('name'))

    return tables
예제 #22
0
 def __init__(self, xml=None, dataFile=None):
     self.xml = xml
     self.dataFile = dataFile
     self.startPosition = dataFile.tell()
     self.recordLength = 0
     self.metadata = {}
     if not xml == None:
         self.xmlFP = open(self.xml, 'r')
         self.rootChildren = list(ET(file=self.xmlFP).getroot())
     else:
         self.xmlFP = None
         self.rootChildren = []
예제 #23
0
def LoadXMLFile(*,
                oFile: cFileName,
                oParser: Optional[XMLParser] = None,
                bNoCache=False) -> Element:
    """
    Loads a simple XML file to an Elementree Node without any manipulating

    :rtype: xml.etree.ElementTree.Element
    :param cFileName oFile: xml file to load
    :param XMLParser oParser: Optional: Parser to use
    :param bool bNoCache: Optional: Do not use cached files
    :return: Element tree element
    """

    if oParser is None:
        if not bNoCache:
            return fromstring(CachedFile(oFileName=oFile))
        else:
            return ET().parse(source=oFile.string)
    else:
        return ET().parse(source=oFile.string, parser=oParser)
예제 #24
0
def read_session_file(path, simulation_state):
    """Reads the session file to get a list of simulations."""

    global session_element

    sim_list = []
    tree = ET().parse(path)
    session_element = tree

    for simulation in tree.iter('simulation'):
        if (simulation_state == int(simulation.attrib['in_batch'])):
            sim_list.append(list(simulation)[0].text)

    return sim_list
예제 #25
0
def maven_artifactId(path="."):
    """Returns the artifactId of pom.xml in the given path.
    """
    try:
        jfilename = path + '/pom.xml'
        ns='{http://maven.apache.org/POM/4.0.0}'
        from xml.etree.ElementTree import ElementTree as ET
        root = ET().parse(jfilename)
        artifactId     = root.find('./' + ns + 'artifactId' )
        if artifactId is not None:
            return artifactId.text
        return ''
    except IOError:
        print("No pom.xml present in", path)
        raise
예제 #26
0
def WriteXMLFile(*, oFile: cFileName, oElem: Element) -> bool:
    """
    Save an Element node to an xml file

    :param cFileName oFile: xml file to save
    :param Element oElem: XML Element to save
    :return: Element tree element
    """
    try:
        oET = ET(oElem)
        oET.write(oFile.string, encoding="UTF-8", xml_declaration=True)
        return True
    except Exception as e:
        LogError(uMsg='Can' 't write XML File:' + oFile.string, oException=e)
        return False
예제 #27
0
def get_missing_files(qrc_filename, base_path = None):
    ''' Generates a list of missing files 
    @param qrc_filename: QRC File
    @param base_path: Base path, if not given takes dirname(__file__)
    '''
    tree = ET()
    tree.parse(qrc_filename)
    if not base_path:
        base_path = __file__
    base_path = dirname(base_path)
    for file_node in tree.findall('*/file'):
        file_path = file_node.text
        path = normpath(join(base_path, file_path))
        if not exists(path):
            yield file_path
예제 #28
0
def lookupColumn(tableName):
    #scan the xml file and return all the table defined columns information
    tree = ET()
    root = tree.parse(xml_doc)
    tableData = []
    for elem in root.findall("profile/lookup"):
        if elem.get('name') == tableName:
            for child in elem[0]:
                ordDict = OrderedDict()
                ordDict["Column label"] = child.get('name')
                ordDict["Description"] = child.get('fullname')
                ordDict["Data type"] = child.get('type')
                ordDict["Length"] = child.get('size')
                ordDict["Lookups"] = bool
                tableData.append(ordDict)
    return tableData
예제 #29
0
def epost(idlist, db):
    baseurl = 'https://eutils.ncbi.nlm.nih.gov/entrez/eutils/epost.fcgi'
    postdata = {'db': db}
    postdata['id'] = ','.join(list(idlist))

    #f = urllib.urlopen(baseurl, urllib.urlencode(postdata))
    f = _curl(baseurl, urllib.urlencode(postdata))

    et = ET()
    epostresult = et.parse(f)
    querykey, webenv = None, None
    for thing in epostresult:
        if thing.tag.endswith('QueryKey'): querykey = thing.text
        elif thing.tag.endswith('WebEnv'): webenv = thing.text
    time.sleep(DELAY)
    return querykey, webenv
예제 #30
0
def maven_version(path='.'):
    """Returns the version in pom.xml in the given path.
    """
    try:
        jfilename = path + '/pom.xml'
        ns='{http://maven.apache.org/POM/4.0.0}'
        from xml.etree.ElementTree import ElementTree as ET
        root = ET().parse(jfilename)
        parent_version = root.find('./' + ns + 'parent/' + ns + "version" )
        if parent_version is not None: return parent_version.text # child project
        my_version = root.find('./' + ns + 'version' ) # parent project
        if my_version is not None: return my_version.text # parent has explicit version
        raise "no version!"
    except IOError:
        print("No pom.xml present in", path)
        raise