Example #1
0
 def readCode(self,modulepath=None):
     result=Bag()
     modulepath=modulepath or sys.modules[self.__module__].__file__
     with open(modulepath, "r") as source_code:
         red = RedBaron(source_code.read())
     result.fromJson(red.fst())
     return result
Example #2
0
def main():
    """Rewrite Thrift-generated Python clients to handle recursive structs. For
    more details see: https://issues.apache.org/jira/browse/THRIFT-2642.

    Requires package `RedBaron`, available via pip:
    $ pip install redbaron

    To use:

    $ thrift -gen py mapd.thrift
    $ mv gen-py/mapd/ttypes.py gen-py/mapd/ttypes-backup.py
    $ python fix_recursive_structs.py gen-py/mapd/ttypes-backup.py gen-py/mapd/ttypes.py

    """
    in_file = open(sys.argv[1], 'r')
    out_file = open(sys.argv[2], 'w')

    red_ast = RedBaron(in_file.read())

    thrift_specs = [ts.parent for ts in red_ast.find_all(
        'name', 'thrift_spec') if ts.parent.type == 'assignment' and ts.parent.parent.name in ['TDatumVal', 'TColumnData']]

    nodes = []
    for ts in thrift_specs:
        node = ts.copy()
        node.target = ts.parent.name + '.' + str(node.target)
        nodes.append(node)
        ts.value = 'None'

    red_ast.extend(nodes)
    out_file.write(red_ast.dumps())
Example #3
0
def _cleanupPyLintComments(filename, abort):
    from baron.parser import (  # pylint: disable=I0021,import-error,no-name-in-module
        ParsingError,  # @UnresolvedImport
    )
    from redbaron import (  # pylint: disable=I0021,import-error,no-name-in-module
        RedBaron,  # @UnresolvedImport
    )

    old_code = getFileContents(filename)

    try:
        red = RedBaron(old_code)
        # red = RedBaron(old_code.rstrip()+'\n')
    except ParsingError:
        if abort:
            raise

        my_print("PARSING ERROR.")
        return 2

    for node in red.find_all("CommentNode"):
        try:
            _updateCommentNode(node)
        except Exception:
            my_print("Problem with", node)
            node.help(deep=True, with_formatting=True)
            raise

    new_code = red.dumps()

    if new_code != old_code:
        with open(filename, "w") as source_code:
            source_code.write(red.dumps())
Example #4
0
def compare(s1, s2, decay_factor = DEFAULT_DECAY_FACTOR):
    red1 = RedBaron(s1)
    red2 = RedBaron(s2)
    result = []
    
    for ast_f2 in red2.find_all('def'):
        ast_f1 = red1.find('def', name = ast_f2.name)        
        if ast_f1 is not None:
            additions, deletions = preprocess_files(ast_f1.dumps(),
                                                    ast_f2.dumps())
            comments, exceptions = preprocess_comments(ast_f2, additions) 
            for a in additions:
                for c in comments:
                    line, _ = c.left_bounds
                    distance = math.fabs(line - a)
                    score = int(c.score() - float(decay_factor) / (distance * distance))
                    c.setScore(score if score > 0 else 0)
            for d in deletions:
                for c in comments:
                    line, _ = c.left_bounds
                    line = line + 1 if line >= d else line
                    distance = math.fabs(line - d)
                    score = int(c.score() - float(decay_factor) / (distance * distance))

                    c.setScore(score if score > 0 else 0)
            result.extend(comments)
            result.extend(exceptions)
        else:
            result.extend(preprocess_comments(ast_f2, []))
    
    return result
Example #5
0
def insert_output_start_stop_indicators(src):
    """
    Insert identifier strings so that output can be segregated from input.

    Parameters
    ----------
    src : str
        String containing input and output lines.

    Returns
    -------
    str
        String with output demarked.
    """
    rb = RedBaron(src)

    # find lines with trailing comments so we can preserve them properly
    lines_with_comments = {}
    comments = rb.findAll('comment')
    for c in comments:
        if c.previous and c.previous.type != 'endl':
            lines_with_comments[c.previous] = c

    input_block_number = 0

    # find all nodes that might produce output
    nodes = rb.findAll(lambda identifier: identifier in ['print', 'atomtrailers'])
    for r in nodes:
        # assume that whatever is in the try block will fail and produce no output
        # this way we can properly handle display of error messages in the except
        if hasattr(r.parent, 'type') and r.parent.type == 'try':
            continue

        # Output within if/else statements is not a good idea for docs, because
        # we don't know which branch execution will follow and thus where to put
        # the output block. Regardless of which branch is taken, though, the
        # output blocks must start with the same block number.
        if hasattr(r.parent, 'type') and r.parent.type == 'if':
            if_block_number = input_block_number
        if hasattr(r.parent, 'type') and r.parent.type in ['elif', 'else']:
            input_block_number = if_block_number

        if is_output_node(r):
            # if there was a trailing comment on this line, output goes after it
            if r in lines_with_comments:
                r = lines_with_comments[r]  # r is now the comment

            # find the correct node to 'insert_after'
            while hasattr(r, 'parent') and not hasattr(r.parent, 'insert'):
                r = r.parent

            r.insert_after('print(">>>>>%d")\n' % input_block_number)
            input_block_number += 1

    # curse you, redbaron! stop inserting endl before trailing comments!
    for l, c in lines_with_comments.items():
        if c.previous and c.previous.type == 'endl':
            c.previous.value = ''

    return rb.dumps()
Example #6
0
def test_node_elif_ifelseblock_next():
    red = RedBaron("if a:\n    pass\nelif a:\n    pass")
    assert red.elif_.next is None
    red = RedBaron("if a:\n    pass\nelif a:\n    pass\nelse:\n    pass")
    assert red.elif_.next is red.else_
    red = RedBaron("if a:\n    pass\nelif a:\n    pass\nchocolat")
    assert red.elif_.next is red.find("name", "chocolat")
def test_find_empty():
    red = RedBaron("")
    assert red.find("stuff") is None
    assert red.find("something_else") is None
    assert red.find("something_else", useless="pouet") is None
    with pytest.raises(AttributeError):
        red.will_raises
def test_node_if_ifelseblock_previous_intuitive():
    red = RedBaron("if a:\n    pass")
    assert red.if_.previous_intuitive is None
    red = RedBaron("chocolat\nif a:\n    pass")
    assert red.if_.previous_intuitive is red.find("endl")
    red = RedBaron("pouet\nif a:\n    pass\nelif a:\n    pass\nelse:\n    pass")
    assert red.else_.previous_intuitive is red.elif_
    assert red.if_.previous is None
Example #9
0
def test_node_else_elseelseblock_next_generator():
    red = RedBaron("if a:\n    pass\nelse:\n    pass")
    assert len(list(red.else_.next_generator())) == 0
    red = RedBaron("if a:\n    pass\nelse:\n    pass\nchocolat")
    assert list(red.else_.next_generator())[0] is red.find("name", "chocolat")

    red = RedBaron("if a:\n    pass\nelse:\n    pass\nchocolat")
    assert list(red.else_.next_generator()) == [red.find("name", "chocolat")]
Example #10
0
def main(meetup, tc=(255, 255, 255), bg=None, *tags):
    target_url = meetup

    soup = BeautifulSoup(requests.get(target_url).content, "html.parser")

    description = soup.find("div", id="groupDesc")
    description = (" " * 4).join(map(lambda x: str(x), description.contents)) + (" " * 4)
    description = "\n".join(map(lambda x: x.rstrip(), description.split("\n")))

    target_meetup_name = target_url.split("/")[-2]
    target = target_url.split("/")[-2].lower().replace("-", "_")

    if re.match("^\d", target):
        target = "_" + target

    logo_url = soup.find("img", "photo")["src"] if soup.find("img", "photo") else None

    if bg == None:
        if logo_url:
            palette = extract_colors(Image.open(BytesIO(requests.get(logo_url).content)))

            colors = palette.colors
            background_color = colors[0].value
            text_color = tc
        else:
            h = (random.randint(1, 100) * 0.618033988749895) % 1
            background_color = hsv_to_rgb(h, .5, .95)
            text_color = "#000000"

        h, s, v = rgb_to_hsv(background_color)
    else:
        background_color = bg

        text_color = tc

    # background_color = map(lambda x: (x + 255)/2, background_color)

    red = RedBaron(open("agendas/be.py", "r").read())

    for i in red("def", recursive=False):
        if target < i.name:
            break

    i.insert_before(template % {
        "background_color": rgb_to_hex(background_color) if not (isinstance(background_color, basestring) and background_color.startswith("#")) else background_color,
        "text_color": rgb_to_hex(text_color) if not (isinstance(text_color, basestring) and text_color.startswith("#")) else text_color,
        "url": target_url,
        "tags": ", ".join(map(repr, tags)),
        "function_name": target,
        "description": description,
        "meetup_name": target_meetup_name,
    })

    red.dumps()

    open("agendas/be.py", "w").write(red.dumps())

    os.system("python manage.py fetch_events %s" % target)
Example #11
0
def test_comma_proxy_list_set_item():
    red = RedBaron("[1]")
    comma_proxy_list = red[0].value
    comma_proxy_list[0] = "42"
    assert comma_proxy_list[0].type == "int"
    assert comma_proxy_list[0].value == 42
    comma_proxy_list[0] = "plop"
    assert comma_proxy_list[0].type == "name"
    assert comma_proxy_list[0].value == "plop"
    assert red.dumps() == "[plop]"
Example #12
0
def test_comma_proxy_list_indented_set_item():
    red = RedBaron("[\n    1,\n]")
    comma_proxy_list = red[0].value
    comma_proxy_list[0] = "42"
    assert comma_proxy_list[0].type == "int"
    assert comma_proxy_list[0].value == "42"
    comma_proxy_list[0] = "plop"
    assert comma_proxy_list[0].type == "name"
    assert comma_proxy_list[0].value == "plop"
    assert red.dumps() == "[\n    plop,\n]"
Example #13
0
def replace_argument(txt, pos, new):
    """
    """
    red = RedBaron(txt)
    fst = red.fst()[0]
    args  = fst['arguments']
    args = filter(arg_type_no_comma, args)
    args.pop(pos)
    args.insert(pos, new)
    res = reform_input(args, method=fst['name'])
    return res
Example #14
0
def get_method_body(method_code):
    '''Using the RedBaron module, get the body of a method.

    Do not want the definition signature line
    '''

    method_code = '\n' + method_code  # For some reason RedBaron has problems with this if
    #                                                     if it does not start with an empty line
    rb = RedBaron(method_code)
    def_node = rb.findAll("DefNode")[0]  # Look for the 'def' node. Should only be one!
    def_node.value.decrease_indentation(8)  # so that the code is all the way to the left
    return def_node.value.dumps()
Example #15
0
def remove_raise_skip_tests(src):
    """
    Remove from the code any raise unittest.SkipTest lines since we don't want those in
    what the user sees.
    """
    rb = RedBaron(src)
    raise_nodes = rb.findAll("RaiseNode")
    for rn in raise_nodes:
        # only the raise for SkipTest
        if rn.value[:2].dumps() == 'unittestSkipTest':
            rn.parent.value.remove(rn)
    return rb.dumps()
Example #16
0
def test_node_previous_recursive():
    red = RedBaron("def a():\n    b = 1\ndef c():\n    d = 1")
    assert red.previous is None
    assert red.previous_recursive is None
    first, second = red.find_all('def')
    assert second.previous is first
    inner = second.value.node_list
    assert inner[2].previous == inner[1]
    assert inner[2].previous_recursive == inner[1]
    assert inner[1].previous == inner[0]
    assert inner[1].previous_recursive == inner[0]
    assert inner[0].previous == None
    assert inner[0].previous_recursive == first
Example #17
0
def test_node_next_recursive():
    red = RedBaron("def a():\n    b = 1\ndef c():\n    d = 1")
    assert red.next is None
    assert red.next_recursive is None
    first, second = red.find_all('def')
    assert first.next is second
    inner = first.value.node_list
    assert inner[0].next == inner[1]
    assert inner[0].next_recursive == inner[1]
    assert inner[1].next == inner[2]
    assert inner[1].next_recursive == inner[2]
    assert inner[2].next == None
    assert inner[2].next_recursive == second
Example #18
0
def reform_input(args, method="foo"):
    """Re-give the def repr.

    - args: a list of dicts, representing redbaron's arguments

    - return: something like "def foo(args):" (without 'pass')
    """
    # https://redbaron.readthedocs.io/en/latest/nodes_reference.html#funcdefnode
    args = interpose_commas(args)
    newdef = "def {}(): pass".format(method)
    red = RedBaron(newdef)
    red[0].arguments = args
    res = red.dumps().strip()
    res = res.strip(" pass")
    return res
Example #19
0
def replace_asserts_with_prints(source_code):
    """
    Replace asserts with print statements.

    Using RedBaron, replace some assert calls with print statements that print the actual
    value given in the asserts.

    Depending on the calls, the actual value can be the first or second
    argument.
    """

    rb = RedBaron(source_code)  # convert to RedBaron internal structure

    for assert_type in ['assertAlmostEqual', 'assertLess', 'assertGreater', 'assertEqual',
                        'assert_equal_arrays', 'assertTrue', 'assertFalse']:
        assert_nodes = rb.findAll("NameNode", value=assert_type)
        for assert_node in assert_nodes:
            assert_node = assert_node.parent
            remove_redbaron_node(assert_node, 0)  # remove 'self' from the call
            assert_node.value[0].replace('print')
            if assert_type not in ['assertTrue', 'assertFalse']:
                remove_redbaron_node(assert_node.value[1], 1)  # remove the expected value argument

    assert_nodes = rb.findAll("NameNode", value='assert_rel_error')
    for assert_node in assert_nodes:
        assert_node = assert_node.parent
        # If relative error tolerance is specified, there are 4 arguments
        if len(assert_node.value[1]) == 4:
            remove_redbaron_node(assert_node.value[1], -1)  # remove the relative error tolerance
        remove_redbaron_node(assert_node.value[1], -1)  # remove the expected value
        remove_redbaron_node(assert_node.value[1], 0)  # remove the first argument which is
        #                                                  the TestCase
        assert_node.value[0].replace("print")

    assert_nodes = rb.findAll("NameNode", value='assert_almost_equal')
    for assert_node in assert_nodes:
        assert_node = assert_node.parent
        # If relative error tolerance is specified, there are 3 arguments
        if len(assert_node.value[1]) == 3:
            remove_redbaron_node(assert_node.value[1], -1)  # remove the relative error tolerance
        remove_redbaron_node(assert_node.value[1], -1)  # remove the expected value
        assert_node.value[0].replace("print")

    source_code_with_prints = rb.dumps()  # get back the string representation of the code
    return source_code_with_prints
Example #20
0
def test_rendering_iter():
    red = RedBaron("a + 2")
    assert list(red._generate_nodes_in_rendering_order()) == [
        red[0],
        red.name,
        red[0].first_formatting[0],
        red[0],
        red[0].second_formatting[0],
        red.int,
    ]
    assert list(red[0]._generate_nodes_in_rendering_order()) == [
        red[0],
        red.name,
        red[0].first_formatting[0],
        red[0],
        red[0].second_formatting[0],
        red.int,
    ]
Example #21
0
def sort_arguments(txt=""):
    """Txt (str): a valid python code of a def with arguments.

    Be careful, txt must be valid python code.

    example:
    : def foo(arg): pass

    - return: str (the def with sorted arguments).
    """

    if not txt:
        txt = "def foo(first, **kwargs, second=foo): pass"
    red = RedBaron(txt)

    fst = red.fst()[0]
    args = fst['arguments']
    args = filter(arg_type_no_comma, args)
    sargs = sorted(args, cmp=arg_lower)
    res = reform_input(sargs, method=fst['name'])
    return res
Example #22
0
def functionalize(src):
    red = RedBaron(src)
    red.insert(0, 'import pync')
    for func in red.find_all('def'):
        func.decorators.append('@pync.curry')

    for l in red.find_all('list') + red.find_all('list_comprehension'):
        l.replace("pync.list(%s)" % l)

    return red.dumps()
Example #23
0
def compare(s1, s2, decay_factor = PYTHON_DECAY_FACTOR):
    try:
        red1 = RedBaron(s1)
        red2 = RedBaron(s2)
        result = []

        defs = red2.find_all('def')
        length = len(defs)
        for ast_f2 in defs:
            ast_f1 = red1.find('def', name = ast_f2.name)        
            if ast_f1 is not None:
                additions, deletions = preprocess_files(ast_f1.dumps(),
                                                        ast_f2.dumps(),
                                                        ast_f2.absolute_bounding_box.top_left.line)
                comments, exceptions = preprocess_comments(ast_f2, additions) 
                for a in additions:
                    for c in comments:
                        line, _ = c.left_bounds
                        distance = line - a
                        score = compute_addition(c, distance, decay_factor)
                        c.setScore(score if score > 0 else 0)
                for d in deletions:
                    for c in comments:
                        line, _ = c.left_bounds
                        line = line + 1 if line >= d else line
                        distance = line - d
                        score = compute_deletion(c, distance, decay_factor)
                        c.setScore(score if score > 0 else 0)
                result.extend(comments)
                result.extend(exceptions)
            else:
                comments, _ = preprocess_comments(ast_f2, [])
                result.extend(comments)

        print_to_log('Result: ' + str(result))
        return result

    except Exception as e:
        err = 'CommentHealth compare error: ' + str(e)
        return []
Example #24
0
def test_rendering_iter():
    red = RedBaron("a + 2")
    assert_red = RedBaron("assert a == 5")
    assert list(red._generate_nodes_in_rendering_order()) == \
           [red[0],
            red.name,
            red[0].first_formatting[0],
            red[0],
            red[0].second_formatting[0],
            red.int]
    assert list(red[0]._generate_nodes_in_rendering_order()) == \
           [red[0],
            red.name,
            red[0].first_formatting[0],
            red[0],
            red[0].second_formatting[0],
            red.int]

    assert list(assert_red._generate_nodes_in_rendering_order()) == \
           [assert_red[0],
            assert_red[0].first_formatting[0],  # SpaceNode in AssertNode
            assert_red[0].value,  # ComparisonNode
            assert_red.name,
            assert_red[0].value.first_formatting[0],  # SpaceNode in ComparisonNode
            assert_red[0].value.value,  # ComparisonOperatorNode
            assert_red[0].value.second_formatting[0],  # SpaceNode in ComparisonNode
            assert_red.int]

    assert list(assert_red[0]._generate_nodes_in_rendering_order()) == \
           [assert_red[0],
            assert_red[0].first_formatting[0],  # SpaceNode in AssertNode
            assert_red[0].value,  # ComparisonNode
            assert_red.name,
            assert_red[0].value.first_formatting[0],  # SpaceNode in ComparisonNode
            assert_red[0].value.value,  # ComparisonOperatorNode
            assert_red[0].value.second_formatting[0],  # SpaceNode in ComparisonNode
            assert_red.int]
Example #25
0
def test_node_else_elseelseblock_previous_generator():
    red = RedBaron("if a:\n    pass\nelse:\n    pass")
    assert len(list(red.else_.previous_generator())) == 1
    red = RedBaron("chocolat\nif a:\n    pass\nelse:\n    pass\n")
    assert len(list(red.else_.previous_generator())) == 3
    red = RedBaron("chocolat\nif a:\n    pass\nelse:\n    pass\n")
    assert list(red.else_.previous_generator())[0] is red.if_

    red = RedBaron("chocolat\nif a:\n    pass\nelse:\n    pass\n")
    assert list(red.else_.previous_generator()) == [red.find("name", "chocolat"), red.find("endl"), red.if_][::-1]
    def handle(self, *args, **options):
        call_command('migrate', interactive = False)
        
        try:
            company = Entity.objects.get(owner=True)
            self.stdout.write('Already configured')
        except Entity.DoesNotExist:
            entity = Entity()
            entity.name = 'Your company'
            entity.description = 'Your company description'
            entity.endpoint = 'http://*****:*****@test.test'
            liaison.phone = '123456789'
            liaison.address = 'Testing Road'
            liaison.zip = '123456'
            liaison.city = 'Testicity'
            liaison.provider = entity
            liaison.save()
            
            base = getattr(settings, 'BASE_DIR')
            settings_path = os.path.join(base, 'Exchange', 'settings.py')
            
            with open(settings_path, 'r+') as f:
                read_data = f.read()
                f.seek(0)
                red = RedBaron(read_data)
                
                red.find("assignment", target=lambda x: x.dumps() == "SENDER").value.replace("'" + str(entity.id) + "'")
                
                secret_key = ''.join([choice('abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*(-_=+)') for i in range(50)])
                red.find("assignment", target=lambda x: x.dumps() == "SECRET_KEY").value.replace("'" + secret_key + "'")

                f.truncate()
                code = red.dumps()
                
                f.write(code)
            f.closed
            
            print('Database content created')
            print('Provider configured')
            print('New secret key generated')
            
            self.stdout.write('Setup complete')
Example #27
0
def test_comma_proxy_list_indented_delslice():
    red = RedBaron("[\n    1,\n    2,\n    3,\n    4,\n    5,\n    6,\n]")
    comma_proxy_list = red[0].value
    del comma_proxy_list[1:4]
    assert red.dumps() == "[\n    1,\n    5,\n    6,\n]"
Example #28
0
def test_import_multi():
    red = RedBaron("from m import a, b")
    assert red[0].targets.dumps() == "a, b"
Example #29
0
#!/usr/bin/python
# -*- coding:Utf-8 -*-
""" Tests the position feature """

import pytest
import redbaron
# pylint: disable=redefined-outer-name
from redbaron import RedBaron, Path

redbaron.DEBUG = True

fst = RedBaron("""\
@deco

def a(c, d):
    b = c + d
    e = 1
""")

positions = [
    (fst.def_.decorators[0], [(1, 1)]),
    (fst.def_.decorators[0].value.value[0], [(1, 2), (1, 3), (1, 4), (1, 5)]),
    # How to get this one ? (2, 0) and (2, 1) does not work, see out of scope
    #(fst.def_.decorators[1],                                [(?, ?)]),
    (fst.def_, [(3, 1), (3, 2), (3, 3)]),
    (fst.def_.first_formatting[0], [(3, 4)]),
    (fst.def_, [(3, 5), (3, 6)]),
    (fst.def_.arguments.node_list[0].target, [(3, 7)]),
    (fst.def_.arguments.node_list[1], [(3, 8)]),
    (fst.def_.arguments.node_list[1].second_formatting[0], [(3, 9)]),
    (fst.def_.arguments.node_list[2].target, [(3, 10)]),
Example #30
0
def test_empty():
    RedBaron("")
Example #31
0
def test_kwargs_only_marker_node():
    RedBaron("def a(*): pass")
Example #32
0
def test_is_list():
    assert list(RedBaron("")) == []
Example #33
0
def autoformat(filename, abort=False):
    # All the complexity in one place, pylint: disable=too-many-branches,too-many-statements
    print("Consider", filename, end=": ")

    old_code = open(filename, 'r').read()

    try:
        red = RedBaron(old_code)
        # red = RedBaron(old_code.rstrip()+'\n')
    except ParsingError:
        if abort:
            raise

        print("PARSING ERROR.")
        return 2

    def updateCall(call_node):
        max_len = 0
        for argument in call_node:
            if argument.type == "argument_generator_comprehension":
                return

            if hasattr(argument, "target") and argument.target is not None:
                key = argument.target.value
            else:
                key = None

            if key is not None:
                max_len = max(max_len, len(key))

        if '\n' not in call_node.second_formatting.dumps():
            del call_node.second_formatting[:]
            del call_node.third_formatting[:]

        for argument in call_node:
            if hasattr(argument, "target") and argument.target is not None:
                key = argument.target.value
            else:
                key = None

            if key is not None:
                if not argument.second_formatting:
                    argument.second_formatting = ' '

                if '\n' in str(call_node.second_formatting):
                    if argument.first_formatting:
                        spacing = argument.first_formatting[0].value
                    else:
                        spacing = ""

                    if len(key) + len(spacing) != max_len + 1:
                        argument.first_formatting = ' ' * (max_len - len(key) +
                                                           1)
                else:
                    argument.first_formatting = ' '
            else:
                if '\n' not in str(call_node.second_formatting):
                    if argument.value.type in ("string", "binary_string",
                                               "raw_string"):
                        argument.value.second_formatting = ""

    def updateTuple(tuple_node):
        if '\n' not in str(tuple_node.dumps()):
            tuple_node.second_formatting = ""
            tuple_node.third_formatting = ""

            if tuple_node.type == "tuple" and tuple_node.with_parenthesis:
                if tuple_node.value.node_list:
                    if tuple_node.value.node_list[-1].type not in (
                            "yield_atom", ):
                        tuple_node.value.node_list[-1].second_formatting = ""

            for argument in tuple_node.value:
                if argument.type in ("string", "binary_string", "raw_string"):
                    argument.second_formatting = ""

    def updateString(string_node):
        # Skip doc strings for now.
        if not hasattr(node.parent, "type") or \
           string_node.parent.type in ("class", "def", None):
            return

        value = string_node.value

        def isQuotedWith(quote):
            return value.startswith(quote) and value.endswith(quote)

        quote = None  # For PyLint.
        for quote in "'''", '"""', "'", '"':
            if isQuotedWith(quote):
                break
        else:
            sys.exit("Error, quote not understood.")

        real_value = value[len(quote):-len(quote)]
        assert quote + real_value + quote == value

        if '\n' not in real_value:
            # Single characters, should be quoted with "'"
            if len(eval(value)) == 1:  # pylint: disable=eval-used
                if real_value != "'":
                    string_node.value = "'" + real_value + "'"
            else:
                if '"' not in real_value:
                    string_node.value = '"' + real_value + '"'

    def updateDefNode(def_node):
        # This is between "def" and function name.
        def_node.first_formatting = ' '

        # This is after the opening/closing brace, we don't want it there.
        def_node.third_formatting = ""
        def_node.fourth_formatting = ""

        # This is to insert/remove spaces or new lines, depending on line length
        # so far, but is not functional at all.
        for argument_node in def_node.arguments:
            argument_node.first_formatting = ' '
            argument_node.second_formatting = ' '

    def updateCommentNode(comment_node):

        if "pylint:" in str(comment_node.value):

            def replacer(part):
                def renamer(pylint_token):
                    # pylint: disable=too-many-return-statements
                    if pylint_token == "E0602":
                        return "undefined-variable"
                    elif pylint_token in ("E0401", "F0401"):
                        return "import-error"
                    elif pylint_token == "E1102":
                        return "not-callable"
                    elif pylint_token == "E1133":
                        return "  not-an-iterable"
                    elif pylint_token == "E1128":
                        return "assignment-from-none"
# Save line length for this until isort is better at long lines.
                    elif pylint_token == "useless-suppression":
                        return "I0021"
#                     elif pylint_token == "I0021":
#                        return "useless-suppression"
                    elif pylint_token == "R0911":
                        return "too-many-return-statements"
                    elif pylint_token == "R0201":
                        return "no-self-use"
                    elif pylint_token == "R0902":
                        return "too-many-instance-attributes"
                    elif pylint_token == "R0912":
                        return "too-many-branches"
                    elif pylint_token == "R0914":
                        return "too-many-locals"
                    elif pylint_token == "R0915":
                        return "too-many-statements"
                    elif pylint_token == "W0123":
                        return "eval-used"
                    elif pylint_token == "W0603":
                        return "global-statement"
                    elif pylint_token == "W0613":
                        return "unused-argument"
                    elif pylint_token == "W0622":
                        return "redefined-builtin"
                    elif pylint_token == "W0703":
                        return "broad-except"
                    else:
                        return pylint_token

                return part.group(1) + ','.join(
                    sorted(
                        renamer(token) for token in part.group(2).split(',')))

            new_value = re.sub(r"(pylint\: disable=)(.*)",
                               replacer,
                               str(comment_node.value),
                               flags=re.M)
            comment_node.value = new_value

    for node in red.find_all("CallNode"):
        try:
            updateCall(node)
        except Exception:
            print("Problem with", node)
            node.help(deep=True, with_formatting=True)
            raise

    for node in red.find_all("TupleNode"):
        try:
            updateTuple(node)
        except Exception:
            print("Problem with", node)
            node.help(deep=True, with_formatting=True)
            raise

    for node in red.find_all("ListNode"):
        try:
            updateTuple(node)
        except Exception:
            print("Problem with", node)
            node.help(deep=True, with_formatting=True)
            raise

    for node in red.find_all("SetNode"):
        try:
            updateTuple(node)
        except Exception:
            print("Problem with", node)
            node.help(deep=True, with_formatting=True)
            raise

    for node in red.find_all("StringNode"):
        try:
            updateString(node)
        except Exception:
            print("Problem with", node)
            node.help(deep=True, with_formatting=True)
            raise

    for node in red.find_all("DefNode"):
        try:
            updateDefNode(node)
        except Exception:
            print("Problem with", node)
            node.help(deep=True, with_formatting=True)
            raise

    for node in red.find_all("CommentNode"):
        try:
            updateCommentNode(node)
        except Exception:
            print("Problem with", node)
            node.help(deep=True, with_formatting=True)
            raise

    new_code = red.dumps()

    if new_code != old_code:
        new_name = filename + ".new"

        with open(new_name, 'w') as source_code:
            source_code.write(red.dumps())

        # There is no way to safely replace a file on Windows, but lets try on Linux
        # at least.
        old_stat = os.stat(filename)

        try:
            os.rename(new_name, filename)
        except OSError:
            shutil.copy(new_name, filename)

        os.chmod(filename, old_stat.st_mode)

        print("updated.")
        changed = 1
    else:
        print("OK.")
        changed = 0

    return changed
Example #34
0
def test_import_on_new_line():
    red = RedBaron("from m import (\n" "   a)")
    assert red[0].targets.dumps() == "(\n   a)"
Example #35
0
def test_double_separator():
    red = RedBaron("fun(a,,)")
    assert red[0].dumps() == "fun(a,)"
Example #36
0
def test_import_multiline():
    red = RedBaron("from m import (a,\n   b)")
    assert red[0].targets.dumps() == "(a,\n   b)"
Example #37
0
def test_comment_in_args():
    red = RedBaron("fun(\n" "# comment\n" "a)")
    assert red[0].dumps() == "fun(\n# comment\na)"
Example #38
0
def test_mixmatch_with_redbaron_base_node_and_proxy_list_on_parent():
    red = RedBaron("foo = 42\nprint('bar')\n")
    red.insert(0, "baz")
    assert red[0].on_attribute == "root"
    assert red[0].parent is red
Example #39
0
def test_class_inline():
    code = """
class C: pass
"""
    red = RedBaron(code)
    assert red.dumps() == code
Example #40
0
def test_import():
    red = RedBaron("from m import a")
    assert red[0].targets.dumps() == "a"
Example #41
0
def test_def_inline():
    code = """
def fun(): pass
"""
    red = RedBaron(code)
    assert red.dumps() == code
Example #42
0
def test_name():
    red = RedBaron("a\n")
    assert len(red.value.node_list) == 2
    assert isinstance(red.value.node_list[0], NameNode)
    assert isinstance(red.value.node_list[1], EndlNode)
    assert red[0].value == "a"
Example #43
0
def test_path_str():
    red = RedBaron("name")
    assert str(Path(red[0]))
Example #44
0
import copy
import sys

from redbaron import RedBaron


in_file = out_file = sys.argv[1]

code = open(in_file).read()
red_baron = RedBaron(code)

super_nodes = red_baron.find_all('AtomtrailersNode')

for super_node in super_nodes:
    node = copy.copy(super_node)
    class_name = node.find_all('name')[1].name.dumps()

    while node.parent:
        node = node.parent
        if node.name == class_name:
            super_node.value[1] = '()'

with open(out_file, "w") as fh:
    fh.write(red_baron.dumps())
Example #45
0
def test_ternary_dict():
    code = """
{'scheme': 'https' if condition else 'http'}
"""
    red = RedBaron(code)
    assert red.dumps() == code
Example #46
0
#     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#     See the License for the specific language governing permissions and
#     limitations under the License.
#

from __future__ import print_function

import sys, os, shutil

from redbaron import RedBaron  # @UnresolvedImport

print("Consider", sys.argv[1], end = ": ")

old_code = open(sys.argv[1], "r").read()

red = RedBaron(old_code.rstrip()+"\n")

def updateCall(call_node):
    max_len = 0
    for argument in call_node:
        if argument.type == "argument_generator_comprehension":
            return

        if argument.target is not None:
            key = argument.target.value
        else:
            key = None

        if key is not None:
            max_len = max(max_len, len(key))
Example #47
0
 def _parse(python_file):
     with open(python_file) as fp:
         # NB: RedBaron is used instead of ``ast`` since it can round-trip from source code without
         # losing formatting. See: https://github.com/PyCQA/redbaron
         return RedBaron(fp.read())