Exemplo n.º 1
0
 def _parse_statement(self, tokens):
     # Handle: param, func call or literal
     if not tokens:
         raise ExpressionSyntaxError('Unexpected end of expression')
     if tokens[0] == '[':
         return self._parse_list(tokens)
     elif tokens[0] in self.tokens \
     or tokens[0] in ('or', 'and', 'not'):
         raise ExpressionSyntaxError('Unexpected token: "%s"' % tokens[0])
     elif self._param_re.match(tokens[0]) \
     and not tokens[0] in ('True', 'False', 'None'):
         param = ExpressionParameter(tokens.pop(0))
         if tokens and tokens[0] == '(':
             args = self._parse_list(tokens)
             return ExpressionFunctionCall(param, args)
         else:
             return param
     else:
         text = tokens.pop(0)
         try:
             value = ast.literal_eval(text)
         except SyntaxError:
             raise ExpressionSyntaxError('Invalid literal: %s' % text)
         else:
             return ExpressionLiteral(value)
Exemplo n.º 2
0
 def get(name):
     param = ExpressionParameter(name)
     return param(self.context)
Exemplo n.º 3
0
    def runTest(self):
        def get(name):
            param = ExpressionParameter(name)
            return param(self.context)

        # Test context setup
        self.assertIsInstance(get('generator.name'), basestring)
        self.assertTrue(get('generator.name').startswith('Zim'))
        self.assertIsInstance(get('generator.user'), basestring)

        self.assertEqual(get('title'), 'Test Export')

        pages = list(get('pages'))
        self.assertEqual(len(pages), 1)
        self.assertTrue(all(isinstance(o, PageProxy) for o in pages))
        self.assertEqual(pages[0]._page, self.content[0])
        ## TODO
        # pages
        #   content
        #   special
        #
        # navigation	- links to other export pages (if not included here)
        #	home
        #	up
        # 	prev			-- prev export file or None
        # 	next			-- next export file or None
        #
        # links			-- links to other export pages (index & plugins / ...) - sorted dict to have Index, Home first followed by plugins
        #
        #	link
        #		.name
        #		.basename

        # Test PageProxy
        self.context['mypage'] = pages[0]
        self.assertEqual(get('mypage.title'), 'Foo')
        self.assertEqual(get('mypage.name'), 'Test:foo')
        self.assertEqual(get('mypage.namespace'), 'Test')
        self.assertEqual(get('mypage.basename'), 'foo')

        self.assertEqual(get('mypage.heading'), 'Foo')
        self.assertIsInstance(get('mypage.content'), basestring)
        self.assertIsInstance(get('mypage.body'), basestring)
        self.assertIsInstance(get('mypage.meta'), dict)

        #			.links
        #			.backlinks
        #			.attachments
        #

        # Test HeadingsProxy
        mycall = ExpressionFunctionCall(
            ExpressionParameter('mypage.headings'),
            ExpressionList(),
        )
        headings = list(mycall(self.context))
        self.assertEqual(len(headings), 2)

        self.context['h1'] = headings[0]
        self.context['h2'] = headings[1]
        self.assertEqual(get('h1.level'), 1)
        self.assertEqual(get('h2.level'), 2)
        self.assertIsInstance(get('h1.heading'), basestring)
        self.assertIsInstance(get('h1.body'), basestring)
        self.assertIsInstance(get('h1.content'), basestring)