def convert_nexson_format(blob, out_nexson_format, current_format=None, remove_old_structs=True, pristine_if_invalid=False, sort_arbitrary=False): '''Take a dict form of NexSON and converts its datastructures to those needed to serialize as out_nexson_format. If current_format is not specified, it will be inferred. If `remove_old_structs` is False and different honeybadgerfish varieties are selected, the `blob` will be 'fat" containing both types of lookup structures. If pristine_if_invalid is False, then the object may be corrupted if it is an invalid nexson struct. Setting this to False can result in faster translation, but if an exception is raised the object may be polluted with partially constructed fields for the out_nexson_format. ''' if not current_format: current_format = detect_nexson_version(blob) out_nexson_format = resolve_nexson_format(out_nexson_format) if current_format == out_nexson_format: if sort_arbitrary: sort_arbitrarily_ordered_nexson(blob) return blob two2zero = _is_by_id_hbf(out_nexson_format) and _is_badgerfish_version(current_format) zero2two = _is_by_id_hbf(current_format) and _is_badgerfish_version(out_nexson_format) if two2zero or zero2two: # go from 0.0 -> 1.0 then the 1.0->1.2 should succeed without nexml... blob = convert_nexson_format(blob, DIRECT_HONEY_BADGERFISH, current_format=current_format, remove_old_structs=remove_old_structs, pristine_if_invalid=pristine_if_invalid) current_format = DIRECT_HONEY_BADGERFISH ccdict = {'output_format':out_nexson_format, 'input_format':current_format, 'remove_old_structs': remove_old_structs, 'pristine_if_invalid': pristine_if_invalid} ccfg = ConversionConfig(ccdict) if _is_badgerfish_version(current_format): converter = Badgerfish2DirectNexson(ccfg) elif _is_badgerfish_version(out_nexson_format): assert _is_direct_hbf(current_format) converter = Direct2BadgerfishNexson(ccfg) elif _is_direct_hbf(current_format) and (out_nexson_format == BY_ID_HONEY_BADGERFISH): converter = Direct2OptimalNexson(ccfg) elif _is_direct_hbf(out_nexson_format) and (current_format == BY_ID_HONEY_BADGERFISH): converter = Optimal2DirectNexson(ccfg) else: raise NotImplementedError('Conversion from {i} to {o}'.format(i=current_format, o=out_nexson_format)) blob = converter.convert(blob) if sort_arbitrary: sort_arbitrarily_ordered_nexson(blob) return blob
def create_validation_adaptor(obj, logger, **kwargs): try: nexson_version = detect_nexson_version(obj) except: return BadgerFishValidationAdaptor(obj, logger, **kwargs) if _is_by_id_hbf(nexson_version): # _LOG.debug('validating as ById...') return ByIdHBFValidationAdaptor(obj, logger, **kwargs) elif _is_badgerfish_version(nexson_version): # _LOG.debug('validating as BadgerFish...') return BadgerFishValidationAdaptor(obj, logger, **kwargs) elif _is_direct_hbf(nexson_version): # _LOG.debug('validating as DirectHBF...') return DirectHBFValidationAdaptor(obj, logger, **kwargs) raise NotImplementedError('nexml2json version {v}'.format(v=nexson_version))
def add_schema_attributes(container, nexson_version): """Adds several attributes to `container`: _using_hbf_meta - boolean. True for HoneyBadgerFish v1-style meta elements ('^prop': value rather than 'meta': {'$':value}) and the following _SchemaFragment instances: _NexmlEl_Schema """ if _is_by_id_hbf(nexson_version): _add_by_id_nexson_schema_attributes(container) elif _is_badgerfish_version(nexson_version): _add_badgerfish_nexson_schema_attributes(container) elif _is_direct_hbf(nexson_version): _add_direct_nexson_schema_attributes(container) else: raise NotImplementedError('unrecognized nexson variant {}'.format(nexson_version))
def _nexson_directly_translatable_to_nexml(vers): 'TEMP: until we refactor nexml writing code to be more general...' return (_is_badgerfish_version(vers) or _is_direct_hbf(vers) or vers == 'nexml')