Skip to content

Commit

Permalink
style: Add pre-commit ruff and meta hooks, format
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewferrier committed Aug 30, 2024
1 parent 9bf3594 commit 641a9a6
Show file tree
Hide file tree
Showing 9 changed files with 1,021 additions and 483 deletions.
9 changes: 9 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# See https://pre-commit.com for more information
---
repos:
- repo: meta
hooks:
- id: check-hooks-apply
- id: check-useless-excludes
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.6.0
hooks:
Expand All @@ -20,3 +24,8 @@ repos:
rev: v3.17.0
hooks:
- id: pyupgrade
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.6.3
hooks:
- id: ruff
- id: ruff-format
42 changes: 13 additions & 29 deletions normfn
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ def main(argv, syserr_handler):
"--verbose",
action="count",
default=0,
help="Add debugging output. "
"Using this twice makes it doubly verbose.",
help="Add debugging output. " "Using this twice makes it doubly verbose.",
)

_ = parser.add_argument(
Expand Down Expand Up @@ -201,9 +200,7 @@ def main(argv, syserr_handler):
"This is the default.",
)

parser.set_defaults(
time_option="earliest", undo_log_file=get_default_log_file()
)
parser.set_defaults(time_option="earliest", undo_log_file=get_default_log_file())

class FilenamesAction(argparse.Action):
# pylint: disable=too-few-public-methods
Expand Down Expand Up @@ -376,11 +373,7 @@ def datetime_prefix(args, non_extension: str, filename: str):
if matchobj.group("prefix") != ""
else ""
)
+ (
matchobj.group("suffix")
if matchobj.group("suffix") != ""
else ""
)
+ (matchobj.group("suffix") if matchobj.group("suffix") != "" else "")
)

logger.debug("replacement() returned: " + replaceValue)
Expand All @@ -407,7 +400,9 @@ def datetime_prefix(args, non_extension: str, filename: str):
DATE_TIME_SEPARATOR = r"([-_T\s]|\sat\s|,\s)"

REGEX = (
r"^(?P<prefix>.*?)[-_]?" + r"(" +
r"^(?P<prefix>.*?)[-_]?"
+ r"("
+
# Y-M-D style
r"(?P<year1>"
+ YEAR
Expand Down Expand Up @@ -473,13 +468,10 @@ def datetime_prefix(args, non_extension: str, filename: str):

if args.add_time:
newname = (
timetouse.strftime("%Y-%m-%dT%H-%M-%S")
+ newname_with_dash_if_needed
timetouse.strftime("%Y-%m-%dT%H-%M-%S") + newname_with_dash_if_needed
)
else:
newname = (
timetouse.strftime("%Y-%m-%d") + newname_with_dash_if_needed
)
newname = timetouse.strftime("%Y-%m-%d") + newname_with_dash_if_needed

return newname

Expand All @@ -493,9 +485,7 @@ def process_filename(filename: str, args):
if not args.all:
(exclude, why) = should_exclude(filename, basename)
if exclude:
logger.info(
f"Skipping {filename.strip()} as it matches pattern {why}"
)
logger.info(f"Skipping {filename.strip()} as it matches pattern {why}")
return filename

(non_extension, extension) = os.path.splitext(basename)
Expand All @@ -506,9 +496,7 @@ def process_filename(filename: str, args):

original_filename = filename
filename = os.path.join(os.path.dirname(original_filename), newname)
logger.debug(
f"Potential new filename for {original_filename} is {filename}"
)
logger.debug(f"Potential new filename for {original_filename} is {filename}")

if filename == original_filename:
logger.debug("New filename would be identical, skipping.")
Expand Down Expand Up @@ -586,7 +574,7 @@ def validate_move(args, original_filename: str, filename: str) -> None:
)


def rlinput(prompt: str, prefill: str=""):
def rlinput(prompt: str, prefill: str = ""):
if os.name == "nt":
return input(prompt)
else:
Expand Down Expand Up @@ -629,9 +617,7 @@ def shiftfile(args, source: str, target: str):
def check_undo_log_file_header(args):
if not os.path.exists(args.undo_log_file):
with open(args.undo_log_file, "w") as log_file:
wrapper = textwrap.TextWrapper(
initial_indent="# ", subsequent_indent="# "
)
wrapper = textwrap.TextWrapper(initial_indent="# ", subsequent_indent="# ")
log_file.write("#!/bin/sh\n")
log_file.write(
wrapper.fill(
Expand Down Expand Up @@ -700,9 +686,7 @@ def readchar():


def insensitiveize(string: str) -> str:
return "".join(
map(lambda char: ("[" + char.lower() + char.upper() + "]"), string)
)
return "".join(map(lambda char: ("[" + char.lower() + char.upper() + "]"), string))


class FatalException(Exception):
Expand Down
65 changes: 48 additions & 17 deletions tests/BaseTestClasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,48 @@


class NormalizeFilenameTestCase(unittest.TestCase):
COMMAND = os.path.normpath(os.path.join(os.getcwd(), 'normfn'))
COMMAND = os.path.normpath(os.path.join(os.getcwd(), "normfn"))

def setUp(self):
self.workingDir = tempfile.mkdtemp()

def getDatePrefix(self, postfixDash=True):
if (postfixDash is True):
if postfixDash is True:
return datetime.now().strftime("%Y-%m-%d-")
else:
return datetime.now().strftime("%Y-%m-%d")

def directoryFileCount(self, directory):
return len([item for item in os.listdir(directory) if os.path.isfile(os.path.join(directory, item))])
return len(
[
item
for item in os.listdir(directory)
if os.path.isfile(os.path.join(directory, item))
]
)

def directoryDirCount(self, directory):
return len([item for item in os.listdir(directory) if os.path.isdir(os.path.join(directory, item))])
return len(
[
item
for item in os.listdir(directory)
if os.path.isdir(os.path.join(directory, item))
]
)

def getOriginalScriptPath(self):
module_path = inspect.getfile(inspect.currentframe())
module_path = os.path.join(os.path.dirname(os.path.dirname(module_path)), 'normfn')
module_path = os.path.join(
os.path.dirname(os.path.dirname(module_path)), "normfn"
)

return module_path

def invokeDirectly(self, inputFiles, extraParams=[]):
import importlib.machinery

module_path = self.getOriginalScriptPath()
loader = importlib.machinery.SourceFileLoader('normfn', module_path)
loader = importlib.machinery.SourceFileLoader("normfn", module_path)
spec = spec_from_loader(os.path.basename(module_path), loader)
normalize_filename = module_from_spec(spec)
spec.loader.exec_module(normalize_filename)
Expand All @@ -51,11 +66,11 @@ def invokeDirectly(self, inputFiles, extraParams=[]):

options.extend(inputFiles)
options.extend(extraParams)
options.extend(['--no-undo-log-file'])
options.extend(["--no-undo-log-file"])

stream = io.StringIO()
handler = logging.StreamHandler(stream)
log = logging.getLogger('normfn')
log = logging.getLogger("normfn")
log.propagate = False
log.setLevel(logging.DEBUG)
log.addHandler(handler)
Expand All @@ -70,21 +85,29 @@ def invokeDirectly(self, inputFiles, extraParams=[]):

return error

def invokeAsSubprocess(self, inputFiles, extraParams=[], feedInput=None, cwd=None, expectOutput=False, useUndoFile=False):
def invokeAsSubprocess(
self,
inputFiles,
extraParams=[],
feedInput=None,
cwd=None,
expectOutput=False,
useUndoFile=False,
):
if cwd is None:
cwd = self.workingDir

with tempfile.NamedTemporaryFile(delete=False) as undo_log_file:
undo_log_file.close()

if os.name == "nt":
options = ['python', NormalizeFilenameTestCase.COMMAND]
options = ["python", NormalizeFilenameTestCase.COMMAND]
else:
options = [NormalizeFilenameTestCase.COMMAND]

options.extend(inputFiles)
options.extend(extraParams)
options.extend(['--undo-log-file=' + undo_log_file.name])
options.extend(["--undo-log-file=" + undo_log_file.name])

if feedInput:
p = Popen(options, stdin=PIPE, stdout=PIPE, stderr=PIPE, cwd=cwd)
Expand Down Expand Up @@ -122,13 +145,19 @@ def executeUndoCommands(self, commands):
return maxReturnCode

@contextmanager
def invokeAsPexpect(self, inputFiles, extraParams=[], expectedExitStatus=None, expectedOutputRegex=None):
def invokeAsPexpect(
self,
inputFiles,
extraParams=[],
expectedExitStatus=None,
expectedOutputRegex=None,
):
options = [NormalizeFilenameTestCase.COMMAND]
options.extend(inputFiles)
options.extend(extraParams)
options.extend(['--no-undo-log-file'])
options.extend(["--no-undo-log-file"])

command = ' '.join(options)
command = " ".join(options)

stream = io.BytesIO()

Expand All @@ -144,18 +173,20 @@ def invokeAsPexpect(self, inputFiles, extraParams=[], expectedExitStatus=None, e
self.assertEqual(expectedExitStatus, child.exitstatus)

if expectedOutputRegex is not None:
self.assertRegex(str(child.logfile_read.getvalue(), 'utf-8'), expectedOutputRegex)
self.assertRegex(
str(child.logfile_read.getvalue(), "utf-8"), expectedOutputRegex
)

def touch(self, fname):
os.makedirs(os.path.dirname(fname), exist_ok=True)
open(fname, 'w').close()
open(fname, "w").close()

def remove_dir_write_permissions(self, fname):
os.chmod(fname, S_IRUSR | S_IXUSR)

def writeFile(self, fname, contents):
os.makedirs(os.path.dirname(fname), exist_ok=True)
with open(fname, 'w') as filename:
with open(fname, "w") as filename:
filename.write(contents)

def readFile(self, fname):
Expand Down
6 changes: 3 additions & 3 deletions tests/Direct/test_Direct_Arguments.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ def setUp(self):
super().setUp()

def test_targetfile_exists_with_force(self):
filename = os.path.join(self.workingDir, 'blah.txt')
filename = os.path.join(self.workingDir, "blah.txt")
self.writeFile(filename, "original")
filename2 = os.path.join(self.workingDir, self.getDatePrefix() + 'blah.txt')
filename2 = os.path.join(self.workingDir, self.getDatePrefix() + "blah.txt")
self.writeFile(filename2, "new")
self.invokeDirectly([filename], extraParams=['--force'])
self.invokeDirectly([filename], extraParams=["--force"])
self.assertFalse(os.path.exists(filename))
self.assertTrue(os.path.exists(filename2))
self.assertEqual(self.readFile(filename2), "original")
Expand Down
Loading

0 comments on commit 641a9a6

Please sign in to comment.