11import argparse
2- import os
32from pathlib import Path
43
5-
64# Argument Handling
75parser = argparse .ArgumentParser (
86 prog = "wc" ,
2018metrics = ["line_count" , "word_count" , "byte_size" ]
2119displayed_metrics = []
2220
21+
2322# ===== wc Procedure =====
2423def wc (args ):
25- cwd = os .getcwd ()
2624 file_names = args .files
2725
2826 execute_flags ()
29- all_files_data = add_totals (extract_files_data (file_names , cwd ))
27+ all_files_data = add_totals (extract_files_data (file_names ))
3028 print_output (all_files_data )
3129
30+
3231# ===== Flag Handling =====
3332def execute_flags ():
3433 if args .w :
@@ -38,44 +37,50 @@ def execute_flags():
3837 if args .c :
3938 displayed_metrics .append ("byte_size" )
4039
40+
4141# ===== Extracting Files Data =====
42- def extract_files_data (file_names , cwd ):
42+ def extract_files_data (file_names ):
4343 all_files_data = []
4444
4545 for file_name in file_names :
4646 file_data = {}
4747 file_data ["name" ] = file_name
48- file_data ["text" ] = read_file (file_name , cwd )
48+ file_data ["text" ] = read_file (file_name )
4949 file_data ["line_count" ] = calculate_line_count (file_data ["text" ])
5050 file_data ["word_count" ] = calculate_word_count (file_data ["text" ])
51- file_data ["byte_size" ] = read_byte_size (file_name , cwd )
51+ file_data ["byte_size" ] = read_byte_size (file_name )
5252
5353 all_files_data .append (file_data )
5454
5555 return all_files_data
5656
57- def read_file (file_name , cwd ):
58- file_path = Path (cwd ) / file_name
59- return file_path .read_text (encoding = "utf-8" ).rstrip ()
57+
58+ def read_file (file_name ):
59+ file_path = Path (file_name )
60+ return file_path .read_text (encoding = "utf-8" ).rstrip ()
61+
6062
6163def calculate_line_count (text ):
62- if not text :
63- return 0
64- return len (text .splitlines ())
64+ if not text :
65+ return 0
66+ return len (text .splitlines ())
67+
6568
6669def calculate_word_count (text ):
67- return len (text .split ())
70+ return len (text .split ())
71+
72+
73+ def read_byte_size (file_name ):
74+ file_path = Path (file_name )
75+ return Path (file_path ).stat ().st_size
6876
69- def read_byte_size (file_name , cwd ):
70- file_path = os .path .join (cwd , file_name )
71- return os .path .getsize (file_path )
7277
7378def add_totals (all_files_data ):
7479 if len (all_files_data ) <= 1 :
7580 return all_files_data
7681
7782 totals_data = {"name" : "total" }
78-
83+
7984 for metric in metrics :
8085 metric_sum = 0
8186 for file in all_files_data :
@@ -85,6 +90,7 @@ def add_totals(all_files_data):
8590 all_files_data .append (totals_data )
8691 return all_files_data
8792
93+
8894# ===== Outputting Data =====
8995def print_output (output_data ):
9096 for file in output_data :
@@ -97,5 +103,6 @@ def print_output(output_data):
97103 output_string += f" { file ['name' ]} "
98104 print (output_string )
99105
106+
100107# ===== Run wc =====
101- wc (parser .parse_args ())
108+ wc (parser .parse_args ())
0 commit comments