Skip to content

Commit

Permalink
more refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
aerickson committed Aug 7, 2024
1 parent e00edde commit 2ac41d2
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 45 deletions.
61 changes: 16 additions & 45 deletions tf_authoritative_scanner/scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@
import argparse
import os.path

from tf_authoritative_scanner.util import _get_version, remove_leading_trailing_newline
from tf_authoritative_scanner.util import (
_get_version,
remove_leading_trailing_newline,
_verify_paths,
_get_first_two_word_parts,
)


class TFAuthoritativeScanner:
Expand Down Expand Up @@ -174,56 +179,21 @@ def run(self, paths):
print(f"PASS: {authoritative_files_found} of {total_files} scanned files are authoritative.")
sys.exit(0)


def print_tfas_banner():
# return
print(
remove_leading_trailing_newline(
r"""
def print_tfas_banner(self):
# return
print(
remove_leading_trailing_newline(
r"""
__ ___
/\ \__ /'___\
\ \ ,_\/\ \__/ __ ____
\ \ \/\ \ ,__\/'__`\ /',__\
\ \ \_\ \ \_/\ \L\.\_/\__, `\
\ \__\\ \_\\ \__/.\_\/\____/
\/__/ \/_/ \/__/\/_/\/___/
"""
"""
)
)
)


# TODO: move these to util files


def _verify_paths(paths):
for path in paths:
if not os.path.exists(path):
print(f"Error: The path '{path}' does not exist.")
sys.exit(1)


def _remove_inner_quotes(s):
# Define patterns for both single and double quotes
double_quote_pattern = r"\"([^\"]*?)\""
single_quote_pattern = r"\'([^\']*?)\'"

# Remove inner quotes for double quotes
s = re.sub(double_quote_pattern, lambda m: m.group(0).replace('"', ""), s)
# Remove inner quotes for single quotes
s = re.sub(single_quote_pattern, lambda m: m.group(0).replace("'", ""), s)

return s


# known issue: returns "", "" on less than two word-part strings
def _get_first_two_word_parts(string):
word_parts = string.split()
if len(word_parts) < 2:
return "", ""
first_word = _remove_inner_quotes(word_parts[0])
second_word = _remove_inner_quotes(word_parts[1])
return first_word, second_word


# TODO: move this to a cli.py file
Expand Down Expand Up @@ -251,8 +221,9 @@ def main():
parser.add_argument("--no-ascii-art", "-A", action="store_true", help="Do not print ASCII art")
args = parser.parse_args()

scanner = TFAuthoritativeScanner(args.include_dotdirs, args.verbose)
if not args.no_ascii_art:
print_tfas_banner()
scanner.print_tfas_banner()
# TODO: move this to scanner.run()
_verify_paths(args.paths)
scanner = TFAuthoritativeScanner(args.include_dotdirs, args.verbose)
scanner.run(args.paths)
32 changes: 32 additions & 0 deletions tf_authoritative_scanner/util.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import os
import codecs
import re
import sys


def _get_version(rel_path):
Expand All @@ -23,3 +25,33 @@ def remove_leading_trailing_newline(text):
if text.endswith("\n"):
text = text[:-1]
return text


def _verify_paths(paths):
for path in paths:
if not os.path.exists(path):
print(f"Error: The path '{path}' does not exist.")
sys.exit(1)


def _remove_inner_quotes(s):
# Define patterns for both single and double quotes
double_quote_pattern = r"\"([^\"]*?)\""
single_quote_pattern = r"\'([^\']*?)\'"

# Remove inner quotes for double quotes
s = re.sub(double_quote_pattern, lambda m: m.group(0).replace('"', ""), s)
# Remove inner quotes for single quotes
s = re.sub(single_quote_pattern, lambda m: m.group(0).replace("'", ""), s)

return s


# known issue: returns "", "" on less than two word-part strings
def _get_first_two_word_parts(string):
word_parts = string.split()
if len(word_parts) < 2:
return "", ""
first_word = _remove_inner_quotes(word_parts[0])
second_word = _remove_inner_quotes(word_parts[1])
return first_word, second_word

0 comments on commit 2ac41d2

Please sign in to comment.