def test_is_in_context(example, output): from refactorlib.parse import parse lxmlnode = parse(example) top_level_directives = lxmlnode.xpath('/cheetah/*/*[1][self::Directive]') top_level_directives = [ b'#' + d.name.encode('UTF-8') + b' ' + d.var.totext(with_tail=False) if d.var is not None else b'#' + d.name.encode('UTF-8') for d in top_level_directives ] # for each Placeholder, print if it's "in context" of each top-level # directive new_output = [] for placeholder in lxmlnode.xpath('//Placeholder'): new_output.append( b'Placeholder: ' + placeholder.totext(with_tail=False) ) for d in top_level_directives: new_output.append( b' ' + d + b' ' + six.text_type(placeholder.is_in_context(d)).encode('UTF-8') ) new_output.append(b'') new_output = b'\n'.join(new_output) assert_same_content(output, new_output)
def test_is_in_context(example, output): from refactorlib.parse import parse lxmlnode = parse(example) top_level_directives = lxmlnode.xpath('/cheetah/*/*[1][self::Directive]') top_level_directives = [ b'#' + d.name.encode('UTF-8') + b' ' + d.var.totext(with_tail=False) if d.var is not None else b'#' + d.name.encode('UTF-8') for d in top_level_directives ] # for each Placeholder, print if it's "in context" of each top-level # directive new_output = [] for placeholder in lxmlnode.xpath('//Placeholder'): new_output.append(b'Placeholder: ' + placeholder.totext(with_tail=False)) for d in top_level_directives: new_output.append( b' ' + d + b' ' + str(placeholder.is_in_context(d)).encode('UTF-8')) new_output.append(b'') new_output = b'\n'.join(new_output) assert_same_content(output, new_output)
def xmlfrom(filename): from refactorlib.parse import parse from lxml.etree import tostring tree = parse(filename).getroottree() encoding = tree.docinfo.encoding return tostring(tree, encoding=encoding)
def test_encoding_detection(example): from refactorlib.python.parse import detect_encoding text = open(example).read() example = parse(example) detected_encoding = detect_encoding(text) assert (example.encoding == detected_encoding or (example.encoding, detected_encoding) == ('UTF-8', None))
def test_encoding_detection(example): from refactorlib.python.parse import detect_encoding text = open(example, 'rb').read() example = parse(example) detected_encoding = detect_encoding(text) assert ( example.encoding == detected_encoding or (example.encoding, detected_encoding) == ('UTF-8', None) )
def test_is_in_context(example, output): from refactorlib.parse import parse lxmlnode = parse(example) top_level_directives = lxmlnode.xpath('/cheetah/*/*[1][self::Directive]') top_level_directives = [ "#%s %s" % (d.name, d.var.totext(with_tail=False)) if d.var else "#%s" % (d.name) for d in top_level_directives ] #for each Placeholder, print if it's "in context" of each top-level directive new_output = [] for placeholder in lxmlnode.xpath('//Placeholder'): new_output.append('Placeholder: %s' % placeholder.totext(with_tail=False)) for d in top_level_directives: new_output.append(' %s %s' % (d, placeholder.is_in_context(d))) new_output.append('') new_output = '\n'.join(new_output) assert_same_content(output, new_output)
def test_matches_known_good_parsing(example, output): example = parse(example).tostring() assert_same_content(output, example)
def test_can_make_round_trip(example): text = open(example).read() example = parse(example) assert text == example.totext().decode('UTF-8')
def test_can_make_round_trip(example): text = open(example, 'rb').read() example = parse(example) assert text == example.totext()
#!/usr/bin/env python from refactorlib.parse import parse c = parse('simple.tmpl') import pudb pudb.set_trace() for decorator in c.find_calls('foo'): print decorator.tostring() decorator.remove_self() print c.totext()
#!/usr/bin/env python from refactorlib.parse import parse c = parse('simple.tmpl') import pudb; pudb.set_trace() for decorator in c.find_calls('foo'): print decorator.tostring() decorator.remove_self() print c.totext()