<commit_before>from __future__ import print_function

import copy

from ast_tool_box.controllers.tree_transform_controller import TreeTransformController
import ast_tool_box.models.code_models.code_model as code_model
import ast_tool_box.models.transform_models.transform_file as transform_model
import ast_tool_box.controllers.transform_presenter as transform_controllers

from ast_tool_box.views.code_views.code_pane import CodePane
import ast


class CodePresenter(object):
    def __init__(self, tree_transform_controller=None):
        print("tree_transform_controller %s" % tree_transform_controller)
        print("type is                   %s" % TreeTransformController)
        assert isinstance(tree_transform_controller, TreeTransformController), "got %s" % tree_transform_controller

        self.code_items = []
        self.transform_presenter = None
        self.tree_transform_controller = tree_transform_controller

        self.code_pane = CodePane(code_presenter=self)

    def new_file(self, file_name):
        self.clear()
        self.new_item_from_file(file_name)

    def set_transform_presenter(self, transform_presenter):
        assert isinstance(transform_presenter, transform_controllers.TransformPresenter)
        self.transform_presenter = transform_presenter

    def clear(self):
        while len(self.code_items) > 0:
            self.delete_last_item()
        self.code_pane.clear()

    def delete_last(self):
        self.delete(len(self.code_items)-1)

    def count(self):
        return len(self.code_items)

    def delete_last_item(self):
        self.code_items.remove(self.code_items[-1])

    def __getitem__(self, item):
        return self.code_items[item]

    def __iter__(self):
        return iter(self.code_items)

    def get_valid_index(self, index):
        """
        convenience method for checking index,
        if index is a string it will be converted to int
        None returned if failed to convert or index out of range
        """
        if not isinstance(index, int):
            try:
                index = int(index)
            except ValueError:
                return None

        if index >= 0:
            if index < len(self.code_items):
                return index
        return None

    def current_item(self):
        return self.code_items[-1]

    def add_code_item(self, code_item):
        self.code_items.append(code_item)
        self.code_pane.add_code_item(code_item)

    def resolve_transform_args(self, transform_thing):
        return self.code_pane.resolve_transform_arguments(transform_thing)

    def apply_transform(self, code_item, transform_item):
        """
        transform some kind of code thing into another kind of code thing
        using some kind of transform thing, update views accordingly
        """
        if isinstance(code_item, ast.AST):
            code_item = code_model.AstTreeItem(code_item)

        assert isinstance(code_item, code_model.CodeItem)
        if transform_item is not None:
            assert isinstance(transform_item, transform_model.TransformThing), "bad type %s" % transform_item

        def apply_codegen_transform(ast_root, argument_list):
            """
            Code generates each file in the project
            """
            import ctree.nodes
            assert isinstance(ast_root, ctree.nodes.Project), \
                "apply_code_gen root of tree not Project is a %s" % ast_root

            # TODO: stay more in line with ctree and use ResolveGeneratedPathRefs

            # transform all files a combined source_text string
            combined_source = ""
            if ast_root.files and len(ast_root.files) > 0:
                for f in ast_root.files:
                    combined_source += "File %s\n" % f.name
                    combined_source += f.codegen()
                return combined_source
            else:
                return ast_root.codegen()

        if isinstance(code_item, code_model.FileItem):
            self.show_error("Transformation cannot be applied to source_text code")
        elif isinstance(code_item, code_model.AstTreeItem):
            if isinstance(transform_item, transform_model.AstTransformItem):<commit_msg>Allow codegen of non project nodes<commit_after>from __future__ import print_function

import copy

from ast_tool_box.controllers.tree_transform_controller import TreeTransformController
import ast_tool_box.models.code_models.code_model as code_model
import ast_tool_box.models.transform_models.transform_file as transform_model
import ast_tool_box.controllers.transform_presenter as transform_controllers

from ast_tool_box.views.code_views.code_pane import CodePane
import ast


class CodePresenter(object):
    def __init__(self, tree_transform_controller=None):
        print("tree_transform_controller %s" % tree_transform_controller)
        print("type is                   %s" % TreeTransformController)
        assert isinstance(tree_transform_controller, TreeTransformController), "got %s" % tree_transform_controller

        self.code_items = []
        self.transform_presenter = None
        self.tree_transform_controller = tree_transform_controller

        self.code_pane = CodePane(code_presenter=self)

    def new_file(self, file_name):
        self.clear()
        self.new_item_from_file(file_name)

    def set_transform_presenter(self, transform_presenter):
        assert isinstance(transform_presenter, transform_controllers.TransformPresenter)
        self.transform_presenter = transform_presenter

    def clear(self):
        while len(self.code_items) > 0:
            self.delete_last_item()
        self.code_pane.clear()

    def delete_last(self):
        self.delete(len(self.code_items)-1)

    def count(self):
        return len(self.code_items)

    def delete_last_item(self):
        self.code_items.remove(self.code_items[-1])

    def __getitem__(self, item):
        return self.code_items[item]

    def __iter__(self):
        return iter(self.code_items)

    def get_valid_index(self, index):
        """
        convenience method for checking index,
        if index is a string it will be converted to int
        None returned if failed to convert or index out of range
        """
        if not isinstance(index, int):
            try:
                index = int(index)
            except ValueError:
                return None

        if index >= 0:
            if index < len(self.code_items):
                return index
        return None

    def current_item(self):
        return self.code_items[-1]

    def add_code_item(self, code_item):
        self.code_items.append(code_item)
        self.code_pane.add_code_item(code_item)

    def resolve_transform_args(self, transform_thing):
        return self.code_pane.resolve_transform_arguments(transform_thing)

    def apply_transform(self, code_item, transform_item):
        """
        transform some kind of code thing into another kind of code thing
        using some kind of transform thing, update views accordingly
        """
        if isinstance(code_item, ast.AST):
            code_item = code_model.AstTreeItem(code_item)

        assert isinstance(code_item, code_model.CodeItem)
        if transform_item is not None:
            assert isinstance(transform_item, transform_model.TransformThing), "bad type %s" % transform_item

        def apply_codegen_transform(ast_root, argument_list):
            """
            Code generates each file in the project
            """
            import ctree.nodes
            # assert isinstance(ast_root, ctree.nodes.Project), \
            #     "apply_code_gen root of tree not Project is a %s" % ast_root

            # TODO: stay more in line with ctree and use ResolveGeneratedPathRefs

            # transform all files a combined source_text string
            combined_source = ""
            if isinstance(ast_root, ctree.nodes.Project) and ast_root.files and len(ast_root.files) > 0:
                for f in ast_root.files:
                    combined_source += "File %s\n" % f.name
                    combined_source += f.codegen()
                return combined_source
            else:
                return ast_root.codegen()

        if isinstance(code_item, code_model.FileItem):
            self.show_error("Transformation cannot be applied to source_text code")
        elif isinstance(code_item, code_model.AstTreeItem):
            if isinstance(transform_item, transform_model.AstTransformItem):
{'fraction_non_alphanumeric': 0.06999648258881463, 'fraction_numerical': 0.0014069644741470278, 'mean_word_length': 2.6499786050492085, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5989534', 'n_tokens_mistral': 2331, 'n_tokens_neox': 2273, 'n_words': 571}