[addresses] Using YAML inheritance instead of baking it into the config parser
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -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