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. """ from invenio.base.utils import try_to_eval from invenio.modules.jsonalchemy.parser import get_producer_rules from invenio.modules.jsonalchemy.registry import functions 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.additional_info['namespace']): 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, functions( self.additional_info.namespace ), 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 for indexing purposes. :param fields: list of fields to include in the output, if None or empty list all available tags will be included. """ from invenio.base.utils import try_to_eval from invenio.modules.jsonalchemy.parser import get_producer_rules from invenio.modules.jsonalchemy.registry import functions out = self.dumps(without_meta_metadata=True, with_calculated_fields=True, keywords=fields) for field, values in iteritems(out): if field.startswith('__'): continue json_id = self.meta_metadata[field]['json_id'] tmp_dict = dict() if not isinstance(values, (list, tuple)): values = (values, ) for value in values: try: for rule in get_producer_rules( json_id, 'json_for_indexer', self.additional_info['namespace']): # FIXME add support of indexer names. # indexer_names = rule[0] if isinstance(rule[0], tuple) \ # else (rule[0], ) # if indexer_names and not any( # [m == cfg['INDEXER_ENGINE']) # for m in indexer_names]) # # Not match, continue to next rule # continue for subfield, value_or_function in iteritems(rule[1]): try: # Evaluate only non keyword values. if value_or_function in __builtins__: raise ImportError tmp_dict[subfield] = try_to_eval( value_or_function, functions( self.additional_info.namespace ), 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: if value is None: value = tmp_dict elif isinstance(value, dict): value.update(tmp_dict) else: raise RuntimeError("Invalid field structure.") 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 record in JSON-LD format. @param tags: list of tags to include in the output, if None or empty list all available tags will be included. """ from invenio.base.utils import try_to_eval from invenio.modules.jsonalchemy.parser import get_producer_rules from invenio.modules.jsonalchemy.registry import functions 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: rules = get_producer_rules(json_id, 'json_for_ld', 'recordext') for rule in rules: tags = rule[0] if isinstance(rule[0], tuple) \ else (rule[0], ) if tags and not any([ tag in tags for tag in self.meta_metadata[field]['function']]): continue tmp_dict = {} for key, subfield in rule[1].items(): if not subfield: tmp_dict[key] = value else: try: tmp_dict[key] = value[subfield] except: try: tmp_dict[key] = try_to_eval( subfield, functions( self.additional_info.namespace ), 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: for k, v in tmp_dict.items(): if isinstance(v, list): if k not in out: out[k] = [] for element in v: out[k].append(element) else: out[k] = v except KeyError as e: self.continuable_errors.append( "Producer CError - Unable to produce '%s'" " (No rule found).\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. """ from invenio.base.utils import try_to_eval from invenio.modules.jsonalchemy.parser import get_producer_rules from invenio.modules.jsonalchemy.registry import functions 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.additional_info['namespace']): 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, functions( self.additional_info.namespace), 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 record in JSON-LD format. @param tags: list of tags to include in the output, if None or empty list all available tags will be included. """ from invenio.base.utils import try_to_eval from invenio.modules.jsonalchemy.parser import get_producer_rules from invenio.modules.jsonalchemy.registry import functions 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: rules = get_producer_rules(json_id, 'json_for_ld', 'recordext') for rule in rules: tags = rule[0] if isinstance(rule[0], tuple) \ else (rule[0], ) if tags and not any([ tag in tags for tag in self.meta_metadata[field]['function'] ]): continue tmp_dict = {} for key, subfield in rule[1].items(): if not subfield: tmp_dict[key] = value else: try: tmp_dict[key] = value[subfield] except: try: tmp_dict[key] = try_to_eval( subfield, functions( self.additional_info.namespace), 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: for k, v in tmp_dict.items(): if isinstance(v, list): if k not in out: out[k] = [] for element in v: out[k].append(element) else: out[k] = v except KeyError as e: self.continuable_errors.append( "Producer CError - Unable to produce '%s'" " (No rule found).\n %s" % (field, str(e))) return out