def test_get_producer_rules(self): """BibField - check producer rules""" self.assertTrue( get_producer_rules('authors[0]', 'json_for_marc')[0] in get_producer_rules('authors', 'json_for_marc')) self.assertTrue( len(get_producer_rules('keywords', 'json_for_marc')) == 1) self.assertRaises(KeyError, lambda: get_producer_rules('foo', 'json_for_marc'))
def produce_json_for_marc(self, fields=None): """ Export the record in marc format. @param tags: list of tags to include in the output, if None or empty list all available tags will be included. """ from invenio.bibfield_config_engine import get_producer_rules if not fields: fields = self.keys() out = [] for field in fields: if field.startswith('__'): continue try: marc_rules = get_producer_rules(field, 'json_for_marc') for rule in marc_rules: field = self.get(rule[0], None) if field is None: continue if not isinstance(field, list): field = [ field, ] for f in field: for r in rule[1]: tmp_dict = {} #FIXME: check field meta_metadata for key, subfield in r[1].iteritems(): if not subfield: tmp_dict[key] = f else: try: tmp_dict[key] = f[subfield] except: try: tmp_dict[key] = self._try_to_eval( subfield, value=f) except Exception as e: self[ '__error_messages.cerror[n]'] = 'Producer CError - Unable to produce %s - %s' % ( field, str(e)) if tmp_dict: out.append(tmp_dict) except KeyError: self[ '__error_messages.cerror[n]'] = 'Producer CError - No producer rule for field %s' % field return out
def produce_json_for_marc(self, fields=None): """ Export the record in marc format. @param tags: list of tags to include in the output, if None or empty list all available tags will be included. """ from invenio.bibfield_config_engine import get_producer_rules if not fields: fields = self.keys() out = [] for field in fields: if field.startswith('__'): continue try: marc_rules = get_producer_rules(field, 'json_for_marc') for rule in marc_rules: field = self.get(rule[0], None) if field is None: continue if not isinstance(field, list): field = [field, ] for f in field: for r in rule[1]: tmp_dict = {} #FIXME: check field meta_metadata for key, subfield in r[1].iteritems(): if not subfield: tmp_dict[key] = f else: try: tmp_dict[key] = f[subfield] except: try: tmp_dict[key] = self._try_to_eval(subfield, value=f) except Exception as e: self['__error_messages.cerror[n]'] = 'Producer CError - Unable to produce %s - %s' % (field, str(e)) if tmp_dict: out.append(tmp_dict) except KeyError: self['__error_messages.cerror[n]'] = 'Producer CError - No producer rule for field %s' % field return out
def test_get_producer_rules(self): """BibField - check producer rules""" self.assertTrue(get_producer_rules('authors[0]', 'json_for_marc')[0] in get_producer_rules('authors', 'json_for_marc')) self.assertTrue(len(get_producer_rules('keywords', 'json_for_marc')) == 1) self.assertRaises(KeyError, lambda: get_producer_rules('foo', 'json_for_marc'))