[formatting] Constants for field names, a few options in format_address
This commit is contained in:
@@ -28,12 +28,6 @@ class AddressFormatter(object):
|
|||||||
address_formatter.format_address('es', components)
|
address_formatter.format_address('es', components)
|
||||||
'''
|
'''
|
||||||
|
|
||||||
MINIMAL_COMPONENT_KEYS = [
|
|
||||||
('road', 'house_number'),
|
|
||||||
('road', 'house'),
|
|
||||||
('road', 'postcode')
|
|
||||||
]
|
|
||||||
|
|
||||||
whitespace_component_regex = re.compile('[\r\n]+[\s\r\n]*')
|
whitespace_component_regex = re.compile('[\r\n]+[\s\r\n]*')
|
||||||
|
|
||||||
splitter = ' | '
|
splitter = ' | '
|
||||||
@@ -41,37 +35,54 @@ class AddressFormatter(object):
|
|||||||
separator_tag = 'SEP'
|
separator_tag = 'SEP'
|
||||||
field_separator_tag = 'FSEP'
|
field_separator_tag = 'FSEP'
|
||||||
|
|
||||||
|
HOUSE = 'house'
|
||||||
|
HOUSE_NUMBER = 'house_number'
|
||||||
|
ROAD = 'road'
|
||||||
|
SUBURB = 'suburb'
|
||||||
|
CITY = 'city'
|
||||||
|
STATE = 'state'
|
||||||
|
POSTCODE = 'postcode'
|
||||||
|
COUNTRY = 'country'
|
||||||
|
|
||||||
aliases = OrderedDict([
|
aliases = OrderedDict([
|
||||||
('name', 'house'),
|
('name', HOUSE),
|
||||||
('addr:housename', 'house'),
|
('addr:housename', HOUSE),
|
||||||
('addr:housenumber', 'house_number'),
|
('addr:housenumber', HOUSE_NUMBER),
|
||||||
('addr:house_number', 'house_number'),
|
('addr:house_number', HOUSE_NUMBER),
|
||||||
('addr:street', 'road'),
|
('addr:street', ROAD),
|
||||||
('addr:city', 'city'),
|
('addr:city', CITY),
|
||||||
('addr:locality', 'city'),
|
('addr:locality', CITY),
|
||||||
('addr:municipality', 'city'),
|
('addr:municipality', CITY),
|
||||||
('addr:hamlet', 'village'),
|
('addr:hamlet', CITY),
|
||||||
('addr:suburb', 'suburb'),
|
('addr:suburb', SUBURB),
|
||||||
('addr:neighbourhood', 'suburb'),
|
('addr:neighbourhood', SUBURB),
|
||||||
('addr:neighborhood', 'suburb'),
|
('addr:neighborhood', SUBURB),
|
||||||
('addr:district', 'suburb'),
|
('addr:district', SUBURB),
|
||||||
('addr:state', 'state'),
|
('addr:state', STATE),
|
||||||
('addr:province', 'state'),
|
('addr:province', STATE),
|
||||||
('addr:region', 'state'),
|
('addr:region', STATE),
|
||||||
('addr:postal_code', 'postcode'),
|
('addr:postal_code', POSTCODE),
|
||||||
('addr:postcode', 'postcode'),
|
('addr:postcode', POSTCODE),
|
||||||
('addr:country', 'country'),
|
('addr:country', COUNTRY),
|
||||||
('street', 'road'),
|
('street', ROAD),
|
||||||
('street_name', 'road'),
|
('street_name', ROAD),
|
||||||
('residential', 'road'),
|
('residential', ROAD),
|
||||||
('hamlet', 'village'),
|
('hamlet', CITY),
|
||||||
('neighborhood', 'suburb'),
|
('neighborhood', SUBURB),
|
||||||
('neighbourhood', 'suburb'),
|
('neighbourhood', SUBURB),
|
||||||
('city_district', 'suburb'),
|
('city_district', SUBURB),
|
||||||
('state_code', 'state'),
|
('state_code', STATE),
|
||||||
('country_name', 'country'),
|
('country_name', COUNTRY),
|
||||||
|
('postal_code', POSTCODE),
|
||||||
|
('post_code', POSTCODE),
|
||||||
])
|
])
|
||||||
|
|
||||||
|
MINIMAL_COMPONENT_KEYS = [
|
||||||
|
(ROAD, HOUSE_NUMBER),
|
||||||
|
(ROAD, HOUSE),
|
||||||
|
(ROAD, POSTCODE)
|
||||||
|
]
|
||||||
|
|
||||||
def __init__(self, scratch_dir='/tmp', splitter=None):
|
def __init__(self, scratch_dir='/tmp', splitter=None):
|
||||||
if splitter is not None:
|
if splitter is not None:
|
||||||
self.splitter = splitter
|
self.splitter = splitter
|
||||||
@@ -213,20 +224,16 @@ class AddressFormatter(object):
|
|||||||
|
|
||||||
return u' '.join(tokens[start:end])
|
return u' '.join(tokens[start:end])
|
||||||
|
|
||||||
def format_address(self, country, components, minimal_only=True, tag_components=True):
|
def format_address(self, country, components, minimal_only=True, tag_components=True, replace_aliases=True):
|
||||||
template = self.config.get(country.upper())
|
template = self.config.get(country.upper())
|
||||||
if not template:
|
if not template:
|
||||||
return None
|
return None
|
||||||
template_text = template['address_template']
|
template_text = template['address_template']
|
||||||
self.replace_aliases(components)
|
if replace_aliases:
|
||||||
|
self.replace_aliases(components)
|
||||||
|
|
||||||
if not self.minimal_components(components):
|
if minimal_only and not self.minimal_components(components):
|
||||||
if minimal_only:
|
return None
|
||||||
return None
|
|
||||||
if 'fallback_template' in template:
|
|
||||||
template_text = template['fallback_template']
|
|
||||||
else:
|
|
||||||
template_text = self.config['default']['fallback_template']
|
|
||||||
|
|
||||||
self.apply_replacements(template, components)
|
self.apply_replacements(template, components)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user