def __init__(self, source, target=None, time_util=None, skip_missing=True, default=None, *args, **kwargs): self.target = target if target is not None else {} self.map_type = kwargs.get('msg_type', MSG_MAPPER.DICT_TO_DICT) self.skip_ns = kwargs.get('skip_ns', True) self.time_util = time_util self.skip_missing = skip_missing self.default = default self.subs = {} self.funcs = { 'int': int, 'long': long, 'bool': asbool, 'dec': Decimal, 'arrow': arrow_get } self.func_keys = self.funcs.keys() self.times = {} self.cache = {} if isinstance(source, DictNav): self.source = source else: if self.map_type.startswith('dict-to-'): self.source = DictNav(source)
def __init__(self, source, target=None, time_util=None, *args, **kwargs): self.target = target if target is not None else {} self.map_type = kwargs.get('msg_type', MSG_MAPPER.DICT_TO_DICT) self.skip_ns = kwargs.get('skip_ns', True) self.time_util = time_util self.subs = {} self.funcs = {'int':int, 'bool':asbool} self.func_keys = self.funcs.keys() self.times = {} self.cache = {} if isinstance(source, DictNav): self.source = source else: if self.map_type.startswith('dict-to-'): self.source = DictNav(source)
def __init__(self, source, target=None, time_util=None, skip_missing=True, default=None, *args, **kwargs): self.target = target if target is not None else {} self.map_type = kwargs.get('msg_type', MSG_MAPPER.DICT_TO_DICT) self.skip_ns = kwargs.get('skip_ns', True) self.time_util = time_util self.skip_missing = skip_missing self.default = default self.subs = {} self.funcs = {'int':int, 'long':long, 'bool':asbool, 'dec':Decimal, 'arrow':arrow_get} self.func_keys = self.funcs.keys() self.times = {} self.cache = {} if isinstance(source, DictNav): self.source = source else: if self.map_type.startswith('dict-to-'): self.source = DictNav(source)
class Mapper(object): def __init__(self, source, target=None, time_util=None, skip_missing=True, default=None, *args, **kwargs): self.target = target if target is not None else {} self.map_type = kwargs.get('msg_type', MSG_MAPPER.DICT_TO_DICT) self.skip_ns = kwargs.get('skip_ns', True) self.time_util = time_util self.skip_missing = skip_missing self.default = default self.subs = {} self.funcs = { 'int': int, 'long': long, 'bool': asbool, 'dec': Decimal, 'arrow': arrow_get } self.func_keys = self.funcs.keys() self.times = {} self.cache = {} if isinstance(source, DictNav): self.source = source else: if self.map_type.startswith('dict-to-'): self.source = DictNav(source) def set_time(self, name, format): self.times[name] = format def set_substitution(self, name, value): self.subs[name] = value def set_func(self, name, func): self.funcs[name] = func self.func_keys = self.funcs.keys() def map(self, from_, to, separator='/', skip_missing=ZATO_NOT_GIVEN, default=ZATO_NOT_GIVEN): """ Maps 'from_' into 'to', splitting from using the 'separator' and applying transformation functions along the way. """ if skip_missing == ZATO_NOT_GIVEN: skip_missing = self.skip_missing if default == ZATO_NOT_GIVEN: default = self.default # Store for later use, such as in log entries. orig_from = from_ force_func = None force_func_name = None needs_time_reformat = False from_format, to_format = None, None # Perform any string substitutions first. if self.subs: from_.format(**self.subs) to.format(**self.subs) # Pick at most one processing functions. for key in self.func_keys: if from_.startswith(key): from_ = from_.replace('{}:'.format(key), '', 1) force_func = self.funcs[key] force_func_name = key break # Perhaps it's a date value that needs to be converted. if from_.startswith('time:'): needs_time_reformat = True from_format, from_ = self._get_time_format(from_) to_format, to_ = self._get_time_format(to) # Obtain the value. value = self.source.get(from_.split(separator)[1:]) if needs_time_reformat: value = self.time_util.reformat(value, from_format, to_format) # Don't return anything if we are to skip missing values # or, we aren't, return a default value. if not value: if skip_missing: return else: value = default if default != ZATO_NOT_GIVEN else value # We have some value, let's process it using the function found above. if force_func: try: value = force_func(value) except Exception, e: logger.warn( 'Error in force_func:`%s` `%s` over `%s` in `%s` -> `%s` e:`%s`', force_func_name, force_func, value, orig_from, to, format_exc(e)) raise dpath_util.new(self.target, to, value)
class Mapper(object): def __init__(self, source, target=None, time_util=None, skip_missing=True, default=None, *args, **kwargs): self.target = target if target is not None else {} self.map_type = kwargs.get('msg_type', MSG_MAPPER.DICT_TO_DICT) self.skip_ns = kwargs.get('skip_ns', True) self.time_util = time_util self.skip_missing = skip_missing self.default = default self.subs = {} self.funcs = {'int':int, 'long':long, 'bool':asbool, 'dec':Decimal, 'arrow':arrow_get} self.func_keys = self.funcs.keys() self.times = {} self.cache = {} if isinstance(source, DictNav): self.source = source else: if self.map_type.startswith('dict-to-'): self.source = DictNav(source) def set_time(self, name, format): self.times[name] = format def set_substitution(self, name, value): self.subs[name] = value def set_func(self, name, func): self.funcs[name] = func self.func_keys = self.funcs.keys() def map(self, from_, to, separator='/', skip_missing=ZATO_NOT_GIVEN, default=ZATO_NOT_GIVEN): """ Maps 'from_' into 'to', splitting from using the 'separator' and applying transformation functions along the way. """ if skip_missing == ZATO_NOT_GIVEN: skip_missing = self.skip_missing if default == ZATO_NOT_GIVEN: default = self.default # Store for later use, such as in log entries. orig_from = from_ force_func = None force_func_name = None needs_time_reformat = False from_format, to_format = None, None # Perform any string substitutions first. if self.subs: from_.format(**self.subs) to.format(**self.subs) # Pick at most one processing functions. for key in self.func_keys: if from_.startswith(key): from_ = from_.replace('{}:'.format(key), '', 1) force_func = self.funcs[key] force_func_name = key break # Perhaps it's a date value that needs to be converted. if from_.startswith('time:'): needs_time_reformat = True from_format, from_ = self._get_time_format(from_) to_format, to_ = self._get_time_format(to) # Obtain the value. value = self.source.get(from_.split(separator)[1:]) if needs_time_reformat: value = self.time_util.reformat(value, from_format, to_format) # Don't return anything if we are to skip missing values # or, we aren't, return a default value. if not value: if skip_missing: return else: value = default if default != ZATO_NOT_GIVEN else value # We have some value, let's process it using the function found above. if force_func: try: value = force_func(value) except Exception, e: logger.warn('Error in force_func:`%s` `%s` over `%s` in `%s` -> `%s` e:`%s`', force_func_name, force_func, value, orig_from, to, format_exc(e)) raise dpath_util.new(self.target, to, value)