Skip to content

Instantly share code, notes, and snippets.

@em230418
Created January 17, 2020 09:18
Show Gist options
  • Save em230418/087f45c196e85c289964d4032fab12eb to your computer and use it in GitHub Desktop.
Save em230418/087f45c196e85c289964d4032fab12eb to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
from pprint import pprint
from time import sleep
import xml.etree.ElementTree as ET
def write_header(f):
f.write('''<?xml version="1.0" encoding="UTF-8"?>
<odoo>
''')
def write_footer(f):
f.write("</odoo>\n")
def convert_fias_id_to_record_id(fias_id):
return "fias_" + fias_id.replace('-', '_')
def write_record(f, model, record_id, vals, refs=None):
# print("record", record_id)
if not refs:
refs = {}
f.write(' <record id="{}" model="{}">\n'.format(record_id, model))
for k, v in vals.items():
f.write(' <field name="{}">{}</field>\n'.format(k, v))
for k, v in refs.items():
f.write(' <field name="{}" ref="{}" />\n'.format(k, v))
f.write(' </record>\n\n')
def make_vals(attrib, extra_vals=None):
data = {
'name': attrib['OFFNAME'],
'fias_aoguid': attrib['AOGUID'],
'fias_okato': attrib['OKATO'],
}
if 'OKTMO' in attrib:
data['fias_oktmo'] = attrib['OKTMO']
if extra_vals:
data.update(extra_vals)
return data
def write_region(f, attrib):
print('region', attrib['OFFNAME'])
write_record(f, "res.country.state", convert_fias_id_to_record_id(attrib['AOGUID']), make_vals(attrib, {
"code": attrib['REGIONCODE'],
}), {
"country_id": "base.ru",
})
def write_city(f, attrib):
print('city', attrib['OFFNAME'])
write_record(f, "res.city", convert_fias_id_to_record_id(attrib['AOGUID']), make_vals(attrib), {
"country_id": "base.ru",
})
def write_state_district(f, attrib):
print('state_disctrict', attrib)
write_record(f, "res.state.district", convert_fias_id_to_record_id(attrib['AOGUID']), make_vals(attrib), {
'state_id': convert_fias_id_to_record_id(attrib['PARENTGUID']),
})
if __name__ == '__main__':
gen = ET.iterparse("AS_ADDROBJ_20191226_9985b7cf-05ef-4113-8b40-1dcf76ce6363.XML")
in_region = False
cities = open("res.city.xml", "w")
region_f = open("res.country.state.xml", "w")
state_districts = open("res.state.district.xml", "w")
fs = (cities, region_f,state_districts)
for f in fs:
write_header(f)
for event, el in gen:
# сбрасываем неактуальные записи
if el.attrib['CURRSTATUS'] != '0':
el.clear()
continue
if el.attrib['AOLEVEL'] == '1':
if in_region:
break
in_region = el.attrib['OFFNAME'] == 'Самарская'
if in_region:
pprint(el.attrib)
write_region(region_f, el.attrib)
print(el.attrib['OFFNAME'])
#pprint(el.attrib)
is_state_district = el.attrib['AOLEVEL'] == '3'
is_city = el.attrib['AOLEVEL'] in ('4', '6')
if in_region:
if is_city:
write_city(cities, el.attrib)
elif is_state_district:
write_state_district(state_districts, el.attrib)
# write_region(region_f, el.attrib)
pass
el.clear()
for f in fs:
write_footer(f)
f.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment