Ejemplo n.º 1
0
 def __create_json_extra(self, rule):
     from invenio.bibfield_utils import CFG_BIBFIELD_FUNCTIONS
     json_id = rule.json_id[0]
     if rule.json_ext:
         self.__class__._field_definitions[json_id]['json_ext'] = \
                 {'loads': try_to_eval(rule.json_ext.loads.strip(), CFG_BIBFIELD_FUNCTIONS),
                  'dumps': try_to_eval(rule.json_ext.dumps.strip(), CFG_BIBFIELD_FUNCTIONS)}
Ejemplo n.º 2
0
 def __create_json_extra(self, rule):
     from invenio.bibfield_utils import CFG_BIBFIELD_FUNCTIONS
     json_id = rule.json_id[0]
     if rule.json_ext:
         self.__class__._field_definitions[json_id]['json_ext'] = \
                 {'loads': try_to_eval(rule.json_ext.loads.strip(), CFG_BIBFIELD_FUNCTIONS),
                  'dumps': try_to_eval(rule.json_ext.dumps.strip(), CFG_BIBFIELD_FUNCTIONS)}
Ejemplo n.º 3
0
    def _apply_rules(self, json_id, field_name, rule_def):
        """Tries to apply a 'creator' rule"""
        applied = False
        for rule in rule_def['rules'].get(
                self.json['__meta_metadata__']['__additional_info__']['master_format'], []):
            elements = self._get_elements_from_blob(rule['source_tag'])
            if not elements:
                self._set_default_value(json_id, field_name)
                return False
            if not self._evaluate_decorators(rule):
                return False
            if 'entire_record' in rule['source_tag'] or '*' in rule['source_tag']:
                try:
                    value = try_to_eval(rule['value'], self.functions, value=elements, self=self.json)
                    self._remove_none_values(value)
                    info = self._find_meta_metadata(json_id, field_name, 'creator', rule, rule_def)
                    if 'json_ext' in rule_def:
                        value = rule_def['json_ext']['dumps'](value)
                    self.json.set(field_name, value, extend=True)
                    self.json['__meta_metadata__.%s' % (SmartDict.main_key_pattern.sub('', field_name), )] = info
                    applied = True
                except Exception as e:
                    self.json['__meta_metadata__']['__errors__']\
                            .append('Rule Error - Unable to apply rule for field %s - %s' % (field_name, str(e)),)
                    applied = False

            else:
                for element in elements:
                    if not isinstance(element, (list, tuple)):
                        element = (element, )
                    applied = False
                    for e in element:
                        if rule['only_if_master_value'] and \
                           not all(try_to_eval(rule['only_if_master_value'], self.functions, value=e, self=self.json)):
                            applied = applied or False
                        else:
                            try:
                                value = try_to_eval(rule['value'], self.functions, value=e, self=self.json)
                                self._remove_none_values(value)
                                info = self._find_meta_metadata(json_id, field_name, 'creator', rule, rule_def)
                                if 'json_ext' in rule_def:
                                    value = rule_def['json_ext']['dumps'](value)
                                self.json.set(field_name, value, extend=True)
                                self.json['__meta_metadata__.%s' % (SmartDict.main_key_pattern.sub('', field_name), )] = info
                                applied = applied or True
                            except Exception as e:
                                self.json['__meta_metadata__']['__errors__']\
                                        .append('Rule Error - Unable to apply rule for field %s - %s' % (field_name, str(e)),)
                                applied = applied or False

        if field_name not in self.json or not applied:
            self._set_default_value(json_id, field_name)

        return applied
Ejemplo n.º 4
0
 def _evaluate_decorators(self, rule):
     """Evaluates all 'decorators' related with the current rule"""
     if rule['parse_first']:
         map(self._unpack_rule, try_to_eval(rule['parse_first']))
     if rule['depends_on']:
         for key in try_to_eval(rule['depends_on']):
             if key in self.json:
                 continue
             main_key = SmartDict.main_key_pattern.sub('', key)
             if not self._unpack_rule(main_key):
                 return False
     if rule['only_if'] and not all(try_to_eval(rule['only_if'], self.functions, self=self.json)):
         return False
     return True
Ejemplo n.º 5
0
 def _evaluate_decorators(self, rule):
     """Evaluates all 'decorators' related with the current rule"""
     if rule['parse_first']:
         map(self._unpack_rule, try_to_eval(rule['parse_first']))
     if rule['depends_on']:
         for key in try_to_eval(rule['depends_on']):
             if key in self.json:
                 continue
             main_key = SmartDict.main_key_pattern.sub('', key)
             if not self._unpack_rule(main_key):
                 return False
     if rule['only_if'] and not all(
             try_to_eval(rule['only_if'], self.functions, self=self.json)):
         return False
     return True
Ejemplo n.º 6
0
    def _apply_virtual_rules(self, json_id, field_name, rule_def):
        """Tries to apply either a 'derived' or 'calculated' rule"""
        rules = []
        rules.append(('calculated', rule_def['rules'].get('calculated', [])))
        rules.append(('derived', rule_def['rules'].get('derived', [])))
        for (rule_type, rrules) in rules:
            for rule in rrules:
                if not self._evaluate_decorators(rule):
                    return False
                try:
                    info = self._find_meta_metadata(json_id, field_name, rule_type, rule, rule_def)
                    if rule_type == 'derived' or rule['memoize']:
                        value = try_to_eval(rule['value'], self.functions, self=self.json)
                        if 'json_ext' in rule_def:
                            value = rule_def['json_ext']['dumps'](value)
                        self._remove_none_values(value)
                    else:
                        value = None
                    self.json.set(field_name, value, extend=True)
                    self.json['__meta_metadata__.%s' % (SmartDict.main_key_pattern.sub('', field_name), )] = info
                except Exception as e:
                    self.json['__meta_metadata__']['__continuable_errors__']\
                            .append('Virtual Rule CError - Unable to evaluate %s - %s' % (field_name, str(e)))
                    return False

        if field_name not in self.json:
            self._set_default_value(json_id, field_name)

        return True
Ejemplo n.º 7
0
    def _apply_virtual_rules(self, json_id, field_name, rule_def):
        """Tries to apply either a 'derived' or 'calculated' rule"""
        rules = []
        rules.append(('calculated', rule_def['rules'].get('calculated', [])))
        rules.append(('derived', rule_def['rules'].get('derived', [])))
        for (rule_type, rrules) in rules:
            for rule in rrules:
                if not self._evaluate_decorators(rule):
                    return False
                try:
                    info = self._find_meta_metadata(json_id, field_name,
                                                    rule_type, rule, rule_def)
                    if rule_type == 'derived' or rule['memoize']:
                        value = try_to_eval(rule['value'],
                                            self.functions,
                                            self=self.json)
                        if 'json_ext' in rule_def:
                            value = rule_def['json_ext']['dumps'](value)
                        self._remove_none_values(value)
                    else:
                        value = None
                    self.json.set(field_name, value, extend=True)
                    self.json['__meta_metadata__.%s' %
                              (SmartDict.main_key_pattern.sub(
                                  '', field_name), )] = info
                except Exception as e:
                    self.json['__meta_metadata__']['__continuable_errors__']\
                            .append('Virtual Rule CError - Unable to evaluate %s - %s' % (field_name, str(e)))
                    return False

        if field_name not in self.json:
            self._set_default_value(json_id, field_name)

        return True
Ejemplo n.º 8
0
    def _apply_rules(self, json_id, field_name, rule_def):
        """Tries to apply a 'creator' rule"""
        applied = False
        for rule in rule_def['rules'].get(
                self.json['__meta_metadata__']['__additional_info__']
            ['master_format'], []):
            elements = self._get_elements_from_blob(rule['source_tag'])
            if not elements:
                self._set_default_value(json_id, field_name)
                return False
            if not self._evaluate_decorators(rule):
                return False
            if 'entire_record' in rule['source_tag'] or '*' in rule[
                    'source_tag']:
                try:
                    value = try_to_eval(rule['value'],
                                        self.functions,
                                        value=elements,
                                        self=self.json)
                    self._remove_none_values(value)
                    info = self._find_meta_metadata(json_id, field_name,
                                                    'creator', rule, rule_def)
                    if 'json_ext' in rule_def:
                        value = rule_def['json_ext']['dumps'](value)
                    self.json.set(field_name, value, extend=True)
                    self.json['__meta_metadata__.%s' %
                              (SmartDict.main_key_pattern.sub(
                                  '', field_name), )] = info
                    applied = True
                except Exception as e:
                    self.json['__meta_metadata__']['__errors__']\
                            .append('Rule Error - Unable to apply rule for field %s - %s' % (field_name, str(e)),)
                    applied = False

            else:
                for element in elements:
                    if not isinstance(element, (list, tuple)):
                        element = (element, )
                    applied = False
                    for e in element:
                        if rule['only_if_master_value'] and \
                           not all(try_to_eval(rule['only_if_master_value'], self.functions, value=e, self=self.json)):
                            applied = applied or False
                        else:
                            try:
                                value = try_to_eval(rule['value'],
                                                    self.functions,
                                                    value=e,
                                                    self=self.json)
                                self._remove_none_values(value)
                                info = self._find_meta_metadata(
                                    json_id, field_name, 'creator', rule,
                                    rule_def)
                                if 'json_ext' in rule_def:
                                    value = rule_def['json_ext']['dumps'](
                                        value)
                                self.json.set(field_name, value, extend=True)
                                self.json['__meta_metadata__.%s' %
                                          (SmartDict.main_key_pattern.sub(
                                              '', field_name), )] = info
                                applied = applied or True
                            except Exception as e:
                                self.json['__meta_metadata__']['__errors__']\
                                        .append('Rule Error - Unable to apply rule for field %s - %s' % (field_name, str(e)),)
                                applied = applied or False

        if field_name not in self.json or not applied:
            self._set_default_value(json_id, field_name)

        return applied