Fix indents

This commit is contained in:
Benjamin Renard 2018-12-12 17:23:27 +01:00
parent 41a2dde02c
commit 664a8bc0e3
2 changed files with 122 additions and 124 deletions

View file

@ -5,38 +5,38 @@
# #
# Mapping configuration # Mapping configuration
# { # {
# '[dst key 1]': { # Key name in the result # '[dst key 1]': { # Key name in the result
# #
# 'order': [int], # Processing order between destinations keys # 'order': [int], # Processing order between destinations keys
# #
# # Source values # # Source values
# 'other_key': [key], # Other key of the destination to use as source of values # 'other_key': [key], # Other key of the destination to use as source of values
# 'key' : '[src key]', # Key of source hash to get source values # 'key' : '[src key]', # Key of source hash to get source values
# 'keys' : ['[sk1]', '[sk2]', ...], # List of source hash's keys to get source values # 'keys' : ['[sk1]', '[sk2]', ...], # List of source hash's keys to get source values
# #
# # Clean / convert values # # Clean / convert values
# 'cleanRegex': '[regex]', # Regex that be use to remove unwanted characters. Ex : [^0-9+] # 'cleanRegex': '[regex]', # Regex that be use to remove unwanted characters. Ex : [^0-9+]
# 'convert': [function], # Function to use to convert value : Original value will be passed # 'convert': [function], # Function to use to convert value : Original value will be passed
# # as argument and the value retrieve will replace source value in # # as argument and the value retrieve will replace source value in
# # the result # # the result
# # Ex : # # Ex :
# # lambda x: x.strip() # # lambda x: x.strip()
# # lambda x: "myformat : %s" % x # # lambda x: "myformat : %s" % x
# # Deduplicate / check values # # Deduplicate / check values
# 'deduplicate': [bool], # If True, sources values will be depluplicated # 'deduplicate': [bool], # If True, sources values will be depluplicated
# 'check': [function], # Function to use to check source value : Source value will be passed # 'check': [function], # Function to use to check source value : Source value will be passed
# # as argument and if function return True, the value will be preserved # # as argument and if function return True, the value will be preserved
# # Ex : # # Ex :
# # lambda x: x in my_global_hash # # lambda x: x in my_global_hash
# # Join values # # Join values
# 'join': '[glue]', # If present, sources values will be join using the "glue" # 'join': '[glue]', # If present, sources values will be join using the "glue"
# #
# # Alternative mapping # # Alternative mapping
# 'or': { [map configuration] } # If this mapping case does not retreive any value, try to get value(s) # 'or': { [map configuration] } # If this mapping case does not retreive any value, try to get value(s)
# # with this other mapping configuration # # with this other mapping configuration
# }, # },
# '[dst key 2]': { # '[dst key 2]': {
# [...] # [...]
# } # }
# } # }
# #
@ -50,88 +50,88 @@
import logging, re import logging, re
def clean_value(value): def clean_value(value):
if isinstance(value, int): if isinstance(value, int):
value=str(value) value=str(value)
return value.encode('utf8') return value.encode('utf8')
def map(map_keys,src,dst={}): def map(map_keys,src,dst={}):
def get_values(dst_key,src,m): def get_values(dst_key,src,m):
# Extract sources values # Extract sources values
values=[] values=[]
if 'other_key' in m: if 'other_key' in m:
if m['other_key'] in dst: if m['other_key'] in dst:
values=dst[m['other_key']] values=dst[m['other_key']]
if 'key' in m: if 'key' in m:
if m['key'] in src and src[m['key']]!='': if m['key'] in src and src[m['key']]!='':
values.append(clean_value(src[m['key']])) values.append(clean_value(src[m['key']]))
if 'keys' in m: if 'keys' in m:
for key in m['keys']: for key in m['keys']:
if key in src and src[key]!='': if key in src and src[key]!='':
values.append(clean_value(src[key])) values.append(clean_value(src[key]))
# Clean and convert values # Clean and convert values
if 'cleanRegex' in m and len(values)>0: if 'cleanRegex' in m and len(values)>0:
new_values=[] new_values=[]
for v in values: for v in values:
nv=re.sub(m['cleanRegex'],'',v) nv=re.sub(m['cleanRegex'],'',v)
if nv!='': if nv!='':
new_values.append(nv) new_values.append(nv)
values=new_values values=new_values
if 'convert' in m and len(values)>0: if 'convert' in m and len(values)>0:
new_values=[] new_values=[]
for v in values: for v in values:
nv=m['convert'](v) nv=m['convert'](v)
if nv!='': if nv!='':
new_values.append(nv) new_values.append(nv)
values=new_values values=new_values
# Deduplicate values # Deduplicate values
if m.get('deduplicate') and len(values)>1: if m.get('deduplicate') and len(values)>1:
new_values=[] new_values=[]
for v in values: for v in values:
if v not in new_values: if v not in new_values:
new_values.append(v) new_values.append(v)
values=new_values values=new_values
# Check values # Check values
if 'check' in m and len(values)>0: if 'check' in m and len(values)>0:
new_values=[] new_values=[]
for v in values: for v in values:
if m['check'](v): if m['check'](v):
new_values.append(v) new_values.append(v)
else: else:
logging.debug('Invalid value %s for key %s' % (v,dst_key)) logging.debug('Invalid value %s for key %s' % (v,dst_key))
if dst_key not in invalid_values: if dst_key not in invalid_values:
invalid_values[dst_key]=[] invalid_values[dst_key]=[]
if v not in invalid_values[dst_key]: if v not in invalid_values[dst_key]:
invalid_values[dst_key].append(v) invalid_values[dst_key].append(v)
values=new_values values=new_values
# Join values # Join values
if 'join' in m and len(values)>1: if 'join' in m and len(values)>1:
values=[m['join'].join(values)] values=[m['join'].join(values)]
# Manage alternative mapping case # Manage alternative mapping case
if len(values)==0 and 'or' in m: if len(values)==0 and 'or' in m:
values=get_values(dst_key,src,m['or']) values=get_values(dst_key,src,m['or'])
return values return values
for dst_key in sorted(map_keys.keys(), key=lambda x: map_keys[x]['order']): for dst_key in sorted(map_keys.keys(), key=lambda x: map_keys[x]['order']):
values=get_values(dst_key,src,map_keys[dst_key]) values=get_values(dst_key,src,map_keys[dst_key])
if len(values)==0: if len(values)==0:
if 'required' in map_keys[dst_key] and map_keys[dst_key]['required']: if 'required' in map_keys[dst_key] and map_keys[dst_key]['required']:
logging.debug('Destination key %s could not be filled from source but is required' % dst_key) logging.debug('Destination key %s could not be filled from source but is required' % dst_key)
return False return False
continue continue
dst[dst_key]=values dst[dst_key]=values
return dst return dst
if __name__ == '__main__': if __name__ == '__main__':

View file

@ -2,43 +2,43 @@ import datetime, re, time, logging
week_days=['lundi', 'mardi', 'mercredi', 'jeudi', 'vendredi', 'samedi', 'dimanche'] week_days=['lundi', 'mardi', 'mercredi', 'jeudi', 'vendredi', 'samedi', 'dimanche']
def easter_date(year): def easter_date(year):
a=year//100 a=year//100
b=year%100 b=year%100
c=(3*(a+25))//4 c=(3*(a+25))//4
d=(3*(a+25))%4 d=(3*(a+25))%4
e=(8*(a+11))//25 e=(8*(a+11))//25
f=(5*a+b)%19 f=(5*a+b)%19
g=(19*f+c-e)%30 g=(19*f+c-e)%30
h=(f+11*g)//319 h=(f+11*g)//319
j=(60*(5-d)+b)//4 j=(60*(5-d)+b)//4
k=(60*(5-d)+b)%4 k=(60*(5-d)+b)%4
m=(2*j-k-g+h)%7 m=(2*j-k-g+h)%7
n=(g-h+m+114)//31 n=(g-h+m+114)//31
p=(g-h+m+114)%31 p=(g-h+m+114)%31
day=p+1 day=p+1
month=n month=n
return datetime.date(year, month, day) return datetime.date(year, month, day)
def nonworking_french_public_days_of_the_year(year=None): def nonworking_french_public_days_of_the_year(year=None):
if year is None: if year is None:
year=datetime.date.today().year year=datetime.date.today().year
dp=easter_date(year) dp=easter_date(year)
return { return {
'1janvier': datetime.date(year, 1, 1), '1janvier': datetime.date(year, 1, 1),
'paques': dp, 'paques': dp,
'lundi_paques': (dp+datetime.timedelta(1)), 'lundi_paques': (dp+datetime.timedelta(1)),
'1mai': datetime.date(year, 5, 1), '1mai': datetime.date(year, 5, 1),
'8mai': datetime.date(year, 5, 8), '8mai': datetime.date(year, 5, 8),
'jeudi_ascension': (dp+datetime.timedelta(39)), 'jeudi_ascension': (dp+datetime.timedelta(39)),
'pentecote': (dp+datetime.timedelta(49)), 'pentecote': (dp+datetime.timedelta(49)),
'lundi_pentecote': (dp+datetime.timedelta(50)), 'lundi_pentecote': (dp+datetime.timedelta(50)),
'14juillet': datetime.date(year, 7, 14), '14juillet': datetime.date(year, 7, 14),
'15aout': datetime.date(year, 8, 15), '15aout': datetime.date(year, 8, 15),
'1novembre': datetime.date(year, 11, 1), '1novembre': datetime.date(year, 11, 1),
'11novembre': datetime.date(year, 11, 11), '11novembre': datetime.date(year, 11, 11),
'noel': datetime.date(year, 12, 25), 'noel': datetime.date(year, 12, 25),
'saint_etienne': datetime.date(year, 12, 26), 'saint_etienne': datetime.date(year, 12, 26),
} }
def parse_exceptional_closures(values): def parse_exceptional_closures(values):
exceptional_closures=[] exceptional_closures=[]
@ -205,5 +205,3 @@ def is_closed(normal_opening_hours_values=[],exceptional_closures_values=[],nonw
# Not a nonworking day, not during exceptional closure and no normal opening hours defined => Opened # Not a nonworking day, not during exceptional closure and no normal opening hours defined => Opened
return {'closed': False, 'exceptional_closure': False, 'exceptional_closure_all_day': False} return {'closed': False, 'exceptional_closure': False, 'exceptional_closure_all_day': False}