19
19
from rich .columns import Columns
20
20
from rich .rule import Rule
21
21
from rich .status import Status
22
+ from rich .text import Text
23
+
24
+ console = Console ()
25
+ print = console .print
26
+
22
27
23
28
install_traceback (show_locals = False )
24
29
41
46
parser .add_argument ("-df" , help = "Input Absolute Path to Directory or File" )
42
47
# df is directory or file it will kinda figure out itself assuming theirs only 2 files in
43
48
# the dir and u do -neglect
44
- parser .add_argument ("-onefile" , help = "Input Absolute Path to File to Analyze" )
49
+ parser .add_argument (
50
+ "-onefile" , help = "Input Absolute Path to File to Analyze" )
45
51
parser .add_argument (
46
52
"-neglect" , help = "Input Absolute Path to File to Ignore ONLY IN DIRECTORY"
47
53
)
48
- #Argument if to get how many variables
49
- parser .add_argument ("--vars" , help = "Get how many variables are in the file" )
54
+ # Argument if to get how many variables
55
+ parser .add_argument (
56
+ "--vars" , help = "Get how many variables are in the file" )
50
57
# Debug argument to print out the file names
51
58
path = parser .parse_args ()
52
59
args = parser .parse_args ()
88
95
for dir_path , _ , filenames in os .walk (path .df ):
89
96
for f in filenames :
90
97
if f .endswith (".py" ):
91
- paths .append (os .path .abspath (os .path .join (dir_path , f )))
98
+ paths .append (os .path .abspath (
99
+ os .path .join (dir_path , f )))
92
100
# remove neglect from paths
93
101
if path .neglect is not None :
94
102
for line in paths :
@@ -143,28 +151,32 @@ def add_from_imports_results(from_import_list, result_dictionary):
143
151
def __scrape_imports (self , get_assets = True ):
144
152
result = {}
145
153
if len (self .directory ) > 1 :
146
- with Status ('[blue]Scraping imports from multiple files...' ):
154
+ with Status ('[blue]Scraping imports from multiple files...' ):
147
155
# if get_assets:
148
156
# print('[blue]Calculating assets ...\n')
149
157
for file_path in self .directory :
150
- from_imp = from_imports (input_file = file_path , get_assets = get_assets )
158
+ from_imp = from_imports (
159
+ input_file = file_path , get_assets = get_assets )
151
160
imp_ = import_imports (input_file = file_path )
152
161
153
162
self .add_from_imports_results (
154
163
from_import_list = from_imp , result_dictionary = result
155
164
)
156
165
157
- self .add_imports_to_results (import_list = imp_ , result_dictionary = result )
166
+ self .add_imports_to_results (
167
+ import_list = imp_ , result_dictionary = result )
158
168
else :
159
169
with Status ('[blue]Scraping imports from single file...' ):
160
- from_imp = from_imports (input_file = self .directory [0 ], get_assets = get_assets )
170
+ from_imp = from_imports (
171
+ input_file = self .directory [0 ], get_assets = get_assets )
161
172
imp_ = import_imports (input_file = self .directory [0 ])
162
173
163
174
self .add_from_imports_results (
164
175
from_import_list = from_imp , result_dictionary = result
165
176
)
166
177
167
- self .add_imports_to_results (import_list = imp_ , result_dictionary = result )
178
+ self .add_imports_to_results (
179
+ import_list = imp_ , result_dictionary = result )
168
180
169
181
return result
170
182
@@ -214,7 +226,8 @@ def line_count(self, exclude_empty_line: bool = False):
214
226
count = sum (1 for _ in open_file if _ .rstrip ("\n " ))
215
227
216
228
file_path = (
217
- file_path .split ("../" )[1 ] if ".." in file_path else file_path
229
+ file_path .split (
230
+ "../" )[1 ] if ".." in file_path else file_path
218
231
)
219
232
line_count [file_path ] = count
220
233
else :
@@ -242,7 +255,7 @@ def most_used_variable(self, n_variables=None):
242
255
reverse = True ,
243
256
)
244
257
}
245
-
258
+
246
259
if n_variables is not None :
247
260
return {k : v for k , v in list (most_used_variable .items ())[:n_variables ]}
248
261
else :
@@ -253,11 +266,11 @@ def get_import_names(self, import_type: str = "all"):
253
266
imports = self .__scrape_imports (get_assets = True )
254
267
255
268
from_ = [k for k in imports .keys () if "from" in k ]
256
- #sort by frequency of imports
269
+ # sort by frequency of imports
257
270
from_ .sort ()
258
271
259
272
import_ = [k for k in imports .keys () if "import" in k ]
260
- #Sort by frequency of import
273
+ # Sort by frequency of import
261
274
import_ .sort (key = lambda x : imports [x ], reverse = True )
262
275
import_ .sort ()
263
276
@@ -271,7 +284,7 @@ def get_import_names(self, import_type: str = "all"):
271
284
all_imports .extend (all_ )
272
285
all_imports = list (set (all_imports ))
273
286
all_imports .sort ()
274
-
287
+
275
288
return all_imports
276
289
else :
277
290
raise OutputNotSpecified (
@@ -298,16 +311,17 @@ def most_called_func(self):
298
311
for line in lines :
299
312
for each in func_names :
300
313
if each in line :
301
- most_called_func [each ] = most_called_func .get (each , 0 ) + 1
302
-
303
- #Sort by frquency of use
314
+ most_called_func [each ] = most_called_func .get (
315
+ each , 0 ) + 1
316
+
317
+ # Sort by frquency of use
304
318
most_called_func = {
305
319
k : v
306
320
for k , v in sorted (
307
321
most_called_func .items (), key = lambda item : item [1 ], reverse = True
308
322
)
309
323
}
310
- #if frequency is 1 then replace it with text 'Only Defined'
324
+ # if frequency is 1 then replace it with text 'Only Defined'
311
325
for key , value in most_called_func .items ():
312
326
if value == 1 :
313
327
most_called_func [key ] = "[red]Defined Only[/]"
@@ -392,11 +406,11 @@ def get_line_count(self):
392
406
def get_varible (self , n_variables = 3 ):
393
407
if args .vars :
394
408
n_variables = int (args .vars )
395
- print (len (self .stat .most_used_variable (100000 ))) #Gets max number of varibles assuming their not more then 100000 cloud implement a fix to this astro but it will do for now
409
+ # Gets max number of varibles assuming their not more then 100000 cloud implement a fix to this astro but it will do for now
410
+ print (len (self .stat .most_used_variable (100000 )))
396
411
if int (n_variables ) > int (len (self .stat .most_used_variable (100000 ))):
397
412
n_variables = int (len (self .stat .most_used_variable (100000 )))
398
-
399
-
413
+
400
414
if self .adhd_mode :
401
415
coul = self .get_random_colour ()
402
416
else :
@@ -428,9 +442,8 @@ def get_varible(self, n_variables=3):
428
442
# print(self.variable_panel)
429
443
return self .variable_panel
430
444
431
- def get_import_count (
432
- self ,
433
- ):
445
+ def get_import_count (self ):
446
+ learn = 0
434
447
if self .adhd_mode :
435
448
coul = self .get_random_colour ()
436
449
else :
@@ -440,36 +453,67 @@ def get_import_count(
440
453
else :
441
454
coulv2 = "pale_turquoise1"
442
455
imports = self .stat .import_count ()
443
- #get only from stuff from imports
456
+ # get only from stuff from imports
444
457
all_imports = imports
445
458
imports = {k : v for k , v in imports .items () if k .startswith ("import" )}
459
+ len_import_imports = len (imports )
446
460
# add \n after each element except after last element
447
461
imports_md = "\n " .join (
448
462
[f"[{ coulv2 } ]{ k } [/]: [{ coul } ]{ v } [/]" for k , v in imports .items ()]
449
463
)
464
+
450
465
imports_md = re .sub (r"(.*?): (\d+)" , r"\1: [b]\2[/]" , imports_md )
451
466
self .import_md = f"""[pale_turquoise1]{ imports_md } [/]"""
452
467
self .import_panel = Panel (
453
468
self .import_md ,
454
469
title = "[black]Count of 'import' statements" ,
455
470
title_align = "left" ,
456
471
border_style = "blue" ,
472
+ height = learn
457
473
)
474
+ #amount of elements in dict imports_md
458
475
459
- imports = {k : v for k , v in all_imports .items () if k .startswith ("from" )}
476
+
477
+ imports_from = {k : v for k , v in all_imports .items ()
478
+ if k .startswith ("from" )}
479
+ len_from_imports = len (imports_from )
460
480
# add \n after each element except after last element
461
- imports_md = "\n " .join (
462
- [f"[{ coulv2 } ]{ k } [/]: [{ coul } ]{ v } [/]" for k , v in imports .items ()]
481
+ imports_md_from = "\n " .join (
482
+ [f"[{ coulv2 } ]{ k } [/]: [{ coul } ]{ v } [/]" for k , v in imports_from .items ()]
463
483
)
464
- imports_md = re .sub (r"(.*?): (\d+)" , r"\1: [b]\2[/]" , imports_md )
465
- self .import_md = f"""[pale_turquoise1]{ imports_md } [/]"""
484
+ imports_md_from = re .sub (r"(.*?): (\d+)" , r"\1: [b]\2[/]" , imports_md_from )
485
+ self .import_md_from = f"""[pale_turquoise1]{ imports_md_from } [/]"""
486
+
487
+
466
488
self .from_imports = Panel (
467
- self .import_md ,
489
+ self .import_md_from ,
468
490
title = "[black]Count of 'from' statements" ,
469
491
title_align = "left" ,
470
492
border_style = "blue" ,
493
+ height = learn
494
+ )
495
+
496
+ if len_import_imports > len_from_imports :
497
+ learn = len_import_imports
498
+ elif len_import_imports < len_from_imports :
499
+ learn = len_from_imports
500
+
501
+ self .import_panel = Panel (
502
+ self .import_md ,
503
+ title = "[black]Count of 'import' statements" ,
504
+ title_align = "left" ,
505
+ border_style = "blue" ,
506
+ height = learn + 2
471
507
)
472
508
509
+ self .from_imports = Panel (
510
+ self .import_md_from ,
511
+ title = "[black]Count of 'from' statements" ,
512
+ title_align = "left" ,
513
+ border_style = "blue" ,
514
+ height = learn + 2
515
+ )
516
+
473
517
474
518
# print(self.import_panel)
475
519
return self .import_panel , self .from_imports
@@ -487,7 +531,8 @@ def get_most_called_func(self):
487
531
# add \n after each element except after last element
488
532
489
533
func_names_md = "\n " .join (
490
- [f"[{ coulv2 } ]{ k } [/]: [{ coul } ]{ v } [/]" for k , v in most_called_func .items ()]
534
+ [f"[{ coulv2 } ]{ k } [/]: [{ coul } ]{ v } [/]" for k ,
535
+ v in most_called_func .items ()]
491
536
)
492
537
493
538
func_names_md = re .sub (r"(.*?): (\d+)" , r"\1: [b]\2[/]" , func_names_md )
@@ -503,8 +548,10 @@ def get_most_called_func(self):
503
548
return self .func_panel
504
549
505
550
def get_all (self ):
551
+ imp_count = self .get_import_count ()
506
552
grp = Columns ([self .get_line_count (), self .get_varible ()])
507
- grp2 = Columns ([self .get_import_count ()[0 ], self .get_import_count ()[1 ]], padding = (0 , 1 ))
553
+ grp2 = Columns ([imp_count [0 ],
554
+ imp_count [1 ]], padding = (0 , 1 ))
508
555
mygrp = Group (
509
556
self .get_quickstat (),
510
557
Rule ('[black]Stats' ),
@@ -513,13 +560,13 @@ def get_all(self):
513
560
grp2 ,
514
561
)
515
562
516
- return Panel (mygrp , title = "[black]All Stat " , title_align = "middle" , width = None )
563
+ return Panel (mygrp , title = "[black]All Stats " , title_align = "middle" , width = None )
517
564
518
565
519
566
old_info = Stat (paths )
520
- info = VisualWrapper (paths )
567
+ info = VisualWrapper (paths , adhd_mode = True )
521
568
522
- print (info .get_all ())
569
+ print (info .get_all ())
523
570
524
571
# print(info.get_quickstat())
525
572
# print(info.get_line_count())
0 commit comments