예제 #1
0
def test_find_end_directive(example, output):
	text = open(example).read()

	from refactorlib.cheetah.parse import parse
	lxmlnode = parse(text)
	tree = lxmlnode.getroottree()

	new_output = []
	for directive in lxmlnode.xpath('//Directive'):
		new_output.append(
			'Directive: %s' % tree.getpath(directive),
		)
		if directive.is_multiline_directive:
			try:
				new_output.append(
					'End: %s' % tree.getpath(directive.get_end_directive()),
				)
			except:
				import pudb; pudb.set_trace()
				raise
		else:
			new_output.append(
				'Single-line: %s' % directive.totext()
			)
		new_output.append('')

	new_output = '\n'.join(new_output)
	assert_same_content(output, new_output)
예제 #2
0
def test_get_enclosing_blocks(example, output):
    text = open(example).read()

    from refactorlib.cheetah.parse import parse
    lxmlnode = parse(text)
    tree = lxmlnode.getroottree()

    unique_contexts = {}
    for directive in lxmlnode.xpath('//Directive'):
        context = tuple(
            tree.getpath(block) for block in directive.get_enclosing_blocks())

        if context and context not in unique_contexts:
            unique_contexts[context] = directive

    new_output = []
    for context, directive in sorted(unique_contexts.items()):
        new_output.append(b'Directive: ' +
                          tree.getpath(directive).encode('UTF-8'))
        for c in context:
            new_output.append(b'  ' + c.encode('UTF-8'))
        new_output.append(b'')

    new_output = b'\n'.join(new_output)
    assert_same_content(output, new_output)
예제 #3
0
def test_get_enclosing_blocks(example, output):
    text = open(example).read()

    from refactorlib.cheetah.parse import parse
    lxmlnode = parse(text)
    tree = lxmlnode.getroottree()

    unique_contexts = {}
    for directive in lxmlnode.xpath('//Directive'):
        context = tuple(
            tree.getpath(block) for block in directive.get_enclosing_blocks()
        )

        if context and context not in unique_contexts:
            unique_contexts[context] = directive

    new_output = []
    for context, directive in sorted(unique_contexts.items()):
        new_output.append(
            b'Directive: ' + tree.getpath(directive).encode('UTF-8')
        )
        for c in context:
            new_output.append(b'  ' + c.encode('UTF-8'))
        new_output.append(b'')

    new_output = b'\n'.join(new_output)
    assert_same_content(output, new_output)
예제 #4
0
def test_can_find_calls():
    example = parse('''
		foo $foo() bar
	''')

    calls = example.find_calls('foo')
    assert len(calls) == 1
    assert calls[0].totext() == '$foo()'
예제 #5
0
def test_can_find_calls():
	example = parse('''
		foo $foo() bar
	''')

	calls = example.find_calls('foo')
	assert len(calls) == 1
	assert calls[0].totext() == '$foo()'
예제 #6
0
def test_can_find_calls():
    from refactorlib.cheetah.parse import parse

    example = parse('''
        foo $foo() bar
    ''')

    calls = example.find_calls('foo')
    assert len(calls) == 1
    assert calls[0].totext() == b'$foo()'
예제 #7
0
def perform_step(file_contents, step):
    """Performs a step of the transformation.

    :param text file_contents: Contends of the cheetah template
    :param function step: Function taking xmldoc and returning new contents
    :returns: new contents of the file.
    """
    assert type(file_contents) is not bytes
    xmldoc = parse(file_contents)
    return step(xmldoc)
예제 #8
0
def perform_step(file_contents, step):
    """Performs a step of the transformation.

    :param text file_contents: Contends of the cheetah template
    :param function step: Function taking xmldoc and returning new contents
    :returns: new contents of the file.
    """
    assert type(file_contents) is not bytes
    xmldoc = parse(file_contents)
    return step(xmldoc)
예제 #9
0
def test_remove_foo(example, output):
	from refactorlib.cheetah.parse import parse
	example = open(example).read()
	example = parse(example)

	for decorator in example.find_decorators('@foo'):
		decorator.remove_self()

	# Check the text.
	example = example.totext()
	assert_same_content(output, example)
예제 #10
0
def test_remove_foo(example, output):
    from refactorlib.cheetah.parse import parse
    example = open(example).read()
    example = parse(example)

    for decorator in example.find_decorators('@foo'):
        decorator.remove_self()

    # Check the text.
    example = example.totext()
    assert_same_content(output, example)
예제 #11
0
def test_can_find_calls():
    from refactorlib.cheetah.parse import parse

    example = parse(
        """
        foo $foo() bar
    """
    )

    calls = example.find_calls("foo")
    assert len(calls) == 1
    assert calls[0].totext() == b"$foo()"
예제 #12
0
def get_parsed_doc():
    doc = ('#compiler-settings\n'
           'useLegacyImportMode = True\n'
           '#end compiler-settings\n'
           '#extends templates.base\n'
           '#implements respond\n'
           '\n'
           '#import itertools\n'
           '#import yelp.util.helpers.template as h\n'
           '#from foo.bar import baz\n'
           '#from a import b as c\n')
    return parse(doc)
예제 #13
0
def test_replace_directive(example, output):
    from refactorlib.parse import parse
    lxmlnode = parse(example)

    for directive in lxmlnode.xpath('//Directive[not(starts-with(., "#end"))]'):
        if directive.var is None:
            directive.replace_directive('#{{{%s}}}' % directive.name)
        else:
            directive.replace_directive('#{{{%s}}} [%s]' % (directive.name, directive.var.totext(with_tail=False).decode('UTF-8')))

    new_output = lxmlnode.totext()
    assert_same_content(output, new_output)
예제 #14
0
def test_can_remove_calls(example, output):
	example = open(example).read()
	example = parse(example)

	calls = example.find_calls('foo')
	assert len(calls) == 5

	for call in calls:
		call.remove_call()

	# Check the text.
	example = example.totext()
	assert_same_content(output, example)
예제 #15
0
def test_remove_foo_dot_bar(example, output):
    from refactorlib.cheetah.parse import parse

    example = open(example).read()
    example = example.replace("#@foo\n", "#@foo.bar\n")
    example = parse(example)

    for decorator in example.find_decorators("@foo.bar"):
        decorator.remove_self()

    # Check the text.
    example = example.totext()
    assert_same_content(output, example)
예제 #16
0
def test_can_remove_calls(example, output):
    example = open(example).read()
    example = parse(example)

    calls = example.find_calls('foo')
    assert len(calls) == 5

    for call in calls:
        call.remove_call()

    # Check the text.
    example = example.totext()
    assert_same_content(output, example)
예제 #17
0
def test_not_multiline_directives():
    xmldoc = parse(textwrap.dedent("""
        #def foo(): not multiline

        #if True: not multiline

        #while False: not multiline

        #for _ in range(3): not multiline
    """))

    directives = xmldoc.xpath('//Directive')
    for directive in directives:
        assert not directive.is_multiline_directive
예제 #18
0
def test_can_remove_calls(example, output):
    from refactorlib.cheetah.parse import parse
    example = open(example).read()
    example = parse(example)

    calls = example.find_calls('foo')
    assert calls

    for call in calls:
        call.remove_call()

    # Check the text.
    example = example.totext()
    assert_same_content(output, example)
예제 #19
0
def get_parsed_doc():
    doc = (
        '#compiler-settings\n'
        'useAutoCalling = True\n'
        '#end compiler-settings\n'
        '#extends templates.base\n'
        '#implements respond\n'
        '\n'
        '#import itertools\n'
        '#import yelp.util.helpers.template as h\n'
        '#from foo.bar import baz\n'
        '#from a import b as c\n'
    )
    return parse(doc)
예제 #20
0
def test_replace_directive(example, output):
    from refactorlib.parse import parse
    lxmlnode = parse(example)

    for directive in lxmlnode.xpath(
            '//Directive[not(starts-with(., "#end"))]'):
        if directive.var is None:
            directive.replace_directive(f'#[[[{directive.name}]]]')
        else:
            directive.replace_directive(
                f'#[[[{directive.name}]]] [{directive.var.totext(with_tail=False).decode()}]'
            )

    new_output = lxmlnode.totext()
    assert_same_content(output, new_output)
예제 #21
0
def test_not_multiline_directives():
    xmldoc = parse(
        textwrap.dedent("""
        #def foo(): not multiline

        #if True: not multiline

        #while False: not multiline

        #for _ in range(3): not multiline
    """))

    directives = xmldoc.xpath('//Directive')
    for directive in directives:
        assert not directive.is_multiline_directive
예제 #22
0
def test_can_remove_calls(example, output):
    from refactorlib.cheetah.parse import parse

    example = open(example).read()
    example = parse(example)

    calls = example.find_calls("foo")
    assert calls

    for call in calls:
        call.remove_call()

    # Check the text.
    example = example.totext()
    assert_same_content(output, example)
예제 #23
0
def test_replace_directive(example, output):
	from refactorlib.parse import parse
	lxmlnode = parse(example)

	for directive in lxmlnode.xpath('//Directive'):
		if directive.xpath('./EndDirective'):
			# Don't mess with #end statements
			continue

		if directive.var is None:
			directive.replace_directive('#{{{%s}}}' % directive.name)
		else:
			directive.replace_directive('#{{{%s}}} [%s]' % (directive.name, directive.var.totext(with_tail=False)))

	new_output = lxmlnode.totext()
	assert_same_content(output, new_output)
예제 #24
0
def test_replace_directive(example, output):
    from refactorlib.parse import parse
    lxmlnode = parse(example)

    for directive in lxmlnode.xpath('//Directive'):
        if directive.xpath('./EndDirective'):
            # Don't mess with #end statements
            continue

        if directive.var is None:
            directive.replace_directive('#{{{%s}}}' % directive.name)
        else:
            directive.replace_directive(
                '#{{{%s}}} [%s]' %
                (directive.name, directive.var.totext(with_tail=False)))

    new_output = lxmlnode.totext()
    assert_same_content(output, new_output)
예제 #25
0
def test_can_add_comments():
    """
    It's often useful to simply add comment to code, automatically.
    """
    from refactorlib.cheetah.parse import parse

    example = parse('''
        #def foo()
            Escaped thing: $esc(
                $_('My translated string.')
            )
        #end def
    ''')

    calls = example.find_calls('_')
    assert len(calls) == 1, calls

    calls[0].add_comment('1 _')
    calls[0].add_comment('2 _')
    calls[0].add_comment('3 _')

    calls = example.find_calls('esc')
    assert len(calls) == 1, calls

    calls[0].add_comment('1 esc')
    calls[0].add_comment('2 esc')
    calls[0].add_comment('3 esc')

    assert b'''
        #def foo()
            ## 1 esc
            ## 2 esc
            ## 3 esc
            Escaped thing: $esc(
                ## 1 _
                ## 2 _
                ## 3 _
                $_('My translated string.')
            )
        #end def
    ''' == example.totext()
예제 #26
0
def test_find_end_directive(example, output):
    text = open(example).read()

    from refactorlib.cheetah.parse import parse
    lxmlnode = parse(text)
    tree = lxmlnode.getroottree()

    new_output = []
    for directive in lxmlnode.xpath('//Directive'):
        new_output.append(
            b'Directive: ' + tree.getpath(directive).encode('UTF-8'), )
        if directive.is_multiline_directive:
            new_output.append(
                b'End: ' +
                tree.getpath(directive.get_end_directive()).encode('UTF-8'), )
        else:
            new_output.append(b'Single-line: ' + directive.totext())
        new_output.append(b'')

    new_output = b'\n'.join(new_output)
    assert_same_content(output, new_output)
예제 #27
0
def test_multiline_directives():
    xmldoc = parse(
        textwrap.dedent("""
        #def foo():
            multiline
        #end def

        #def foo()
            multiline
        #end def

        #if True
            multiline
        #end if

        #if True:
            multiline
        #end if

        #while False
            multiline
        #end while

        #while False:
            multiline
        #end while

        #for _ in range(3)
            multiline
        #end for

        #for _ in range(3):
            multiline
        #end for
    """))

    directives = xmldoc.xpath('//Directive[not(starts-with(., "#end"))]')
    for directive in directives:
        assert directive.is_multiline_directive
예제 #28
0
def test_can_add_comments():
	"""
	It's often useful to simply add comment to code, automatically.
	"""
	example = parse('''
		#def foo
			$esc(
				$_('My translated string.')
			)
		#end def
	''')

	calls = example.find_calls('_')
	assert len(calls) == 1, calls

	calls[0].add_comment('1 _')
	calls[0].add_comment('2 _')
	calls[0].add_comment('3 _')
	
	calls = example.find_calls('esc')
	assert len(calls) == 1, calls

	calls[0].add_comment('1 esc')
	calls[0].add_comment('2 esc')
	calls[0].add_comment('3 esc')

	assert '''
		#def foo
			## 1 esc
			## 2 esc
			## 3 esc
			$esc(
				## 1 _
				## 2 _
				## 3 _
				$_('My translated string.')
			)
		#end def
	''' == example.totext()
예제 #29
0
def test_multiline_directives():
    xmldoc = parse(textwrap.dedent("""
        #def foo():
            multiline
        #end def

        #def foo()
            multiline
        #end def

        #if True
            multiline
        #end if

        #if True:
            multiline
        #end if

        #while False
            multiline
        #end while

        #while False:
            multiline
        #end while

        #for _ in range(3)
            multiline
        #end for

        #for _ in range(3):
            multiline
        #end for
    """))

    directives = xmldoc.xpath('//Directive[not(starts-with(., "#end"))]')
    for directive in directives:
        assert directive.is_multiline_directive
예제 #30
0
def test_find_end_directive(example, output):
    text = open(example).read()

    from refactorlib.cheetah.parse import parse
    lxmlnode = parse(text)
    tree = lxmlnode.getroottree()

    new_output = []
    for directive in lxmlnode.xpath('//Directive'):
        new_output.append(
            b'Directive: ' + tree.getpath(directive).encode('UTF-8'),
        )
        if directive.is_multiline_directive:
            new_output.append(
                b'End: ' + tree.getpath(directive.get_end_directive()).encode('UTF-8'),
            )
        else:
            new_output.append(
                b'Single-line: ' + directive.totext()
            )
        new_output.append(b'')

    new_output = b'\n'.join(new_output)
    assert_same_content(output, new_output)
예제 #31
0
def test_find_end_directive(example, output):
    text = open(example).read()

    from refactorlib.cheetah.parse import parse
    lxmlnode = parse(text)
    tree = lxmlnode.getroottree()

    new_output = []
    for directive in lxmlnode.xpath('//Directive'):
        new_output.append('Directive: %s' % tree.getpath(directive), )
        if directive.is_multiline_directive:
            try:
                new_output.append(
                    'End: %s' % tree.getpath(directive.get_end_directive()), )
            except:
                import pudb
                pudb.set_trace()
                raise
        else:
            new_output.append('Single-line: %s' % directive.totext())
        new_output.append('')

    new_output = '\n'.join(new_output)
    assert_same_content(output, new_output)
예제 #32
0
def from_import_from_string(s):
    return get_from_imports(parse(s))[0]
예제 #33
0
def import_import_from_string(s):
    return get_import_imports(parse(s))[0]