vwc
vwc.wc.wc
Base class for word count (wc) implementations. This is the base UNIX implementation.
WC Objects
class WC()
Usage: wc [-cmlwL] [FILE]…
Count lines, words, and bytes for FILEs (or stdin)
create_parser
def create_parser() -> argparse.ArgumentParser
Create a basic argument parser with core options.
add_platform_args
def add_platform_args(parser)
Add platform-specific arguments - overridden by subclasses
parse_args
def parse_args(argv)
Parse command line arguments
get_display_width
def get_display_width(text)
Calculate the display width of text according to POSIX rules.
reset_counts
def reset_counts()
Reset file-specific counts.
reset_totals
def reset_totals()
Reset total counts.
process_line
def process_line(line)
Process a single line and update instance state.
get_counts_array
def get_counts_array(use_totals=False)
Get current counts as an array in the standard order.
print_line
def print_line(counts, filename, file=sys.stdout)
Format and print count line for a file, totals or preview.
print_totals
def print_totals(file=sys.stdout)
Print total counts.
print_counts
def print_counts(filename, file=sys.stdout)
Print counts for the current file.
print_progress
def print_progress(filename)
Show progress to stderr if it’s a TTY.
handle_error
def handle_error(error, filename)
Handle file error and report it.
set_status
def set_status(code)
Set the exit status - generic implementation.
get_file_names
def get_file_names()
Files to process, can be overriden
get_files
def get_files()
Get file objects to process based on arguments as a generator.
run
def run()
Process files and print counts.
update_totals
def update_totals()
Update total counts from current file counts.
process_file
def process_file(filename, file_obj)
Process a file and update instance state.
vwc.wc
get_wc
def get_wc() -> WC
Get the appropriate platform implementation by checking the $PATH.
We don’t use subprocess in here because the project will be flagged as unsafe by security tools. Which is fair.
vwc.wc.linux
Linux Objects
class Linux(WC)
Shared base class for BusyBox and GNU, since they have similar implementations.
get_file
def get_file(filename)
Open a file for reading. In Linux, ‘-‘ is treated as a regular file name.
handle_error
def handle_error(error, filename)
In Linux, print the counts on directories.
vwc.wc.busybox
BusyBox Objects
class BusyBox(Linux)
wc - word, line, and byte count
Usage: wc [-cmlwL] [FILE]…
Count lines, words, and bytes for FILEs (or stdin)
add_platform_args
def add_platform_args(parser)
BusyBox-specific arguments.
print_line
def print_line(counts, filename, file=sys.stdout)
Format and print count line for a file with BusyBox formatting.
use_padding
def use_padding()
BusyBox-specific padding rules. BusyBox uses padding for -L only when processing multiple files.
vwc.wc.gnu
GNU Objects
class GNU(Linux)
wc - print newline, word, and byte counts for each file
Usage: wc [OPTION]… [FILE]… or: wc [OPTION]… –files0-from=F
Print newline, word, and byte counts for each FILE, and a total line if more than one FILE is specified. A word is a non-zero-length sequence of characters delimited by white space.
With no FILE, or when FILE is -, read standard input.
add_platform_args
def add_platform_args(parser)
GNU-specific arguments.
get_file_names
def get_file_names()
Return list of file names from –files0-from or args.files.
print_totals
def print_totals(file=sys.stdout)
Print total counts.
print_counts
def print_counts(filename, file=sys.stdout)
Print counts for a file.
print_line
def print_line(counts, filename, file=sys.stdout)
GNU-specific line printing using width.
set_column_width
def set_column_width(filenames)
Do the same as compute_number_width in GNU’s wc.c
use_padding
def use_padding()
GNU-specific padding rules.
vwc.wc.bsd
BSD Objects
class BSD(WC)
wc - count lines, words, characters, and bytes
Usage: wc [-clmwL] [file …]
Count lines, words, characters, and bytes for each input file. With no file, or when file is -, read standard input.
add_platform_args
def add_platform_args(parser)
BSD-specific arguments.
print_line
def print_line(counts, filename, file=sys.stdout)
Format and print count line for a file with BSD formatting.
vwc.main
Main entry point for vwc.
main
def main()
Main entry point.