[addresses] Using YAML inheritance instead of baking it into the config parser
This commit is contained in:
@@ -10,6 +10,7 @@
|
|||||||
# country overrides section. Each country can create its own copy of the entire top-level
|
# country overrides section. Each country can create its own copy of the entire top-level
|
||||||
# structure and it will be recursively merged with the defaults.
|
# structure and it will be recursively merged with the defaults.
|
||||||
|
|
||||||
|
default: &default
|
||||||
# Number
|
# Number
|
||||||
# ======
|
# ======
|
||||||
# Number, No., #, etc. can be used in both floor and apartment numbers,
|
# Number, No., #, etc. can be used in both floor and apartment numbers,
|
||||||
@@ -1175,6 +1176,7 @@ units:
|
|||||||
countries:
|
countries:
|
||||||
# United States
|
# United States
|
||||||
us:
|
us:
|
||||||
|
<<: *default
|
||||||
levels:
|
levels:
|
||||||
storey: &story
|
storey: &story
|
||||||
canonical: story
|
canonical: story
|
||||||
@@ -1261,6 +1263,7 @@ countries:
|
|||||||
# Canada
|
# Canada
|
||||||
# Specifically Canadian English. If the address is in French it will use fr.yaml
|
# Specifically Canadian English. If the address is in French it will use fr.yaml
|
||||||
ca:
|
ca:
|
||||||
|
<<: *default
|
||||||
levels:
|
levels:
|
||||||
# Note: Canadian English uses "storey" keeping with the British convention, so no need to change that
|
# Note: Canadian English uses "storey" keeping with the British convention, so no need to change that
|
||||||
|
|
||||||
@@ -1288,6 +1291,7 @@ countries:
|
|||||||
combined_probability: 0.1
|
combined_probability: 0.1
|
||||||
# Australia
|
# Australia
|
||||||
au:
|
au:
|
||||||
|
<<: *default
|
||||||
po_boxes: &australia_po_boxes
|
po_boxes: &australia_po_boxes
|
||||||
alphanumeric:
|
alphanumeric:
|
||||||
default: *po_box
|
default: *po_box
|
||||||
@@ -1330,6 +1334,7 @@ countries:
|
|||||||
|
|
||||||
# New Zealand - same rules as Australia
|
# New Zealand - same rules as Australia
|
||||||
nz:
|
nz:
|
||||||
|
<<: *default
|
||||||
po_boxes: *australia_po_boxes
|
po_boxes: *australia_po_boxes
|
||||||
units: *australia_unit_types
|
units: *australia_unit_types
|
||||||
|
|
||||||
|
|||||||
@@ -7,9 +7,9 @@ import yaml
|
|||||||
from collections import Mapping
|
from collections import Mapping
|
||||||
|
|
||||||
from geodata.address_expansions.address_dictionaries import address_phrase_dictionaries
|
from geodata.address_expansions.address_dictionaries import address_phrase_dictionaries
|
||||||
|
from geodata.configs.utils import nested_get, DoesNotExist, recursive_merge
|
||||||
from geodata.math.sampling import cdf, check_probability_distribution
|
from geodata.math.sampling import cdf, check_probability_distribution
|
||||||
|
|
||||||
|
|
||||||
this_dir = os.path.realpath(os.path.dirname(__file__))
|
this_dir = os.path.realpath(os.path.dirname(__file__))
|
||||||
|
|
||||||
ADDRESS_CONFIG_DIR = os.path.join(this_dir, os.pardir, os.pardir, os.pardir,
|
ADDRESS_CONFIG_DIR = os.path.join(this_dir, os.pardir, os.pardir, os.pardir,
|
||||||
@@ -19,35 +19,6 @@ DICTIONARIES_DIR = os.path.join(this_dir, os.pardir, os.pardir, os.pardir,
|
|||||||
'resources', 'dictionaries')
|
'resources', 'dictionaries')
|
||||||
|
|
||||||
|
|
||||||
def recursive_merge(a, b):
|
|
||||||
for k, v in six.iteritems(b):
|
|
||||||
if isinstance(v, Mapping):
|
|
||||||
existing = a.get(k, v)
|
|
||||||
merged = recursive_merge(existing, v)
|
|
||||||
a[k] = merged
|
|
||||||
else:
|
|
||||||
a[k] = b[k]
|
|
||||||
return a
|
|
||||||
|
|
||||||
|
|
||||||
class DoesNotExist:
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
def nested_get(obj, keys):
|
|
||||||
if len(keys) == 0:
|
|
||||||
return obj
|
|
||||||
try:
|
|
||||||
for key in keys[:-1]:
|
|
||||||
obj = obj.get(key, {})
|
|
||||||
if not hasattr(obj, 'items'):
|
|
||||||
return DoesNotExist
|
|
||||||
key = keys[-1]
|
|
||||||
return obj.get(key, DoesNotExist)
|
|
||||||
except AttributeError:
|
|
||||||
return DoesNotExist
|
|
||||||
|
|
||||||
|
|
||||||
class AddressConfig(object):
|
class AddressConfig(object):
|
||||||
def __init__(self, config_dir=ADDRESS_CONFIG_DIR, dictionaries_dir=DICTIONARIES_DIR):
|
def __init__(self, config_dir=ADDRESS_CONFIG_DIR, dictionaries_dir=DICTIONARIES_DIR):
|
||||||
self.address_configs = {}
|
self.address_configs = {}
|
||||||
@@ -58,17 +29,14 @@ class AddressConfig(object):
|
|||||||
continue
|
continue
|
||||||
|
|
||||||
config = yaml.load(open(os.path.join(ADDRESS_CONFIG_DIR, filename)))
|
config = yaml.load(open(os.path.join(ADDRESS_CONFIG_DIR, filename)))
|
||||||
|
default = config['default']
|
||||||
countries = config.pop('countries', {})
|
countries = config.pop('countries', {})
|
||||||
|
|
||||||
for k in countries.keys():
|
if countries:
|
||||||
country_config = countries[k]
|
default['countries'] = countries
|
||||||
config_copy = copy.deepcopy(config)
|
|
||||||
countries[k] = recursive_merge(config_copy, country_config)
|
|
||||||
|
|
||||||
config['countries'] = countries
|
|
||||||
|
|
||||||
lang = filename.strip('.yaml')
|
lang = filename.strip('.yaml')
|
||||||
self.address_configs[lang] = config
|
self.address_configs[lang] = default
|
||||||
|
|
||||||
self.sample_phrases = {}
|
self.sample_phrases = {}
|
||||||
|
|
||||||
@@ -87,6 +55,7 @@ class AddressConfig(object):
|
|||||||
if country_config:
|
if country_config:
|
||||||
config = country_config
|
config = country_config
|
||||||
|
|
||||||
|
|
||||||
value = nested_get(config, keys)
|
value = nested_get(config, keys)
|
||||||
if value is not DoesNotExist:
|
if value is not DoesNotExist:
|
||||||
return value
|
return value
|
||||||
|
|||||||
Reference in New Issue
Block a user