Files
tryton/scripts/countries.py
T

55 lines
2.1 KiB
Python
Raw Normal View History

2008-10-22 20:20:29 +02:00
#!/usr/bin/env python
2014-12-02 12:55:15 +01:00
# This file is part of Tryton. The COPYRIGHT file at the top level of
# this repository contains the full copyright notices and license terms.
2008-10-22 20:20:29 +02:00
import sys
2008-10-22 20:20:29 +02:00
import pycountry
2018-06-26 19:11:47 +02:00
sys.stdout.write('<?xml version="1.0"?>\n')
sys.stdout.write('<tryton>\n')
sys.stdout.write(' <data skiptest="1" grouped="1">\n')
2017-04-23 13:25:02 +02:00
2008-10-22 20:20:29 +02:00
for country in pycountry.countries:
2018-06-26 19:11:47 +02:00
record = '''
2008-10-22 20:20:29 +02:00
<record model="country.country" id="%s">
<field name="name">%s</field>
<field name="code">%s</field>
2016-03-23 11:49:00 +01:00
<field name="code3">%s</field>
<field name="code_numeric">%s</field>
2017-04-23 13:25:02 +02:00
</record>\n''' % (country.alpha_2.lower(), country.name,
country.alpha_2, country.alpha_3, country.numeric)
2019-04-14 17:53:01 +02:00
sys.stdout.write(record)
2018-06-26 19:11:47 +02:00
sys.stdout.write(' </data>\n')
2008-10-22 20:20:29 +02:00
2017-04-23 13:25:02 +02:00
subdivision_codes = {s.code.lower() for s in pycountry.subdivisions}
2013-09-02 17:38:55 +02:00
existing_parents = set()
2017-04-23 13:25:02 +02:00
while existing_parents < subdivision_codes:
2018-06-26 19:11:47 +02:00
sys.stdout.write(' <data skiptest="1" grouped="1">\n')
2013-09-02 17:38:55 +02:00
new_parents = set()
for subdivision in pycountry.subdivisions:
if (subdivision.code.lower() in existing_parents
or (subdivision.parent_code
and subdivision.parent_code.lower() not in
existing_parents)):
continue
new_parents.add(subdivision.code.lower())
2018-06-26 19:11:47 +02:00
record = '''
2008-10-22 20:20:29 +02:00
<record model="country.subdivision" id="%s">
<field name="name">%s</field>
<field name="code">%s</field>
<field name="type">%s</field>\n''' % (subdivision.code.lower(),
2013-09-02 17:38:55 +02:00
subdivision.name, subdivision.code,
subdivision.type.lower())
if subdivision.parent_code:
2018-06-26 19:11:47 +02:00
record += '''\
2013-02-23 17:29:40 +01:00
<field name="parent" ref="%s"/>\n''' % \
2013-09-02 17:38:55 +02:00
subdivision.parent.code.lower()
2018-06-26 19:11:47 +02:00
record += '''\
2008-10-22 20:20:29 +02:00
<field name="country" ref="%s"/>
2017-04-23 13:25:02 +02:00
</record>\n''' % subdivision.country.alpha_2.lower()
2019-04-14 17:53:01 +02:00
sys.stdout.write(record)
2018-06-26 19:11:47 +02:00
sys.stdout.write(' </data>\n')
2013-09-02 17:38:55 +02:00
existing_parents |= new_parents
2017-04-23 13:25:02 +02:00
2018-06-26 19:11:47 +02:00
sys.stdout.write('</tryton>\n')