def test_render_and_parse_complex_data(self): """ Test XML rendering. """ renderer = XMLRenderer() content = StringIO(renderer.render(self._complex_data, 'application/xml')) parser = XMLParser() complex_data_out = parser.parse(content) error_msg = "complex data differs!IN:\n %s \n\n OUT:\n %s" % (repr(self._complex_data), repr(complex_data_out)) self.assertEqual(self._complex_data, complex_data_out, error_msg)
def supermarket_list(request): """ List all code home, or create a new home. """ if request.method == 'POST': data = XMLParser().parse(request) serializer = shopSerializer(data=data) if serializer.is_valid(): serializer.save() return XMLResponse(serializer.data, status=201) return XMLResponse(serializer.errors, status=400) if request.GET.get('from_id'): # retrieve objects from the value to last django (doesn't work correct!) all_shops = supermarket.objects.all() test = all_shops.count() min_house = int(request.GET['from_id']) roznica = test - min_house table = all_shops[:roznica] serializer = shopSerializer(table, many=True) return XMLResponse(serializer.data) if not request.GET.get('from_id'): sklep = supermarket.objects.all() serializer = shopSerializer(sklep, many=True) return XMLResponse(serializer.data)
def shop_detail(request, pk): """ Detail of home in XML :param request: :param pk: id of home in database :return: xml of detailed home """ try: sklep = supermarket.objects.get(pk=pk) except supermarket.DoesNotExist: return HttpResponse(status=404) if request.method == 'GET': serializer = shopSerializer(sklep) return XMLResponse(serializer.data) elif request.method == 'PUT': data = XMLParser().parse(request) serializer = shopSerializer(sklep, data=data) if serializer.is_valid(): serializer.save() return XMLResponse(serializer.data) return XMLResponse(serializer.errors, status=400) elif request.method == 'DELETE': sklep.delete() return HttpResponse(status=204)
def home_detail(request, pk): """ Detail of home in XML :param request: :param pk: id of home in database :return: xml of detailed home """ try: dom = Home.objects.get(pk=pk) except Home.DoesNotExist: return HttpResponse(status=404) if request.method == 'GET': serializer = homeSerializer(dom) return XMLResponse(serializer.data) elif request.method == 'PUT': data = XMLParser().parse(request) serializer = homeSerializer(dom, data=data) if serializer.is_valid(): serializer.save() return XMLResponse(serializer.data) return XMLResponse(serializer.errors, status=400) elif request.method == 'DELETE': dom.delete() return HttpResponse(status=204)
def scrap_data(request): extract_text( pdf_path=os.getcwd()+'/PDF/ASGPN0660F_Equity&Commodity_Signed.pdf') # Open the file back and read the contents with open(os.getcwd()+"/TXT/ASGPN0660F_Equity&Commodity_Signed.txt", "r", encoding='UTF8') as f: if f.mode == 'r': contents = f.read() top = Element('Customer') child = SubElement(top, 'Name') Name = re.search('MR(.*)Maiden Name If', contents) child.text = Name.group(1) Address = re.search( 'Client Name(.*)Verification of the client signature done by Name of Employee', contents) address = re.search('Address(.*)City/Town/Village', Address.group(1)) City = re.search('City/Town/Village(.*)PIN Code', Address.group(1)) PIN_Code = re.search('PIN Code(.*)State', Address.group(1)) State = re.search('State(.*)Tel No.', Address.group(1)) email = re.findall('\S+@\S+', contents) child = SubElement(top, 'Email') child.text = email[-1] Pan = re.search("PAN(.*)SecondNAHolder", contents) Pan_No = re.findall('[A-Z]{5}\d{4}[A-Z]{1}', Pan.group(1)) mobile_no = re.findall('\d{2}-\d{10}', contents) child = SubElement(top, 'Address') child.text = address.group(1) child = SubElement(top, 'PIN_CODE') child.text = PIN_Code.group(1) child = SubElement(top, 'State') child.text = State.group(1) child = SubElement(top, 'City') child.text = City.group(1) child = SubElement(top, 'Mobile') child.text = mobile_no[-1] child = SubElement(top, 'Pan_NO') child.text = Pan_No[0] Aadhaar = re.search("IDClient ID(.*)UID IDDear Sir/Madam,", contents) child = SubElement(top, 'Aadhaar') child.text = Aadhaar.group(1) Customer_Ref_No = re.findall('[A-Z]{2}\d{4}', contents) child = SubElement(top, 'Customer_Ref_No') child.text = Customer_Ref_No[0] DOB = re.search(r'\d{2}/\d{2}/\d{4}', contents) Date_Of_Birth = datetime.datetime.strptime( DOB.group(), '%m/%d/%Y').date().strftime("%Y-%m-%d") child = SubElement(top, 'Date_Of_Birth') child.text = Date_Of_Birth data = tostring(top) Customer = open( os.getcwd()+"/XML/Customer.xml", "wb") Customer.write(data) data_stream = BytesIO(data) parsed_data = XMLParser().parse(data_stream) print(parsed_data) print(data_stream) test = ElementTree(top) print(test) content = JSONRenderer().render(parsed_data).decode("utf-8") print(content) print(os.getcwd()) return Response(content, content_type="application/json; charset=utf-8")
def parse_xml(data): """ Parse xml dict :param data: string :return: dict """ try: tree = etree.fromstring(data) except (etree.ParseError, ValueError) as exc: raise ParseError('XML parse error - %s' % six.text_type(exc)) data = XMLParser()._xml_convert(tree) return data
def get_parser(cls): """ Cf from rest_framework.parsers import JSONParser """ if ROA_FORMAT == 'json': return JSONParser() elif ROA_FORMAT == 'xml': return XMLParser() elif ROAException == 'yaml': return YAMLParser() else: raise NotImplementedError
def validate(cls, data): """ Validate transaction data :param data: json, dict, xml :return: dict with validation result and errors """ if cls.is_valid_json(data): data = json.loads(data) if cls.is_valid_xml(data): data = XMLParser().parse(data) serializer_data = TransactionSerializer(data=data) return { 'is_valid': serializer_data.is_valid(), 'errors': serializer_data.errors }
def post(self, request): _data = StringIO.StringIO(request.FILES["config"].read()) if _data.getvalue().startswith("<configuration>"): # old value _tree = etree.fromstring(_data.getvalue()) # @UndefinedVariable new_tree = E.root() for _config in _tree.findall(".//config"): c_el = interpret_xml("list-item", _config, {}) mapping = {"config": c_el.findtext("name")} for targ_list in [ "mon_check_command", "config_bool", "config_str", "config_int", "config_blob", "config_script" ]: c_el.append(getattr(E, "{}_set".format(targ_list))()) new_tree.append(c_el) for sub_el in _config.xpath( ".//config_str|.//config_int|.//config_bool|.//config_blob|.//config_script|.//mon_check_command", smart_strings=False): if "type" in sub_el.attrib: t_list = c_el.find("config_{}_set".format( sub_el.get("type"))) else: t_list = c_el.find("{}_set".format(sub_el.tag)) if sub_el.tag == "config_script": sub_el.attrib["description"] = "config script" t_list.append(interpret_xml("list-item", sub_el, mapping)) _data = StringIO.StringIO( etree.tostring(new_tree, pretty_print=False)) # @UndefinedVariable # print etree.tostring(new_tree, pretty_print=True) # sys.exit(-1) # print etree.tostring(_tree, pretty_print=True) try: conf_list = XMLParser().parse(_data) except: logger.error("cannot interpret upload file: {}".format( process_tools.get_except_info())) else: # store in local cache # get list of uploads _upload_list = cache.get("ICSW_UPLOAD_LIST", []) new_key = "ICSW_UPLOAD_{:d}".format(int(time.time())) store_cached_upload({"upload_key": new_key, "list": conf_list}) _upload_list.append(new_key) cache.set("ICSW_UPLOAD_LIST", _upload_list, None) return HttpResponse(json.dumps("done"), content_type="application/json")
def home_list(request): """ List all code home, or create a new home. """ if request.method == 'POST': data = XMLParser().parse(request) serializer = homeSerializer(data=data) if serializer.is_valid(): serializer.save() return XMLResponse(serializer.data, status=201) return XMLResponse(serializer.errors, status=400) if request.GET.get('from_id'): # retrieve objects from the value to last django (doesn't work correct!) all_houses = Home.objects.all() test = all_houses.count() min_house = int(request.GET['from_id']) roznica = test - min_house table = all_houses[:roznica] serializer = homeSerializer(table, many=True) return XMLResponse(serializer.data) if not request.GET.get('from_id'): data = serializers.serialize("xml", Myhouse.objects.all()) return HttpResponse(data, content_type='application/xml')
def test_complex_data_parse(self): parser = XMLParser() data = parser.parse(self._complex_data_input) self.assertEqual(data, self._complex_data)
def load_xml(stream): """ Parses the incoming bytestream as XML and returns the resulting data. """ return XMLParser().parse(ContentFile(stream))