def test_add_mult_precedence(): test = Plywood('foo + bar * baz').compile()[0] assert_operator(test, '+') assert_variable(test.left, 'foo') assert_operator(test.right, '*') assert_variable(test.right.left, 'bar') assert_variable(test.right.right, 'baz') test = Plywood('foo * bar + baz').compile()[0] assert_operator(test, '+') assert_variable(test.right, 'baz') assert_operator(test.left, '*') assert_variable(test.left.left, 'foo') assert_variable(test.left.right, 'bar') test = Plywood('foo + bar + baz').compile()[0] assert_operator(test, '+') assert_variable(test.right, 'baz') assert_operator(test.left, '+') assert_variable(test.left.left, 'foo') assert_variable(test.left.right, 'bar') test = Plywood('foo * bar + baz * foo').compile()[0] assert_operator(test, '+') assert_operator(test.left, '*') assert_variable(test.left.left, 'foo') assert_variable(test.left.right, 'bar') assert_operator(test.right, '*') assert_variable(test.right.left, 'baz') assert_variable(test.right.right, 'foo')
def include(states, scope, arguments, block): if len(arguments.args) != 1: raise InvalidArguments('`include` only accepts one argument') if len(block.lines): raise InvalidArguments('`include` does not accept a block') restore_scope = {} delete_scope = [] if isinstance(scope['self'], PlywoodValue): context = scope['self'].python_value(scope) else: context = scope['self'] if len(arguments.kwargs): kwargs = dict( (item.key.get_name(), item.value) for item in arguments.kwargs ) for key, value in kwargs.items(): if key in context: restore_scope[key] = context[key] else: delete_scope.append(key) context[key] = value if '__env' in scope: env = scope['__env'] else: env = PlywoodEnv({'separator': ' '}) template_name = arguments.args[0].python_value(scope) plywood = None for path in scope['__paths']: template_path = os.path.join(path, template_name) + '.ply' if template_path in env.includes: plywood = env.includes[template_path] elif os.path.exists(template_path): retval = '' with open(template_path) as f: input = f.read() plywood = Plywood(input) env.includes[template_path] = plywood # scope is not pushed/popped - `include` adds its variables to the local scope. if plywood: break if not plywood: raise Exception('Could not find template: {0!r}'.format(template_name)) retval = plywood.run(context, env) if len(arguments.kwargs): for key, value in restore_scope.items(): context[key] = value for key in delete_scope: del context[key] return states, retval
def test_comment_with_variable(): test = Plywood('foo # comment').compile()[0] assert_variable(test, 'foo') test = Plywood('foo # comment\nbar # comment').compile()[0] assert_variable(test, 'foo') test = Plywood( '\n# comment1\nfoo # comment2\n\n\n # comment3').compile()[0] assert_variable(test, 'foo')
def test_hexadecimal(): test = Plywood('0x1e32').compile()[0] assert_number(test, 7730) test = Plywood('0x0001ea').compile()[0] assert_number(test, 490) test = Plywood('0x0').compile()[0] assert_number(test, 0) test = Plywood('0xFFFF').compile()[0] assert_number(test, 65535)
def test_multiline_dict(): test = Plywood('{\n "foo": bar,\n "quux": 0}').compile()[0] assert_dict(test, 2) assert_kvp(test.values[0], 'foo') assert_kvp(test.values[1], 'quux') assert_variable(test.values[0].value, 'bar') assert_number(test.values[1].value, 0)
def test_dict_two(): test = Plywood('{"foo": bar, bar: baz}').compile()[0] assert_dict(test, 2) assert_kvp(test.values[0], 'foo') assert_variable(test.values[0].value, 'bar') assert_kvp(test.values[1], PlywoodVariable(-1, 'bar')) assert_variable(test.values[1].value, 'baz')
def test_args_three(): test = Plywood('foo a, b, c').compile()[0] assert_function(test, 'foo') assert_variable(test.right.args[0], 'a') assert_variable(test.right.args[1], 'b') assert_variable(test.right.args[2], 'c')
def test_multi_mixed_block_in_out(): test = Plywood(''' foo: foo BAH: BAH bar: baz baz: 'baz' ''').compile() assert_block(test, 4) assert_function(test[0], 'foo') assert_block(test[0].block, 1) assert_variable(test[0].block[0], 'foo') assert_function(test[1], 'BAH') assert_block(test[1].block, 1) assert_variable(test[1].block[0], 'BAH') assert_function(test[2], 'bar') assert_block(test[2].block, 1) assert_variable(test[2].block[0], 'baz') assert_function(test[3], 'baz') assert_block(test[3].block, 1) assert_string(test[3].block[0], 'baz')
def test_function(): test = Plywood('foo(bar, baz)').compile()[0] assert_function(test, 'foo') assert_parens(test.right, 2) assert_variable(test.right.args[0], 'bar') assert_variable(test.right.args[1], 'baz')
def render(self, site=None): try: env = Registry.get('plywood_environment') template_path = fix_path(self.source_path) with open(template_path) as f: contents = f.read() front_matter_match = re.match( r"\A([-]{3,}|[`]{3,})(\r\n|\r|\n)", contents) number_yaml_lines = 0 if front_matter_match: newline = front_matter_match.group( 2) # group(2) contains the newline/CRLF number_yaml_lines += 1 offset = len(front_matter_match.group(0)) delim = re.compile("^" + front_matter_match.group(1) + "$") lines = contents.splitlines() for line in lines[ 1:]: # skip the first line, the opening front matter offset += len(line) + len(newline) number_yaml_lines += 1 if delim.match(line): break contents = (newline * number_yaml_lines) + contents[offset:] except UnicodeDecodeError as e: e.args += "Could not process '%s' because of unicode error." % self.source_path raise env.scope.push() env.scope['site'] = site env.scope['my'] = self retval = Plywood(contents).run(self.config, env) env.scope.pop() return retval
def test_autofunction(): test = Plywood('foo bar, baz(a b), c=d').compile()[0] assert_function(test, 'foo') assert_variable(test.right.args[0], 'bar') assert_operator(test.right.args[1], '()') assert_kvp(test.right.kwargs[0], 'c') assert_variable(test.right.kwargs[0].value, 'd')
def test_parens(): test = Plywood('(-foo + -bar)').compile()[0] assert_parens(test, 1) assert_operator(test.args[0], '+') assert_unary(test.args[0].left, '-') assert_variable(test.args[0].left.value, 'foo') assert_unary(test.args[0].right, '-') assert_variable(test.args[0].right.value, 'bar')
def test_pipe_precedence(): test = Plywood('"str" + foo | fn("test") + "str"').compile()[0] assert_operator(test, '+') assert_string(test.right, 'str') assert_operator(test.left, '+') assert_string(test.left.left, 'str') assert_operator(test.left.right, '|') assert_variable(test.left.right.left, 'foo') assert_operator(test.left.right.right, '()')
def test_function_kwargs(): test = Plywood('foo(bar, 1, key="value")').compile()[0] assert_function(test, 'foo') assert_variable(test.left, 'foo') assert_parens(test.right, 2, 1) assert_variable(test.right.args[0], 'bar') assert_number(test.right.args[1], 1) assert_kvp(test.right.kwargs[0], 'key') assert_string(test.right.kwargs[0].value, 'value')
def test_mixed_block_indented_last(): test = Plywood(''' foo: bar: baz ''').compile() assert_block(test, 1) assert_function(test[0], 'foo') assert_block(test[0].block, 1) assert_block(test[0].block[0].block, 1)
def test_mixed_block_fail(): with raises(IndentError): test = Plywood(''' foo: bar: baz baz ''').compile() assert False
def test_attr_call_precedence(): test = Plywood(''' a.b() ''').compile() assert_block(test, 1) assert_function(test[0]) assert_operator(test[0].left, '.') assert_variable(test[0].left.left, 'a') assert_variable(test[0].left.right, 'b')
def test_parens(): code = """ a = b (-foo + -bar """ with raises(CompilationError) as e: Plywood(code).compile()[0] assert e.value.buffer.position == len(code)
def test_whitespace_dict(): test_str = '''{ "foo" : "bar" , } ''' test = Plywood(test_str).compile()[0] assert_dict(test, 1)
def test_dict_multiline(): test = Plywood('''{ "foo": bar, "bar": baz }''').compile()[0] assert_dict(test, 2) assert_kvp(test.values[0], 'foo') assert_variable(test.values[0].value, 'bar') assert_kvp(test.values[1], PlywoodVariable(-1, 'bar')) assert_variable(test.values[1].value, 'baz')
def test_assign(): test = Plywood('a = b = c + 1').compile()[0] assert_operator(test, '=') assert_variable(test.left, 'a') assert_operator(test.right, '=') assert_variable(test.right.left, 'b') assert_operator(test.right.right, '+') assert_variable(test.right.right.left, 'c') assert_number(test.right.right.right, 1)
def test_happy_fun_block(): test = Plywood(''' div: p: 'text' ''').compile() assert_block(test, 1) assert_function(test[0], 'div') assert_block(test[0].block, 1) assert_function(test[0].block[0], 'p') assert_block(test[0].block[0].block, 1) assert_string(test[0].block[0].block[0], 'text')
def test_parens_multiline(): test = Plywood('''( a, b, c, )''').compile()[0] assert_parens(test, 3) assert_variable(test.args[0], 'a') assert_variable(test.args[1], 'b') assert_variable(test.args[2], 'c')
def test_parens_args(): test = Plywood('''( a, b, c=d, )''').compile()[0] assert_parens(test, 2, 1) assert_variable(test.args[0], 'a') assert_variable(test.args[1], 'b') assert_kvp(test.kwargs[0], 'c') assert_variable(test.kwargs[0].value, 'd')
def test_multi_inline(): test = Plywood(''' foo: bar foo: bar ''').compile() assert_block(test, 2) assert_function(test[0], 'foo') assert_block(test[0].block, 1) assert_variable(test[0].block[0], 'bar') assert_function(test[1], 'foo') assert_block(test[1].block, 1) assert_variable(test[0].block[0], 'bar')
def test_args_three_kwarg(): test = Plywood('foo a, b, d=e, c, f=g').compile()[0] assert_function(test, 'foo') assert_parens(test.right, 3, 2) assert_variable(test.right.args[0], 'a') assert_variable(test.right.args[1], 'b') assert_variable(test.right.args[2], 'c') assert_kvp(test.right.kwargs[0], 'd') assert_variable(test.right.kwargs[0].value, 'e') assert_kvp(test.right.kwargs[1], 'f') assert_variable(test.right.kwargs[1].value, 'g')
def test_getitem(): test = Plywood(''' div['something'] = 'text' ''').compile() assert_block(test, 1) assert_operator(test[0], '=') assert_operator(test[0].left, '[]') assert_variable(test[0].left.left, 'div') assert_indices(test[0].left.right, 1) assert_string(test[0].left.right[0], 'something') assert_string(test[0].right, 'text')
def test_classy_div(): test = Plywood(''' div.classy: 'text' ''').compile() assert_block(test, 1) assert_function(test[0]) assert_operator(test[0].left, '.') assert_variable(test[0].left.left, 'div') assert_variable(test[0].left.right, 'classy') assert_block(test[0].block, 1) assert_string(test[0].block[0], 'text')
def test_list(): test = Plywood('[1, "two", three, (four) + -five]').compile()[0] assert_list(test, 4) assert_number(test.values[0], 1) assert_string(test.values[1], 'two') assert_variable(test.values[2], 'three') assert_operator(test.values[3], '+') assert_parens(test.values[3].left, 1) assert_variable(test.values[3].left.args[0], 'four') assert_unary(test.values[3].right, '-') assert_variable(test.values[3].right.value, 'five')
def test_precedence(): test = Plywood('1 < 2 and 2 > 1 and 2+2 == 4').compile()[0] assert_operator(test, 'and') assert_operator(test.left, 'and') assert_operator(test.left.left, '<') assert_number(test.left.left.left, 1) assert_number(test.left.left.right, 2) assert_operator(test.right, '==') assert_operator(test.right.left, '+') assert_number(test.right.left.left, 2) assert_number(test.right.left.right, 2) assert_number(test.right.right, 4) test = Plywood('2*3**4').compile()[0] assert_operator(test, '*') assert_number(test.left, 2) assert_operator(test.right, '**') assert_number(test.right.left, 3) assert_number(test.right.right, 4) test = Plywood('2**3*4').compile()[0] assert_operator(test, '*') assert_operator(test.left, '**') assert_number(test.left.left, 2) assert_number(test.left.right, 3) assert_number(test.right, 4) test = Plywood('2**3**4').compile()[0] assert_operator(test, '**') assert_number(test.left, 2) assert_operator(test.right, '**') assert_number(test.right.left, 3) assert_number(test.right.right, 4) test = Plywood('-foo + - bar').compile()[0] assert_operator(test, '+') assert_unary(test.left, '-') assert_variable(test.left.value, 'foo') assert_unary(test.right, '-') assert_variable(test.right.value, 'bar')
from plywood import Plywood,Join from solid import * boxlength = 80 boxwidth = 60 boxheight = 20 p = Plywood(boxlength,boxwidth) p2 = Plywood(boxlength,boxheight) p3 = Plywood(boxlength,boxheight) p4 = Plywood(boxheight,boxwidth+.0001) p5 = Plywood(boxheight,boxwidth+.0001) p.addCutSides(Join(p2, p, 2)) p.addCutSides(Join(p3, p, 2)) p.addCutSides(Join(p4, p, 1)) p2.addCutSides(Join(p4, p2, 0)) p3.addCutSides(Join(p4, p3, 2)) p.addCutSides(Join(p5, p, 1)) p2.addCutSides(Join(p5, p2, 0)) p3.addCutSides(Join(p5, p3, 2)) parts = [] parts += [p,p2,p3,p4,p5] scene = union() scene += p.getSolid() scene += color([1,0,0])(translate([0,boxwidth/2+0.001,boxheight/2 - 0.001])(rotate(v=[1,0,0], a=90)(p2.getSolid()))) scene += color([0,1,0])(translate([0,-boxwidth/2-0.001+p3.materialsize,boxheight/2 - 0.001])(rotate(v=[1,0,0], a=90)(p3.getSolid()))) scene += color([0,0,1])(translate([boxlength/2 - p4.materialsize,0,p4.width/2-0.001])(rotate(v=[0,1,0], a=90)(p4.getSolid()))) scene += color([0,0,1])(translate([-boxlength/2,0,p4.width/2-0.001])(rotate(v=[0,1,0], a=90)(p5.getSolid())))