Skip to content

Commit

Permalink
Modified exception handling, still looking into FileNotFoundError but…
Browse files Browse the repository at this point in the history
… for now it should work. Close #28 and Close #29
  • Loading branch information
jtbryan committed Mar 23, 2020
1 parent 87c062f commit c8fd367
Showing 1 changed file with 28 additions and 26 deletions.
54 changes: 28 additions & 26 deletions caanalyzer/analyzer.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@

import argparse
import os
import lizard
import json
import class_finder
import re
from cadistributor import log


class Analyzer:
'''
Provided a repository, uses static code analysis to output data about
Expand All @@ -33,7 +31,7 @@ def __init__(self, file_extension, file_path):
"num_lines": 0,
"file_extension": file_extension,
"file_name": file_path,
"methods": [], # Pair/Tuples with start and end lines of methods/classes
"methods": [], # Pair/Tuples with start and end lines of methods/classes
"classes": [],
"line_objs": [],
"nloc": None,
Expand All @@ -52,7 +50,7 @@ def __init__(self, line_num):
}

def __init__(self, ignorefile=None, expand_tabs=4, output_raw=True,
debug=False):
debug=False):
"""
output_raw:
Enabling this flag will add information about each line to the output json. This may significantly increase RAM usage and output size.
Expand All @@ -66,13 +64,21 @@ def __init__(self, ignorefile=None, expand_tabs=4, output_raw=True,

self.repo_obj = None

log.debug("Analyzer instance created.")
#log.debug("Analyzer instance created.")

'''
returns the serializable dictionary that can be outputted as a json file
Required argument: input_path - The path to a repo containing code.
Optional arguments: output_path, the name/path to the output json file.
'''
def class_finder(self, file_object):
for line_num, line in enumerate(file_object):
pattern = re.compile("^\#.*class.*\:")
if line.contains(pattern):
num_white = line.count(' ')
for line2 in file_object[line_num:]:
# check num whitespace before non commented line
pass

def analyze(self, input_path, output_path=None):

Expand Down Expand Up @@ -101,8 +107,7 @@ def analyze(self, input_path, output_path=None):
'''

# Walk through all files
# go through every file within a given directory
for subdir, dirs, files in os.walk(input_path):
for subdir, dirs, files in os.walk(input_path): # go through every file within a given directory
# Exclude hidden files/directories
# (see https://stackoverflow.com/questions/13454164/os-walk-without-hidden-folders)
files = [f for f in files if not f[0] == '.']
Expand All @@ -122,20 +127,20 @@ def analyze(self, input_path, output_path=None):
"num_lines": 0,
"file_extension": file_extension,
"file_name": file_path,
"methods": [], # Pair/Tuples with start and end lines of methods/classes
"methods": [], # Pair/Tuples with start and end lines of methods/classes
"classes": [],
"line_objs": [],
"nloc": None,
"token_count": 0
}

i = lizard.analyze_file(file_path)
try:
i = lizard.analyze_file(file_path)
except RecursionError:
log.err("Error with lizard analysis")
continue

file_obj["token_count"] = i.token_count
# ADDING LIST OF TUPLES OF CLASS INFO TO file_obj
# EACH TUPLE IS SIZE 2 WITH STARTING AND ENDING
# LINE NUMBER OF CLASS (ZERO INDEXED)
file_obj["classes"] = class_finder.find_classes(file_path)

# Append info about methods
for func_dict in i.function_list:
Expand All @@ -146,9 +151,9 @@ def analyze(self, input_path, output_path=None):
}
file_obj["methods"].append(method_obj)

# Go thru each line in the file
with open(file_path) as file:
try:
try:
# Go thru each line in the file
with open(file_path) as file:
for line_num, line in enumerate(file):
file_obj["num_lines"] += 1
# Don't bother with lines that are just a newline
Expand All @@ -169,8 +174,7 @@ def analyze(self, input_path, output_path=None):
line = line.expandtabs(self.expand_tabs)

# detect start & end index
line_obj["start_index"] = len(
line) - len(line.lstrip())
line_obj["start_index"] = len(line) - len(line.lstrip())
line_obj["end_index"] = len(line.rstrip())
# TODO: detecting imports

Expand Down Expand Up @@ -206,10 +210,10 @@ def analyze(self, input_path, output_path=None):
repo_obj["line_freqs"][line_num]["num_tabs"] += line_obj["num_tabs"]
repo_obj["line_freqs"][line_num]["num_spaces"] += line_obj["num_tabs"]

except UnicodeDecodeError or FileNotFoundError:
# TODO: add logger & note error
log.err("Unicode error")
continue
except Exception as e:
# TODO: add logger & note error
log.err("Unexpected error: " + str(e))
continue

# Add file obj to repo obj
repo_obj["file_objs"].append(file_obj)
Expand All @@ -223,9 +227,7 @@ def analyze(self, input_path, output_path=None):
repo_obj["num_lines"] += obj["num_lines"]

# get average lines per file
if repo_obj["num_files"] > 0:
repo_obj["avg_file_length"] = repo_obj["num_lines"] / \
repo_obj["num_files"]
repo_obj["avg_file_length"] = repo_obj["num_lines"] / repo_obj["num_files"]

if output_path is not None:
# Write the repo object to json
Expand Down

0 comments on commit c8fd367

Please sign in to comment.