[Checkins] SVN: z3c.pt/trunk/z3c/pt/ Change in verbiage.
Malthe Borch
mborch at gmail.com
Sun Dec 23 10:12:49 EST 2007
Log message for revision 82411:
Change in verbiage.
Changed:
D z3c.pt/trunk/z3c/pt/attributes.py
U z3c.pt/trunk/z3c/pt/clauses.py
A z3c.pt/trunk/z3c/pt/expressions.py
U z3c.pt/trunk/z3c/pt/tests/test_doctests.py
U z3c.pt/trunk/z3c/pt/translation.py
-=-
Deleted: z3c.pt/trunk/z3c/pt/attributes.py
===================================================================
--- z3c.pt/trunk/z3c/pt/attributes.py 2007-12-23 14:05:04 UTC (rev 82410)
+++ z3c.pt/trunk/z3c/pt/attributes.py 2007-12-23 15:12:48 UTC (rev 82411)
@@ -1,224 +0,0 @@
-import parser
-
-def name(string):
- return string
-
-def expression(string):
- """
- Specification:
-
- expression :: = python_expression [ |* expression ]
- python_expresion ::= a python expression string
-
- *) Using | as logical or is not supported.
-
- >>> expression("4 + 5")
- ['4 + 5']
-
- Complex expressions:
-
- >>> expression("a.non_defined_method() | 1")
- ['a.non_defined_method() ', '1']
-
- Expression with non-semantic horizontal bar.
-
- >>> expression("'|'")
- ["'|'"]
-
- Expression with non-semantic horizontal bar and semantic bar.
-
- >>> expression("a.non_defined_method() | '|'")
- ['a.non_defined_method() ', "'|'"]
-
- """
-
- string = string.replace('\n', '').strip()
-
- if not string:
- return []
-
- expressions = []
-
- i = j = 0
- while i < len(string):
- j = string.find('|', j + 1)
- if j == -1:
- j = len(string)
-
- expr = string[i:j].lstrip()
-
- try:
- # we use the ``parser`` module to determine if
- # an expression is a valid python expression
- parser.expr(expr)
- except SyntaxError, e:
- if j < len(string):
- continue
-
- raise e
-
- expressions.append(expr)
- i = j + 1
-
- return expressions
-
-def variable(string):
- """
- Specification:
-
- variables :: = variable_name [, variables]
-
- Single variable:
-
- >>> variable("variable")
- ('variable',)
-
- Multiple variables:
-
- >>> variable("variable1, variable2")
- ('variable1', 'variable2')
-
- """
-
- variables = []
- for var in string.split(', '):
- var = var.strip()
-
- if var in ('repeat',):
- raise ValueError, "Invalid variable name '%s' (reserved)." % variable
-
- if var.startswith('_'):
- raise ValueError, \
- "Invalid variable name '%s' (starts with an underscore)." % variable
- variables.append(var)
-
- return tuple(variables)
-
-def mapping(string):
- """
-
- >>> mapping("abc def")
- [('abc', 'def')]
-
- >>> mapping("abc")
- [('abc', None)]
-
- >>> mapping("abc; def ghi")
- [('abc', None), ('def', 'ghi')]
-
- """
-
- defs = string.split(';')
- mappings = []
- for d in defs:
- d = d.strip()
- while ' ' in d:
- d = d.replace(' ', ' ')
-
- parts = d.split(' ')
- if len(parts) == 1:
- mappings.append((d, None))
- elif len(parts) == 2:
- mappings.append((parts[0], parts[1]))
- else:
- raise ValueError, "Invalid mapping (%s)." % string
-
- return mappings
-
-def definitions(string):
- """
- Specification:
-
- argument ::= define_var [';' define_var]
- define_var ::= Name python_expression
-
- Single define:
-
- >>> definitions("variable expression")
- [(['variable'], ['expression'])]
-
- Multiple defines:
-
- >>> definitions("variable1 expression1; variable2 expression2")
- [(['variable1'], ['expression1']), (['variable2'], ['expression2'])]
-
- Tuple define:
-
- >>> definitions("(variable1, variable2) (expression1, expression2)")
- [(['variable1', 'variable2'], ['(expression1, expression2)'])]
-
- Use of unescaped semicolon in an expression:
-
- >>> definitions("variable ';'")
- [(['variable'], ["';'"])]
-
- A define clause that ends in a semicolon:
-
- >>> definitions("variable expression;")
- [(['variable'], ['expression'])]
-
- A define clause with a trivial expression (we do allow this):
-
- >>> definitions("variable")
- [(['variable'], None)]
-
- A proper define clause following one with a trivial expression:
-
- >>> definitions("variable1 expression; variable2")
- [(['variable1'], ['expression']), (['variable2'], None)]
-
- """
-
- string = string.replace('\n', '').strip()
-
- defines = []
-
- i = 0
- while i < len(string):
- while string[i] == ' ':
- i += 1
-
- # get variable definition
- if string[i] == '(':
- j = string.find(')', i+1)
- if j == -1:
- raise ValueError, "Invalid variable tuple definition (%s)." % string
- var = variable(string[i+1:j])
- j += 1
- else:
- j = string.find(' ', i + 1)
- if j == -1:
- var = variable(string[i:])
- j = len(string)
- else:
- var = variable(string[i:j])
-
- # get expression
- i = j
- while j < len(string):
- j = string.find(';', j+1)
- if j == -1:
- j = len(string)
-
- try:
- expr = expression(string[i:j])
- except SyntaxError, e:
- if j < len(string):
- continue
- raise e
- break
- else:
- expr = None
-
- defines.append((list(var), expr))
-
- i = j + 1
-
- return defines
-
-def definition(string):
- defs = definitions(string)
- if len(defs) != 1:
- raise ValueError, "Multiple definitions not allowed."
-
- return defs[0]
Modified: z3c.pt/trunk/z3c/pt/clauses.py
===================================================================
--- z3c.pt/trunk/z3c/pt/clauses.py 2007-12-23 14:05:04 UTC (rev 82410)
+++ z3c.pt/trunk/z3c/pt/clauses.py 2007-12-23 15:12:48 UTC (rev 82411)
@@ -1,4 +1,4 @@
-from attributes import expression
+from expressions import value
class Assign(object):
"""
@@ -6,7 +6,7 @@
Simple assignment:
- >>> assign = Assign(expression("1"))
+ >>> assign = Assign(value("1"))
>>> assign.begin(stream, 'a')
>>> exec stream.getvalue()
>>> a == 1
@@ -15,7 +15,7 @@
Try-except chain:
- >>> assign = Assign(expression("float('abc') | 1"))
+ >>> assign = Assign(value("float('abc') | 1"))
>>> assign.begin(stream, 'b')
>>> exec stream.getvalue()
>>> b == 1
@@ -24,7 +24,7 @@
Try-except chain part 2:
- >>> assign = Assign(expression("'abc' | 1"))
+ >>> assign = Assign(value("'abc' | 1"))
>>> assign.begin(stream, 'b')
>>> exec stream.getvalue()
>>> b == 'abc'
@@ -63,7 +63,7 @@
Variable scope:
- >>> define = Define("a", expression("b"))
+ >>> define = Define("a", value("b"))
>>> b = object()
>>> define.begin(stream)
>>> exec stream.getvalue()
@@ -82,8 +82,8 @@
Multiple defines:
>>> stream = CodeIO()
- >>> define1 = Define("a", expression("b"))
- >>> define2 = Define("c", expression("d"))
+ >>> define1 = Define("a", value("b"))
+ >>> define2 = Define("c", value("d"))
>>> d = object()
>>> define1.begin(stream)
>>> define2.begin(stream)
@@ -109,7 +109,7 @@
Tuple assignments:
>>> stream = CodeIO()
- >>> define = Define(['e', 'f'], expression("[1, 2]"))
+ >>> define = Define(['e', 'f'], value("[1, 2]"))
>>> define.begin(stream)
>>> exec stream.getvalue()
>>> e == 1 and f == 2
@@ -131,7 +131,7 @@
Using semicolons in expressions within a define:
>>> stream = CodeIO()
- >>> define = Define("a", expression("';'"))
+ >>> define = Define("a", value("';'"))
>>> define.begin(stream)
>>> exec stream.getvalue()
>>> a
@@ -144,7 +144,7 @@
>>> a = 1
>>> stream.scope[-1].add('a')
>>> stream.scope.append(set())
- >>> define = Define("a", expression("2"))
+ >>> define = Define("a", value("2"))
>>> define.begin(stream)
>>> define.end(stream)
>>> exec stream.getvalue()
@@ -210,8 +210,8 @@
Unlimited scope:
>>> stream = CodeIO()
- >>> true = Condition(expression("True"))
- >>> false = Condition(expression("False"))
+ >>> true = Condition(value("True"))
+ >>> false = Condition(value("False"))
>>> true.begin(stream)
>>> stream.write("print 'Hello'")
>>> true.end(stream)
@@ -228,8 +228,8 @@
>>> stream = CodeIO()
>>> from StringIO import StringIO
>>> _out = StringIO()
- >>> true = Condition(expression("True"), [Write(expression("'Hello'"))])
- >>> false = Condition(expression("False"), [Write(expression("'Hallo'"))])
+ >>> true = Condition(value("True"), [Write(value("'Hello'"))])
+ >>> false = Condition(value("False"), [Write(value("'Hallo'"))])
>>> true.begin(stream)
>>> true.end(stream)
>>> false.begin(stream)
@@ -243,8 +243,8 @@
>>> stream = CodeIO()
>>> from StringIO import StringIO
>>> _out = StringIO()
- >>> true = Condition(expression("True"), [Tag('div')], finalize=False)
- >>> false = Condition(expression("False"), [Tag('span')], finalize=False)
+ >>> true = Condition(value("True"), [Tag('div')], finalize=False)
+ >>> false = Condition(value("False"), [Tag('span')], finalize=False)
>>> true.begin(stream)
>>> stream.out("Hello World!")
>>> true.end(stream)
@@ -325,7 +325,7 @@
>>> from StringIO import StringIO
>>> _out = StringIO(); stream = CodeIO()
- >>> tag = Tag('div', dict(alt=expression(repr('Hello World!'))))
+ >>> tag = Tag('div', dict(alt=value(repr('Hello World!'))))
>>> tag.begin(stream)
>>> stream.out('Hello Universe!')
>>> tag.end(stream)
@@ -388,7 +388,7 @@
Simple repeat loop and repeat data structure:
- >>> _repeat = Repeat("i", expression("range(5)"))
+ >>> _repeat = Repeat("i", value("range(5)"))
>>> _repeat.begin(stream)
>>> stream.write("r = repeat['i']")
>>> stream.write("print (i, r.index, r.start, r.end, r.number(), r.odd(), r.even())")
@@ -404,7 +404,7 @@
def __init__(self, v, e, scope=()):
self.variable = v
- self.define = Define(v, expression("None"))
+ self.define = Define(v, value("None"))
self.assign = Assign(e)
def begin(self, stream):
@@ -440,14 +440,14 @@
>>> from StringIO import StringIO
>>> _out = StringIO()
- >>> write = Write(expression("'New York'"))
+ >>> write = Write(value("'New York'"))
>>> write.begin(stream)
>>> write.end(stream)
>>> exec stream.getvalue()
>>> _out.getvalue()
'New York'
>>> _out = StringIO()
- >>> write = Write(expression("undefined | ', New York!'"))
+ >>> write = Write(value("undefined | ', New York!'"))
>>> write.begin(stream)
>>> write.end(stream)
>>> exec stream.getvalue()
Copied: z3c.pt/trunk/z3c/pt/expressions.py (from rev 82406, z3c.pt/trunk/z3c/pt/attributes.py)
===================================================================
--- z3c.pt/trunk/z3c/pt/expressions.py (rev 0)
+++ z3c.pt/trunk/z3c/pt/expressions.py 2007-12-23 15:12:48 UTC (rev 82411)
@@ -0,0 +1,224 @@
+import parser
+
+def name(string):
+ return string
+
+def value(string):
+ """
+ Specification:
+
+ value :: = python_expression [ |* value ]
+ python_expresion ::= a python expression string
+
+ *) Using | as logical or is not supported.
+
+ >>> value("4 + 5")
+ ['4 + 5']
+
+ Complex expressions:
+
+ >>> value("a.non_defined_method() | 1")
+ ['a.non_defined_method() ', '1']
+
+ Expression with non-semantic horizontal bar.
+
+ >>> value("'|'")
+ ["'|'"]
+
+ Expression with non-semantic horizontal bar and semantic bar.
+
+ >>> value("a.non_defined_method() | '|'")
+ ['a.non_defined_method() ', "'|'"]
+
+ """
+
+ string = string.replace('\n', '').strip()
+
+ if not string:
+ return []
+
+ expressions = []
+
+ i = j = 0
+ while i < len(string):
+ j = string.find('|', j + 1)
+ if j == -1:
+ j = len(string)
+
+ expr = string[i:j].lstrip()
+
+ try:
+ # we use the ``parser`` module to determine if
+ # an expression is a valid python expression
+ parser.expr(expr)
+ except SyntaxError, e:
+ if j < len(string):
+ continue
+
+ raise e
+
+ expressions.append(expr)
+ i = j + 1
+
+ return expressions
+
+def variable(string):
+ """
+ Specification:
+
+ variables :: = variable_name [, variables]
+
+ Single variable:
+
+ >>> variable("variable")
+ ('variable',)
+
+ Multiple variables:
+
+ >>> variable("variable1, variable2")
+ ('variable1', 'variable2')
+
+ """
+
+ variables = []
+ for var in string.split(', '):
+ var = var.strip()
+
+ if var in ('repeat',):
+ raise ValueError, "Invalid variable name '%s' (reserved)." % variable
+
+ if var.startswith('_'):
+ raise ValueError, \
+ "Invalid variable name '%s' (starts with an underscore)." % variable
+ variables.append(var)
+
+ return tuple(variables)
+
+def mapping(string):
+ """
+
+ >>> mapping("abc def")
+ [('abc', 'def')]
+
+ >>> mapping("abc")
+ [('abc', None)]
+
+ >>> mapping("abc; def ghi")
+ [('abc', None), ('def', 'ghi')]
+
+ """
+
+ defs = string.split(';')
+ mappings = []
+ for d in defs:
+ d = d.strip()
+ while ' ' in d:
+ d = d.replace(' ', ' ')
+
+ parts = d.split(' ')
+ if len(parts) == 1:
+ mappings.append((d, None))
+ elif len(parts) == 2:
+ mappings.append((parts[0], parts[1]))
+ else:
+ raise ValueError, "Invalid mapping (%s)." % string
+
+ return mappings
+
+def definitions(string):
+ """
+ Specification:
+
+ argument ::= define_var [';' define_var]
+ define_var ::= Name python_expression
+
+ Single define:
+
+ >>> definitions("variable expression")
+ [(['variable'], ['expression'])]
+
+ Multiple defines:
+
+ >>> definitions("variable1 expression1; variable2 expression2")
+ [(['variable1'], ['expression1']), (['variable2'], ['expression2'])]
+
+ Tuple define:
+
+ >>> definitions("(variable1, variable2) (expression1, expression2)")
+ [(['variable1', 'variable2'], ['(expression1, expression2)'])]
+
+ Use of unescaped semicolon in an expression:
+
+ >>> definitions("variable ';'")
+ [(['variable'], ["';'"])]
+
+ A define clause that ends in a semicolon:
+
+ >>> definitions("variable expression;")
+ [(['variable'], ['expression'])]
+
+ A define clause with a trivial expression (we do allow this):
+
+ >>> definitions("variable")
+ [(['variable'], None)]
+
+ A proper define clause following one with a trivial expression:
+
+ >>> definitions("variable1 expression; variable2")
+ [(['variable1'], ['expression']), (['variable2'], None)]
+
+ """
+
+ string = string.replace('\n', '').strip()
+
+ defines = []
+
+ i = 0
+ while i < len(string):
+ while string[i] == ' ':
+ i += 1
+
+ # get variable definition
+ if string[i] == '(':
+ j = string.find(')', i+1)
+ if j == -1:
+ raise ValueError, "Invalid variable tuple definition (%s)." % string
+ var = variable(string[i+1:j])
+ j += 1
+ else:
+ j = string.find(' ', i + 1)
+ if j == -1:
+ var = variable(string[i:])
+ j = len(string)
+ else:
+ var = variable(string[i:j])
+
+ # get expression
+ i = j
+ while j < len(string):
+ j = string.find(';', j+1)
+ if j == -1:
+ j = len(string)
+
+ try:
+ expr = value(string[i:j])
+ except SyntaxError, e:
+ if j < len(string):
+ continue
+ raise e
+ break
+ else:
+ expr = None
+
+ defines.append((list(var), expr))
+
+ i = j + 1
+
+ return defines
+
+def definition(string):
+ defs = definitions(string)
+ if len(defs) != 1:
+ raise ValueError, "Multiple definitions not allowed."
+
+ return defs[0]
Modified: z3c.pt/trunk/z3c/pt/tests/test_doctests.py
===================================================================
--- z3c.pt/trunk/z3c/pt/tests/test_doctests.py 2007-12-23 14:05:04 UTC (rev 82410)
+++ z3c.pt/trunk/z3c/pt/tests/test_doctests.py 2007-12-23 15:12:48 UTC (rev 82411)
@@ -9,7 +9,7 @@
def test_suite():
filesuites = ['README.txt', 'BENCHMARKS.txt', 'translation.txt', 'i18n.txt', 'codegen.txt']
- testsuites = ['z3c.pt.translation', 'z3c.pt.attributes', 'z3c.pt.clauses']
+ testsuites = ['z3c.pt.translation', 'z3c.pt.expressions', 'z3c.pt.clauses']
return unittest.TestSuite(
[zope.testing.doctest.DocTestSuite(doctest,
Modified: z3c.pt/trunk/z3c/pt/translation.py
===================================================================
--- z3c.pt/trunk/z3c/pt/translation.py 2007-12-23 14:05:04 UTC (rev 82410)
+++ z3c.pt/trunk/z3c/pt/translation.py 2007-12-23 15:12:48 UTC (rev 82411)
@@ -3,7 +3,7 @@
import io
import utils
-import attributes as attrs
+import expressions
import clauses
wrapper = """\
@@ -237,27 +237,27 @@
return attributes
define = attribute(
- "{http://xml.zope.org/namespaces/tal}define", attrs.definitions)
+ "{http://xml.zope.org/namespaces/tal}define", expressions.definitions)
condition = attribute(
- "{http://xml.zope.org/namespaces/tal}condition", attrs.expression)
+ "{http://xml.zope.org/namespaces/tal}condition", expressions.value)
repeat = attribute(
- "{http://xml.zope.org/namespaces/tal}repeat", attrs.definition)
+ "{http://xml.zope.org/namespaces/tal}repeat", expressions.definition)
attributes = attribute(
- "{http://xml.zope.org/namespaces/tal}attributes", attrs.definitions)
+ "{http://xml.zope.org/namespaces/tal}attributes", expressions.definitions)
content = attribute(
- "{http://xml.zope.org/namespaces/tal}content", attrs.expression)
+ "{http://xml.zope.org/namespaces/tal}content", expressions.value)
replace = attribute(
- "{http://xml.zope.org/namespaces/tal}replace", attrs.expression)
+ "{http://xml.zope.org/namespaces/tal}replace", expressions.value)
omit = attribute(
- "{http://xml.zope.org/namespaces/tal}omit-tag", attrs.expression)
+ "{http://xml.zope.org/namespaces/tal}omit-tag", expressions.value)
i18n_translate = attribute(
- "{http://xml.zope.org/namespaces/i18n}translate", attrs.name)
+ "{http://xml.zope.org/namespaces/i18n}translate", expressions.name)
i18n_attributes = attribute(
- "{http://xml.zope.org/namespaces/i18n}attrs", attrs.mapping)
+ "{http://xml.zope.org/namespaces/i18n}attributes", expressions.mapping)
i18n_domain = attribute(
- "{http://xml.zope.org/namespaces/i18n}domain", attrs.name)
+ "{http://xml.zope.org/namespaces/i18n}domain", expressions.name)
i18n_name = attribute(
- "{http://xml.zope.org/namespaces/i18n}name", attrs.name)
+ "{http://xml.zope.org/namespaces/i18n}name", expressions.name)
# set up namespace
lookup = lxml.etree.ElementNamespaceClassLookup()
More information about the Checkins
mailing list