def runQuery(outFile, outFormat, queryType, rQuery, verboseOut): try: GNCli.test_query( rQuery, queryType, outFormat) # Will lead to program exit if any issues found. # TODO: controller for this decision making. if rQuery: cQuery = re.sub("[/]+", "\\/", rQuery) # Escaping backslashes else: cQuery = False if queryType == "context" or queryType == "quick" or queryType == "raw" or not queryType: result = GNCli.singleIP(cQuery, queryType) elif queryType == "multi": result = GNCli.multiQuery(cQuery) # takes a list of ips elif queryType == "bulk": result = GNCli.bulkQuery() # defaults to today's date elif queryType == "date": result = GNCli.bulkQuery(cQuery) # param is a date YYYY-MM-DD elif queryType == "actors": result = GNCli.actors() # you can handle special cases for anything by returning False to runQuery. if result: jResult = json.loads(result.decode('utf-8')) else: jResult = False # TODO: formatting.py as described above - encapsulate the following if outFormat == "xml": if jResult: if outFile: GNCli.writeToFile(dict2xml.dict2xml(jResult)) else: print(dict2xml.dict2xml(jResult)) elif outFormat == "txt": if jResult: if queryType != "quick": print(GNCli.banner) GNCli.makeTxt(jResult, queryType, verboseOut) elif outFormat == "csv": if outFile: of = outFile else: # Timestamped file name generated if none is given of = "greynoise-" + time.strftime("%Y%m%d-%H%M%S") + ".csv" if jResult: GNCli.makeCSV(jResult, of, queryType) elif outFormat == "json": if jResult: print(json.dumps(jResult)) elif not outFormat or outFormat == "raw": if jResult: if outFile: GNCli.writeToFile(jResult) else: print( jResult ) # Print raw if nothing specified # TODO: add default except Exception as e: print(" General Error! %s" % e)
def search_xml(request, collection=None, return_keys=(), query={}): if not query: kwargs = {} for k,v in request.GET.items(): kwargs[k]=v else: kwargs = query result = query_mongo(kwargs, collection, return_keys=return_keys) if int(result['code'])==200: listresults=result['results'] else: response = dict2xml({"flangio":result}) return HttpResponse(response, status=int(result['code']), mimetype="application/xml") if settings.RESPECT_SOCIAL_GRAPH: listresults=filter_social_graph(request, listresults) len_results=len(listresults) if len_results < result['num_results']: result['ommitted-results']= result['num_results'] - len_results result['results']=listresults xmlresults=dict2xml({"flangio":result}) return HttpResponse(xmlresults, status=int(result['code']), mimetype="application/xml") else: xmlresults=dict2xml({"flangio":normalize_results(result)}) return HttpResponse(xmlresults, status=int(result['code']), mimetype="application/xml")
async def delete_object( bucket: str, key: str, req: Request, version_id: Optional[str] = None, authorization: Optional[str] = Header(None), x_amz_mfa: Optional[str] = Header(None), x_amz_request_payer: Optional[str] = Header(None), x_amz_bypass_governance_retention: Optional[bool] = Header(None), x_amz_expected_bucket_owner: Optional[str] = Header(None), ): account_id = simple_aws_account_id(authorization) has_errors = False if model.is_bucket_owner(bucket, account_id): if model.is_existing_bucket(bucket, account_id) and model.is_existing_key( bucket, key, account_id): for object_id in model.list_objects_of_key(bucket, key, account_id): # Send DataPutter "Delete ObjectId" response = await store.delete(object_id.decode("UTF-8")) print(f"Delete {bucket}/{key} yielded {response}") if response.decode("UTF-8") == object_id.decode("UTF-8"): model.delete_key_object( object_id.decode("UTF-8"), bucket, key, account_id, ) else: has_errors = True else: if model.is_existing_bucket(bucket, account_id): raise S3ApiException( dict2xml({ "Error": { "Code": "NoSuchKey", "Message": f"s3://{bucket}/{key} does not exist", } }), 404, ) else: raise S3ApiException( dict2xml({ "Error": { "Code": "NoSuchBucket", "Message": f"s3://{bucket} does not exist", } }), 404, ) if not has_errors: model.delete_key(bucket, key, account_id) return Response(status_code=200) print(f"Error deleting object {object_id.decode('UTF-8')}") return Response(status_code=503)
def XMLCategories(): """ Show all categories in XML format """ categories = session.query(Category).all() data = dict2xml([i.serialize for i in categories], wrap="category", indent=" ") xml = '<?xml version="1.0" encoding="UTF-8"?>\n' + dict2xml(data, wrap="categories") response = make_response(xml) response.mimetype = 'text/xml' return response
def XMLCocktails(category_id): """ Return all cocktails within a category in XML format """ cocktails = session.query(Cocktails).filter_by(category_id=category_id).all() data = dict2xml([i.serialize for i in cocktails], wrap="cocktails", indent=" ") xml = '<?xml version="1.0" encoding="UTF-8"?>\n' + dict2xml(data, wrap="category") response = make_response(xml) response.mimetype = 'text/xml' return response
def XMLAlgorithms(category_id): """ Return all algorithms within a category in XML format """ algorithms = session.query(Algorithms).filter_by(category_id=category_id).all() data = dict2xml([i.serialize for i in algorithms], wrap="algorithms", indent=" ") xml = '<?xml version="1.0" encoding="UTF-8"?>\n' + dict2xml(data, wrap="category") response = make_response(xml) response.mimetype = 'text/xml' return response
def saveDictToXmlFile(): xml = dict2xml(matchesDetails) text_file = open("RawXML.txt", "w") text_file.write(xml) text_file.close() xml2 = dict2xml(Counter(enemiesList)) text_file = open("CountedEnemies.txt", "w") text_file.write(xml2) text_file.close()
async def create_bucket( bucket: str, authorization: Optional[str] = Header(None), x_amz_acl: Optional[str] = Header(None), x_amz_grant_full_control: Optional[str] = Header(None), x_amz_grant_read: Optional[str] = Header(None), x_amz_grant_read_acp: Optional[str] = Header(None), x_amz_grant_write: Optional[str] = Header(None), x_amz_grant_write_acp: Optional[str] = Header(None), x_amz_bucket_object_lock_enabled: Optional[str] = Header(None), ): if re.match(BUCKET_NAME_EXP, bucket) is None: raise S3ApiException( dict2xml({ "Error": { "Code": "InvalidBucketName", "Message": f"The bucket name {bucket} must match {BUCKET_NAME_EXP}", "Resource": f"/{bucket}", "RequestId": "SomeRequestID", # TODO: Support Reqest ID } }), 400, ) if len(bucket) < BUCKET_NAME_RANGE[0] or len( bucket) > BUCKET_NAME_RANGE[1]: raise S3ApiException( dict2xml({ "Error": { "Code": "InvalidBucketName", "Message": f"The bucket name {bucket} must be between {BUCKET_NAME_RANGE[0]} and {BUCKET_NAME_RANGE[1]} characters long", "Resource": f"/{bucket}", "RequestID": "SomeRequestID", } }), 400, ) account_id = simple_aws_account_id(authorization) try: result = model.create_bucket(bucket, account_id) model.set_bucket_creation_date(bucket, account_id) # result == 1 ? new : existed return Response(headers={"Location": f"/{bucket}"}) except Exception as e: raise S3ApiException( dict2xml({ "Error": { "Code": "InternalError", "Message": "Unexpected server error while creating bucket", } }), 503, )
def xml_formatter(result, _verbose): """Format result as xml.""" xml_formatted = "" if type(result) is list: xml_formatted = dict2xml({"item": result}, wrap="root", indent="\t") else: xml_formatted = dict2xml(result, wrap="root", indent=" ") # dict2xml does not add header, so add header manually xml_header = '<?xml version="1.0" ?>' return "{}\n{}".format(xml_header, xml_formatted)
def format(self, info, fmt): """ format the result of the operation :param info: content that needs to be converted :param fmt: converted format :returns output: converted result """ if fmt in ("json", "table", "xml"): with open('/dev/null', 'w') as no_print: o_json = subprocess.check_output( "{cmd} -json".format(cmd=self.__cmd).split(), stderr=no_print) info = o_json.decode() json_content = json.loads(info) dict_datas = get_class_type(json_content, "memory", "System Memory") if fmt == "json": return json.dumps(dict_datas, indent=2) if fmt == "xml": import dict2xml return dict2xml.dict2xml(dict_datas, "topology") return None return Monitor.format(self, info, fmt)
def refundorder(self,out_trade_no=None,transaction_id=None,total_fee=None,refund_fee=None): """退款接口""" post_dict = { 'appid': self.appid, 'mch_id': self.mch_id, 'nonce_str': self.generate_nonce_str(), 'out_trade_no': out_trade_no, "out_refund_no" : out_trade_no, "transaction_id" : transaction_id, "total_fee" : total_fee, 'refund_fee': refund_fee, "op_user_id" : self.mch_id } post_dict["sign"] = self.generate_sign(post_dict) ret_xml = dict2xml(post_dict, wrap='xml') log.debug("请求参数") log.debug(ret_xml) r = requests.post(self.URL_REFUND_ORDER, data=ret_xml.encode('utf-8') ,cert=self.cert_path) r.encoding = 'UTF-8' data = r.text.encode('utf-8') ret_dict = {} x = ElementTree.fromstring(data) if x.find('return_code').text.upper() == 'FAIL': raise ParameterValueError(x.find('return_msg').text) if x.find('result_code').text.upper() == 'FAIL': raise ParameterValueError(x.find('err_code').text) if x.find('return_code').text.upper() == "SUCCESS" and x.find('result_code').text.upper() == "SUCCESS": return True return False
def serialDoc(doc, nest_datagrid=True, serial_as='json'): """ Take a Plomino document :doc: and extract its data in a JSON-serializable structure for printing porposes. Item values are renderized according to the field definition and only defined fields will be considered. """ # bad_items are indistinguishable from good behaved citizen: they are unicode values # that just don't belong to the data (they are in fact metadata) # We want to skip those, and to do that we must explicitly list 'em bad_items = ['Plomino_Authors', 'Form'] res = [] form = doc.getForm() for field in form.getFormFields(includesubforms=True, doc=None, applyhidewhen=False): #for itemname in doc.getItems(): itemname = field.id if itemname not in bad_items: itemvalue = doc.getItem(itemname, '') or '' fieldname = itemname res += serialItem(form, fieldname, itemvalue, doc=doc, nest_datagrid=nest_datagrid) if serial_as == 'json': return json_dumps(dict(res)) elif serial_as == 'xml': from dict2xml import dict2xml return dict2xml(dict(res)) else: return res
def post(self): format = self.request.get('format', 'json').lower() id = self.request.get('level_id', None) if id is None: res = {"response": {"error": "no level id with submission"}} else: level = Level.get_by_id(long(id)) user_name = self.request.get('user') user = User.all().filter('name =', user_name).get() if user is None: user = User(name = user_name) user.put() score = self.request.get('score', 0) words_found = self.request.get('words_found').split(",") submission = Submission() submission.user = user submission.level = level submission.score = score submission.words_found = words_found submission.time = datetime.datetime.now() submission.put() res = {"response": {"status" : "OK"}} if format == "json": self.response.out.write(simplejson.dumps(res)) elif format == "xml": self.response.out.write(dict2xml(res).to_string())
def get(self): action = self.request.get('action', 'get_state') format = self.request.get('format', 'json') if action == 'get_state': res = {'game_length' : GAME_LENGTH, 'highscore_length' : HIGHSCORE_LENGTH, 'buffer_length' : BUFFER_LENGTH} level = get_current_level() if level is not None: now = datetime.datetime.now() game_end = level.time + datetime.timedelta(seconds = GAME_LENGTH) buffer_end = game_end + datetime.timedelta(seconds = BUFFER_LENGTH) highscore_end = buffer_end + datetime.timedelta(seconds = HIGHSCORE_LENGTH) if now < game_end: res['state'] = 'game' res['remaining_time'] = (game_end - now).seconds elif now < buffer_end: res['state'] = 'buffer' res['remaining_time'] = (buffer_end - now).seconds else : res['state'] = 'highscore' res['remaining_time'] = (highscore_end - now).seconds else : res = {'error': 'could not find any levels'} if format == 'json': res = simplejson.dumps({'response': res}) elif format == 'xml': res = dict2xml({'response': res}).to_string() self.response.out.write(res)
def convert_raw_to_elan( transcription_data: Dict[str, Any], xml_path: str, elan_path: str, style_path: str = "/elpis/elpis/engines/common/output/templates/elan.xsl"): """ Convert a dict of raw data to an XML-Elan file. :param transcription_data: dict of various data (author, participant, tier id, segments, etc.). :param xml_path: xml path (output file of dict and input file for XSL transformation). :param elan_path: final output path. :param style_path: XSL style path for XSL transformation. :return: elan_path output path. """ xml_data = dict2xml.dict2xml(transcription_data, wrap="data", indent=" " * 4) with open(xml_path, "w") as xml_file: xml_file.write(xml_data) xslt_command = "/elpis-gui/node_modules/.bin/xslt3" # Later put this folder in $PATH? parameters = " ".join([ f'%s="{value}"' % re.sub(r"\s", "_", key) for key, value in transcription_data.items() if key != "segments" ]) command = f"""{xslt_command} -s:'{xml_path}' -xsl:'{style_path}' -o:'{elan_path}' {parameters}""" stream = subprocess.run(command, shell=True) return elan_path
def retornar(dados): if not request.GET.get("tipo", "json") == "xml": response.content_type = 'application/json' return dados else: response.content_type = 'application/xml' return dict2xml(dados).ToString()
def notify_success_xml(): ret_dict = { 'return_code': 'SUCCESS', 'return_msg': 'OK', } xml_str = dict2xml(ret_dict, wrap='xml') return xml_str
def dict_selector(self, d_data): from xml.etree.ElementTree import Element from scrapy import Selector from dict2xml import dict2xml return getattr(Selector(text=dict2xml({"data": d_data})), self.syntax)
def dict2xform(jsform, form_id, root=None, username=None, gen_uuid=False): """ Converts a dictionary containing submission data into an XML Submission for the appropriate form. :param jsform (dict): A python dictionary object containing the submission data :param form_id (str or XForm): An XForm object or a string value representing the forms id_string :param root (str): An optional string that should be used as the root nodes name. Defaults to None :param: username (str): An optional string representing a users username. Used alongside the `form_id` to locate the XForm object the user is trying to submit data too. Defaults to None :returns: Returns a string containing the Submission XML :rtype: str """ if not root: if username: if isinstance(form_id, XForm): root = form_id.survey.name else: form = XForm.objects.filter(id_string__iexact=form_id, user__username__iexact=username, deleted_at__isnull=True).first() root = form.survey.name if form else 'data' else: root = 'data' if gen_uuid: jsform['meta'] = {'instanceID': 'uuid:' + get_uuid(hex_only=False)} return "<?xml version='1.0' ?><{0} id='{1}'>{2}</{0}>".format( root, form_id, dict2xml(jsform))
def crea_xml(DICC_COORDENADAS_DEFECTOS_AUX, label): clases = [] coordenadas = [] for valores in DICC_COORDENADAS_DEFECTOS_AUX.values(): clases.append(valores[0]) coordenadas_str = '' # Creamos un string con las coordenadas de manera que su pueda escribir en el fichero xml if valores[1] != '-': for i in valores[1]: for indice, j in enumerate(i): if indice != 3: coordenadas_str = coordenadas_str + str(j) + ',' else: coordenadas_str = coordenadas_str + str(j) coordenadas_str = coordenadas_str + '; ' else: coordenadas_str = valores[1] coordenadas.append(coordenadas_str) diccionario = dict(zip(clases, coordenadas)) xml = dict2xml(diccionario, wrap=label) # , indent =" ") return xml
def verify_order(self, out_trade_no=None, transaction_id=None): if out_trade_no is None and transaction_id is None: raise MissingParameter(u'out_trade_no, transaction_id 不能同时为空') params_dict = { 'appid': self.appid, 'mch_id': self.mch_id, 'nonce_str': self.generate_nonce_str(), } if transaction_id is not None: params_dict['transaction_id'] = transaction_id elif out_trade_no is not None: params_dict['out_trade_no'] = out_trade_no params_dict['sign'] = self.generate_sign(params_dict) xml_str = dict2xml(params_dict, wrap='xml') r = requests.post(self.URL_VERIFY_ORDER, xml_str) r.encoding = 'UTF-8' data = r.text.encode('UTF-8') xml_dict = {} x = ElementTree.fromstring(data) xml_dict['return_code'] = x.find('return_code').text xml_dict['return_msg'] = x.find('return_msg').text if xml_dict['return_code'] == 'FAIL': return xml_dict xml_dict['appid'] = x.find('appid').text xml_dict['mch_id'] = x.find('mch_id').text # xml_dict['device_info'] = x.find('device_info').text xml_dict['nonce_str'] = x.find('nonce_str').text xml_dict['sign'] = x.find('sign').text xml_dict['result_code'] = x.find('result_code').text # xml_dict['err_code'] = x.find('err_code').text # xml_dict['err_code_des'] = x.find('err_code_des').text xml_dict['openid'] = x.find('openid').text xml_dict['is_subscribe'] = x.find('is_subscribe').text xml_dict['trade_type'] = x.find('trade_type').text xml_dict['bank_type'] = x.find('bank_type').text xml_dict['total_fee'] = x.find('total_fee').text xml_dict['fee_type'] = x.find('fee_type').text xml_dict['cash_fee'] = x.find('cash_fee').text # xml_dict['cash_fee_type'] = x.find('cash_fee_type').text # xml_dict['coupon_fee'] = x.find('coupon_fee').text # xml_dict['coupon_count'] = int(x.find('coupon_count').text) # for i in range(xml_dict['coupon_count']): # xml_dict['coupon_batch_id_%d' % i+1] = x.find('coupon_batch_id_%d' % i+1).text # xml_dict['coupon_id_%d' % i+1] = x.find('coupon_id_%d' % i+1).text # xml_dict['coupon_fee_%d' % i+1] = x.find('coupon_fee_%d' % i+1).text xml_dict['transaction_id'] = x.find('transaction_id').text xml_dict['out_trade_no'] = x.find('out_trade_no').text xml_dict['attach'] = x.find('attach').text xml_dict['time_end'] = x.find('time_end').text xml_dict['trade_state'] = x.find('trade_state').text sign = xml_dict.pop('sign') if sign == self.generate_sign(xml_dict): return xml_dict else: raise TokenAuthorizationError(u'签名验证失败')
def format(self, info, fmt): """ format the result of the operation :param info: content that needs to be converted :param fmt: converted format :returns output: converted result """ if fmt in ("json", "xml"): with open("/dev/null") as no_print: o_json = subprocess.check_output( "{cmd} -json".format(cmd=self.__cmd).split(), stderr=no_print) info = o_json.decode() json_content = json.loads(info) datas = [] walk_class_type(json_content, "memory", "BIOS", datas) json_content.pop("children") datas.append(json_content) dict_datas = {} dict_datas["BIOS"] = datas if fmt == "json": return json.dumps(dict_datas, indent=2) if fmt == "xml": import dict2xml return dict2xml.dict2xml(dict_datas, "info") return None return Monitor.format(self, info, fmt)
def write_config(self, filename: str, config_data: dict): xml = dict2xml.dict2xml(config_data, wrap="config") xmlfile = open( ConfigManager.CONFIG_FOLDER + filename + ConfigManagerXml.FILE_SUFFIX, 'w') xmlfile.write(xml) xmlfile.close()
def add_with_url(self, url, content, img_filename=None): """ Add (POST) a resource @param url: A full URL which for the resource type to create @param content: a string containing the full XML of new resource or an image encoded in base64. @param img_filename: a string containing the filename of the image. @return: a dict of the response from the web service or True if the response is a binary. """ if isinstance(content, dict): xml_content = dict2xml.dict2xml({'prestashop': content}) else: xml_content = content res = super(PrestaShopWebServiceDict, self).add_with_url(url, xml_content, img_filename=img_filename) if isinstance(res, dict) and res.get('prestashop'): res_l2 = res['prestashop'].keys() if 'content' in res['prestashop'].keys(): res_l2.remove('content') return res['prestashop'][res_l2[0]]['id'] else: return True
def dict2xform_sms(jsform, form_id, form_name): dd = {'form_id': form_id} fn = {'form_name': form_name} xml_head = u"<%(form_name)s" % fn xml_head_id = u" id='%(form_id)s'>\n" % dd xml_tail = u"\n</%(form_name)s>" % fn return xml_head + xml_head_id + dict2xml(jsform) + xml_tail
def get_level_stats(self, format, delta = datetime.timedelta(), page = 0, limit = 100): level = get_current_level() if level is not None: submissions = Submission.all().filter("time >", level.time - delta).order('time').order('-score').fetch(limit, page * limit) i = 1 ranks = [] for submission in submissions: obj = {"rank" : i, "user" : submission.user.name, "score" : submission.score} if format == "json": ranks.append(obj) elif format == "xml": ranks.append({"ranking": obj}) i = i + 1 res = {"response": ranks} else : res = {"response": {"error": "could not get the level"}} if format == "json": return simplejson.dumps(res) elif format == "xml": return dict2xml(res).to_string()
def add_with_url(self, url, content=None, img_filename=None): """ Add (POST) a resource @param url: A full URL which for the resource type to create @param content: dict of new resource values. it will be converted to XML with the necessary root tag ie: <prestashop>[[dict converted to xml]]</prestashop> @param content: a string containing the full XML of new resource or an image encoded in base64. @param img_filename: a string containing the filename of the image. @return: a dict of the response from the web service or True if the response is a binary. """ if content is not None and isinstance(content, dict): xml_content = dict2xml.dict2xml({'prestashop': content}) else: xml_content = content res = super(PrestaShopWebServiceDict, self).add_with_url(url, xml_content, img_filename=img_filename) if isinstance(res, dict) and res.get('prestashop'): res_l2 = res['prestashop'].keys() if 'content' in res['prestashop'].keys(): res_l2.remove('content') return res['prestashop'][res_l2[0]]['id'] else: return True
def get(self): bind_id = self.get_argument("bind_id") youtube_id = self.get_argument("youtube_id", "-1") time_watched = self.get_argument("time_watched", -1) logger.debug( "Set-top box list request received: bind_id: %s, youtube_id: %s, time_watched: %s", bind_id, youtube_id, time_watched, ) try: if youtube_id != "-1" and youtube_id != "Title": Video.mark_watched(bind_id=bind_id, youtube_id=youtube_id, time_watched=time_watched) except: dbutils.Session.rollback() try: container = Video.get_list(bind_id=bind_id, is_stb=True) playback_url = fetch_yt_link(container["nowplaying"]["url"]) playback_url = escape(playback_url + "&title=.mp4") container["nowplaying"]["url"] = escape(container["nowplaying"]["url"]) container["nowplaying"]["title"] = escape(container["nowplaying"]["title"]) container["nowplaying"]["youtube_id"] = escape(container["nowplaying"]["youtube_id"]) container["nowplaying"]["playback_url"] = playback_url except: dbutils.Session.rollback() container = [] logger.info("timewatched: %s youtube_id:%s bind_id:%s", time_watched, youtube_id, bind_id) # self.content_type = 'text/xml' //HACK wtf why doesn't this work..? self._headers["Content-Type"] = "text/xml; charset=UTF-8" if container != []: self.write(dict2xml({"nowplaying": container["nowplaying"]})) dbutils.Session.remove() self.finish()
def list_bucket_objects(bucket: str, objects: List, config: Optional[Dict] = {}) -> str: list_bucket_v2_response = { "ListBucketResult": { "@": { "xmlns": "http://doc.s3.amazonaws.com/2006-03-01/" }, "IsTruncated": False, "KeyCount": len(objects), "MaxKeys": len(objects), "Name": bucket, "Prefix": config.get("prefix") or "", "Delimiter": config.get("delimiter") or "", "MaxKeys": config.get("max-keys") or 1000, # TODO: Support delimiter query parameter # "CommonPrefixes": [ # {"Prefix": "CommonPrefix"}, # ], "EncodingType": "UTF-8", "KeyCount": len(objects), "ContinuationToken": config.get("continuation-token") or "", "NextContinuationToken": "nextContinuationToken", "StartAfter": config.get("start-after") or "", } } if len(objects) > 0: contents = [bucket_object(o) for o in objects] list_bucket_v2_response["ListBucketResult"]["Contents"] = contents return dict2xml(list_bucket_v2_response)
def request(whattoget, offset): #parameters offset kan je bepalen. offset is hoe ver je naar voren moet zoeken dus als #naam ben parker id blabla naam ben reilly id blabla is offset 0 en offset 1 is dan #naam reilly id blabla naam ben urich id blabla parameters = "&orderBy=name&limit=100&offset=" + offset #timestamp, iets dat telkens veranderd voor security timestamp = str(time.time()) #dit maakt een string van de timestamp en keys tomd5 = timestamp + privatekey + publickey #dit encode de md5 naar utf-8 voor de hashlib functie tomd5 = tomd5.encode('utf-8') #dit maakt een md5 hash van tomd5 #is een lege md5 som md5_ts_pk_prk = hashlib.md5() #hier voeg je de text aan de lege md5 toe md5_ts_pk_prk.update(tomd5) #hier maak je md5 van de text in t md5 bestand md5_ts_pk_prk = md5_ts_pk_prk.hexdigest() #hier verander je de list van bytes weer in een string md5_ts_pk_prk = str(md5_ts_pk_prk) #hier definier je de request-url testrequest = "http://gateway.marvel.com/v1/public/" + whattoget + "?ts=" + timestamp + parameters + "&apikey=" + publickey + "&hash=" + md5_ts_pk_prk #we printen m voor troubleshooting print(testrequest) #dit slaat de json die de server returnt op in een variable jsonfile = urllib.request.urlopen(testrequest).read() global jsondict #hier wordt de json veranderd in een dict jsondict = json.loads(jsonfile) #de dict verandert in een xml file xmlfile = dict2xml.dict2xml(jsondict) #maak een xmlbestand with open("xmlfromjason.xml", "w") as xmlfilefile: xmlfilefile.write(xmlfile)
def get(self): bind_id = self.get_argument("bind_id") youtube_id = self.get_argument("youtube_id", "-1") response_format = self.get_argument("response_format", "xml") time_watched = self.get_argument("time_watched", -1) limit = self.get_argument("limit", 50) logger.debug( "List request received: bind_id: %s, youtube_id (just watched): %s, response_format: %s, time_watched:%s", bind_id, youtube_id, response_format, time_watched, ) try: if youtube_id != "-1" and youtube_id != "Title": Video.mark_watched(bind_id=bind_id, youtube_id=youtube_id, time_watched=time_watched) except: dbutils.Session.rollback() try: container = Video.get_list(bind_id=bind_id, limit=int(limit)) except: dbutils.Session.rollback() container = {} if response_format == "json": self.content_type = "application/json" self.write(json.dumps(container)) else: self.content_type = "text/xml" self.write(dict2xml(container)) dbutils.Session.remove() self.finish()
def vr_vphrase_make_voc_format(split_type): if split_type != 'train' and split_type != 'test': print 'error' exit(0) m = h5py.File('/home/zawlin/Dropbox/proj/sg_vrd_meta.h5') m_vp = h5py.File('/home/zawlin/Dropbox/proj/sg_vrd_vphrase_meta.h5') root = '/home/zawlin/data/data_vrd/vrd/sg_vp/' anno_root = root + 'Annotations/' + split_type + '/' data_root = root + 'Data/' + split_type + '/' zl.make_dirs(anno_root) zl.make_dirs(data_root) cnt = 0 zl.tick() for k in m_vp['gt/%s' % split_type].keys(): if cnt % 1000 == 0: print cnt, zl.tock() zl.tick() cnt += 1 # todo for vg # im_data= db.image_data.find_one({'image_id':imid}) # im_path_full = im_data['url'].replace('https://cs.stanford.edu/people/rak248/','') # im_path_folder = im_path_full.split('/')[0] # im_path_file = im_path_full.split('/')[1] # im_src_path = vr_root+'%s/%s'%(im_path_folder,im_path_file) # im_dst_path = data_root+'%s/%s'%(im_path_folder,im_path_file) # zl.copy_file(im_src_path,im_dst_path) voc_datum = { "folder": '', "source": { "database": "sg vrd visual phrase" }, "filename": k + '.jpg' } m['train/%s/w' % k][...] w, h = int(m['train/%s/w' % k][...]), int(m['train/%s/h' % k][...]) voc_datum['size'] = {'width': w, 'height': h} objs = [] gt_boxes = m_vp['gt/%s/%s/boxes' % (split_type, k)][...] gt_labels = m_vp['gt/%s/%s/labels' % (split_type, k)][...] for i in xrange(gt_boxes.shape[0]): gt_box = gt_boxes[i] gt_label = gt_labels[i] ymin, ymax, xmin, xmax = gt_box[1], gt_box[3], gt_box[0], gt_box[2] bbox = {'ymin': ymin, 'ymax': ymax, 'xmin': xmin, 'xmax': xmax} name = zl.idx2name_tri(m_vp, gt_label) obj = {'name': name, 'bndbox': bbox} objs.append(obj) voc_datum['object'] = objs #write to xml dst_path = os.path.join( anno_root, voc_datum["folder"], voc_datum["filename"][:voc_datum["filename"].rfind('.')] + '.xml') voc_datum = {'annotation': voc_datum} f = open(dst_path, 'w') f.write(dict2xml(voc_datum) + '\n') f.close() print 'images with annotation=%d\n' % cnt
def xmltext(filename=None, obj=None, bytes=None): if obj is None: obj = pyobj(filename=filename, bytes=bytes) # for i,p in enumerate(obj['Pages']): # obj['Pages'][i] = {'Page': p} return dict2xml.dict2xml(obj, wrap="Document")
def format_output(obj, output_format, pretty_print, xml_library): if output_format == 'json': json_indent = 4 if pretty_print else None output = json.dumps(obj, indent=json_indent) elif output_format == 'xml': if pretty_print: output = dict2xml(obj, wrap='author', indent=' ') elif xml_library == 'dict2xml': output = dict2xml(obj) elif xml_library == 'dicttoxml': output = dicttoxml(obj) else: output = dict2xml(obj) else: output = obj return output
def XMLCocktail(category_id, cocktails_id): """ Return details of a single cocktail in XML format """ cocktail = session.query(Cocktails).filter_by(id=cocktails_id).one() data = dict2xml(cocktail.serialize, wrap="cocktail", indent=" ") xml = '<?xml version="1.0" encoding="UTF-8"?>\n' + data response = make_response(xml) response.mimetype = 'text/xml' return response
def allItemsXML(): items = session.query(CategoryItem).all() serialItems = [] for item in items: serialItems.append(item.serialize) return dict2xml(serialItems, wrap='Items', indent=' '), 200, { 'Content-Type': 'text/css; charset=utf-8' }
def writeXML(file, dict): result = {} if (len(dict) == 0): return with open(file, 'w') as fd: result = "<?xml version=\"1.0\"?>" result = result + dict2xml.dict2xml(dict, wrap="", indent=" ") fd.write(result)
def generate_response(self) -> str: if self.result_data is not None: return dict2xml({ f"{self.operation_name}Response": { f"{self.operation_name}Result": self.result_data, "ResponseMetadata": { "RequestId": self.request_id }, } }) return dict2xml({ f"{self.operation_name}Response": { "ResponseMetadata": { "RequestId": self.request_id }, } })
def categoriesXML(): categories = session.query(Category).all() serialCategories = [] for c in categories: serialCategories.append(c.serialize) return dict2xml(serialCategories, wrap='Category', indent=' '), 200, { 'Content-Type': 'text/css; charset=utf-8' }
def initVariables(self): print("initVariables") self.variables = { 'PERSON': 'Max Mustermann', 'COMPANY': 'Muster Institut', 'EMAIL': '*****@*****.**' } self.xml = dict2xml(self.variables)
def edit_with_url(self, url, content): """ Edit (PUT) a resource from a full URL @param url: an full url to edit a resource @param content: modified dict of the resource. @return: an ElementTree of the Webservice's response """ xml_content = dict2xml.dict2xml({'prestashop': content}) return super(PrestaShopWebServiceDict, self).edit_with_url(url, xml_content)
def convertToXML(self, context): """ Returns the xml representation of the given data """ # Extract important data context = context[self.context_key] if self.context_key is None: raise ImproperlyConfigured( "XMLResponseMixin requires a definition of 'xml_context_key'") if isinstance(context, list) or isinstance(context, tuple): xmlstr = dict2xml( { self.context_key : context }, self.context_key) else: xmlstr = dict2xml(context, self.context_key) return xmlstr
def itemByCategoryXML(category_name): """ returns an XML""" category = session.query(Category).filter_by(name=category_name).one() serialCategory = category.serialize items = session.query(CategoryItem).filter_by(category_id=category.id).all() serialItems =[] for item in items: serialItems.append(item.serialize) serialCategory['items'] = serialItems return dict2xml(serialCategory, wrap='Category', indent=' '), 200, {'Content-Type': 'text/css; charset=utf-8'}
def add_submission_with(request, username, id_string): """ Returns JSON response with Enketo form url preloaded with coordinates. """ def geopoint_xpaths(username, id_string): """ Returns xpaths with elements of type 'geopoint'. """ data_dictionary = DataDictionary.objects.get( user__username__iexact=username, id_string__iexact=id_string) return [ e.get_abbreviated_xpath() for e in data_dictionary.get_survey_elements() if e.bind.get(u'type') == u'geopoint' ] value = request.GET.get('coordinates') xpaths = geopoint_xpaths(username, id_string) xml_dict = {} for path in xpaths: dpath_util.new(xml_dict, path, value) context = { 'username': username, 'id_string': id_string, 'xml_content': dict2xml(xml_dict) } instance_xml = loader.get_template("instance_add.xml")\ .render(context) url = settings.ENKETO_API_INSTANCE_IFRAME_URL return_url = reverse( 'thank_you_submission', kwargs={"username": username, "id_string": id_string}) if settings.DEBUG: openrosa_url = "https://dev.formhub.org/{}".format(username) else: openrosa_url = request.build_absolute_uri("/{}".format(username)) payload = { 'return_url': return_url, 'form_id': id_string, 'server_url': openrosa_url, 'instance': instance_xml, 'instance_id': uuid.uuid4().hex } response = requests.post( url, data=payload, auth=(settings.ENKETO_API_TOKEN, ''), verify=getattr(settings, 'VERIFY_SSL', True)) return HttpResponse(response.text, content_type='application/json')
def add_with_url(self, url, content): """ Add (POST) a resource @param url: A full URL which for the resource type to create @param content: dict of new resource values. it will be converted to XML with the necessary root tag ie: <prestashop>[[dict converted to xml]]</prestashop> @return: a dict of the response from the web service """ xml_content = dict2xml.dict2xml({'prestashop': content}) return super(PrestaShopWebServiceDict, self).add_with_url(url, xml_content)
def get_xml_catalog(): """Returns current catalog formatted to XML. """ categories = db_session.query(Category).all() root = {'category': [category.serialize for category in categories]} xml = dict2xml.dict2xml(root, wrap="catalog", indent=" ") response = make_response(xml) response.headers['Content-Type'] = 'application/xml' return response
def outpuXMLFile(self, jsonFileName): if os.path.isfile(jsonFileName): jsonFile = open(jsonFileName) self.experts = json.load(jsonFile)[0]['experts'] try: output = self.processUsers() xml = dict2xml(output, 'User') xmlFileName = jsonFileName.split('.')[0] + '.xml' xmlFile = open(xmlFileName, 'w') xmlFile.write(xml) finally: jsonFile.close() xmlFile.close()
def outpuXMLFile(self, jsonFileName): if os.path.isfile(jsonFileName): threadsFile = open(jsonFileName) try: threads = json.load(threadsFile) output = self.processThreads(threads) xml = dict2xml(output, 'Thread') xmlFileName = jsonFileName.split('.')[0] + '.xml' xmlFile = open(xmlFileName, 'w') xmlFile.write(xml) xmlFile.close() finally: threadsFile.close()
def json2xform(jsform, form_id): # changing the form_id to match correct Step dd = {'form_id': form_id} xml_head = u"<?xml version='1.0' ?><%(form_id)s id='%(form_id)s'>" % dd xml_tail = u"</%(form_id)s>" % dd for field in jsform.keys(): # treat field starting with underscore are internal ones. # and remove them if field.startswith('_'): jsform.pop(field) return xml_head + dict2xml(jsform) + xml_tail
def _generate_xml(self, url_action, ups_request): access_request = { 'AccessRequest': { 'AccessLicenseNumber': self.license_number, 'UserId': self.user_id, 'Password': self.password, } } xml = u''' <?xml version="1.0"?> {access_request_xml} <?xml version="1.0"?> {api_xml} '''.format( request_type=url_action, access_request_xml=dict2xml(access_request), api_xml=dict2xml(ups_request), ) return xml
def generate_cb_resp(self, resp_dict): ret_dict = { 'appid': self.appid, 'mch_id': self.mch_id, 'nonce_str': self.generate_nonce_str(), 'prepay_id': resp_dict['prepay_id'], 'return_code': resp_dict['return_code'], # 'SUCCESS', 'FAIL' 'return_msg': resp_dict['return_msg'], # 'OK' 'result_code': resp_dict['result_code'], # 'SUCCESS', 'FAIL' 'err_code_des': resp_dict['err_code_des'], # 'OK' } ret_dict['sign'] = self.generate_sign(ret_dict) ret_xml = dict2xml(ret_dict, wrap='xml') return ret_xml
def add_with_url(self, url, content=None, files=None): """ Add (POST) a resource @param url: A full URL which for the resource type to create @param content: dict of new resource values. it will be converted to XML with the necessary root tag ie: <prestashop>[[dict converted to xml]]</prestashop> @param files: a sequence of (type, filename, value) elements for data to be uploaded as files. @return: a dict of the response from the web service """ if content is not None and isinstance(content, dict): xml_content = dict2xml.dict2xml({'prestashop': content}) else: xml_content = content return super(PrestaShopWebServiceDict, self).add_with_url(url, xml_content, files)
def formatLogs(logs,format): """ Formats the logs according to the specified format in the config yaml """ formattedLogs=[] if(format.__eq__("json")): for log in logs: formattedLogs.append(json.dumps(dict(log))) return formattedLogs elif(format.__eq__("xml")): for log in logs: formattedLogs.append(dict2xml.dict2xml(dict(log))) return formattedLogs else: return logs
def write_response(self, obj, nofail=False): format = self.get_format() if format == 'json': self.set_header("Content-Type", "application/javascript") self.write(json.dumps(obj)) elif format == 'jsonp': self.set_header("Content-Type", "application/javascript") callback = self.get_argument('callback', 'callback') self.write('%s(%s);'%(callback, json.dumps(obj))) elif format == 'xml': self.set_header("Content-Type", "application/xml") self.write('<response>%s</response>'%dict2xml.dict2xml(obj)) elif nofail: self.write(json.dumps(obj)) else: raise tornado.web.HTTPError(400, 'Unknown response format requested: %s'%format)
def get_response(data): xml_head = u'<?xml version="1.0" encoding="UTF-8" ?>' response_dict = {'Response': {}} message = data.get('text') if data.get('code') == SMS_API_ERROR: message = None elif data.get('code') != SMS_SUBMISSION_ACCEPTED: message = _(u"[ERROR] %s") % message if message: response_dict.update({"Response": {'Sms': message}}) response = xml_head + dict2xml(response_dict) return HttpResponse(response, mimetype='text/xml')
def add_submission_with(request, username, id_string): import uuid import requests from django.template import loader, Context from dpath import util as dpath_util from dict2xml import dict2xml def geopoint_xpaths(username, id_string): d = DataDictionary.objects.get( user__username__iexact=username, id_string__exact=id_string) return [e.get_abbreviated_xpath() for e in d.get_survey_elements() if e.bind.get(u'type') == u'geopoint'] value = request.GET.get('coordinates') xpaths = geopoint_xpaths(username, id_string) xml_dict = {} for path in xpaths: dpath_util.new(xml_dict, path, value) context = {'username': username, 'id_string': id_string, 'xml_content': dict2xml(xml_dict)} instance_xml = loader.get_template("instance_add.xml")\ .render(Context(context)) url = settings.ENKETO_API_INSTANCE_IFRAME_URL return_url = reverse('thank_you_submission', kwargs={"username": username, "id_string": id_string}) if settings.DEBUG: openrosa_url = "https://dev.formhub.org/{}".format(username) else: openrosa_url = request.build_absolute_uri("/{}".format(username)) payload = {'return_url': return_url, 'form_id': id_string, 'server_url': openrosa_url, 'instance': instance_xml, 'instance_id': uuid.uuid4().hex} r = requests.post(url, data=payload, auth=(settings.ENKETO_API_TOKEN, ''), verify=False) return HttpResponse(r.text, content_type='application/json')
def get_user_stats(self, user, format): user = User.all().filter("name =", user).get() if user is not None: played = 0 submissions = user.submission_set if submissions is not None: played = submissions.count() res = {"response": {"user" : user.name, "played" : played}} else : res = {"response": {"error": "unknown user"}} if format == "json": res = simplejson.dumps(res) elif format == "xml": res = dict2xml(res).to_string() return res