Exemplo n.º 1
0
    def processArticles(self, listOfURLs, domain):
        #=======================================================================
        # CREATE XPATH LIST FOR TRAINING
        #=======================================================================
        for url in listOfURLs:
            print 'Processing ', url
            rd = createDom(url=url)
            rd.readDOMrecursive()
            
            pickleFilePath = self.__filesFolder+str(domain)
            
            if os.path.isfile(pickleFilePath+'.json'):
                xpathValues = json.load(open(pickleFilePath+'.json', 'rb'))
                z = xpathValues.copy()
                z.update(rd._createDom__domDict)
                xml = dicttoxml.dicttoxml(z)
                f= open(pickleFilePath+'.xml', 'wb')
                f.write(xml)
                f.close()
                json.dump(z, open(pickleFilePath+'.json', 'wb'))

            else:
                json.dump(rd._createDom__domDict, open(pickleFilePath+'.json', 'wb'))
                xml = dicttoxml.dicttoxml(rd._createDom__domDict)
                f= open(pickleFilePath+'.xml', 'wb')
                f.write(xml)
                f.close()
Exemplo n.º 2
0
    def _wrap_xml_output(self, gmeta_dict, timestamp):
        import dicttoxml
        from lxml import etree
        keys_to_drop = ["@context", "subject", "frdr:geospatial"]

        parser = etree.XMLParser(remove_blank_text=True, recover=True)
        xml_tree = etree.parse("schema/stub.xml", parser=parser)
        root_tag = xml_tree.getroot()

        context_block = gmeta_dict[0]["content"]["@context"]
        context_xml = etree.fromstring(dicttoxml.dicttoxml(context_block, attr_type=False, custom_root='schema'),
                                       parser=parser)
        root_tag.insert(0, context_xml)

        control_block = {"timestamp": int(round(time.mktime(timestamp))),
                         "datestamp": time.strftime("%Y-%m-%d %H:%M:%S UTC", timestamp)}
        control_xml = etree.fromstring(dicttoxml.dicttoxml(control_block, attr_type=False, custom_root='generated'),
                                       parser=parser)
        root_tag.insert(0, control_xml)

        recordtag = xml_tree.find(".//records")
        for entry in gmeta_dict:
            xml_dict = self.change_keys(entry["content"], keys_to_drop)
            xml_dict["id"] = entry["id"]
            xml_dict["visible_to"] = entry["visible_to"]

            try:
                record_xml = etree.fromstring(
                    dicttoxml.dicttoxml(xml_dict, attr_type=False, custom_root='record', item_func=self.xml_child_namer), parser=parser)
                recordtag.append(record_xml)
            except:
                self.logger.debug("Error converting dict to XML: {}".format(entry["id"]))

        return xml_tree
Exemplo n.º 3
0
def xml_format(labels, results):
    final = [ dict(zip(labels,item)) for item in results]
    try:
       response = HttpResponse(dicttoxml(final), mimetype='application/text')
    except:
       response = HttpResponse(dicttoxml(final), 
        content_type='application/text')
    return response
Exemplo n.º 4
0
def prettyxml(target):
    '''Takes in a target database object and returns cleanly formatted XML'''
    try:
        x = dicttoxml(t.serialize for t in target)
    except:
        x = dicttoxml(target.serialize)
    response = make_response(x)
    response.headers['Content-Type'] = 'application/xml'
    return response
Exemplo n.º 5
0
        def to_xml(*args, **kwargs):
            response.set_header("Content-Type", "text/xml")

            try:
                return dicttoxml(
                    callable_(*args, **kwargs), custom_root="data"
                )
            except Exception as e:
                logger.exception("Error on route: {0}".format(route_))
                return dicttoxml({"error": str(e)}, custom_root="data")
Exemplo n.º 6
0
	def __parseComments(self):
		result = {}
		messages = []
		#get total count of comments
		response = self.graph.get_object(self.id+"/comments?summary=1&filter=toplevel&limit=250")
		xml = dicttoxml.dicttoxml(response)
		output = open(self.filename,'w')
		output.write(xml)
		output.close()
		flag = True
		while flag:
			try:
				xmldoc = minidom.parse(self.filename)
				os.remove(self.filename)
				total_counts = xmldoc.getElementsByTagName('total_count')
				if len(total_counts):
					result['total_count'] = total_counts[0].firstChild.data
				else:
					result['total_count'] = None
				#get up to 250 comments in a page
				#print result['total_count']
				#print xmldoc.toxml()
				if result['total_count'] != None and result['total_count']:
					message = xmldoc.getElementsByTagName('message')
					for x in xrange(0,message.length):
						#print x
						if message[x].firstChild != None:
							messages.append(message[x].firstChild.data)
				link = self.getNext()
				if link !=None:
					r = requests.get(link)
		 			response = r.json()
		 			self.xml =  dicttoxml.dicttoxml(response)
					output = open(self.filename,'w')
					output.write(self.xml)
					output.close()
				else:
					flag = False
			except Exception,e:
				# log = open("ErrorLog","a")
				# if not log :
				# 	log = open("ErrorLog","w")

				# frameinfo = inspect.getframeinfo(inspect.currentframe())
				# log.write(frameinfo.filename+"#"+frameinfo.lineno+"\n")
				# log.write(e+"\n")
				# log.close()
				print "Error %s" % e
				#print response['summary']['total_count']
				result['total_count'] = response['summary']['total_count']
				data = response['data']
				for x in xrange(0,len(data)):
					messages.append(data[x]['message'])
				#result['messages'] = messages
			finally:		
Exemplo n.º 7
0
def as_xml(_request, response, content_type):
    """Return http response object in XML format.
    """
    if hasattr(response, 'root'):
        xml = dicttoxml.dicttoxml(response[response.root], root=False)
    else:
        xml = dicttoxml.dicttoxml(response, root=False)
    xml = ('<?xml version="1.0" encoding="UTF-8" ?>\n'
                    '<{root}>{document}</{root}>'.format(
                root=getattr(response, 'root', 'root'),
                document=xml))
    if settings.DEBUG:
        xml = parseString(xml).toprettyxml()
    return HttpResponse(xml, content_type,
        status=response['_meta']['status'])
Exemplo n.º 8
0
    def format_context(self, context):
        """ Function to format context that will be returned
        to http response
        :param context: context that will be formatted
        :type context: dict

        :return: formatted context
        :rtype: str
        """
        if self.format == 'xml':
            output = dicttoxml.dicttoxml(context)

        elif self.format == 'geojson':
            if not isinstance(context, list):
                context = [context]
            output = json.dumps(
                {'type': 'FeatureCollection', 'features': context},
                cls=DjangoJSONEncoder
            )

        else:
            output = json.dumps(context, cls=DjangoJSONEncoder)

        output.replace('|', ',')
        return output
Exemplo n.º 9
0
def showOne(region_id):
    catalog_mode = request.args.get('catalog')
    xml_format = request.args.get('xml')

    region_list = db_session.query(Region).filter(
        Region.id == region_id).all()

    # Check if query parameters were added to the request
    if (catalog_mode == 'true' or catalog_mode == 'TRUE'):
        # Get single region's catalog
        regions = []

        for i in region_list:
            recipes = []
            for recipe in i.region_recipes:
                recipes.append(recipe.serialize)
            region = i.serialize
            region['recipes'] = recipes
            regions.append(region)

        serialized_result = [regions]
    else:
        # Get single region
        serialized_result = [i.serialize for i in region_list]

    # Lastly we decide which data format to send
    if (xml_format == 'true' or xml_format == 'TRUE'):
        xml_output = dicttoxml.dicttoxml(serialized_result)
        return xml_output, 200, {'Content-Type': 'text/xml; charset=utf-8'}
    else:
        return jsonify(collection=serialized_result)
Exemplo n.º 10
0
def xml_leaf_nodes():
    list = execute_query("""SELECT browse_node_name, browse_node_id
                            FROM browse_nodes
                            WHERE leaf = TRUE
    """)
    nodes_dict = [{'name': x[0], 'id': x[1]} for x in list]
    return dicttoxml.dicttoxml({"leaf_nodes": nodes_dict})
Exemplo n.º 11
0
def write_xml(data, file_name):
    #TO-DO: write the data to an xml tree and save in a file
    print('Working on it')
    tree = dicttoxml.dicttoxml(data)
    f = open("data/" + file_name + ".xml", 'a')
    print >> f, tree
    f.close()
Exemplo n.º 12
0
def DemoApi(request):

    # Demo API
    application_title = settings.APPLICATION_TITLE
    DEBUG = settings.DEBUG_SETTINGS

    if DEBUG:
        print(application_title, "in demo.views.DemoApi")

    fhir_server_url = "http://localhost:8080/fhir-p/"
    fhir_server_param = "baseDstu2/Patient/1/$everything?_format=json"

    data = json.dumps({})
    r = requests.get(fhir_server_url+fhir_server_param, )


    #print(r.json)
    #print(r.content)

    result = r.content
    result_content = json.loads(r.content)
    #result = result["resource"]
    result = json.dumps(result_content, indent=4, )
    xmlresult = dicttoxml.dicttoxml(result_content)

    dom = parseString(xmlresult)
    #print(dom.toprettyxml())

    context = {"APPLICATION_TITLE": application_title,"result": result, "xmlresult": dom.toprettyxml(),
               "url": fhir_server_url, "params": fhir_server_param }
    return render_to_response('demo/demoapi.html', RequestContext(request, context))
Exemplo n.º 13
0
def restaurantMenuXML(restaurant_id):
    restaurant = session.query(Restaurant).filter_by(
                               id=restaurant_id).one()
    items = session.query(MenuItem).filter_by(
        restaurant_id=restaurant_id).all()
    response = make_response(dicttoxml({'items': [i.serialize for i in items]}), 200)
    return response, 200, {'Content-Type': 'text/css; charset=utf-8'}
Exemplo n.º 14
0
def bookListXML(city_id):
    city = session.query(City).filter_by(id = city_id).one()
    books = session.query(Book).filter_by(
        city_id=city.id).all()
    response = make_response(dicttoxml({'Books':[b.serialize for b in books]}), 200)
    response.headers['Content-Type'] = 'application/xml'
    return response
Exemplo n.º 15
0
def processLog(sourceFile, destFile):
	destDict = dict()
	file = open(sourceFile, 'r', encoding='utf8')
	sourceDict  = xmltodict.parse(file.read())
	sourceDict = sourceDict["Tournament"]
	file.close()

	destDict["header"] = dict()
	destDict["header"]["date"] = sourceDict["Date"]

	games = list()
	createMatchesFromRoundsDict(sourceDict["Rounds"], games)
	if "Playoffs" in sourceDict:
		createMatchesFromPlayoffDict(sourceDict["Playoffs"], games)
	if "Playoffs16" in sourceDict:
		createMatchesFromPlayoffDict(sourceDict["Playoffs16"], games)
	if "Playoffs8" in sourceDict:
		createMatchesFromPlayoffDict(sourceDict["Playoffs8"], games)

	destDict["games"] = games

	xml = dicttoxml.dicttoxml(destDict, attr_type=False)
	file = open(destFile, 'w', encoding='utf8')
	file.write(xml.decode("utf-8"))
	file.close()
Exemplo n.º 16
0
    def post(self):
        self.__class__.lock.acquire()
        self.__class__.request_id += 1
        self.__class__.lock.release() 
        options = {}
        options['email']      = self.get_secure_cookie("mail")
        batch_size            = self.get_argument('batch_size')
        options['batch_size'] = 0 if batch_size == 'auto' else batch_size
        options['iterations'] = self.get_argument('iterations')
        options['gpu_model']  = self.get_argument('gpu_model')
        options['topology']   = self.get_argument('topology')
        options['gpu_boost']  = self.get_argument('gpu_boost')
        options['cuda']       = self.get_argument('CUDA')
        options['cudnn']      = self.get_argument('CUDNN')
        options['framework']  = self.get_argument('framework')
        options['profiling']  = self.get_argument('profiling')

        timestamp      = datetime.datetime.now().strftime("%s")
        request_string = 'request_%s_%d' % (timestamp, self.__class__.request_id)
        options['request_id'] = request_string 
        dom = parseString(dicttoxml.dicttoxml(options, attr_type=False))

        if not os.path.exists("./xml"):
            os.mkdir("./xml")
        xml_string = dom.toprettyxml()
        filename = "%s_%d.xml" % (timestamp, self.__class__.request_id)
        filepath = os.path.join('xml', filename)
        with open(filepath, 'w') as f:
            f.write(xml_string)
        scheduler.assign_request(filepath)
        self.redirect('/result?request=%s' % request_string)
Exemplo n.º 17
0
 def getEmployeesXml(self):
     output = self.makeRequest(
         'GET',
         'api/employees/all'
     )
     json_str = json.loads(output)
     return dicttoxml.dicttoxml(json_str)
Exemplo n.º 18
0
def getItemDetailsXML(category_id, item_id):
    item = db_session.query(Item).filter_by(id=item_id).one()
    obj = {"Item": [item.serialize]}
    xml = dicttoxml.dicttoxml(obj)
    resp = app.make_response(xml)
    resp.headers["Content-type"] = "text/xml; charset=utf-8"
    return resp
Exemplo n.º 19
0
    def refund(self, **kwargs):
        """微信支付申请退款,对接口进行封装。

        :param out_trade_no: 订单号
        :param out_refund_no: 退款记录ID
        :param total_fee: 订单金额
        :param refund_fee: 退款金额
        :rtype: 微信接口返回结果
        """
        if 'out_trade_no' not in kwargs:
            raise ValueError('out_trade_no is required.')

        kwargs.setdefault('appid', self.config.get('appid'))
        kwargs.setdefault('mch_id', self.config.get('mchid'))
        kwargs.setdefault('device_info', 'WEB')
        kwargs.setdefault('out_refund_no', kwargs.get('out_trade_no'))
        kwargs.setdefault('total_fee', 1)
        kwargs.setdefault('refund_fee', kwargs.get('total_fee'))
        kwargs.setdefault('refund_fee_type', 'CNY')
        kwargs.setdefault('op_user_id', self.config.get('mchid'))
        kwargs.setdefault('nonce_str', randstr(32))
        kwargs.setdefault('sign', self.sign(**kwargs))

        data = dicttoxml(kwargs, custom_root='xml', attr_type=False)
        try:
            xml = requests.post(self.REFUND_URL, data=data, cert=self.config.get('cert')).content
            return self.xml2dict(xml)
        except Exception, e:
            return dict(return_code='ERROR', return_msg=str(e))
Exemplo n.º 20
0
    def prepay(self, **kwargs):
        """微信支付预付款下单,对接口进行封装。

        :param body: 订单名称
        :param detail: 订单详情
        :param out_trade_no: 订单号
        :param openid: 用户OpenID
        :param type: 订单分类,默认为normal
        :rtype: 微信接口返回结果
        """
        type = kwargs.pop('type', 'normal')
        kwargs.setdefault('appid', self.config.get('appid'))
        kwargs.setdefault('mch_id', self.config.get('mchid'))
        kwargs.setdefault('spbill_create_ip', get_ip())
        kwargs.setdefault('notify_url', url_for('wxpay_callback', type=type, _external=True))
        kwargs.setdefault('trade_type', 'JSAPI')
        kwargs.setdefault('body', '微信支付')
        kwargs.setdefault('out_trade_no', 'wxtest')
        kwargs.setdefault('total_fee', 100)
        kwargs.setdefault('nonce_str', randstr(32))
        kwargs.setdefault('sign', self.sign(**kwargs))

        if 'openid' not in kwargs:
            raise ValueError('openid is required.')

        data = dicttoxml(kwargs, custom_root='xml', attr_type=False)
        try:
            xml = requests.post(self.PREPAY_URL, data=data).content
            return self.xml2dict(xml)
        except Exception, e:
            return dict(return_code='ERROR', return_msg=str(e))
Exemplo n.º 21
0
def prepare_request(request, parameters):
    """
    把数据转换成为xml
    @parameters 是一个字典,字典嵌套字典。
    """
    return dicttoxml.dicttoxml(parameters, attr_type=False,
                               custom_root=request, fold_list=False)
Exemplo n.º 22
0
    def send_red_pack(self, **kwargs):
        """微信支付发红包,对接口进行封装。

        :param total_amount: 金额总数
        :param total_num: 红包数量
        :param wishing: 祝福语
        :param act_name: 活动名称
        :param remark: 备注
        :param re_openid: 用户OpenID
        :rtype: 微信接口返回结果
        """
        kwargs.setdefault('wxappid', self.config.get('appid'))
        kwargs.setdefault('mch_id', self.config.get('mchid'))
        kwargs.setdefault('client_ip', self.config.get('client_ip'))
        kwargs.setdefault('send_name', self.config.get('send_name', '小酷科技'))
        kwargs.setdefault('total_amount', 100)
        kwargs.setdefault('total_num', 1)
        kwargs.setdefault('nonce_str', randstr(32))
        kwargs.setdefault('wishing', '恭喜发财')
        kwargs.setdefault('act_name', '现金红包')
        kwargs.setdefault('remark', '备注')
        kwargs['mch_billno'] = kwargs['mch_id'] + kwargs.get('mch_billno', '')
        kwargs.setdefault('sign', self.sign(**kwargs))

        if 're_openid' not in kwargs:
            raise ValueError('re_openid is required.')

        data = dicttoxml(kwargs, custom_root='xml', attr_type=False)
        try:
            xml = requests.post(self.SEND_RED_PACK, data=data, cert=self.config.get('cert')).content
            return self.xml2dict(xml)
        except Exception, e:
            return dict(return_code='ERROR', return_msg=str(e))
Exemplo n.º 23
0
def output_extraction(data_dict, out_format, out_mode, out_path_file):
    try:
        output_data = None
        output_fileext = None
        if out_format == 'json':
            output_data = json.dumps(data_dict, sort_keys=True, indent=4, separators=(',', ': '))
            output_fileext = '.json'
        if out_format == 'xml':
            output_data = minidom.parseString(dicttoxml.dicttoxml(data_dict, attr_type=False)).toprettyxml(indent='\t')
            output_fileext = '.xml'
        if out_mode == '@s':
            # give out to screen
            print(output_data)
            return None
        elif out_mode == '@none':
            # silent mode
            pass
        else:
            # output path is given in <out_mode>
            if out_path_file is not None:
                if os.path.basename(out_path_file) != CONFIG['output_md_filename']:
                    timestamp = re.sub('\D', '', str(datetime.datetime.now().strftime('%Y%m%d%H:%M:%S.%f')[:-4]))
                    # "meta_" prefix as distinctive feature for metabroker later in workflow
                    out_path_file = os.path.join(out_mode, '_'.join(('meta', timestamp, os.path.basename(out_path_file)[:8].replace('.', '_'), output_fileext)))
            if not os.path.exists(out_mode):
                os.makedirs(out_mode)
            with open(out_path_file, 'w', encoding='utf-8') as outfile:
                outfile.write(output_data)
            status_note([str(round(os.stat(out_path_file).st_size / 1024, 4)), ' KB written to ', os.path.normpath(os.path.relpath(out_path_file))])
    except Exception as exc:
        status_note(str(exc), d=is_debug)
Exemplo n.º 24
0
def zip_chunk(req_dict, ichunk, hits_list, zip_file, csv_writer, format):
    """Zip a chunk of documents.

    Documents are retrieved from elasticsearch in chunks.
    """
    msg = "%s: %s" % (__name__, "zip_chunk()")
    logger.debug(msg)
    if settings.DEBUG:
        print >> stderr, msg

    if hits_list is None:
        print >> stderr, "zip_chunk(): empty hit list"
        return

    for h in range(len(hits_list)):
        hit = hits_list[h]

        _id = hit["_id"]

        # use '-' instead of ':' in file names
        pseudo_filename = _id.replace(':', '-')
        if format == "xml":
            pseudo_filename += ".xml"
            xml = dicttoxml(hit)
            zip_file.writestr(pseudo_filename, xml.encode("utf-8"))
        elif format == "csv":
            if h == 0:
                if ichunk == 0:
                    hit2csv_metadata(csv_writer, req_dict)
                es_header_names, kb_header_names = hit2csv_header(csv_writer, ichunk)
            hit2csv_data(csv_writer, hit, es_header_names, kb_header_names)
        else:         # "json"
            pseudo_filename += ".json"
            zip_file.writestr(pseudo_filename, json.dumps(hit))
Exemplo n.º 25
0
Arquivo: bibs.py Projeto: bfk/bibs
 def convert_results(self, results, output_format, 
                     return_format, inherit_from):
     if output_format == 'json':
         if return_format.lower() == 'xml':
             results = dicttoxml(json.loads(results))
         elif return_format.lower() == 'object':
             results = self.json_to_object(json.loads(results), 
                                           'QueryObject', 
                                           inherit_from)
         else:
             results = json.loads(results)
     elif output_format == 'xml':
         if return_format.lower() == 'json':
             results = json.loads(json.dumps(xmltodict.parse(results)))
         elif return_format.lower() == 'object':
             jsonresults = json.loads(json.dumps(xmltodict.parse(results)))
             results = self.json_to_object(jsonresults, 
                                           'QueryObject',
                                           inherit_from)
     elif output_format == 'javascript':
         if return_format.lower() in ('json', 'xml', 'object'):
             print ('Cannot Convert \'JavaScript\' response to \'' + 
                    return_format.lower() +'\'...returning \'JavaScript\'')
         pass
     return results
Exemplo n.º 26
0
def showItemEndPoint(category_id, item_id, endPoint='XML'):

    item = session.query(Items).filter_by(id=item_id).all()

    # category = session.query(Categories).filter_by(id=category_id).\
    #     all()

    # return jsonify(Categories=[c.serialize for c in categories])
    # print category[0].vehicle_type
    serial_data = {item[0].model: [i.serialize for i in item]}

    if endPoint == 'JSON':
        JSON_data = json.dumps(serial_data, indent=4)
        endPoint_type = "JSON endpoint data"
        return render_template('endPoint.html',
                               endPoint_type=endPoint_type,
                               endPointData=JSON_data)

    if endPoint == 'XML':
        xml_string = BeautifulStoneSoup(dicttoxml.dicttoxml(serial_data)).\
            prettify()
        endPoint_type = "XML endpoint data"
        return render_template('endPoint.html',
                               endPoint_type=endPoint_type,
                               endPointData=xml_string)
Exemplo n.º 27
0
def search(request, place_name, map=None, layer=None,
           start_date=None, end_date=None, project=None, services=None,
           user=None, format='json'):
    """
    Search the Gazetteer and return results in JSON or XML format.
    """
    if not format:
        out_format = 'json'
    else:
        out_format = format.lower()
    if out_format not in ('xml', 'json'):
        out_format = 'json'

    if place_name.isdigit():
        posts = getGazetteerEntry(place_name)
    else:
        posts = getGazetteerResults(place_name, map, layer, start_date, end_date, project, user)
    if services is not None:
        posts.extend(getExternalServiceResults(place_name, services))
    if out_format == 'json':
        return HttpResponse(json.dumps(posts, sort_keys=True, indent=4),
                            content_type="application/json")
    elif out_format == 'xml':
        return HttpResponse(dicttoxml([{'resource': post} for post in posts], attr_type=False, custom_root='response'),
                            content_type="application/xml")
Exemplo n.º 28
0
def get_cases_bykey(type):
    if(request.args.get('key1') is not None):
        key1 = request.args.get('key1')
    else:
        key1 = ""
    if(request.args.get('key2') is not None):
        key2 = request.args.get('key2')
    else:
        key2 = ""
    if(key1 != "" and key2 != ""):
        query_string = 'MATCH (n:`case`) WHERE ".*'+key1+'.*" IN n.keywords AND ".*'+key2+'.*" IN n.keywords RETURN n'
        result = neo4j.CypherQuery(graph_db, query_string).execute()
        if(result.__len__() == 0):
            query_string = 'MATCH (n:`case`) WHERE ".*'+key1+'.*" IN n.keywords OR ".*'+key2+'.*" IN n.keywords RETURN n'
            result = neo4j.CypherQuery(graph_db, query_string).execute()
    else:
        if(key2 != ""):
            query_string = 'MATCH (n:`case`) WHERE ".*'+key2+'.*" IN n.keywords RETURN n'
            result = neo4j.CypherQuery(graph_db, query_string).execute()
        else:
            if(key1 != ""):
                query_string = 'MATCH (n:`case`) WHERE "'+key1+'" IN n.keywords RETURN n'
                result = neo4j.CypherQuery(graph_db, query_string).execute()
    to_send = []
    for node in result:
        to_send.append(node.values[0]._properties)
    print to_send
    if(type == 'json'):
        json_data = jsonify({"response" : (to_send)})
        return json_data
    else:
        if(type == 'xml'):
            xml_data = dicttoxml({"response" : (to_send)})
            return (xml_data)
Exemplo n.º 29
0
def get_case_bydate(type):
    if(request.args.get('date') is not None):
        date = request.args.get('date')
    date_split = date.split('-')
    year = date_split[0]
    query_string = 'MATCH (n:date)-->(m:case)<--(j),(m:case)-->(c) WHERE n.year = "'+ year +'" RETURN m,j,c'
    result = neo4j.CypherQuery(graph_db, query_string).execute()
    print result
    #court_name = 'The Administrative Review Tribunal'
    #date = date.replace('-','/')
    #query_string = 'MATCH (n:court) WHERE n.court_name = "'+ date +'" RETURN n'
    nodes_list = []
    for node in result:
        if(node.values[0]['date'] == date):
            nodes_list.append(node)
    to_send = []
    for node in nodes_list:
        #add judge and court properties to case
        node.values[0]._properties['judge'] = (node.values[1]._properties['j_name'] + " " + node.values[1]._properties['j_surname'])
        node.values[0]._properties['court'] = (node.values[2]._properties['court_name'])
        to_send.append(node.values[0]._properties )
        #to_send.append(node.values[1]._properties )
        #to_send.append(node.values[2]._properties )
    #result = neo4j.CypherQuery(graph_db, query_string).execute()
    #print result.data[0].values[0]._properties
    print to_send
    xml_data = dicttoxml({"response" : (to_send)})
    json_data = jsonify({"response" : (to_send)})
    if(type == 'json'):
        return json_data
    else:
        if(type == 'xml'):
            #dom = parseString(xml_data,unicode)
            #print(dom.toprettyxml())
            return (xml_data)
Exemplo n.º 30
0
def showAll():
    """RESTful method for reading recipes"""
    region_id = request.args.get('region_id')
    user_id = request.args.get('user_id')
    xml_format = request.args.get('xml')

    # Check if query parameters were added to the request
    if (region_id != '' and region_id is not None):
        # Get recipes from one region
        recipes_list = db_session.\
            query(Recipe).filter_by(region_id=region_id).order_by(
                Recipe.last_update.desc()).all()
    elif (user_id != '' and user_id is not None):
        # Get recipes from one user
        recipes_list = db_session.\
            query(Recipe).filter_by(user_id=user_id).order_by(
                Recipe.last_update.desc()).all()
    else:
        # Get all recipes
        recipes_list = db_session.query(Recipe).order_by(
            Recipe.last_update.desc()).all()

    serialized_result = [i.serialize for i in recipes_list]
    # Lastly we decide which data format to send
    if (xml_format == 'true' or xml_format == 'TRUE'):
        xml_output = dicttoxml.dicttoxml(serialized_result)
        return xml_output, 200, {'Content-Type': 'text/xml; charset=utf-8'}
    else:
        return jsonify(collection=serialized_result)
Exemplo n.º 31
0
def supplier(keys, website):
    if website == '1688':
        import requests
        from bs4 import BeautifulSoup
        from dicttoxml import dicttoxml
        url = 'https://api.exchangerate-api.com/v4/latest/CNY'
        response = requests.get(url)
        data = response.json()
        ratekrw = data["rates"]["KRW"]
        content = {}
        i = 0
        for key in keys:
            xml = {}
            xml['SearchItemsParameters'] = {}
            xml['SearchItemsParameters']["Provider"] = "Alibaba1688"
            xml['SearchItemsParameters']["LanguageOfQuery"] = "en"
            xml['SearchItemsParameters']['ItemTitle'] = key
            xml_str = dicttoxml(xml, attr_type=False)
            xml_str = str(xml_str)
            xml_str = xml_str[xml_str.find('<SearchItemsParameters>'):xml_str.
                              find('</root>')]
            req = requests.get(
                'http://otapi.net/OtapiWebService2.asmx/SearchItemsFrame?instanceKey=36bafd6e-baea-41e4-9e8d-4eb2436b0166&language=en&xmlParameters='
                + str(xml_str) + '&framePosition=0&frameSize=40')
            soup = BeautifulSoup(req.content, 'xml')
            Items = soup.find("Items")
            itms = Items.findAll('Item')
            for item in itms:
                try:
                    content[i] = {}
                    title = item.find('OriginalTitle')
                    content[i]['group'] = 'Uncategorized'
                    content[i]['title'] = title.text
                    content[i]['image'] = []
                    pics = item.findAll('ItemPicture')
                    for j, pic in enumerate(pics):
                        if j == 3:
                            break
                        content[i]['image'].append(pic.Url.text)
                    price = item.find('OriginalPrice')
                    content[i]['price'] = round(float(price.text), 2)
                    content[i]['price_krw'] = round(
                        (ratekrw) * float(content[i]['price']), 2)
                    location = item.find('Location')
                    content[i]['region'] = ''
                    if location.City is not None:
                        content[i]['region'] += location.City.text + " "
                    if location.State is not None:
                        content[i]['region'] += location.State.text
                    if content[i]['region'] == '':
                        content[i]['region'] = "China"
                    vendorid = item.find('VendorId').text
                    req = requests.get(
                        'http://otapi.net/OtapiWebService2.asmx/GetVendorInfo?instanceKey=36bafd6e-baea-41e4-9e8d-4eb2436b0166&language=en&vendorId='
                        + vendorid)
                    soup = BeautifulSoup(req.content, 'xml')
                    vendor = soup.find('ShopName')
                    if vendor is not None:
                        content[i]['shop-name'] = vendor.text
                    else:
                        vendor = soup.find('DisplayName')
                        content[i]['shop-name'] = vendor.text
                    url = item.find('ExternalItemUrl')
                    content[i]['url'] = url.text
                    i += 1
                except Exception as e:
                    print(e)
                    pass
        return content

    elif website == "Alibaba":
        from selenium import webdriver
        import time
        from selenium.webdriver.chrome.options import Options
        options = Options()
        options.add_argument("--headless")
        options.add_argument("start-maximized")
        options.add_argument("disable-infobars")
        options.add_argument("--disable-extensions")
        options.add_argument("--no-sandbox")
        options.add_argument("window-size=1920,1080")
        options.add_argument("--disable-dev-shm-usage")
        from selenium.webdriver.common.keys import Keys
        urls = {}
        driver = webdriver.Chrome('contentdownloader/chromedriver',
                                  options=options)
        driver.get('https://www.alibaba.com/corporations/watch.html')
        i = 0
        for key in keys:
            time.sleep(5)
            Send = driver.find_element_by_name('SearchText')
            Send.clear()
            Send.send_keys(key)
            button = driver.find_element_by_xpath(
                '//*[@id="J_SC_header"]/header/div[2]/div[2]/div/div[1]/form/input[4]'
            ).click()
            for company_detali in driver.find_elements_by_xpath(
                    '//div[@class="item-main"]'):
                name = company_detali.find_elements_by_xpath(
                    './/*[@class="ellipsis search"]')
                region = name[0].text
                try:
                    link = company_detali.find_element_by_xpath(
                        './/*[@class="img-thumb"]/a')
                    urls[i] = {}
                    urls[i]['region'] = region
                    urls[i]['url'] = link.get_attribute('href')
                    i += 1
                except Exception as e:
                    pass
        supplier = alibaba(urls)
        return supplier
Exemplo n.º 32
0
 def to_xml(self, output_file_name):
     with open(str(output_file_name), "wb") as xml_file:
         xml_file.write(dicttoxml.dicttoxml(self.generate(), attr_type=True, cdata=True))
#!/usr/bin/env python
# (c) 2016 John Strickler
#
import csv
from dicttoxml import dicttoxml  # <1>
import lxml.etree as ET

rivers = {'rivers': []}  # <2>
with open('../DATA/longest_rivers.csv') as rivers_in:
    rdr = csv.reader(rivers_in)
    for row in rdr:  # <3>
        rivers['rivers'].append({'name': row[0], 'length': row[1]})

snippet = dicttoxml(rivers)  # <4>

river_root = ET.fromstring(snippet)  # <5>
river_doc = ET.ElementTree(river_root)  # <6>

print(
    ET.tostring(river_doc, pretty_print=True,
                xml_declaration=True).decode())  # <7>

river_doc.write('longest_rivers_default.xml',
                pretty_print=True,
                xml_declaration=True)  # <8>
Exemplo n.º 34
0
 def create_full(req_type, content):
     req = {
         'request_type': req_type,
         'request_content': content
     }
     return dicttoxml(req, custom_root='req', attr_type=False)
Exemplo n.º 35
0
import json
import dicttoxml

f = open('files/products.json', 'r', encoding='utf-8')
jsonStr = f.read()
# 将JSON字符串转换为字典
d = json.loads(jsonStr)
print(d)
# 将字典转换为XML字符串
xmlStr = dicttoxml.dicttoxml(d).decode('utf-8')
print(xmlStr)
f.close()
Exemplo n.º 36
0
#conexion-datos
try:
    cnx = mysql.connector.connect(host="127.0.0.1", user="******", passwd="12345", db="bd_reto")
except mysql.connector.Error as er:
    if er.errno == errorcode.ER_ACCESS_DENIED_ERROR:
        print("Usuario o contraseña incorrecto.")
        print('')
    elif er.errno == errorcode.ER_BAD_DB_ERROR:
        print("No existe la Base de Datos indicada.")
        print('')
    else:
        print(er)
else:
    exe = cnx.cursor(dictionary=True)

    #codigos
    query = "SELECT * FROM oc"
    exe.execute(query)
    filas = exe.fetchall()

    cnx.close()

    #Archivo XML
    ruta = "E:/C disk/Local C/Documentos/Universidad/5° Semestre/Repos 5°/Bases de datos/proyecto/Reto"

    xml = dicttoxml.dicttoxml(filas, custom_root='productos', attr_type=False)
    print(xml)
    tree = ET.ElementTree(ET.fromstring(xml))

    tree.write("extra/output.xml", xml_declaration=True)
Exemplo n.º 37
0
def create():
    # get the HTTP arguments
    lgaName = request.headers.get("lgaName")
    postcode = request.headers.get("postcode")
    content_type = request.headers.get("Content-Type")
    # a list that store lgaName, may be more than 1 name
    lgaName_list = []
    if postcode:
        lgaName_list = postcode_database[int(postcode)][:]
    # normilize lgaName
    if lgaName:
        lgaName = name_to_lgaName(lgaName)
        lgaName_list.append(lgaName)

    # save to database
    lgaName_to_return = set()
    for name in lgaName_list:
        # check whether it is in lgaName_database
        if name in lgaName_database:
            lgaName_to_return.add(name)
        else:
            # check whether it is in database by query
            my_jason = models.query_database(name)
            if my_jason:
                lgaName_to_return.add(name)
                lgaName_database.add(name)
            else:
                # try to download the excel and add it to database
                if download_xlsx(name):
                    my_dict = read_excel_by_year(name)
                    my_json = simplejson.dumps(my_dict)
                    models.save_database(name, my_json)
                    lgaName_to_return.add(name)
                    lgaName_database.add(name)

    # check the database
    if PRINT_INFO:
        print("lgaName = ", lgaName)
        print("postcode = ", postcode)
        print("Content-Type = ", content_type)
        print("lgaName_list = ", lgaName_list)
        print("lgaName_for_return = ", lgaName_to_return)
        print("lgaName_database = ", lgaName_database)

    # generate return dictionary
    my_dict = {}
    for name in lgaName_to_return:
        my_dict[name] = {"title": name, "url": URL_COLLECTION + name}

    # generate reture code and error message
    if my_dict:
        status_code = 201
    else:
        my_dict["ERROR"] = "Nothing created!"
        status_code = 400

    # return json
    if content_type == "application/json":
        return jsonify(my_dict), status_code
    else:  # return xml(by default)
        return dicttoxml.dicttoxml(my_dict,
                                   attr_type=False,
                                   custom_root="entry"), status_code
Exemplo n.º 38
0
def save_as_xml(file_base_name, obj):
    file_name = file_base_name + ".xml"
    xml = dicttoxml.dicttoxml(obj)
    with open(file_name, "wb") as f:
        f.write(xml)
Exemplo n.º 39
0
def serialize(sequence, file_path):
    dict = get_dict(sequence)
    xml = dicttoxml.dicttoxml(dict)
    with open(file_path, 'w') as file:
        file.writelines(str(xml, 'utf-8'))
Exemplo n.º 40
0
 def _validar(self):
     dicttoxml.set_debug(False)
     company_id = self.company_id
     signature_id = self.env.user.get_digital_signature(self.company_id)
     if not signature_id:
         raise UserError(
             _('''There are not a Signature Cert Available for this user, pleaseupload your signature or tell to someelse.'''
               ))
     resumenes = []
     resumenesPeriodo = {}
     recs = self._get_moves()
     for rec in recs:
         TpoDoc = rec.document_class_id.sii_code
         if TpoDoc not in resumenesPeriodo:
             resumenesPeriodo[TpoDoc] = {}
         if self.tipo_operacion == 'BOLETA':
             resumen = self._get_resumen_boleta(rec)
             resumenesPeriodo[TpoDoc] = self._setResumenPeriodoBoleta(
                 resumen, resumenesPeriodo[TpoDoc])
             del (resumen['MntNeto'])
             del (resumen['MntIVA'])
             if resumen.get('TasaIVA'):
                 del (resumen['TasaIVA'])
             resumenes.extend([{'Detalle': resumen}])
         else:
             resumen = self.getResumen(rec)
             resumenes.extend([{'Detalle': resumen}])
             resumenesPeriodo[TpoDoc] = self._setResumenPeriodo(
                 resumen, resumenesPeriodo[TpoDoc])
     if self.boletas:  #no es el libro de boletas especial
         for boletas in self.boletas:
             resumenesPeriodo[boletas.tipo_boleta.id] = {}
             resumen = self._setResumenBoletas(boletas)
             del (resumen['TotDoc'])
             resumenesPeriodo[
                 boletas.tipo_boleta.id] = self._setResumenPeriodo(
                     resumen, resumenesPeriodo[boletas.tipo_boleta.id])
             #resumenes.extend([{'Detalle':resumen}])
     lista = [
         'TpoDoc',
         'TpoImp',
         'TotDoc',
         'TotAnulado',
         'TotMntExe',
         'TotMntNeto',
         'TotalesServicio',
         'TotOpIVARec',
         'TotMntIVA',
         'TotMntIVA',
         'TotOpActivoFijo',
         'TotMntIVAActivoFijo',
         'itemNoRec',
         'TotOpIVAUsoComun',
         'TotIVAUsoComun',
         'FctProp',
         'TotCredIVAUsoComun',
         'itemOtrosImp',
         'TotImpSinCredito',
         'TotIVARetTotal',
         'TotIVARetParcial',
         'TotMntTotal',
         'TotIVANoRetenido',
         'TotTabPuros',
         'TotTabCigarrillos',
         'TotTabElaborado',
         'TotImpVehiculo',
     ]
     ResumenPeriodo = []
     for r, value in resumenesPeriodo.items():
         total = collections.OrderedDict()
         if value:
             for v in lista:
                 if v in value:
                     total[v] = value[v]
             ResumenPeriodo.extend([{'TotalesPeriodo': total}])
     dte = collections.OrderedDict()
     if ResumenPeriodo:
         dte['ResumenPeriodo'] = ResumenPeriodo
         dte['item'] = resumenes
     dte['TmstFirma'] = self.time_stamp()
     resol_data = self.get_resolution_data(company_id)
     RUTEmisor = self.format_vat(company_id.vat)
     xml = dicttoxml.dicttoxml(dte, root=False, attr_type=False).decode()
     doc_id = '%s_%s' % (self.tipo_operacion, self.periodo_tributario)
     libro = self.create_template_envio(
         RUTEmisor, self.periodo_tributario,
         resol_data['dte_resolution_date'],
         resol_data['dte_resolution_number'], xml,
         signature_id.subject_serial_number, self.tipo_operacion,
         self.tipo_libro, self.tipo_envio, self.folio_notificacion, doc_id)
     xml = self.create_template_env(libro)
     env = 'libro'
     if self.tipo_operacion in ['BOLETA']:
         xml = self.create_template_env_boleta(libro)
         env = 'libro_boleta'
     root = etree.XML(xml)
     xml_pret = etree.tostring(root, pretty_print=True).decode('iso-8859-1')\
             .replace('<item/>', '\n')\
             .replace('<item>', '\n').replace('</item>', '')\
             .replace('<itemNoRec>', '').replace('</itemNoRec>', '\n')\
             .replace('<itemOtrosImp>', '').replace('</itemOtrosImp>', '\n')\
             .replace('<item_refs>', '').replace('</item_refs>', '\n')\
             .replace('_no_rec', '')
     envio_dte = self.sign_full_xml(xml_pret, doc_id, env)
     self.sii_xml_request = self.env['sii.xml.envio'].create({
         'xml_envio':
         envio_dte,
         'name':
         doc_id,
         'company_id':
         company_id.id,
     }).id
__author__ = 'Chung HD'
_logger = logging.getLogger(__name__)

from dicttoxml import dicttoxml


def customer_item_func(parent):
    if parent == "Customers":
        return 'Customer'
    return "Item"


data = {
    "Customers": [{
        "name": "abc",
        "email": "abc@gmail",
        "hello": ["abc"]
    }, {
        "name": "def",
        "email": "def@gmail"
    }]
}

xml_str = dicttoxml(data,
                    root=False,
                    attr_type=False,
                    custom_root='Customers',
                    item_func=customer_item_func)

print(xml_str)
Exemplo n.º 42
0
 def json_to_xml(json_string):
     """The function is used to translate json string to xml string"""
     dict_string = json.loads(json_string)
     xml_string = dicttoxml.dicttoxml(dict_string)
     return str(xml_string)
Exemplo n.º 43
0
import json
import urllib
import dicttoxml
import sys

page = urllib.urlopen('http://173.246.108.142/oop/cars.json')
content = page.read()
obj = json.loads(content)
xml = dicttoxml.dicttoxml(obj)

f = open('cars.xml', 'w')
f.write(xml)
Exemplo n.º 44
0
def json_xml(inputStr):
    jdata = json.loads(inputStr)
    xml = dicttoxml.dicttoxml(jdata, root=True, attr_type=False)
    return parseString(xml).toprettyxml()
Exemplo n.º 45
0
    unitprices = driver.find_elements(By.XPATH,
                                      "//div[@class='unitPrice']/span")
    for unitprice in unitprices:
        #         print(unitprice.text[2:])
        ls_unitprices.append(unitprice.text[2:])

    driver.find_element_by_link_text('下一页').click()
    i += 1
print(ls_total_price)
print(ls_areas)
print(ls_unitprices)
ls = []
for i in range(len(ls_total_price)):
    dict1 = {}
    dict1['totalPrice'] = ls_total_price[i]
    dict1['area'] = ls_areas[i]
    dict1['utilPrice'] = ls_unitprices[i]
    ls.append(dict1)

xml = dicttoxml(ls, custom_root='Itemlist', attr_type=False,
                root=True)  #dicttoxml接受可迭代对象并将它们视为列表
'''parseString方法创建一个XML解析器并解析xml字符串:DOM是Document Object Model的简称,
    它是以对象树来表示一个XML文档的方法,使用它的好处就是你可以非常灵活的在对象中进行遍历。
'''
dom = parseString(xml)
print(dom.toprettyxml())  #以xml的格式漂亮打印
file_name = '链家'
with open(file_name + ".xml", "w", encoding="utf8") as outfile:
    outfile.write(dom.toprettyxml())

# driver.get_screenshot_as_file('D:\\screenshot')
Exemplo n.º 46
0
def printCVE_xml(item):
    xml = dicttoxml(item)
    print(xml.decode("utf-8"))
Exemplo n.º 47
0
    def do_receipt(self):
        message = ""
        for inv in self.invoice_ids:
            if inv.claim in ["ACD", "RCD"]:
                continue
            signature_d = self.env.user.get_digital_signature(inv.company_id)
            if not signature_d:
                raise UserError(
                    _("""There is no Signer Person with an authorized
                     signature for you in the system. Please make sure that
                     'user_signature_key' module has been installed and enable
                     a digital signature, for you or make the signer to
                     authorize you to use his signature."""))
            dict_recept = self._recep(inv,
                                      signature_d["subject_serial_number"])
            identifier = \
                ("T" + str(inv.sii_document_class_id.sii_code) + "F" +
                 str(inv.get_folio()))
            doc = """
<Recibo version="1.0" xmlns="http://www.sii.cl/SiiDte"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.sii.cl/SiiDte Recibos_v10.xsd" >
    <DocumentoRecibo ID="{0}" >
    {1}
    </DocumentoRecibo>
</Recibo>
            """.format(
                identifier,
                dicttoxml.dicttoxml(dict_recept, root=False,
                                    attr_type=False).decode())
            message += ("\n " + str(dict_recept["Folio"]) + " " +
                        dict_recept["Declaracion"])
            receipt = (
                '<?xml version="1.0" encoding="ISO-8859-1"?>\n' +
                inv.sign_full_xml(
                    doc.replace(
                        '<?xml version="1.0"'
                        ' encoding="ISO-8859-1"?>\n', ""), "Recibo", "recep"))
            RutRecibe = inv.format_vat(inv.partner_id.vat)
            dict_caratula = self._caratula_recep(
                inv.format_vat(inv.company_id.vat), RutRecibe)
            caratula = dicttoxml.dicttoxml(dict_caratula,
                                           root=False,
                                           attr_type=False).decode()
            envio_dte = self._envio_recep(caratula, receipt)
            envio_dte = (
                '<?xml version="1.0" encoding="ISO-8859-1"?>\n' +
                inv.sign_full_xml(
                    envio_dte.replace(
                        '<?xml version="1.0" encoding="ISO-8859-1"?>\n', ""),
                    "SetDteRecibidos", "env_recep"))
            att = self._create_attachment(
                envio_dte,
                "recepcion_mercaderias_" + str(inv.sii_xml_request.name))
            inv.message_post(body=_("XML of the Reception Document\n %s") %
                             message,
                             subject=_("XML of the Reception Document"),
                             partner_ids=[inv.partner_id.id],
                             attachment_ids=att.ids,
                             message_type="comment",
                             subtype="mt_comment")
            inv.claim = "ERM"
            try:
                inv.set_dte_claim(
                    rut_emisor=inv.format_vat(inv.partner_id.vat))
            except:
                _logger.warning("TODO: Create the code that adds the answer.")
Exemplo n.º 48
0
 def print_result_xml(result):
     custom_root = "CloudStack-%s" % self.profile.replace(" ", "_")
     xml = dicttoxml(result, attr_type=False, custom_root=custom_root)
     self.monkeyprint(parseString(xml).toprettyxml())
Exemplo n.º 49
0
 def get_response(self, response):
     if self.request_json:
         return json.dumps(response)
     else:
         xml = dicttoxml.dicttoxml(response, attr_type=False, root=False)
         return xml.decode("utf-8")
Exemplo n.º 50
0
def PrintToXML(fileName, data):
    makeFolderIfNotExists(DEFAULTOUTPUTPATH)
    with open(f'{DEFAULTOUTPUTPATH}/{fileName}.xml', 'w+') as file:
        file.write(dicttoxml(data).decode('UTF-8'))
        file.close()
Exemplo n.º 51
0
 def do_validate_comercial(self):
     id_seq = self.env.ref('l10n_cl_dte.response_sequence').id
     IdRespuesta = self.env['ir.sequence'].browse(id_seq).next_by_id()
     NroDetalles = 1
     for inv in self.invoice_ids:
         if inv.claim in ['ACD', 'RCD']:
             continue
         try:
             signature_d = self.env.user.get_digital_signature(inv.company_id)
         except:
             return False
         #     raise UserError(_('''There is no Signer Person with an \
         # authorized signature for you in the system. Please make sure that \
         # 'user_signature_key' module has been installed and enable a digital \
         # signature, for you or make the signer to authorize you to use his \
         # signature.'''))
         certp = signature_d['cert'].replace(
             BC, '').replace(EC, '').replace('\n', '')
         dte = self._resultado(
             TipoDTE=inv.document_type_id.code,
             Folio=inv.document_number,
             FchEmis=inv.date_invoice,
             RUTEmisor=inv.format_vat(inv.partner_id.main_id_number),
             RUTRecep=inv.format_vat(inv.company_id.main_id_number),
             MntTotal=int(round(inv.amount_total, 0)),
             IdRespuesta=IdRespuesta,
         )
         ResultadoDTE = dicttoxml.dicttoxml(
             dte,
             root=False,
             attr_type=False,
         ).decode().replace('<item>','\n').replace('</item>','\n')
         RutRecibe = inv.format_vat(inv.partner_id.main_id_number)
         caratula_validacion_comercial = self._caratula_respuesta(
             inv.format_vat(inv.company_id.main_id_number),
             RutRecibe,
             IdRespuesta,
             NroDetalles,
         )
         caratula = dicttoxml.dicttoxml(
             caratula_validacion_comercial,
             root=False,
             attr_type=False,
         ).decode().replace('<item>','\n').replace('</item>','\n')
         resp = self._dte_result(
             caratula,
             ResultadoDTE,
         )
         respuesta = '<?xml version="1.0" encoding="ISO-8859-1"?>\n' + inv.sign_full_xml(
             resp.replace('<?xml version="1.0" encoding="ISO-8859-1"?>\n', ''),
             signature_d['priv_key'],
             certp,
             'Odoo_resp',
             'env_resp',
         )
         inv.sii_message = respuesta
         att = self._create_attachment(
             respuesta,
             'validacion_comercial_' + str(IdRespuesta),
         )
         template = self.env.ref('l10n_cl_dte_incoming.email_template_receipt_commercial_accept', False)
         mail_id = template.send_mail(
             inv.id,
             force_send=True,
             email_values={'attachment_ids': att.ids})
         inv.dte_reception_status = 'validate'
         
         inv.claim = 'ACD'
         try:
             inv.set_dte_claim(
                 rut_emisor=inv.format_vat(inv.partner_id.main_id_number),
             )
         except:
             _logger.warning("@TODO crear código que encole la respuesta")
Exemplo n.º 52
0
 def dict_to_xml(dictionary):
     xml_str = dicttoxml(dictionary, custom_root="yandex", attr_type=False)
     return xml.dom.minidom.parseString(xml_str).toprettyxml()
Exemplo n.º 53
0
                 "is able to use Web-API functions. It is only available "
                 "for automation users.")

    @property
    def defaults(self):
        return config.builtin_role_ids


Formatter = Callable[[Dict[str, Any]], Text]

_FORMATTERS = {
    "json": (json.dumps, lambda response: json.dumps(
        response, sort_keys=True, indent=4, separators=(',', ': '))),
    "python": (repr, pprint.pformat),
    "xml": (dicttoxml.dicttoxml, lambda response: xml.dom.minidom.parseString(
        dicttoxml.dicttoxml(response)).toprettyxml()),
}  # type: Dict[str, Tuple[Formatter, Formatter]]


@cmk.gui.pages.register("webapi")
def page_api():
    try:
        pretty_print = False
        if not html.request.has_var("output_format"):
            html.set_output_format("json")
        if html.output_format not in _FORMATTERS:
            html.set_output_format("python")
            raise MKUserError(
                None, "Only %s are supported as output formats" %
                " and ".join('"%s"' % f for f in _FORMATTERS))
Exemplo n.º 54
0
    def do_receipt(self):
        message = ""
        for inv in self.invoice_ids:
            if inv.claim in ['ACD', 'RCD']:
                continue
            try:
                signature_d = self.env.user.get_digital_signature(inv.company_id)
            except:
                return False
            #     raise UserError(_('''There is no Signer Person with an \
            # authorized signature for you in the system. Please make sure that \
            # 'user_signature_key' module has been installed and enable a digital \
            # signature, for you or make the signer to authorize you to use his \
            # signature.'''))
            certp = signature_d['cert'].replace(
                BC,
                '',
            ).replace(EC, '').replace('\n', '')
            dict_recept = self._recep(
                inv,
                signature_d['subject_serial_number'],
            )
            id = "T" + str(inv.document_type_id.code) + "F" + str(inv.document_number)
            doc = '''
<Recibo version="1.0" xmlns="http://www.sii.cl/SiiDte" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" \
xsi:schemaLocation="http://www.sii.cl/SiiDte Recibos_v10.xsd" >
    <DocumentoRecibo ID="{0}" >
    {1}
    </DocumentoRecibo>
</Recibo>
            '''.format(
                id,
                dicttoxml.dicttoxml(
                    dict_recept,
                    root=False,
                    attr_type=False,
                ).decode(),
            )
            message += '\n ' + str(dict_recept['Folio']) + ' ' + dict_recept['Declaracion']
            receipt = '<?xml version="1.0" encoding="ISO-8859-1"?>\n' + inv.sign_full_xml(
                doc.replace('<?xml version="1.0" encoding="ISO-8859-1"?>\n', ''),
                signature_d['priv_key'], certp, 'Recibo', 'recep')
            RutRecibe = inv.format_vat(inv.partner_id.main_id_number)
            dict_caratula = self._caratula_recep(
                inv.format_vat(inv.company_id.main_id_number),
                RutRecibe,
            )
            caratula = dicttoxml.dicttoxml(
                dict_caratula,
                root=False,
                attr_type=False,
            ).decode()
            envio_dte = self._envio_recep(
                caratula,
                receipt,
            )
            envio_dte = '<?xml version="1.0" encoding="ISO-8859-1"?>\n' + inv.sign_full_xml(
                envio_dte.replace('<?xml version="1.0" encoding="ISO-8859-1"?>\n', ''),
                signature_d['priv_key'],
                certp,
                'SetDteRecibidos',
                'env_recep',
            )
            id_seq = self.env.ref('l10n_cl_dte.response_sequence').id
            IdRespuesta = self.env['ir.sequence'].browse(id_seq).next_by_id()
            att = self._create_attachment(
                envio_dte,
                'recepcion_mercaderias_' + str(IdRespuesta),
                )
            template = self.env.ref('l10n_cl_dte_incoming.email_template_receipt_of_goods', False)
            mail_id = template.send_mail(
                inv.id,
                force_send=True,
                email_values={'attachment_ids': att.ids})
            inv.dte_reception_status = 'mercaderias'
            inv.claim = 'ERM'
            try:
                inv.set_dte_claim(
                    rut_emisor=inv.format_vat(inv.partner_id.main_id_number),
                )
            except:
                _logger.warning("(mejora:) crear código que encole la respuesta")
Exemplo n.º 55
0
def xml(dataset_blobs):
    #datasets = []
    #contributors = []
    subjects = []
    resources = []
    errors = []
    error_reports = []

    def normv(v):
        if is_list_or_tuple(v):
            return [normv(_) for _ in v]
        elif isinstance(v, dict):
            return {k:normv(v) for k, v in v.items()}
        elif isinstance(v, str) and v.startswith('http'):
            # needed for loading from json that has been serialized
            # rather than from our internal representation
            # probably better to centralized the reload ...

            # XXX NOTE these days this will only happen if someone
            # supplies us with a uri in a field where we aren't
            # expecting one, in which case we should just return it
            try:
                v = OntTerm(v)
                return v.asCell()
            except Exception as e:
                loge.error(f'something went wrong with {v}')
                loge.exception(e)
                return v
                #raise e
        elif isinstance(v, rdflib.URIRef):  # FIXME why is this getting converted early?
            ot = OntTerm(v)
            return ot.asCell()
        elif isinstance(v, ProtcurExpression):
            return str(v)  # FIXME for xml?
        elif isinstance(v, Quantity):
            return str(v)
        elif isinstance(v, AsJson):  # XXX returns value not tested, may be extremely strange
            return str(v)
        elif isinstance(v, pathlib.Path):
            return str(v)
        elif isinstance(v, idlib.Stream):
            return v.asCell()
        #elif isinstance(v, list) or isinstance(v, str):
            #return v
        elif isinstance(v, BaseException):
            return repr(v)
        else:
            #loge.debug(repr(v))
            return v

    for dataset_blob in dataset_blobs:
        id = dataset_blob['id']
        dowe = dataset_blob
        #id = dataset.id
        #dowe = dataset.data
        if 'subjects' in dowe:
            for subject in dowe['subjects']:
                subject['dataset_id'] = id
                subject = {k:normv(v) for k, v in subject.items()}
                subjects.append(subject)

        if 'resources' in dowe:
            for res in dowe['resources']:
                res['dataset_id'] = id
                res = {k:normv(v) for k, v in res.items()}
                resources.append(res)

        if 'errors' in dowe:
            ers = get_all_errors(dowe)
            for path, er in ers:
                if not isinstance(er, dict):
                    #breakpoint()
                    loge.critical(er)
                    continue

                if er['pipeline_stage'] in pipes.PipelineEnd._shadowed:
                    continue

                er['dataset_id'] = id
                er = {k:normv(v) for k, v in er.items()}
                errors.append(er)

        if 'status' in dowe:
            if 'path_error_report' in dowe['status']:
                error_reports.append(dowe['status']['path_error_report'])

    xs = dicttoxml.dicttoxml({'subjects': subjects})
    xr = dicttoxml.dicttoxml({'resources': resources})
    xe = dicttoxml.dicttoxml({'errors': errors})
    xer = dicttoxml.dicttoxml({'error_reports': error_reports})
    return (('subjects', xs),
            ('resources', xr),
            ('errors', xe),
            ('error_reports', xer),)
Exemplo n.º 56
0
    def do_reject(self, document_ids):
        inv_obj = self.env['account.invoice']
        id_seq = self.env.ref('l10n_cl_dte.response_sequence').id
        IdRespuesta = self.env['ir.sequence'].browse(id_seq).next_by_id()
        NroDetalles = 1
        for doc in document_ids:
            try:
                signature_d = self.env.user.get_digital_signature(doc.company_id)
            except:
                return False
            #     raise UserError(_('''There is no Signer Person with an \
            # authorized signature for you in the system. Please make sure that \
            # 'user_signature_key' module has been installed and enable a digital \
            # signature, for you or make the signer to authorize you to use his \
            # signature.'''))
            certp = signature_d['cert'].replace(
                BC, '').replace(EC, '').replace('\n', '')
            xml = xmltodict.parse(doc.xml)['DTE']['Documento']
            dte = self._resultado(
                TipoDTE=xml['Encabezado']['IdDoc']['TipoDTE'],
                Folio=xml['Encabezado']['IdDoc']['Folio'],
                FchEmis=xml['Encabezado']['IdDoc']['FchEmis'],
                RUTEmisor=xml['Encabezado']['Emisor']['RUTEmisor'],
                RUTRecep=xml['Encabezado']['Receptor']['RUTRecep'],
                MntTotal=xml['Encabezado']['Totales']['MntTotal'],
                IdRespuesta=IdRespuesta,
            )
            ResultadoDTE = dicttoxml.dicttoxml(
                dte,
                root=False,
                attr_type=False,
            ).decode().replace('<item>', '\n').replace('</item>', '\n')
            RutRecibe = xml['Encabezado']['Emisor']['RUTEmisor']
            caratula_validacion_comercial = self._caratula_respuesta(
                xml['Encabezado']['Receptor']['RUTRecep'],
                RutRecibe,
                IdRespuesta,
                NroDetalles)
            caratula = dicttoxml.dicttoxml(
                caratula_validacion_comercial,
                root=False,
                attr_type=False).decode().replace('<item>','\n').replace('</item>','\n')
            resp = self._dte_result(
                caratula,
                ResultadoDTE,
            )
            respuesta = '<?xml version="1.0" encoding="ISO-8859-1"?>\n' + inv_obj.sign_full_xml(
                resp.replace('<?xml version="1.0" encoding="ISO-8859-1"?>\n', ''),
                signature_d['priv_key'],
                certp,
                'Odoo_resp',
                'env_resp', )
            att = self._create_attachment(
                respuesta,
                'rechazo_comercial_' + str(IdRespuesta),
                id=doc.id,
                model="mail.message.dte.document", )
            #partners = doc.partner_id.ids
            #if not doc.partner_id:
            #    if att:
            #        values = {
            #            'res_id': doc.id,
            #            'email_from': doc.company_id.dte_email,
            #            'email_to': doc.dte_id.sudo().mail_id.email_from,
            #            'auto_delete': False,
            #            'model': "mail.message.dte.document",
            #            'body': 'XML de Respuesta Envío, Estado: %s , Glosa: %s ' % (
            #                recep['EstadoRecepEnv'], recep['RecepEnvGlosa']),
            #            'subject': 'XML de Respuesta Envío',
            #            'attachment_ids': att.ids,
            #        }
            #        send_mail = self.env['mail.mail'].create(values)
            #        send_mail.send()
            #doc.message_post(
            #    body='XML de Rechazo Comercial, Estado: %s, Glosa: %s' % (
            #        dte['ResultadoDTE']['EstadoDTE'], dte['ResultadoDTE']['EstadoDTEGlosa']),
            #    subject='XML de Validación Comercial',
            #    partner_ids=partners,
            #    attachment_ids=[att.id],
            #    message_type='comment',
            #    subtype='mt_comment', )
            template = self.env.ref('l10n_cl_dte_incoming.email_template_commercial_reject', False)
            mail_id = template.send_mail(
                doc.dte_id.id,
                force_send=True,
                email_values={'attachment_ids': att.ids})
            doc.dte_id.reception_response_number = IdRespuesta

            context = dict(self._context or {})
            incoming_reject = context.get('incoming_reject', False) or []
            if not incoming_reject:
                inv_obj.set_dte_claim(
                    rut_emisor = xml['Encabezado']['Emisor']['RUTEmisor'],
                    company_id=doc.company_id,
                    sii_document_number=doc.number,
                    document_type_id=doc.document_type_id,
                    claim='RCD', )
Exemplo n.º 57
0
def format_xml(data, root, lst=list()):
    """将dict转换为xml, xml_config是一个bytes"""
    xml_config = dicttoxml(data, item_func=lambda x: x, custom_root=root, attr_type=False)
    for i in lst:
        xml_config = xml_config.replace(to_bytes(i+i), to_bytes(i))
    return xml_config
Exemplo n.º 58
0
def downloadOneStream(stream, args):
    try:
        url = stream.url
        networkParser = networkParserFactory(url)
        progMetadata = networkParser.getProgMetaData(url)

    except Exception as err:
        # log the error and continue with next stream
        logger.error(err.__repr__())
        updateStreamWithError(stream, err.__repr__())
        return

    # create the filename accoding to file meta-data
    # generic video filename (without ext)
    folder = datetime.fromtimestamp(
        progMetadata.airDate).strftime("%Y-%m-%d_%a")
    dstFullPath = os.path.join(args.outDir, folder)

    # create dest folder if doesn't exist
    if os.path.exists(dstFullPath) is False:
        os.mkdir(dstFullPath)

    # add filename
    dstFullPath = os.path.join(dstFullPath, progMetadata.filename)

    # rename file if it already exist.
    fileIndex = 2
    while os.path.isfile(dstFullPath + ".mp4") is True:
        dstFullPath = os.path.join(
            args.outDir, folder, progMetadata.filename + "_" + str(fileIndex))
        fileIndex += 1

    # downloading with ffmpeg
    logger.info("------------")
    logger.info("Downloading: %s" % (url))
    logger.info("++")
    updateStreamStatus(stream, "downloading")

    try:
        ffmpegHLSDownloader = FfmpegHLSDownloader(url=progMetadata.manifestUrl,
                                                  userAgent=CURRENT_USER_AGENT)
        ffmpegHLSDownloader.downloadAndConvertFile(dst=dstFullPath + ".mp4")
    except Exception as err:
        # log the error and continue with next stream
        logger.error(err.__repr__())
        updateStreamWithError(stream, err.__repr__())
        logger.info("++")
        logger.info("Download interupted with error")
        logger.info("-------")
        return

    # check log file for error
    logger.info("------------")
    logger.info(f"Checking log file: {dstFullPath}.log")
    logger.info("++")
    with open(dstFullPath + ".log", "r") as f:
        lines = f.readlines()
    if any(("error" in l for l in lines)):
        logger.info("------------")
        logger.info("Found downloading error. Clean up and will retry later")
        logger.info("++")
        cleanUp(dstFullPath)
        updateStreamWithError(stream, "Segment download error")
        updateStreamStatus(stream, "pending")
        return

    logger.info("++")
    logger.info("Download completed: %s" % (url))
    logger.info("Video : %s" % (dstFullPath))
    logger.info("-------")

    # save metadata
    if args.saveMetadata:
        logger.info("Saving metadata")
        xmlMeta = dicttoxml.dicttoxml(progMetadata._asdict(), attr_type=False)
        dom = minidom.parseString(xmlMeta)
        with open(dstFullPath + ".meta", "w") as text_file:
            print(dom.toprettyxml(), file=text_file)

    dstFullPath += ".mp4"
    # save metadata to mongo
    logger.info("Saving video info")
    updateStreamWithMetadata(stream, progMetadata)
    addVideo(dstFullPath, folder, args.repo, progMetadata, stream)
Exemplo n.º 59
0
    def get_barcode(self, no_product=False):
        partner_id = self.partner_id or self.company_id.partner_id
        ted = False
        RutEmisor = self.format_vat(self.company_id.vat)
        result['TED']['DD']['RE'] = RutEmisor
        result['TED']['DD'][
            'TD'] = self.location_id.sii_document_class_id.sii_code
        result['TED']['DD']['F'] = self.get_folio()
        result['TED']['DD']['FE'] = fields.Datetime.context_timestamp(
            self.with_context(tz='America/Santiago'),
            fields.Datetime.from_string(self.scheduled_date)).strftime(DF)
        if not partner_id.commercial_partner_id.vat:
            raise UserError(_("Fill Partner VAT"))
        result['TED']['DD']['RR'] = self.format_vat(
            partner_id.commercial_partner_id.vat)
        result['TED']['DD']['RSR'] = self._acortar_str(
            partner_id.commercial_partner_id.name, 40)
        result['TED']['DD']['MNT'] = int(round(self.amount_total))
        if no_product:
            result['TED']['DD']['MNT'] = 0
        for line in self.move_lines:
            result['TED']['DD']['IT1'] = self._acortar_str(
                line.product_id.name, 40)
            if line.product_id.default_code:
                result['TED']['DD']['IT1'] = self._acortar_str(
                    line.product_id.name.replace(
                        '[' + line.product_id.default_code + '] ', ''), 40)
            break

        resultcaf = self.location_id.sequence_id.get_caf_file()
        result['TED']['DD']['CAF'] = resultcaf['AUTORIZACION']['CAF']
        if RutEmisor != result['TED']['DD']['CAF']['DA']['RE']:
            raise UserError(
                _('NO coincide el Dueño del CAF : %s con el emisor Seleccionado: %s'
                  % (result['TED']['DD']['CAF']['DA']['RE'], RutEmisor)))
        dte = result['TED']['DD']
        timestamp = self.time_stamp()
        picking_date = fields.Datetime.context_timestamp(
            self.with_context(tz='America/Santiago'),
            fields.Datetime.from_string(self.date)).strftime(DTF)
        if date(int(timestamp[:4]), int(timestamp[5:7]), int(
                timestamp[8:10])) < date(int(picking_date[:4]),
                                         int(picking_date[5:7]),
                                         int(picking_date[8:10])):
            raise UserError(
                "La fecha de timbraje no puede ser menor a la fecha de emisión del documento"
            )
        dte['TSTED'] = timestamp
        dicttoxml.set_debug(False)
        ddxml = '<DD>' + dicttoxml.dicttoxml(
            dte, root=False, attr_type=False
        ).decode().replace('<key name="@version">1.0</key>', '', 1).replace(
            '><key name="@version">1.0</key>', ' version="1.0">', 1).replace(
                '><key name="@algoritmo">SHA1withRSA</key>',
                ' algoritmo="SHA1withRSA">').replace(
                    '<key name="#text">', '').replace('</key>', '').replace(
                        '<CAF>', '<CAF version="1.0">') + '</DD>'
        keypriv = resultcaf['AUTORIZACION']['RSASK'].replace('\t', '')
        root = etree.XML(ddxml)
        ddxml = etree.tostring(root)
        frmt = self.signmessage(ddxml, keypriv)
        ted = ('''<TED version="1.0">{}<FRMT algoritmo="SHA1withRSA">{}\
</FRMT></TED>''').format(ddxml.decode(), frmt)
        self.sii_barcode = ted
        image = False
        if ted:
            barcodefile = BytesIO()
            image = self.pdf417bc(ted)
            image.save(barcodefile, 'PNG')
            data = barcodefile.getvalue()
            self.sii_barcode_img = base64.b64encode(data)
        ted += '<TmstFirma>{}</TmstFirma>'.format(timestamp)
        return ted
Exemplo n.º 60
0
def exportbookings(request):
    global user_timezone
    error_message = ""
    if request.GET:
        # pdb.set_trace()
        duration = request.GET['coming_period']
        docformat = request.GET['format']
        operation_type = request.GET['type']
        user_timezone = request.session['visitor_timezone']

        bookingsdetails = getbookinginfos(duration, user_timezone)

        # create xml or pdf or csv as requested

        if operation_type == "feed":
            pass
            instance = BookingsFeed(bookingsdetails)
        #get the booking id and all other details and create dictionary
        elif operation_type == "file":
            if len(bookingsdetails):
                if docformat == "csv":
                    response = HttpResponse(content_type='text/csv')
                    response[
                        'Content-Disposition'] = 'attachment; filename=' + "bookings" + duration + "." + docformat
                    headings = bookingsdetails[0].keys()
                    csvWriter = csv.DictWriter(response,
                                               fieldnames=headings,
                                               dialect='excel',
                                               quoting=csv.QUOTE_NONNUMERIC)
                    csvWriter.writeheader()
                    for data in bookingsdetails:
                        csvWriter.writerow(data)
                if docformat == "xml":
                    xml = dicttoxml(bookingsdetails,
                                    custom_root='Bookings',
                                    attr_type=False)
                    response = HttpResponse(xml,
                                            content_type='application/xml')
                    response[
                        'Content-Disposition'] = 'attachment; filename=' + "bookings" + duration + "." + docformat

                if docformat == "pdf":
                    fields = list()
                    for key in bookingsdetails[0].keys():

                        fields.append((key, key))
                    buffer = BytesIO()

                    doc = DataToPdf(fields,
                                    bookingsdetails,
                                    title='Booking Details')
                    doc.export(buffer)
                    response = HttpResponse(content_type='application/xml')
                    response[
                        'Content-Disposition'] = 'attachment; filename=' + "bookings" + duration + "." + docformat
                    pdf = buffer.getvalue()
                    buffer.close()
                    response.write(pdf)
                return response
            else:
                error_message = "Booking not available for this " + duration
    template_name = "exportbookings.html"
    return render(request, template_name, {"error_message": error_message})