def delete(self, testCaseId): NsmUtil.printHeadLine2('START: delete test case ' + testCaseId) self.deleteUrl = self.uid + '/delete' nameDash = self.createName.replace(' ', '-') debugDeleteFilePath = Define._PathResponseDelete + '/' + nameDash + '-delete-' + testCaseId + '.xml' self.logger.info('delete url: ' + self.deleteUrl) status = None if (self.isOffLine): self.logger.debug('get delete task from file: ' + debugDeleteFilePath) self.deleteTaskXml = Util.readFile(debugDeleteFilePath) myTask = Task(self.deleteTaskXml) status = myTask.checkResult() self.deleteTaskXml = myTask.taskXml else: self.deleteTaskXml = HttpUtil.doPost(self.deleteUrl, Define._credential) self.logger.debug('write pending delete task to file: ' + debugDeleteFilePath + '-pending.xml') Util.writeFile(debugDeleteFilePath+'-pending.xml', self.deleteTaskXml) myTask = Task(self.deleteTaskXml) status = myTask.checkResult() self.deleteTaskXml = myTask.taskXml self.logger.info('write to file ' + debugDeleteFilePath) #self.logger.info(self.deleteTaskXml) Util.writeFile(debugDeleteFilePath, self.deleteTaskXml) NsmUtil.printHeadLine2('END: delete test case ' + testCaseId) return status
def execute(self,script=None,scriptFile=None): """This runs a uEngine script. Supply the script with the script=<script text> argument or use scriptFile=<filename> to read the script text from a file. Returns xml string. The status of the uEngine script must be checked with class attribute status. 0 for success. Any other value there were problems. """ # The script to run is either passed in or can be read from a file xml="" if script == None and scriptFile != None: script = self.readscript(scriptFile) if script != None: # submit the input to the server and obtain result ch = CH.CommHandler(self._connection, self._service) ch.process(script) # expect message 200, if not print error message and return error code if not ch.isGoodStatus(): print "######### ERROR reportHTTPResponse" util.reportHTTPResponse(ch.formatResponse()) self.status = 1 else: # Pull the responses element out of the xml xml=ch.getContents() self.status = 0 return xml
def __init__(self, catalog, name=None, description=None, requestBodyFileName=None, requestParams=None): ''' Constructor ''' Element.__init__(self) self.createName = name self.getCatalogUrl = Util.getXpathValue(catalog, XPath._TopIndexCatalogLinkHref) self.getProviderListUrl = Util.getXpathValue(catalog, XPath._TopIndexListProviderLinkHref)
def populateCreateDescription(self, description): if not description: try: self.createDescription = self.requestParams['description'] except KeyError: self.createDescription = Util.getXpathValue(Util.readFile(self.requestBodyFilePath), XPath._RequestBodyDescription) if (self.createDescription.startswith('$')): raise ValueError('request body description [' + self.createDescription + '] is not defined in file ' + self.requestBodyFilePath)
def populateCreateName(self, name): if not name: try: self.createName = self.requestParams['name'] except KeyError: self.createName = Util.getXpathValue(Util.readFile(self.requestBodyFilePath), XPath._RequestBodyName) if (self.createName.startswith('$')): raise ValueError('request body name [' + self.createName + '] is not defined in file ' + self.requestBodyFilePath) self.logger.info('-----------> element object name or create name: ' + self.createName)
def execute(self): status = 0 # parse the command line try: status = self.__readCommandLine() if status != 0: exit(status) except Exception, e: util.printMessage(sys.stderr, "Error", repr(e)) return 1
def saveUid(uidList): line = '' NsmUtil.__logger.debug('') for pair in uidList: objectUid = pair[0] objectClass = pair[1] objectName = pair[2] NsmUtil.__logger.debug('uid: ' + objectUid + ' ------> ' + objectClass + ' [' + objectName + ']') line += objectUid + ';' + objectClass + ';' + objectName + "\n" Util.writeFile(Define._PathUidDefault, line) NsmUtil.__logger.debug('')
def execute(self): status = 0 # parse the command line try: status = self.__readCommandLine() if status != 0: util.printMessage(sys.stderr,"Canceled","Unable to process request -- exiting...") return status except Exception,e: util.printMessage(sys.stderr,"Error",repr(e)) return 1
def execute(self): status = 0 # parse the command line try: status = self.__readCommandLine() if status != 0: util.printMessage(sys.stderr,"Canceled","Unable to process request -- exiting...") return status except Exception,e: util.printMessage(sys.stderr,"Error",repr(e)) return 1
def __processRequest(self): msg = self.__createMessage() service = config.endpoint.get('subscribe') connection=str(os.getenv("DEFAULT_HOST", "localhost") + ":" + os.getenv("DEFAULT_PORT", "9581")) ch = CH.CommHandler(connection, service) ch.process(msg) if not ch.isGoodStatus(): util.reportHTTPResponse(ch.formatResponse()) retVal = self.__processResponse(ch.getContents()) return retVal
def getIpAddressListByPairList(ipAddressPairList, count): index = 0 returnIpAddressList = [] for onePair in ipAddressPairList: ipv4Start = onePair[0] ipv4End = onePair[1] ipv4StartNumber = Util.dottedQuadToNum(ipv4Start) ipv4EndNumber = Util.dottedQuadToNum(ipv4End) for oneIpNumber in range(ipv4StartNumber, ipv4EndNumber+1): returnIpAddressList.append(Util.numToDottedQuad(oneIpNumber)) index += 1 if index == count: return returnIpAddressList return None
def verifyCreate(goldenFilePath, currentFilePath): goldenStr = Util.readFile(goldenFilePath) currentStr = Util.readFile(currentFilePath) goldenStr = ResponseVerification.formalize(goldenStr) currentStr = ResponseVerification.formalize(currentStr) if goldenStr == currentStr: ResponseVerification.__logger.info('Response Verification test result: PASSED') return True else: ResponseVerification.__logger.info('golden: ' + goldenStr) ResponseVerification.__logger.info('current: ' + currentStr) ResponseVerification.__logger.info('Response Verification test result: FAILED') return False
def __submitRequestMessage(self,msg): try: runner = self.commands.get('runner') service = config.endpoint.get(runner) # send the request to the server connection=str(os.getenv("DEFAULT_HOST", "localhost") + ":" + os.getenv("DEFAULT_PORT", "9581")) ch = CH.CommHandler(connection,service) ch.process(msg) if not ch.isGoodStatus(): util.reportHTTPResponse(ch.formatResponse()) return None return ch.getContents() except Exception,e: raise CH.CommError("Unable to submit request to server",e)
def parseCatalog(self): for relKey in IpReservation._linkRelKeys: evalXpath = XPath._LinkIpReservation evalXpath = evalXpath.replace('${rel}', relKey) link = Util.getXpathValue(self.catalogXml, evalXpath) self.logger.info('Link of ' + relKey + ': ' + link) self.relLinkDictionary[relKey] = link
def __init__(self, taskXml): ''' Constructor ''' self.logger = Util.getLogger(self.__class__.__name__) self._loopCount = 10 self.taskXml = taskXml
def validateByFile(xsdFilePaths, xmlFilePath): xsdStreamSources = [] for xsdFilePath in xsdFilePaths: xsdStreamSource = StreamSource(FileReader(xsdFilePath)) xsdStreamSources.append(xsdStreamSource) xmlStreamSource = StreamSource(StringReader(Util.readFile(xmlFilePath))) return validate(xsdStreamSources, xmlStreamSource)
def __init__(self, catalog, name=None, description=None, requestFileName='default-private-edge-zone.xml', requestParams=None): self.logger = Util.getLogger(self.__class__.__name__) Element.__init__(self) Element.setCatalogMatchParameter(self, PrivateEdgeZone._catalogServiceOfferingType, PrivateEdgeZone._catalogServiceOfferingName, PrivateEdgeZone._catalogLinkTitle) Element.setCreateParameter(self, name, description, requestFileName, requestParams) Element.parseCatalog(self, catalog)
def __init__(self): self._logger = Util.getLogger(self.__class__.__name__) self._topName = 'My Default Top' self._thisObject = None self._dataStore = {} self._uidList = [] self._resultList = [] self._totalTestCase = 0
def __createMessage(self): multimap = collections.defaultdict(list) for key in self.commands: if key == 'mode': pass elif key == 'substitution': dict = util.convListToDict(self.commands.get(key)) for sub in dict: multimap['substitution'].append(str(sub)+":"+str(dict.get(sub))) elif key == 'update': dict = util.convListToDict(self.commands.get(key)) for sub in dict: multimap['update'].append(str(sub)+":"+str(dict.get(sub))) else: multimap[key].append(self.commands.get(key)) if self.__hasScript(): multimap['script'].append(self.script) return Message(header=Header(multimap=multimap))
def getRequest(self, getUrl, debugDetailFilePath): getResponseXml = None self.logger.info('get request url: ' + getUrl) if (self.isOffLine): self.logger.debug('get response from file: ' + debugDetailFilePath) getResponseXml = Util.readFile(debugDetailFilePath) else: getResponseXml = HttpUtil.doGet(getUrl, Define._credential) self.logger.debug('write response to file: ' + debugDetailFilePath) Util.writeFile(debugDetailFilePath, getResponseXml) ''' uid = Util.getXpathValue(getResponseXml, XPath._DetailUid) self.logger.info('uid: ' + uid) getCatalogUrl = Util.getXpathValue(getResponseXml, XPath._DetailLinkCatalog) return getCatalogUrl ''' return getResponseXml
def getList(self, getUrl, listClassName, testCaseId): NsmUtil.printHeadLine2('START: get ' + listClassName + ' list test case ' + testCaseId) nameDash = self.createName.replace(' ', '-') debugDetailFilePath = Define._PathResponseCreateDefault + '/' + nameDash + '-' + listClassName + '-list-' + testCaseId + '.xml' getResponseXml = None self.logger.info('get list url: ' + getUrl) if (self.isOffLine): self.logger.debug('get ' + listClassName + ' list response from file: ' + debugDetailFilePath) getResponseXml = Util.readFile(debugDetailFilePath) else: getResponseXml = HttpUtil.doGet(getUrl, Define._credential) self.logger.debug('write ' + listClassName + ' list response to file: ' + debugDetailFilePath) Util.writeFile(debugDetailFilePath, getResponseXml) NsmUtil.printHeadLine2('END: get ' + listClassName + ' list test case ' + testCaseId) return getResponseXml
def getCatalog(self, testCaseId='000'): NsmUtil.printHeadLine2('START: get catalog test case ' + testCaseId) self.logger.debug('get catalog request: ' + self.getCatalogUrl) nameDash = self.createName.replace(' ', '-') debugCatalogFilePath = Define._PathResponseCreateDefault + '/' + nameDash + '-catalog-' + testCaseId + '.xml' if (self.isOffLine): self.logger.debug('get catalog from file: ' + debugCatalogFilePath) self.catalogXml = Util.readFile(debugCatalogFilePath) else: self.catalogXml = HttpUtil.doGet(self.getCatalogUrl, Define._credential) if self.getCatalogUrl and self.getCatalogUrl != '' else None if self.catalogXml: self.logger.debug('catalog output file: ' + debugCatalogFilePath) Util.writeFile(debugCatalogFilePath, self.catalogXml) NsmUtil.printHeadLine2('END: get catalog test case ' + testCaseId) return self.catalogXml
def __createMessage(self): msg = MSG.Message(True) msg.initializeMessage(False) for key in self.commands: if key == 'mode': pass elif key == 'substitution': dict = util.convListToDict(self.commands.get(key)) for sub in dict: msg.addProperty(name='substitution',value=str(sub)+":"+str(dict.get(sub))) elif key == 'update': dict = util.convListToDict(self.commands.get(key)) for sub in dict: msg.addProperty(name='update',value=str(sub)+":"+str(dict.get(sub))) else: msg.addProperty(name=key,value=self.commands.get(key)) if self.__hasScript(): msg.addProperty(name='script',value=self.script) return msg.getXML()
def getDetail(self, testCaseId): NsmUtil.printHeadLine2('START: get detail test case ' + testCaseId) fileName = 'My Default IP Address Pool ' + self.parentName + ' ' + self.category nameDash = fileName.replace(' ', '-') debugGatalogFilePath = Define._PathResponseCreateDefault + '/' + nameDash + '-detail-' + testCaseId + '.xml' if (self.isOffLine): self.logger.debug('get detail from file: ' + debugGatalogFilePath) self.catalogXml = Util.readFile(debugGatalogFilePath) else: self.catalogXml = HttpUtil.doGet(self.ipAddressPoolUid, Define._credential) self.logger.debug('write detail to file: ' + debugGatalogFilePath) Util.writeFile(debugGatalogFilePath, self.catalogXml) self.parseCatalog() NsmUtil.printHeadLine2('END: get detail test case ' + testCaseId) return self.catalogXml
def doService(httpMethod, url, credential, requestBody=None): Security.addProvider(MySSLProvider()) Security.setProperty("ssl.TrustManagerFactory.algorithm", "TrustAllCertificates") HttpsURLConnection.setDefaultHostnameVerifier(MyHostnameVerifier()) urlObj = URL(url) con = urlObj.openConnection() con.setRequestProperty("Accept", "application/xml") con.setRequestProperty("Content-Type", "application/xml") con.setRequestProperty("Authorization", credential) con.setDoInput(True); if httpMethod == 'POST': con.setDoOutput(True) con.setRequestMethod(httpMethod) output = DataOutputStream(con.getOutputStream()); if requestBody: output.writeBytes(requestBody); output.close(); responseCode = con.getResponseCode() logger.info('response code: ' + str(responseCode)) responseMessage = con.getResponseMessage() logger.info('response message: ' + str(responseMessage)) contentLength = con.getHeaderField('Content-Length') logger.info('content length: ' + str(contentLength)) stream = None if responseCode == 200 or responseCode == 201 or responseCode == 202: stream = con.getInputStream() elif contentLength: stream = con.getErrorStream() if stream: dataString = getStreamData(stream) logger.info(httpMethod + ' url: ' + url) if not url.endswith('.xsd') and len(dataString) < 4096: xmlStr = Util.prettfyXmlByString(dataString) logger.info(httpMethod + ' result: \n\n' + xmlStr) else: logger.info('response body too big, no print out') if responseCode == 200 or responseCode == 201 or responseCode == 202: return dataString else: ''' to mark the case failed if response code is not 200-202 ''' return None else: logger.error('') logger.error('---------------------------------------------------------------------------------------------------') logger.error('-------->>> Input or Error stream is None, it may be a defect if it is positive test case') logger.error('---------------------------------------------------------------------------------------------------') logger.error('') return None
def __init__(self): ''' Constructor ''' self.logger = Util.getLogger(self.__class__.__name__) self.mySubClassName = self.__class__.__name__ self.logger.info('') self.logger.info('') self.logger.info('======================>>>>>> class name: ' + self.mySubClassName + ' <<<<<<<======================') self.logger.info('') self.logger.info('') self.getCatalogUrl = '' # for matching catalog self.catalogServiceOfferingType = '' self.catalogServiceOfferingName = '' self.catalogLinkTitle = '' # get from parent catalog for this child creation self.catalogUid = '' self.catalogName = '' self.catalogDescription = '' self.catalogVersion = '' self.catalogSelfUrl = '' self.catalogCreateUrl = '' self.createUid = '' self.createName = '' self.createDescription = '' self.createTaskXml = None self.getDetailUrl = '' self.detailXml = '' self.catalogXml = '' self.deleteUrl = '' self.deleteTaskXml = '' self.updateUrl = '' self.updateTaskXml = '' self.requestBodyFileName = '' self.requestBodyFilePath = '' self.requestParams = {} # get from detail self.uid = '' self.pod = None # debug switch, online by default self.isOffLine = False
def execute(self): try: status = self.__readCommandLine() if CL.HELP_KEY in self.commands: self.__usage() return 1 if 'afos_cmds' in self.commands: self.__afosCmds() return 1 if 'awips_cmds' in self.commands: self.__awipsCmds() return 1 if self.__isScriptRequest(): self.__handleScriptRequest() return 0 if self.__isTriggerScriptRequest(): self.__handleWatchWarnRequest() return 0 if self.__hasProduct(): self.__readProduct() msg = self.__generateRequestMessage() msg = self.__submitRequestMessage(msg) if msg == None: return 0 status = self.__processRequestResponse(msg) except Exception, e: util.printMessage(sys.stderr, header='Error processing request', body=str(e)) return 1
def __init__(self): self._logger = Util.getLogger(self.__class__.__name__) self._topName = 'My Default Top' self._thisObject = None self._dataStore = {} self._uidList = [] self._resultList = [] self._totalTestCase = 0 self._metaPropertyTestCases = {} self._internetEdgeZoneLayer3VlanReservedIpAddressList = None self._securedInternetEdgeZoneLayer3VlanReservedIpAddressList = None
def employee(): api_method = 'employee/v2/queryEmployee' # api_bodyValue= '' km = KangMian(api_method, '{}') responseData = km.callAPI() emp = ParseEmployee() sqlDict, colDict = emp.getTabAndSql_Dict() list_sql = Util.dealResponseData(responseData, sqlDict, colDict) print(len(list_sql)) pg = pypgsql.dba() pg.execListSql(list_sql)
def parseCatalog(self, catalog): evalXpath = Element.evaluateXpath(self, XPath._CatalogUid) self.catalogUid = Util.getXpathValue(catalog, evalXpath) self.logger.info('catalog uid: ' + self.catalogUid) self.createUid = self.catalogUid evalXpath = Element.evaluateXpath(self, XPath._CatalogName) self.catalogName = Util.getXpathValue(catalog, evalXpath) self.logger.info('catalog name: ' + self.catalogName) evalXpath = Element.evaluateXpath(self, XPath._CatalogDescription) self.catalogDescription = Util.getXpathValue(catalog, evalXpath) self.logger.info('catalog description: ' + self.catalogDescription) evalXpath = Element.evaluateXpath(self, XPath._CatalogVersion) self.catalogVersion = Util.getXpathValue(catalog, evalXpath) self.logger.info('catalog version: ' + self.catalogVersion) evalXpath = Element.evaluateXpath(self, XPath._CatalogLinkSelf) self.catalogSelfUrl = Util.getXpathValue(catalog, evalXpath) self.logger.info('catalog self url: ' + self.catalogSelfUrl) evalXpath = Element.evaluateXpath(self, XPath._CatalogLinkCreate) self.catalogCreateUrl = Util.getXpathValue(catalog, evalXpath) self.logger.info('catalog create url: ' + self.catalogCreateUrl)
def create(self, testCaseId): NsmUtil.printHeadLine2('START: create test case ' + testCaseId) Element.populateCreateParameter(self) requestBody = Util.readFile(self.requestBodyFilePath) requestBody = requestBody.replace('${uid}', self.createUid) requestBody = requestBody.replace('${name}', self.createName) requestBody = requestBody.replace('${description}', self.createDescription) for key, value in self.requestParams.items(): keyStr = '${' + key + '}' requestBody = requestBody.replace(keyStr, value) self.logger.info('create request: ' + self.catalogCreateUrl) # pod creation request body is too huge, skip it if self.mySubClassName != 'Pod': self.logger.info('request body: ' + "\n\n" + requestBody +"\n") nameDash = self.createName.replace(' ', '-') debugCreateFilePath = Define._PathResponseCreateDefault + '/' + nameDash + '-create-' + testCaseId + '.xml' status = None if (self.isOffLine): timeStart = datetime.now() self.logger.debug('get create task from file: ' + debugCreateFilePath) self.createTaskXml = Util.readFile(debugCreateFilePath) myTask = Task(self.createTaskXml) status = myTask.checkResult() self.createTaskXml = myTask.taskXml if Define._Performance and Define._CurrentPerfAction: timeEnd = datetime.now() Define._PerformanceData.setCurrentTenantClassLog(Define._CurrentPerfClassName, Define._CurrentPerfObjectName, Define._CurrentPerfAction, timeStart, timeEnd) else: timeStart = datetime.now() self.createTaskXml = HttpUtil.doPost(self.catalogCreateUrl, Define._credential, requestBody) self.logger.debug('write pending create task to file: ' + debugCreateFilePath + '-pending.xml') Util.writeFile(debugCreateFilePath+'-pending.xml', self.createTaskXml) myTask = Task(self.createTaskXml) status = myTask.checkResult() self.createTaskXml = myTask.taskXml if Define._Performance and Define._CurrentPerfAction: timeEnd = datetime.now() Define._PerformanceData.setCurrentTenantClassLog(Define._CurrentPerfClassName, Define._CurrentPerfObjectName, Define._CurrentPerfAction, timeStart, timeEnd) self.logger.info('write final create task to file ' + debugCreateFilePath) #self.logger.info(self.createTaskXml) Util.writeFile(debugCreateFilePath, self.createTaskXml) NsmUtil.printHeadLine2('END: create test case ' + testCaseId) return status
def getDetail(self, testCaseId): NsmUtil.printHeadLine2('START: get detail test case ' + testCaseId) nameDash = self.createName.replace(' ', '-') debugDetailFilePath = Define._PathResponseCreateDefault + '/' + nameDash + '-detail-' + testCaseId + '.xml' if (self.isOffLine): self.logger.debug('get detail from file: ' + debugDetailFilePath) self.detailXml = Util.readFile(debugDetailFilePath) else: try: self.getDetailUrl = Util.getXpathValue(self.createTaskXml, XPath._TaskElementUid) except: self.getDetailUrl = self.uid self.logger.info('get detail url: ' + self.getDetailUrl) self.detailXml = HttpUtil.doGet(self.getDetailUrl, Define._credential) if self.getDetailUrl and self.getDetailUrl != '' else None if self.detailXml: self.logger.debug('write detail to file: ' + debugDetailFilePath) Util.writeFile(debugDetailFilePath, self.detailXml) if self.detailXml: self.uid = Util.getXpathValue(self.detailXml, XPath._DetailUid) self.logger.info('uid for ' + self.createName + ': ' + self.uid) self.getCatalogUrl = Util.getXpathValue(self.detailXml, XPath._DetailLinkCatalog) Element._uidList.append([self.uid, self.mySubClassName, self.createName]) NsmUtil.printHeadLine2('END: get detail test case ' + testCaseId) return self.detailXml
def __handleWatchWarnRequest(self): cmd = self.commands.get('command')[0] args = self.commands.get(cmd) if cmd == 'trig_add': if (os.path.exists(args[0])): try: self.__deleteWatchWarn() self.__deleteSubscriptions() except Exception,e: raise MSG.MessageError('unable to create message for textdb request',e) try: for i in range(len(args)): f = open(args[i],'r') for line in f: # remove all white-spaces at beginning of the line. line = line.lstrip() if (line.find('#') == -1): try: values = line.split(' ', 1) try: msg = MSG.Message(True) msg.initializeMessage(False) msg.addProperty(name='VIEW',value='warn',replace=False) msg.addProperty(name='OP',value='PUT',replace=False) msg.addProperty(name='PRODID',value=values[0],replace=False) msg.addProperty(name='SCRIPT',value=values[1],replace=False) except Exception,e: raise MSG.MessageError('unable to create message for textdb request',e) if ((len(values[0]) >= 8) & (len(values[1].strip('\r\n')) > 0)): # print "Insert Subscription [" + values[0] + "] Status:" + str(sm.execute()) cline = ['-o','add', '-t','ldad', '-r','ldad', '-p', values[0], '-f', values[1], '-c','%TRIGGER%'] sm = SM.SubscriptionManager(name='textdb',args=cline) sm.execute() msg = self.__submitRequestMessage(msg.getXML()) status = self.__processRequestResponse(msg) except ValueError: util.printMessage(sys.stderr, header='Error processing line', body=line)
def checkResult(self): while self.taskXml and self._loopCount > 0: #if (self._loopCount == 10): # self.logger.debug('task link self xpath: ' + XPath._TaskLinkSelf) # self.logger.debug('task status xpath: ' + XPath._TaskTaskStatus) selfUrl = Util.getXpathValue(self.taskXml, XPath._TaskLinkSelf) status = Util.getXpathValue(self.taskXml, XPath._TaskTaskStatus) taskResponseCode = Util.getXpathValue(self.taskXml, XPath._TaskResponseCode) self.logger.info('') self.logger.info('task status [' + str(self._loopCount) + ']: ------------------------------------->>> ' + taskResponseCode + ': ' + status) self.logger.info('') if status == 'success': return True elif status == 'failure': taskFaultType = Util.getXpathValue(self.taskXml, XPath._TaskFaultType) taskFaultMessag = Util.getXpathValue(self.taskXml, XPath._TaskFaultMessag) taskFaultArguments = Util.getXpathValue(self.taskXml, XPath._TaskFaultArguments) self.logger.info('') self.logger.info('') self.logger.info('---------------->>> Task result is FAILURE <<<--------------------') self.logger.info('') self.logger.info('fault type: ' + taskFaultType) self.logger.info('fault message: ' + taskFaultMessag) self.logger.info('fault arguments: ' + taskFaultArguments) self.logger.info('------------------------------------------------------------------') self.logger.info('') return False self._loopCount = self._loopCount - 1 time.sleep(Task._loopInterval) self.taskXml = HttpUtil.doGet(selfUrl, Define._credential) return False
def update(self, objectClassName, requestBody, category, testCaseId): NsmUtil.printHeadLine2('START: update test case ' + str(testCaseId)) self.updateUrl = self.uid + '/put' self.logger.info('update request url: ' + self.updateUrl) if self.mySubClassName != 'Pod': self.logger.info('request body: ' + "\n\n" + requestBody +"\n") else: podRequestFilePath = Define._PathLog + '/pod-request-body.xml' Util.writeFile(podRequestFilePath, requestBody) name = self.createName + ' Update ' + category + ' ' + str(testCaseId) nameDash = name.replace(' ', '-') debugUpdateClassPath = Define._PathResponseUpdate + '/' + objectClassName Util.mkdir(debugUpdateClassPath) debugUpdateFilePath = debugUpdateClassPath + '/' + nameDash + '.xml' status = None if (self.isOffLine): self.logger.debug('get update task from file: ' + debugUpdateFilePath) self.updateTaskXml = Util.readFile(debugUpdateFilePath) myTask = Task(self.updateTaskXml) status = myTask.checkResult() self.updateTaskXml = myTask.taskXml else: self.updateTaskXml = HttpUtil.doPost(self.updateUrl, Define._credential, requestBody) if not self.updateTaskXml: return False self.logger.debug('write pending update task to file: ' + debugUpdateFilePath + '-pending.xml') Util.writeFile(debugUpdateFilePath+'-pending.xml', self.updateTaskXml) myTask = Task(self.updateTaskXml) status = myTask.checkResult() self.updateTaskXml = myTask.taskXml self.logger.info('write final update task to file ' + debugUpdateFilePath) #self.logger.info(self.updateTaskXml) Util.writeFile(debugUpdateFilePath, self.updateTaskXml) NsmUtil.printHeadLine2('END: update test case ' + str(testCaseId)) return status
def invite(): email = request.args.get('email') # check for unused random_string for this email from lib import MySQL mysql = MySQL() query = "SELECT * FROM invitations WHERE email='" + email + "'" random_string = '' try: rows = mysql.fetch_all(query) for row in rows: if(row[3] == False): random_string = row[2] except Exception as e: print e if(random_string == ''): import random import string add_account_parameter = ''.join(random.choice(string.ascii_lowercase + string.ascii_uppercase + string.digits) for x in range(64)) mysql.insert_into_invitations(email, add_account_parameter) else: add_account_parameter = random_string # generate add_account url from lib import Util u = Util() add_account_url = u.get_application_url() + "add_account/" + add_account_parameter print add_account_url # send invitation email u = Util() subject = "Invitation to join Leads' In" text = "Hello, \nPlease click " + add_account_url + " to add yourself to the list of leads in \"Leads' In\". \nThank you." u.send_invitation_email(email, subject, text) return jsonify({'response':True, 'email':email})
def __runScript(self): io = IO.InputOutput() # pass the script to EDEX runner = self.commands.get('runner') service = config.endpoint.get(runner) # submit the input to the server and obtain result connection = str( os.getenv("DEFAULT_HOST", "localhost") + ":" + os.getenv("DEFAULT_PORT", "9581")) ch = CH.CommHandler(connection, service) ch.process(self.script) # expect message 200, if not print error message and return error code if not ch.isGoodStatus(): util.reportHTTPResponse(ch.formatResponse()) return 1 if 'fullMessage' in self.commands: # Return the full XML message to the appropriate stream io.setStream(sys.stdout) io.writeln(data=ch.getContents()) retVal = 0 else: # Pull the responses element out of the xml msg = MSG.Message() msg.parse(ch.getContents()) # process the response and send results to the appropriate stream error, hdr, resp = self.__getResponse(msg) if error: io.setStream(sys.stderr) retVal = 1 else: io.setStream(sys.stdout) retVal = 0 io.writeln(data=hdr) io.writeln(data=resp) return retVal
def __readScript(self): # read the script if self.script == "": io = IO.InputOutput() io.setStream(sys.stdin) text = io.read() else: text = self.script # perform any substitutions if 'substitution' in self.commands: list = self.commands.get('substitution') dict = util.convListToDict(list) self.script = self.__transform(dict, text) else: self.script = text
def dealRespnoseData(self): # # 全量数据先删除 # self.truncateTable() # 拿到结构 self.getDataBaseDict() responseData = self.callAPI() print('返回' + str(responseData) + '条数据') if len(responseData) == 0: return 0 list_sql = Util.dealResponseData(responseData, self.sqlDict, self.colDict) print(len(list_sql)) pg = pypgsql.dba() pg.execListSql(list_sql) return 1
def get_csv_file(yyyymmdd): """Specify an underscored date as the argument. ex: _20190101 \n If blank is specified, the latest data will be getted.""" # url + filename url = 'https://csvex.com/kabu.plus/csv/japan-all-stock-prices/daily/' filename = 'japan-all-stock-prices{0}.csv'.format(yyyymmdd) url = url + filename # download with open('api_setting/credentials.txt', mode='r') as f: consumer_id = f.readline().strip() consumer_pwd = f.readline().strip() res = requests.get(url, auth=requests.auth.HTTPBasicAuth(consumer_id, consumer_pwd)) # check if res.status_code == 200: # load to dataframe df = pd.read_csv(io.BytesIO(res.content), sep=",", encoding="shift-jis") df.to_csv('data/' + filename, index=False, encoding='utf_8_sig') # can open in Excel using BOM # upsert to sqlserver with open('sql/upsert.sql', mode='r') as f: SQL_TEMPLATE = f.read() for line in df.values: editSql = SQL_TEMPLATE # sql template for i,col in enumerate(line): editSql = editSql.replace('{' + str(i) + '}', str(col)) u.ExecuteSQLBySQLServer(editSql) # upsert print('Congrats!') # log now = datetime.datetime.now() with open('result.log', mode='a') as f: f.write('\n' + now.strftime("%Y/%m/%d %a %H:%M:%S ") + str(res.status_code))
def __afosCmds(self): util.printMessage(sys.stderr, body=config.AFOS_CMDS)
def __awipsCmds(self): util.printMessage(sys.stderr, body=config.AWIPS_CMDS)
def __checkText(self,path): try: element = self.tree.find(path) return (not util.isEmptyString(element.text,True)) except Exception,e: raise MessageError("Unable to access element " + path, e)
'store_v1_querystore_linkmans' ] pg = pypgsql.dba() sql_truncate = 'truncate table ' for _ in tableName: _sql = sql_truncate + schemaName + '.' + _ + ';' print(_sql) pg.execsql(_sql) i = 0 while True: i += 1 api_bodyValue = '{"page_number":"' + str(i) + '"}' print(api_bodyValue) # exit(0) km = KangMian(api_method, api_bodyValue) # 拿到ResponData responseData = km.callAPI() if len(responseData) == 0: break # 拿到对应的数据库结构 sqlDict, colDict = common.getDict(schemaName, tableName) list_sql = Util.dealResponseData(responseData, sqlDict, colDict) print(len(list_sql)) t1 = time.time() pg.execListSql(list_sql) print(time.time() - t1)
import urllib import urllib.request import pandas as pd import lib.Util as u # read appId from textfile with open('api_setting/appid.txt', mode='r', encoding='utf-8') as f: appId = f.read() # base url url = r'http://api.e-stat.go.jp/rest/2.1/app/json/getStatsData?' # Query: Cleaning SQL_TEMPLATE = "DELETE FROM [dbo].[e_Stat]" editSql = SQL_TEMPLATE u.ExecuteSQLBySQLServer(editSql) # # The API has a limit of 100,000 cases per acquisition. nextKey = 1 while (nextKey != 0): # Please read the specifications of the API # https://www.e-stat.go.jp/api/sites/default/files/uploads/2016/07/API-specVer2.1.pdf keys = { "appId" : appId, "lang" : "J" , "statsDataId" : "0003143513" , "metaGetFlg" : "Y" , "cntGetFlg" : "N", "sectionHeaderFlg" : "1", "lvTime" : "4",
def __usage(self): util.printMessage(sys.stderr, body=config.USAGE_MESSAGE)