def _retrieve(self, data={}): data.update(self.credentials) request = urllib2.Request(self.base_url, urlencode(data)) response = urllib2.urlopen(request).read() # lowercase strings as keys so we can pass the return values to the # other functions as kwargs. return dict(((str(k).lower(), v) for k, v in xmltodict(response).items()))
def togglescheduleenabled_job(self,href): """Enable/disable the backup job configured on Veeam Enterprise manager server the result will be a DICT in this format in unicode UTF8 risultato =[{u'status':u'OK/ERROR',u'status_code':u'xxxx',u'data':[]}] :param href :reference of the job in this format 'http://tst-veeamsrv.tstsddc.csi.it:9399/api/jobs/016bdba9-462a-4b33-a8fc-0dce63735fc3' :raise VeeamError """ obj = urlparse(href) proto = obj.scheme self.logger.debug("proto %s "%proto) host, port = obj.netloc.split(':') port = int(port) self.logger.debug("host %s , port %s" % (host,port)) path = obj.path self.logger.debug("path %s "%path) method='POST' path=path+"?action=toggleScheduleEnabled" self.logger.debug("action %s "%path) try: res=xmltodict(self.util.call(path, method,'','', 30, self.token , '')[0]) self.logger.debug("risultato :'%s'" % res) risultato = {u'status':u'OK',u'status_code':'202',u'data':res} except VeeamError as e: risultato = {u'status':u'ERROR',u'status_code':e.code,u'data':e.value} return(risultato)
def get_jobs(self): """Get all the jobs configured on Veeam Enterprise manager server the result will be a DICT in this format in unicode UTF8 risultato =[{u'status':u'OK/ERROR',u'status_code':u'xxxx',u'data':[]}] :raise VeeamError """ method='GET' path='/api/jobs' try: res=xmltodict(self.util.call(path, method,'','', 30, self.token , '')[0]) jobs=res['EntityReferences']['Ref'] risultato = {u'status':u'OK',u'status_code':'200',u'data':jobs} self.logger.debug("risultato :'%s'" % risultato) for item in jobs: self.logger.info("Nome %s , UID '%s' , Href '%s' " % (item['@Name'],item['@UID'],item['@Href'])) self.logger.debug("--------------------------------------------------keys: %s " % jobs[0].keys()) except VeeamError as e: risultato = {u'status':u'ERROR',u'status_code':e.code,u'data':e.value} return (risultato)
def get_tasks(self): method='GET' path='/api/tasks' self.logger.debug("action %s "%path) try: res=xmltodict(self.client.call(path, method,'','', 30, self.veeam_token , '')[0]) self.logger.debug("risultato :'%s'" % res) risultato = {u'status':u'OK',u'status_code':'202',u'data':res} except VeeamError as e: risultato = {u'status':u'ERROR',u'status_code':e.code,u'data':e.value} return(risultato)
def add_includes(self,href,XML): """add a new 'XML' include the backup job 'href' configured on Veeam Enterprise manager server the result will be a DICT in this format in unicode UTF8 risultato =[{u'status':u'OK/ERROR',u'status_code':u'xxxx',u'data':[]}] :param href :reference of the job to clone in this format 'http://tst-veeamsrv.tstsddc.csi.it:9399/api/jobs/016bdba9-462a-4b33-a8fc-0dce63735fc3' :param XML : properties to modify in an xml format Example : XML='<?xml version="1.0" encoding="utf-8"?> <CreateObjectInJobSpec xmlns="http://www.veeam.com/ent/v1.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <HierarchyObjRef>urn:VMware:Vm:7f6b6270-1f3b-4dc6-872f-2d1dfc519c00.vm-1401</HierarchyObjRef> <HierarchyObjName>tst-calamari</HierarchyObjName> </CreateObjectInJobSpec> ' :raise VeeamError """ obj = urlparse(href) ''' <scheme>://<netloc>/<path>;<params>?<query>#<fragment> Return a 6-tuple: (scheme, netloc, path, params, query, fragment). ''' proto = obj.scheme self.logger.debug("proto %s "%proto) host, port = obj.netloc.split(':') port = int(port) self.logger.debug("host %s , port %s" % (host,port)) path = obj.path self.logger.debug("path %s "%path) method='POST' path=path+"/includes" self.logger.debug("action %s "%path) try: res=xmltodict(self.util.call(path, method,XML,'', 30, self.token , '')[0]) self.logger.debug("risultato :'%s'" % res) risultato = {u'status':u'OK',u'status_code':'202',u'data':res} except VeeamError as e: risultato = {u'status':u'ERROR',u'status_code':e.code,u'data':e.value} return(risultato)
def clone_job(self,href,XML): """CLONE the backup job 'href' in a new job configured on Veeam Enterprise manager server the result will be a DICT in this format in unicode UTF8 risultato =[{u'status':u'OK/ERROR',u'status_code':u'xxxx',u'data':[]}] :param href :reference of the job to clone in this format 'http://tst-veeamsrv.tstsddc.csi.it:9399/api/jobs/016bdba9-462a-4b33-a8fc-0dce63735fc3' :param XML : properties to modify in an xml format Example : XML='<?xml version="1.0" encoding="utf-8"?> <JobCloneSpec xmlns="http://www.veeam.com/ent/v1.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <BackupJobCloneInfo> <JobName>Prova Cloned Job</JobName> <FolderName>Prova Cloned Job</FolderName> <RepositoryUid>urn:veeam:Repository:b03eb865-79eb-4450-bc52-48a7472314ca</RepositoryUid> </BackupJobCloneInfo> </JobCloneSpec>' :raise VeeamError """ obj = urlparse(href) ''' <scheme>://<netloc>/<path>;<params>?<query>#<fragment> Return a 6-tuple: (scheme, netloc, path, params, query, fragment). ''' proto = obj.scheme self.logger.debug("proto %s "%proto) host, port = obj.netloc.split(':') port = int(port) self.logger.debug("host %s , port %s" % (host,port)) path = obj.path self.logger.debug("path %s "%path) method='POST' path=path+"?action=clone" self.logger.debug("action %s "%path) try: res=xmltodict(self.util.call(path, method,XML,'', 30, self.token , '')[0]) self.logger.debug("risultato :'%s'" % res) risultato = {u'status':u'OK',u'status_code':'202',u'data':res} except VeeamError as e: risultato = {u'status':u'ERROR',u'status_code':e.code,u'data':e.value} return(risultato)
def get_flight_data(): """ Define the flight data xml filepath locally here, however we'll access it in a bucket on AWS. """ script_run_dir = os.path.dirname(os.path.realpath(__file__)) flight_data_file = "flight_data/flightdata_A.xml" flight_data_filepath = "{}/../{}".format(script_run_dir, flight_data_file) # Easier to deal with XML data as a dictionary, so just open in byte format and convert to dict, # this then gets used for result filtering. Convert to JSON after filtering and return to user. # xmltodict uses an ordered dict so no danger of messing around with attribute ordering. flight_data_xml = open(flight_data_filepath, "rb") # Flights just acts as a wrapper around all other attributes so no real need to use it. # While xmltodict groups all flights under the same key ('flight'), so we only need the list of flight data dicts it contains. flight_data_dicts = xmltodict(flight_data_xml)["flights"]["flight"] return flight_data_dicts
def get_flight_data(): """ Retrieve flight data xml file and format for processing """ # Setup S3 access for accessing the flightdata xml file stored in a S3 bucket s3 = boto3.resource("s3") target_bucket = "traveltek-bucket" target_file = "flightdata_A.xml" # Get the flight data file from the required bucket flight_data_object = s3.Object(target_bucket, target_file) # Easier to deal with XML data as a dictionary, so just open in byte format and convert to dict, # this then gets used for result filtering. Convert to JSON after filtering and return to user. # xmltodict uses an ordered dict so no danger of messing around with attribute ordering. flight_data_xml = flight_data_object.get()["Body"].read() # Flights just acts as a wrapper around all other attributes so no real need to use it. # While xmltodict groups all flights under the same key ('flight'), so we only need the list of flight data dicts it contains. flight_data_dicts = xmltodict(flight_data_xml)["flights"]["flight"] return flight_data_dicts
def delete_includes(self,href): """DELETE the include 'href' configured on Veeam Enterprise manager server the result will be a DICT in this format in unicode UTF8 risultato =[{u'status':u'OK/ERROR',u'status_code':u'xxxx',u'data':[]}] :param href :reference of the job in this format 'http://tst-veeamsrv.tstsddc.csi.it:9399/api/jobs/016bdba9-462a-4b33-a8fc-0dce63735fc3' :raise VeeamError """ method='DELETE' path=href self.logger.debug("action %s "%path) try: res=xmltodict(self.util.call(path, method,'','', 30, self.token , '')[0]) self.logger.debug("risultato :'%s'" % res) risultato = {u'status':u'OK',u'status_code':'202',u'data':res} except VeeamError as e: risultato = {u'status':u'ERROR',u'status_code':e.code,u'data':e.value} return(risultato)
def get_all_data(self, identity_number, as_xml=False): """ Get all data available for the provided national identity number from the Swedish population register. @param identity_number: The national identity number to lookup @type identity_number: str @param as_xml: If 'True' return the data as XML, if 'False' return data as an ordered dict (default: False) @type as_xml: bool @return: Navet data, either as XML string or parsed (ordered) dict. """ try: result = self.client.service.getData(self.order_id, identity_number) if not as_xml: result = xmltodict(result) if self.debug: self.logger.debug("NAVET get_all_data lookup result:\n{!r}".format(result)) return result except WebFault as e: self.logger.error(e.message) # TODO: Add translation for exceptions raise except: self.logger.error("Unexpected error.") raise
def get_includes(self,href): """Get all the includes of the backup job 'href' configured on Veeam Enterprise manager server the result will be a DICT in this format in unicode UTF8 risultato =[{u'status':u'OK/ERROR',u'status_code':u'xxxx',u'data':[]}] :param href :reference of the job in this format 'http://tst-veeamsrv.tstsddc.csi.it:9399/api/jobs/016bdba9-462a-4b33-a8fc-0dce63735fc3' :raise VeeamError """ obj = urlparse(href) ''' <scheme>://<netloc>/<path>;<params>?<query>#<fragment> Return a 6-tuple: (scheme, netloc, path, params, query, fragment). ''' proto = obj.scheme self.logger.debug("proto %s "%proto) host, port = obj.netloc.split(':') port = int(port) self.logger.debug("host %s , port %s" % (host,port)) path = obj.path self.logger.debug("path %s "%path) method='GET' path=path+"/includes" self.logger.debug("action %s "%path) try: res=xmltodict(self.util.call(path, method,'','', 30, self.token , '')[0]) self.logger.debug("risultato :'%s'" % res) risultato = {u'status':u'OK',u'status_code':'202',u'data':res} except VeeamError as e: risultato = {u'status':u'ERROR',u'status_code':e.code,u'data':e.value} return(risultato)
def edit_job(self,href,XML): """Edit the properties of a job backup configured on Veeam Enterprise manager server the result will be a DICT in this format in unicode UTF8 risultato =[{u'status':u'OK/ERROR',u'status_code':u'xxxx',u'data':[]}] :param href :reference of the job in this format 'http://tst-veeamsrv.tstsddc.csi.it:9399/api/jobs/016bdba9-462a-4b33-a8fc-0dce63735fc3' :param XML : properties to modify in an xml format Example : XML='<?xml version="1.0" encoding="utf-8"?> <Job Type="Job" xmlns="http://www.veeam.com/ent/v1.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <JobScheduleOptions> <RetryOptions> <RetryTimes>3</RetryTimes> <RetryTimeout>5</RetryTimeout> <RetrySpecified>true</RetrySpecified> </RetryOptions> <OptionsDaily Enabled="true"> <Kind>Everyday</Kind> <Days>Sunday</Days> <Days>Monday</Days> <Days>Tuesday</Days> <Days>Wednesday</Days> <Days>Thursday</Days> <Days>Friday</Days> <Days>Saturday</Days> <Time>22:00:00.0000000</Time> </OptionsDaily> </JobScheduleOptions> </Job>' :raise VeeamError """ obj = urlparse(href) ''' <scheme>://<netloc>/<path>;<params>?<query>#<fragment> Return a 6-tuple: (scheme, netloc, path, params, query, fragment). ''' proto = obj.scheme self.logger.debug("proto %s "%proto) host, port = obj.netloc.split(':') port = int(port) self.logger.debug("host %s , port %s" % (host,port)) path = obj.path self.logger.debug("path %s "%path) method='PUT' path=path+"?action=edit" self.logger.debug("action %s "%path) try: res=xmltodict(self.util.call(path, method,XML,'', 30, self.token , '')[0]) self.logger.debug("risultato :'%s'" % res) risultato = {u'status':u'OK',u'status_code':'202',u'data':res} except VeeamError as e: risultato = {u'status':u'ERROR',u'status_code':e.code,u'data':e.value} return(risultato)
def parse_xml(xmldata): return xmltodict(xmldata)
def _parse_xml(self, xmldata): return xmltodict(xmldata)
def parse(file): for line in file: if line.startswith('<SEC-DOCUMENT>'): secDocument = line.replace('<SEC-DOCUMENT>', '') secDocument = secDocument.strip() break for line in file: if line.startswith('<ACCEPTANCE-DATETIME>'): acceptanceDatetime = line.replace('<ACCEPTANCE-DATETIME>', '') acceptanceDatetime = acceptanceDatetime.strip() break for line in file: if line.startswith('<?xml version="1.0"?>'): break contents = file.read() file.close() # need to delete everything after: </ownershipDocument> seperator = '</XML>' contents = contents.split(seperator, 1)[0] ownershipDocument = xmltodict.xmltodict(contents) issuerTradingSymbol = ownershipDocument['issuer'][0][ 'issuerTradingSymbol'][0] rptOwnerCik = ownershipDocument['reportingOwner'][0]['reportingOwnerId'][ 0]['rptOwnerCik'][0] rptOwnerName = ownershipDocument['reportingOwner'][0]['reportingOwnerId'][ 0]['rptOwnerName'][0] try: isDirector = ownershipDocument['reportingOwner'][0][ 'reportingOwnerRelationship'][0]['isDirector'][0] isDirector = convertToBoolean(isDirector) except KeyError: isDirector = False try: isOfficer = ownershipDocument['reportingOwner'][0][ 'reportingOwnerRelationship'][0]['isOfficer'][0] isOfficer = convertToBoolean(isOfficer) except KeyError: isOfficer = False try: isTenPercentOwner = ownershipDocument['reportingOwner'][0][ 'reportingOwnerRelationship'][0]['isTenPercentOwner'][0] isTenPercentOwner = convertToBoolean(isTenPercentOwner) except KeyError: isTenPercentOwner = False try: isOther = ownershipDocument['reportingOwner'][0][ 'reportingOwnerRelationship'][0]['isOther'][0] isOther = convertToBoolean(isOther) except KeyError: isOther = False try: ownershipDocuments = ownershipDocument['nonDerivativeTable'][0][ 'nonDerivativeTransaction'] except KeyError: return [] except TypeError: return [] transactions = [] i = 0 for nonDerivativeTransaction in ownershipDocuments: try: transactionDate = nonDerivativeTransaction['transactionDate'][0][ 'value'][0] transactionShares = nonDerivativeTransaction['transactionAmounts'][ 0]['transactionShares'][0]['value'][0] transactionPricePerShare = nonDerivativeTransaction[ 'transactionAmounts'][0]['transactionPricePerShare'][0][ 'value'][0] transactionAcquiredDisposedCode = nonDerivativeTransaction[ 'transactionAmounts'][0]['transactionAcquiredDisposedCode'][0][ 'value'][0] sharesOwned = nonDerivativeTransaction['postTransactionAmounts'][ 0]['sharesOwnedFollowingTransaction'][0]['value'][0] if float(transactionPricePerShare) == 0: pass else: transaction = {} transaction['secDocument'] = secDocument + ' ' + str(i) i += 1 transaction['acceptanceDatetime'] = acceptanceDatetime transaction['issuerTradingSymbol'] = issuerTradingSymbol transaction['rptOwnerCik'] = rptOwnerCik transaction['rptOwnerName'] = rptOwnerName transaction['isDirector'] = isDirector transaction['isOfficer'] = isOfficer transaction['isTenPercentOwner'] = isTenPercentOwner transaction['isOther'] = isOther transaction['transactionDate'] = transactionDate transaction['transactionShares'] = float(transactionShares) transaction['transactionPricePerShare'] = float( transactionPricePerShare) transaction[ 'transactionAcquiredDisposedCode'] = transactionAcquiredDisposedCode transaction['sharesOwned'] = float(sharesOwned) transactions.append(transaction) except KeyError: pass return transactions
import json import xmltodict as xml # the xml file is available at http://www.mta.info/status/serviceStatus.txt s = open("code/status.xml").read() # we convert it to a dictionary d = xml.xmltodict(s) # and then dump the subway section into a JSON file to visualise json.dump(d['subway'][0]['line'], open("../viz/data/service_status.json", 'w'))
import json import xmltodict as xml # the xml file is available at http://www.mta.info/status/serviceStatus.txt s = open("code/status.xml").read() # we convert it to a dictionary d = xml.xmltodict(s) # and then dump the subway section into a JSON file to visualise json.dump(d['subway'][0]['line'], open("../viz/data/service_status.json",'w'))