-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPyStats.py
143 lines (123 loc) · 5.21 KB
/
PyStats.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import argparse
# Dashboard imports
import os
from collections import Counter
from rich import pretty, print
# Fine path called d:\1.Autohotkey\.Python\Project Folders\portswwww.py
from rich.traceback import install as install_traceback
# Usage: print(Panel.fit("Hello, [red]World!", title="Welcome", subtitle="Thank you"))
pretty.install()
install_traceback(show_locals=False)
# Dashboard imports \\ or pretty imports?
# Initializing Parser
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("-df", help="Input Absolute Path to Directory or File")
# df is directory or file it will kinda figure out itself assuming theirs only 2 files in
# the dir and u do -neglect
parser.add_argument('-onefile', help="Input Absolute Path to File to Analyze")
parser.add_argument("-neglect", help="Input Absolute Path to File to Ignore ONLY IN DIRECTORY")
# Debug argument to print out the file names
path = parser.parse_args()
# 2 Variables
# path.df
# path.onefile ignores everything and makes sure u have just onefile ur scanning
# Path.neglect
if path.onefile:
paths = [path.onefile]
else:
# Determine if it's a directory or file
if os.path.isdir(path.df):
# If it's a directory get all absolute paths of files in it ending with py
paths = []
for dir_path, _, filenames in os.walk(path.df):
for f in filenames:
if f.endswith('.py'):
paths.append(os.path.abspath(os.path.join(dir_path, f)))
# remove neglect from paths
if path.neglect is not None:
for line in paths:
original_line = line
line = line.replace("\\", "/")
if path.neglect in line:
paths.remove(original_line)
else:
paths = [path.df]
# That ends classification of the file
class Stat:
def __init__(self, directory) -> None:
self.directory = directory
# Add .py to directory if not there
if not self.directory[-1].endswith('.py'):
self.directory[-1] += '.py'
# Check if file exists
if not os.path.exists(self.directory[-1]):
print("[red]File does not exist")
exit()
# The Neglect keyword ain't needed because it's already delt in the if statement from before
def scrape_imports(self):
imports_ = []
real_imports = []
if len(self.directory) > 1:
print("[green]Scraping imports from multiple files...")
for path in self.directory:
with open(path, encoding='utf8') as f:
for line in f:
if line.startswith('import'):
imports_.append(line)
elif line.startswith('from'):
imports_.append(line)
else:
print("[green]Scraping imports from single file...")
with open(self.directory[0], encoding='utf8') as f:
for line in f:
if line.startswith('import'):
imports_.append(line)
elif line.startswith('from'):
imports_.append(line)
for line in imports_:
real_imports.append(line.strip())
return real_imports
def line_count(self):
# if it's a directory return the lines in a dict with the file name as the key
if len(self.directory) > 1:
line_count = {}
for path in self.directory:
with open(path, encoding='utf8') as f:
line_count[path] = len(f.readlines())
else:
with open(self.directory[0], encoding='utf8') as f:
line_count = len(f.readlines())
return line_count
def most_used_variable(self):
variables = self.scrape_variables()
variable_count = Counter(variables)
dictionary = {}
for key, value in variable_count.items():
dictionary[key] = value
most_used_variable = max(dictionary, key=dictionary.get)
return most_used_variable, dictionary[most_used_variable]
def scrape_variables(self, full_line_of_variable=False):
variables = []
var = {}
for i in self.directory:
with open(i, encoding='utf8') as f:
for line in f:
if ' = ' in line:
if full_line_of_variable:
variables.append(line)
variables.append(line.split()[0])
return variables
def get_import_names(self):
imports = self.scrape_imports()
import_names = []
for line in imports:
if line.startswith('import'):
import_names.append(line.split()[1])
elif line.startswith('from'):
import_names.append(line.split()[3])
return import_names
info = Stat(paths)
print(info.line_count(),
info.most_used_variable(),
info.scrape_variables())