Exemple #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
Exemple #2
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
Exemple #3
0
def _load_and_save(filename, cache_file, no_cache=False):
    """Save a tree to file and return the parsed RedBaron."""
    mkpath(os.path.split(cache_file)[0])

    with open(filename) as py:
        root = RedBaron(py.read())
        if not no_cache:
            with open(cache_file, 'w') as output:
                json.dump(root.fst(), output)

    return root
Exemple #4
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
Exemple #5
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
Exemple #6
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
Exemple #7
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
Exemple #8
0
def test_fst():
    some_code = "ax + (z * 4)"
    red = RedBaron(some_code)
    assert baron.parse(some_code) == red.fst()
def test_fst():
    some_code = "ax + (z * 4)"
    red = RedBaron(some_code)
    assert baron.parse(some_code) == red.fst()