Exemplo n.º 1
0
    def test_bibfielddict(self):
        """BibFieldDict - Unit Test"""
        d = BibFieldDict()
        d['foo'] = {'a': 'world', 'b': 'hello'}
        d['a'] = [{'b': 1}, {'b': 2}, {'b': 3}]
        d['_c'] = [1, "random.random()"]
        d['_cc'] = [1, "random.random()"]

        d['__do_not_cache'].append('_cc')
        d['__aliases']['aa'] = 'a'

        self.assertTrue(len(d.keys()) == 6)
        self.assertTrue('foo' in d)
        self.assertTrue('a.b' in d)

        self.assertEqual(d['foo'], {'a': 'world', 'b': 'hello'})
        self.assertEqual(d['a'], d.get('a'))
        self.assertEqual(d['a[-1].b'], 3)

        self.assertEqual(d['a'], d['aa'])
        self.assertEqual(d['a[1].b'], d['aa[1].b'])

        self.assertEqual(d['_c'], 1)
        self.assertNotEqual(d['_c'], d.get('_c', reset_cache=True))

        self.assertNotEqual(d['_cc'], 1)
        self.assertNotEqual(d['_cc'], d.get('_cc'))

        #Python 2.5 or higher
        #self.assertEqual('hello world!', d.get('foo', formatstring="{0[b]} {0[a]}!"))

        def dummy(s):
            return s.upper()

        self.assertEqual('HELLO', d.get('foo.b', formatfunction=dummy))
Exemplo n.º 2
0
    def _create_derived_calculated_rule(self, rule):
        """
        Creates the config_rules entries for the virtual fields
        The result is similar to the one of real fields but in this case there is
        only one rule.
        """
        json_id = rule.json_id[0]
        #Chech duplicate names
        if json_id in self.config_rules:
            raise BibFieldParserException(
                "Name error: '%s' field name already defined" %
                (rule.json_id[0], ))

        depends_on = only_if = parse_first = persistent_id = None

        aliases = []
        if rule.aliases:
            aliases = rule.aliases.asList()
        if re.search('^_[a-zA-Z0-9]', json_id):
            aliases.append(json_id[1:])

        do_not_cache = False
        if rule.depends_on:
            depends_on = rule.depends_on[0]
        if rule.only_if:
            only_if = rule.only_if[0]
        if rule.parse_first:
            parse_first = rule.parse_first[0]
        if rule.do_not_cache:
            do_not_cache = True
        if rule.persistent_identifier:
            persistent_id = int(rule.persistent_identifier[0][0])

        self.config_rules[json_id] = {
            'rules': {},
            'checker': [],
            'documentation': BibFieldDict(),
            'aliases': aliases,
            'type': rule.type_field[0],
            'persistent_identifier': persistent_id,
            'overwrite': False
        }

        self.config_rules[json_id]['rules'] = {
            'value': rule.value[0],
            'parse_first': parse_first,
            'depends_on': depends_on,
            'only_if': only_if,
            'do_not_cache': do_not_cache
        }

        self._create_checkers(rule)
        self._create_documentation(rule)
Exemplo n.º 3
0
    def test_bibfielddict(self):
        """BibFieldDict - Unit Test"""
        d = BibFieldDict()
        d["foo"] = {"a": "world", "b": "hello"}
        d["a"] = [{"b": 1}, {"b": 2}, {"b": 3}]
        d["_c"] = [1, "random.random()"]
        d["_cc"] = [1, "random.random()"]

        d["__do_not_cache"].append("_cc")
        d["__aliases"]["aa"] = "a"

        self.assertTrue(len(d.keys()) == 6)
        self.assertTrue("foo" in d)
        self.assertTrue("a.b" in d)

        self.assertEqual(d["foo"], {"a": "world", "b": "hello"})
        self.assertEqual(d["a"], d.get("a"))
        self.assertEqual(d["a[-1].b"], 3)

        self.assertEqual(d["a"], d["aa"])
        self.assertEqual(d["a[1].b"], d["aa[1].b"])

        self.assertEqual(d["_c"], 1)
        self.assertNotEqual(d["_c"], d.get("_c", reset_cache=True))

        self.assertNotEqual(d["_cc"], 1)
        self.assertNotEqual(d["_cc"], d.get("_cc"))

        # Python 2.5 or higher
        # self.assertEqual('hello world!', d.get('foo', formatstring="{0[b]} {0[a]}!"))

        def dummy(s):
            return s.upper()

        self.assertEqual("HELLO", d.get("foo.b", formatfunction=dummy))
    def _create_documentation(self, rule):
        """
        Creates the documentation dictionary for the given rule
        """
        json_id = rule.json_id[0]
        assert json_id in self.config_rules

        if rule.documentation:
            if self.config_rules[json_id]['overwrite']:
                self.config_rules[json_id]['documentation'] = BibFieldDict()
            config_doc = self.config_rules[json_id]['documentation']
            config_doc['doc_string'] = rule.documentation.main_doc
            config_doc['subfields'] = None

            if rule.documentation.subfields:
                for subfield in rule.documentation.subfields:
                    key = "%s.%s" % ('subfields', subfield[0].replace('.', '.subfields.'))
                    config_doc[key] = {'doc_string': subfield[1],
                                       'subfields' : None}
    def test_bibfielddict(self):
        """BibFieldDict - Unit Test"""
        import random
        from invenio.bibfield_utils import BibFieldDict
        d = BibFieldDict()
        d['foo'] = {'a': 'world', 'b': 'hello'}
        d['a'] = [{'b': 1}, {'b': 2}, {'b': 3}]
        d['_c'] = 1
        d['_cc'] = random.random()

        d['__do_not_cache'].append('_cc')
        d['__calculated_functions']['_c'] = "random.random()"
        d['__calculated_functions']['_cc'] = "random.random()"
        d['__aliases']['aa'] = 'a'

        self.assertTrue(len(d.keys()) == 7)
        self.assertTrue('foo' in d)
        self.assertTrue('a.b' in d)

        self.assertEqual(d['foo'], {'a': 'world', 'b': 'hello'})
        self.assertEqual(d['a'], d.get('a'))
        self.assertEqual(d['a[-1].b'], 3)

        self.assertEqual(d['a'], d['aa'])
        self.assertEqual(d['a[1].b'], d['aa[1].b'])

        self.assertEqual(d['_c'], 1)
        self.assertNotEqual(d['_c'], d.get('_c', reset_cache=True))

        self.assertNotEqual(d['_cc'], 1)
        self.assertNotEqual(d['_cc'], d.get('_cc'))

        #Python 2.5 or higher
        #self.assertEqual('hello world!', d.get('foo', formatstring="{0[b]} {0[a]}!"))

        def dummy(s):
            return s.upper()
        self.assertEqual('HELLO', d.get('foo.b', formatfunction=dummy))
Exemplo n.º 6
0
    def _create_creator_rule(self, rule):
        """
        Creates the config_rule entries for the creator rules.
        The result looks like this:

        {'title':{'rules': { 'inherit_from'  : (inherit_from_list),
                             'source_format' : [translation_rules],
                             'legacy': (legacy_rules)}
                  'checker': [(function_name, arguments), ...]
                  'documentation' : {'doc_string' : '...',
                                     'subfields' : .....},
                  'type' : 'real'
                  'aliases' : [list_of_aliases_ids]
                 },
                 ....
        }
        """
        json_id = rule.json_id[0]

        #Workaround to keep clean doctype files
        #Just creates a dict entry with the main json field name and points it to
        #the full one i.e.: 'authors' : ['authors[0]', 'authors[n]']
        if '[0]' in json_id or '[n]' in json_id:
            main_json_id = re.sub('[\[n\] \[0\]]', '', json_id)
            if not main_json_id in self.config_rules:
                self.config_rules[main_json_id] = []
            self.config_rules[main_json_id].append(json_id)

        aliases = []
        if rule.aliases:
            aliases = rule.aliases.asList()

        persistent_id = None
        if rule.persistent_identifier:
            persistent_id = int(rule.persistent_identifier[0][0])

        inherit_from = None
        if rule.inherit_from:
            self._unresolved_inheritence.append(json_id)
            inherit_from = eval(rule.inherit_from[0])

        rules = {}
        for creator in rule.creator_def:

            source_format = creator.source_format[0]

            if source_format not in rules:
                #Allow several tags point to the same json id
                rules[source_format] = []

            legacy = ()
            if creator.legacy:
                legacy = eval(creator.legacy[0][0])

            depends_on = only_if = parse_first = None
            if creator.parse_first:
                parse_first = creator.parse_first[0]
            if rule.depends_on:
                depends_on = rule.depends_on[0]
            if rule.only_if:
                only_if = rule.only_if[0]

            rules[source_format].append({
                'source_tag':
                creator.source_tag[0].split(),
                'value':
                creator.value[0],
                'legacy':
                legacy,
                'parse_first':
                parse_first,
                'depends_on':
                depends_on,
                'only_if':
                only_if
            })

        #Chech duplicate names to overwrite configuration
        if not json_id in self.config_rules:
            self.config_rules[json_id] = {
                'inherit_from': inherit_from,
                'rules': rules,
                'checker': [],
                'documentation': BibFieldDict(),
                'type': 'real',
                'aliases': aliases,
                'persistent_identifier': persistent_id,
                'overwrite': False
            }
        else:
            self.config_rules[json_id]['overwrite'] = True
            self.config_rules[json_id]['rules'].update(rules)
            self.config_rules[json_id]['aliases'] = \
                    aliases or self.config_rules[json_id]['aliases']
            self.config_rules[json_id]['persistent_identifier'] = \
                    persistent_id or self.config_rules[json_id]['persistent_identifier']
            self.config_rules[json_id]['inherit_from'] = \
                    inherit_from or self.config_rules[json_id]['inherit_from']

        self._create_checkers(rule)
        self._create_documentation(rule)