def test_get_producer_rules(self): """JsonAlchemy - check producer rules""" self.assertEquals( len(get_producer_rules('keywords', 'json_for_marc', self.metadata)), 1) self.assertRaises( KeyError, lambda: get_producer_rules('foo', 'json_for_marc', self.metadata))
def test_get_producer_rules(self): """JsonAlchemy - check producer rules""" self.assertEquals(len( get_producer_rules('keywords', 'json_for_marc', self.metadata) ), 1) self.assertRaises( KeyError, lambda: get_producer_rules('foo', 'json_for_marc', self.metadata))
def produce(self, fields=None): """Export the json in marc format. Produces a list of dictionaries will all the possible marc tags as keys. :param fields: list of fields to include in the output, if None or empty list all available tags will be included. """ if not fields: fields = self.keys() out = [] for field in fields: if field.startswith('__') or self.get(field) is None: continue json_id = self.meta_metadata[field]['json_id'] values = self.get(field) if not isinstance(values, (list, tuple)): values = (values, ) for value in values: try: for rule in get_producer_rules(json_id, 'json_for_marc', self.metadata): marc_tags = rule[0] if isinstance(rule[0], tuple) \ else (rule[0], ) if marc_tags and not any([ re.match(m, t) for m in marc_tags for t in self.meta_metadata[field]['function'] ]): # Not match, continue to next rule continue tmp_dict = dict() for marc_tag, subfield in iteritems(rule[1]): if len(marc_tag) == 1: marc_tag = \ self.meta_metadata[field]['function'][0] + \ marc_tag if not subfield: tmp_dict[marc_tag] = value else: try: tmp_dict[marc_tag] = value[subfield] except: try: # Evaluate only non keyword values. if subfield in __builtins__: raise ImportError tmp_dict[marc_tag] = try_to_eval( subfield, self.metadata.functions, value=value, self=self) except ImportError: pass except Exception as e: self.continuable_errors.append( "Producer CError - Unable to produce " "'%s'.\n %s" % (field, str(e))) if tmp_dict: out.append(tmp_dict) except Exception as e: self.continuable_errors.append( "Producer CError - Unable to produce '%s'.\n %s" % (field, str(e))) return out
def produce(self, fields=None): """Export the json in marc format. Produces a list of dictionaries will all the possible marc tags as keys. :param fields: list of fields to include in the output, if None or empty list all available tags will be included. """ if not fields: fields = self.keys() out = [] for field in fields: if field.startswith('__') or self.get(field) is None: continue json_id = self.meta_metadata[field]['json_id'] values = self.get(field) if not isinstance(values, (list, tuple)): values = (values, ) for value in values: try: for rule in get_producer_rules( json_id, 'json_for_marc', self.metadata): marc_tags = rule[0] if isinstance(rule[0], tuple) \ else (rule[0], ) if marc_tags and not any( [re.match(m, t) for m in marc_tags for t in self.meta_metadata[field]['function']]): # Not match, continue to next rule continue tmp_dict = dict() for marc_tag, subfield in iteritems(rule[1]): if len(marc_tag) == 1: marc_tag = \ self.meta_metadata[field]['function'][0] + \ marc_tag if not subfield: tmp_dict[marc_tag] = value else: try: tmp_dict[marc_tag] = value[subfield] except: try: # Evaluate only non keyword values. if subfield in __builtins__: raise ImportError tmp_dict[marc_tag] = try_to_eval( subfield, self.metadata.functions, value=value, self=self) except ImportError: pass except Exception as e: self.continuable_errors.append( "Producer CError - Unable to produce " "'%s'.\n %s" % (field, str(e))) if tmp_dict: out.append(tmp_dict) except Exception as e: self.continuable_errors.append( "Producer CError - Unable to produce '%s'.\n %s" % (field, str(e))) return out