コード例 #1
0
def handle_global_scope(node, tracker):
    """Internal method for handling names within the global scope

    Args:
        **node (:obj: `ast.Name`)**: Current Name  node

        **tracker (:obj: `DefinitionTracker`)**: The instance of the definition tracker for checking scope

    Returns:
        An ast node
    """
    if not is_in_function_scope_nested(node) and not is_in_class_scope_nested(
            node):
        if node.id in tracker.definitions['variables']:
            node.id = tracker.definitions['variables'][node.id]['new_name']
        elif node.id not in tracker.definitions['classes'] and node.id not in tracker.definitions['functions'] and type(
                node.parent) != ast.Call \
                and type(node.parent) != ast.ClassDef:
            tracker.add_variable({
                'prev_name': node.id,
                'new_name': hex_name(node.id)
            })
            node.id = tracker.definitions['variables'][node.id]['new_name']

    return node
コード例 #2
0
def create_class_dictionary(node):
    class_dict = {
        'new_name': hex_name(node.name),
        'prev_name': node.name,
        'variables': get_variables(node),
        'properties': get_properties(node),
        'methods': get_methods(node),
        'bases': get_base_classes(node)
    }
    return class_dict
コード例 #3
0
def get_return(node):
    """Creates a dictionary of returns defined within a function body

    Args:
        **node (:obj: `ast.FunctionDef`)**: The node to get the returns of

    Returns:
        A dictionary with return names for keys and obscured return names as values
    """
    return_ = {}
    for current_node in node.body:
        if type(current_node) == ast.Return:
            if type(current_node.value) == ast.Name:
                return_[current_node.value.id] = hex_name(
                    current_node.value.id)
            elif type(current_node.value) == ast.Call:
                if hasattr(current_node.value.func, 'id'):
                    return_[current_node.value.func.id] = hex_name(
                        current_node.value.func.id)

    return return_
コード例 #4
0
def get_args(node):
    """Creates a dictionary of parameters defined within a function body

    Args:
        **node (:obj: `ast.FunctionDef`)**: The node to get the args of

    Returns:
        A dictionary with arg names for keys and obscured arg names as values
    """
    args = {}
    for arg in node.args.args:
        args[arg.arg] = {'prev_name': arg.arg, 'new_name': hex_name(arg.arg)}

    return args
コード例 #5
0
def get_methods(node):
    """An ugly function that gets a list of class methods

    Args:
        **node (:obj: `ast.ClassDef`)**: The ClassDef node for which to get the methods of

    Returns:
        List of class methods (str), or empty list if none
    """
    methods = {}
    for method in node.body:
        if type(method) == ast.FunctionDef:
            if method.name != '__init__':
                methods[method.name] = hex_name(method.name)
            else:
                methods[method.name] = method.name
    return methods
コード例 #6
0
def get_variables(node):
    """Creates a dictionary of variables defined within a function body

    Args:
        **node (:obj: `ast.FunctionDef`)**: The node to get the variables of

    Returns:
        A dictionary with variable names for keys and obscured variable names as values
    """
    variables = {}
    for variable in node.body:
        if type(variable) == ast.Assign:
            for target in variable.targets:
                if type(target) == ast.Name:
                    variables[target.id] = {'new_name': hex_name(
                        target.id), 'prev_name': target.id}

    return variables
コード例 #7
0
def create_function_dictionary(node):
    """Creates a dictionary from a node describing a FunctionDef

    Args:
        **node (:obj: `ast.FunctionDef`)**: The node to create the dictionary for

    Returns:
        A dictionary
    """
    func_dict = {
        'new_name': hex_name(node.name),
        'prev_name': node.name,
        'variables': get_variables(node),
        'functions': get_functions(node),
        'args': get_args(node),
        'return': get_return(node),
    }

    return func_dict
コード例 #8
0
def get_variables(node):
    """An ugly function that gets a list of class variables

    Args:
        **node (:obj: `ast.ClassDef`)**: The ClassDef node for which to get the variables of

    Returns:
        List of class variables (str), or empty list if none
    """
    variables = {}
    for variable in node.body:
        if type(variable) == ast.Assign:
            for target in variable.targets:
                if type(target) == ast.Name:
                    variables[target.id] = {
                        'new_name': hex_name(target.id),
                        'prev_name': target.id
                    }
    return variables
コード例 #9
0
def get_properties(node):
    """An ugly function that gets a list of class properties

    Args:
        **node (:obj: `ast.ClassDef`)**: The ClassDef node for which to get the properties of

    Returns:
        List of class properties (str), or empty list if none
    """
    properties = {}
    for method in node.body:
        if type(method) == ast.FunctionDef:
            # This needs to check for all 'self' properties no just those in __init__
            for assign in method.body:
                if type(assign) == ast.Assign:
                    for target in assign.targets:
                        if hasattr(
                                target, 'value'
                        ) and target.value.id == 'self' and target.attr not in properties:
                            properties[target.attr] = hex_name(target.attr)
    return properties