示例#1
0
def execute_python_code_in_parallel_thread(file):
    """ This runs in a separate thread. """
    var_numbers = VarMiddle().value(file)
    nested_for_blocks = NestedBlocks(2, block_type=BlockType.FOR).value(file)
    nested_if_blocks = NestedBlocks(2, block_type=BlockType.IF).value(file)
    entropy = Entropy().value(file)
    spaces = SpacesCounter().value(file)
    concat_str_number = StringConcatFinder().value(file)
    return file, var_numbers, nested_for_blocks, nested_if_blocks, entropy, spaces, concat_str_number
示例#2
0
 def value(self, filename: str) -> List[int]:
     '''Return line numbers in the file where patterns are found'''
     pattern = NestedBlocks(
         2,
         [
             BlockType.WHILE,
             BlockType.FOR,
             BlockType.DO
         ]
     )
     return pattern.value(filename)
示例#3
0
def main():
    exit_status = -1
    try:
        depth_for = 2
        depth_if = 2
        parser = argparse.ArgumentParser(
            description=
            'Find the pattern which has the largest impact on readability')
        parser.add_argument('--filename', help='path for Java file')
        parser.add_argument(
            '--version',
            action='version',
            version='%(prog)s {version}'.format(version=__version__))

        args = parser.parse_args(args=None if sys.argv[1:] else ['--help'])

        if args:
            java_file = str(Path(os.getcwd(), args.filename))

            order_queue = queue.Queue()
            order_queue.put([
                VarMiddle().value(java_file),
                'variable declaration in the middle of the function'
            ])
            order_queue.put([
                NestedBlocks(depth_for,
                             block_type=BlockType.FOR).value(java_file),
                'nested for cycle with depth = {}'.format(depth_for)
            ])
            order_queue.put([
                NestedBlocks(depth_if,
                             block_type=BlockType.IF).value(java_file),
                'nested if condition with depth = 2'.format(depth_if)
            ])
            order_queue.put([
                StringConcatFinder().value(java_file),
                'string concatenation with operator +'
            ])

            lines, output_string = order_queue.get()
            if not lines:
                print('Your code is perfect in aibolit\'s opinion')
            for line in lines:
                if line:
                    print('Line {}. Low readability due to: {}'.format(
                        line, output_string))
            order_queue.task_done()
            exit_status = 0
    except KeyboardInterrupt:
        exit_status = -1
    sys.exit(exit_status)
示例#4
0
class TestNestedBlocks(TestCase):
    depth_level = 2
    cur_file_dir = Path(os.path.realpath(__file__)).parent
    testClass = NestedBlocks(depth_level)

    def test_single_for_loop(self):
        file = str(Path(self.cur_file_dir, 'SingleFor.java'))
        self.assertEqual(self.testClass.value(file), [15, 19])

    def test_nested_for_loops(self):
        file = str(Path(self.cur_file_dir, 'NestedFor.java'))
        self.assertEqual(self.testClass.value(file), [22])

    def test_for_loops_in_different_methods(self):
        file = str(Path(self.cur_file_dir, 'DifferentMethods.java'))
        self.assertEqual(self.testClass.value(file), [28])

    def test_for_loops_in_nested_class(self):
        file = str(Path(self.cur_file_dir, 'NestedForInNestedClasses.java'))
        self.assertEqual(self.testClass.value(file), [9])

    def test_for_loops_in_anonymous_class(self):
        file = str(Path(self.cur_file_dir, 'ForInAnonymousFile.java'))
        self.assertEqual(self.testClass.value(file), [19])

    def test_nested_no_nested_if(self):
        pattern = NestedBlocks(2, BlockType.IF)
        file = str(Path(self.cur_file_dir, 'NestedNoIF.java'))
        self.assertEqual(pattern.value(file), [])

    def test_nested_if(self):
        pattern = NestedBlocks(2, BlockType.IF)
        file = str(Path(self.cur_file_dir, 'NestedIF.java'))
        self.assertEqual(pattern.value(file), [21, 42])
示例#5
0
 def test_nested_if(self):
     pattern = NestedBlocks(2, BlockType.IF)
     file = str(Path(self.cur_file_dir, 'NestedIF.java'))
     self.assertEqual(pattern.value(file), [21, 42])
示例#6
0
 def test_nested_no_nested_if(self):
     pattern = NestedBlocks(2, ASTNodeType.IF_STATEMENT)
     file = str(Path(self.cur_file_dir, 'NestedNoIF.java'))
     self.assertEqual(pattern.value(file), [])
 def test_nested_if(self):
     filepath = self.current_directory / "NestedIF.java"
     ast = AST.build_from_javalang(build_ast(filepath))
     pattern = NestedBlocks(2, ASTNodeType.IF_STATEMENT)
     lines = pattern.value(ast)
     self.assertEqual(lines, [21, 42])
 def test_for_loops_in_anonymous_class(self):
     filepath = self.current_directory / "ForInAnonymousFile.java"
     ast = AST.build_from_javalang(build_ast(filepath))
     pattern = NestedBlocks(2, ASTNodeType.FOR_STATEMENT)
     lines = pattern.value(ast)
     self.assertEqual(lines, [19])
 def test_for_loops_in_different_methods(self):
     filepath = self.current_directory / "DifferentMethods.java"
     ast = AST.build_from_javalang(build_ast(filepath))
     pattern = NestedBlocks(2, ASTNodeType.FOR_STATEMENT)
     lines = pattern.value(ast)
     self.assertEqual(lines, [28])
 def test_single_for_loop(self):
     filepath = self.current_directory / "SingleFor.java"
     ast = AST.build_from_javalang(build_ast(filepath))
     pattern = NestedBlocks(2, ASTNodeType.FOR_STATEMENT)
     lines = pattern.value(ast)
     self.assertEqual(lines, [15, 19])
示例#11
0
from aibolit.patterns.var_middle.var_middle import VarMiddle


def format_f(e):
    return f'{e}-{e}'


FILEPATH_TO_READ = '/01/found-java-files.txt'
DIR_TO_CREATE = '03'
FILE_TO_SAVE = 'patterns.csv'

current_location: str = os.path.realpath(
    os.path.join(os.getcwd(), os.path.dirname(__file__))
)

pattern_for = NestedBlocks(2, BlockType.FOR)
pattern_if = NestedBlocks(2, BlockType.IF)
pattern_var_middle = VarMiddle()
data = []
with open(current_location + FILEPATH_TO_READ) as fp:
    for line in fp.readlines():
        filename = line.strip()
        var_middle = list(map(format_f, pattern_var_middle.value(filename)))
        nested_fors = list(map(format_f, pattern_for.value(filename)))
        nested_ifs = list(map(format_f, pattern_if.value(filename)))
        data += [(
            filename,
            ';'.join(nested_fors),
            ';'.join(nested_ifs),
            ';'.join(var_middle)
        )]
示例#12
0
 def test_nested_no_nested_if(self):
     pattern = NestedBlocks(2, BlockType.IF)
     file = str(Path(self.cur_file_dir, 'NestedNoIF.java'))
     assert pattern.value(file) == []