def _bf_meta_list_to_dict(self, m_list, par, par_vc): d = {} unparseable_m = None for m_el in m_list: try: n = m_el.get('@property') except: if unparseable_m is None: unparseable_m = [] unparseable_m.append(m_el) else: if n is None: n = m_el.get('@rel') if n is None: if unparseable_m is None: unparseable_m = [] unparseable_m.append(m_el) else: _add_value_to_dict_bf(d, n, m_el) if unparseable_m: for m_el in unparseable_m: self._error_event(par_vc.curr_element_type, obj=par, err_type=gen_UnparseableMetaWarning, anc=par_vc.anc_list, obj_nex_id=None, obj_list=unparseable_m) return d
def _bf_meta_list_to_dict(self, m_list, par, par_vc): d = {} unparseable_m = None for m_el in m_list: try: n = m_el.get('@property') except: if unparseable_m is None: unparseable_m = [] unparseable_m.append(m_el) else: if n is None: n = m_el.get('@rel') if n is None: if unparseable_m is None: unparseable_m = [] unparseable_m.append(m_el) else: _add_value_to_dict_bf(d, n, m_el) if unparseable_m: for m_el in unparseable_m: self._error_event(par_vc.curr_element_type, obj=par, err_type=gen_UnparseableMetaWarning, anc=par_vc.anc_list, obj_nex_id=None, obj_list=unparseable_m) return d
def _err_warn_summary(w): d = {} for el in w: msg_adapt_inst = el[0] r = msg_adapt_inst.as_dict(el) key = r['@code'] del r['@code'] _add_value_to_dict_bf(d, key, r) return d
def _err_warn_summary(w): d = {} for el in w: msg_adapt_inst = el[0] r = msg_adapt_inst.as_dict(el) key = r['@code'] del r['@code'] _add_value_to_dict_bf(d, key, r) return d
def add_resource_meta(obj, rel, href, version): if _is_badgerfish_version(version): m = obj.setdefault('meta', []) if not isinstance(m, list): m = [m] obj['meta'] = m m.append({'@href': href, '@rel': rel, '@xsi:type': 'nex:ResourceMeta'}) else: k = '^' + rel _add_value_to_dict_bf(obj, k, {'@href': href})
def add_resource_meta(obj, rel, href, version): if _is_badgerfish_version(version): m = obj.setdefault('meta', []) if not isinstance(m, list): m = [m] obj['meta'] = m m.append({'@href':href, '@rel': rel, '@xsi:type': 'nex:ResourceMeta'}) else: k = '^' + rel _add_value_to_dict_bf(obj, k, {'@href': href})
def _recursive_convert_dict(self, obj): _cull_redundant_about(obj) # rule 10... meta_list = _get_index_list_of_values(obj, 'meta') to_inject = {} for meta in meta_list: xt = meta['@xsi:type'] if _RESOURCE_META_PAT.match(xt): mk, mv = self._transform_resource_meta(meta) else: assert _LITERAL_META_PAT.match(xt) mk, mv = self._transform_literal_meta(meta) _add_value_to_dict_bf(to_inject, mk, mv) if ('meta' in obj) and self.remove_old_structs: del obj['meta'] for k, v in to_inject.items(): _add_value_to_dict_bf(obj, k, v) for k, v in obj.items(): if isinstance(v, dict): self._recursive_convert_dict(v) elif isinstance(v, list): self._recursive_convert_list(v)
def _recursive_convert_dict(self, obj): _cull_redundant_about(obj) # rule 10... meta_list = _get_index_list_of_values(obj, 'meta') to_inject = {} for meta in meta_list: xt = meta['@xsi:type'] if _RESOURCE_META_PAT.match(xt): mk, mv = self._transform_resource_meta(meta) else: assert _LITERAL_META_PAT.match(xt) mk, mv = self._transform_literal_meta(meta) _add_value_to_dict_bf(to_inject, mk, mv) if ('meta' in obj) and self.remove_old_structs: del obj['meta'] for k, v in to_inject.items(): _add_value_to_dict_bf(obj, k, v) for k, v in obj.items(): if isinstance(v, dict): self._recursive_convert_dict(v) elif isinstance(v, list): self._recursive_convert_list(v)
def _partition_keys_for_xml(self, o): '''Breaks o into four content type by key syntax: attrib keys (start with '@'), text (value associated with the '$' or None), child element keys (all others) meta element ''' ak = {} tk = None ck = {} mc = {} #_LOG.debug('o = {o}'.format(o=o)) for k, v in o.items(): if k.startswith('@'): if k == '@xmlns': if '$' in v: ak['xmlns'] = v['$'] for nsk, nsv in v.items(): if nsk != '$': ak['xmlns:' + nsk] = nsv else: s = k[1:] if type(v) == bool: if v == True: v = u'true' else: v = u'false' ak[s] = UNICODE(v) elif k == '$': tk = v elif k.startswith('^') and (not self._migrating_from_bf): s = k[1:] val = _convert_hbf_meta_val_for_xml(s, v) _add_value_to_dict_bf(mc, s, val) elif (k == u'meta') and self._migrating_from_bf: s, val = _convert_bf_meta_val_for_xml(v) _add_value_to_dict_bf(mc, s, val) else: ck[k] = v return ak, tk, ck, mc
def _hbf_handle_child_elements(self, obj, ntl): ''' Indirect recursion through _gen_hbf_el ''' # accumulate a list of the children names in ko, and # the a dictionary of tag to xml elements. # repetition of a tag means that it will map to a list of # xml elements cd = {} ko = [] ks = set() for child in ntl: k = child.nodeName if k == 'meta' and (not self._badgerfish_style_conversion): matk, matv = self._transform_meta_key_value(child) if matk is not None: _add_value_to_dict_bf(obj, matk, matv) else: if k not in ks: ko.append(k) ks.add(k) _add_value_to_dict_bf(cd, k, child) # Converts the child XML elements to dicts by recursion and # adds these to the dict. for k in ko: v = _index_list_of_values(cd, k) dcl = [] ct = None for xc in v: ct, dc = self._gen_hbf_el(xc) dcl.append(dc) # this assertion will trip is the hacky stripping of namespaces # results in a name clash among the tags of the children assert ct not in obj obj[ct] = dcl # delete redundant about attributes that are used in XML, but not JSON (last rule of HoneyBadgerFish) _cull_redundant_about(obj) return obj
def _hbf_handle_child_elements(self, obj, ntl): """ Indirect recursion through _gen_hbf_el """ # accumulate a list of the children names in ko, and # the a dictionary of tag to xml elements. # repetition of a tag means that it will map to a list of # xml elements cd = {} ko = [] ks = set() for child in ntl: k = child.nodeName if k == 'meta' and (not self._badgerfish_style_conversion): matk, matv = self._transform_meta_key_value(child) if matk is not None: _add_value_to_dict_bf(obj, matk, matv) else: if k not in ks: ko.append(k) ks.add(k) _add_value_to_dict_bf(cd, k, child) # Converts the child XML elements to dicts by recursion and # adds these to the dict. for k in ko: v = _index_list_of_values(cd, k) dcl = [] ct = None for xc in v: ct, dc = self._gen_hbf_el(xc) dcl.append(dc) # this assertion will trip is the hacky stripping of namespaces # results in a name clash among the tags of the children assert ct not in obj obj[ct] = dcl # delete redundant about attributes that are used in XML, but not JSON (last rule of HoneyBadgerFish) _cull_redundant_about(obj) return obj
def _partition_keys_for_xml(self, o): '''Breaks o into four content type by key syntax: attrib keys (start with '@'), text (value associated with the '$' or None), child element keys (all others) meta element ''' ak = {} tk = None ck = {} mc = {} #_LOG.debug('o = {o}'.format(o=o)) for k, v in o.items(): if k.startswith('@'): if k == '@xmlns': if '$' in v: ak['xmlns'] = v['$'] for nsk, nsv in v.items(): if nsk != '$': ak['xmlns:' + nsk] = nsv else: s = k[1:] if type(v) == bool: if v == True: v = u'true' else: v = u'false' ak[s] = UNICODE(v) elif k == '$': tk = v elif k.startswith('^') and (not self._migrating_from_bf): s = k[1:] val = _convert_hbf_meta_val_for_xml(s, v) _add_value_to_dict_bf(mc, s, val) elif (k == u'meta') and self._migrating_from_bf: s, val = _convert_bf_meta_val_for_xml(v) _add_value_to_dict_bf(mc, s, val) else: ck[k] = v return ak, tk, ck, mc