refactor: rewrite pattern search, smart-prepend option

The pattern search about the smart-prepend option was rewritten to
be easier to read and maintain.

Signed-off-by: Norwid Behrnd <nbehrnd@yahoo.com>
This commit is contained in:
Norwid Behrnd 2024-11-22 10:24:25 +01:00
parent 62bf545c0c
commit 78157342b7
3 changed files with 68 additions and 71 deletions

View file

@ -3,7 +3,7 @@ name: CI_pytest_appendfilename
# name : pytest.yml # name : pytest.yml
# purpose : regularly run pytest on appendfilename # purpose : regularly run pytest on appendfilename
# date : [2024-10-31 Thu] # date : [2024-10-31 Thu]
# edit : [2024-11-12 Tue] # edit : [2024-11-22 Fri]
on: on:
push: push:
@ -43,7 +43,10 @@ jobs:
- name: run the check by pytest - name: run the check by pytest
run: | run: |
echo "default"
python -m pytest -m "default" python -m pytest -m "default"
echo "prepend"
python -m pytest -m "prepend" python -m pytest -m "prepend"
python -m pytest -m "smart" echo "smart_prepend"
python -m pytest -m "smart_prepend"

View file

@ -4,7 +4,7 @@
# author: nbehrnd@yahoo.com # author: nbehrnd@yahoo.com
# license: GPL v3, 2022. # license: GPL v3, 2022.
# date: 2022-01-05 (YYYY-MM-DD) # date: 2022-01-05 (YYYY-MM-DD)
# edit: [2024-11-21 Thu] # edit: [2024-11-22 Fri]
# #
"""Test pad for functions by appendfilename with pytest. """Test pad for functions by appendfilename with pytest.
@ -231,8 +231,8 @@ def test_smart_prepend(arg1, arg2, arg3):
arg2 the text string to be added arg2 the text string to be added
arg3 the separator (at least in Windows 10, do not use `*` arg3 the separator (at least in Windows 10, do not use `*`
""" """
time_stamp = "" timestamp = ""
# time_stamp_separator = "" # timestamp_separator = ""
old_filename_no_timestamp = "" old_filename_no_timestamp = ""
# create a test file: # create a test file:
@ -253,7 +253,7 @@ def test_smart_prepend(arg1, arg2, arg3):
else: else:
separator = shlex.split(arg3)[1] separator = shlex.split(arg3)[1]
# Time stamps `date2name` provides can be either one of five formats # Timestamps `date2name` provides can be either one of five formats
# #
# YYYY-MM-DDTHH.MM.SS `--withtime` # YYYY-MM-DDTHH.MM.SS `--withtime`
# YYYY-MM-DD default # YYYY-MM-DD default
@ -269,42 +269,39 @@ def test_smart_prepend(arg1, arg2, arg3):
# https://github.com/novoid/appendfilename/issues/15 # https://github.com/novoid/appendfilename/issues/15
# https://github.com/novoid/appendfilename/issues/16 # https://github.com/novoid/appendfilename/issues/16
# pattern `--with-time` patterns = [
if re.search(r"^\d{4}-[012]\d-[0-3]\dT[012]\d\.[0-5]\d\.[0-5]\d", old_filename): r"^\d{4}-[012]\d-[0-3]\dT[012]\d\.[0-5]\d\.[0-5]\d",
time_stamp = old_filename[:19] r"^\d{4}-[012]\d-[0-3]\d",
time_stamp_separator = old_filename[19] r"^\d{4}[012]\d[0-3]\d",
old_filename_no_timestamp = old_filename[20:] r"^\d{4}-[012]\d",
r"^\d{2}[012]\d[0-3]\d"
]
# default pattern for pattern in patterns:
elif re.search(r"^\d{4}-[012]\d-[0-3]\d", old_filename): match = re.search(pattern, old_filename)
time_stamp = old_filename[:10] if match:
time_stamp_separator = old_filename[10] timestamp = re.findall(pattern, old_filename)[0]
old_filename_no_timestamp = old_filename[11:] timestamp_separator = str(old_filename)[len(timestamp)]
old_filename_no_timestamp = old_filename[len(timestamp) + 1:]
# pattern `--compact` # currently fails print("\n\ntest of option smart-prepend:") # `pytest -s` diagnosis
elif re.search(r"^\d{4}[012]\d[0-3]\d", old_filename): print("old_filename:")
time_stamp = old_filename[:8] print(old_filename)
time_stamp_separator = old_filename[8] print("timestamp, timestamp_separator, old_filename_no_timestamp")
old_filename_no_timestamp = old_filename[9:] print(timestamp)
print(timestamp_separator)
print(old_filename_no_timestamp)
# pattern `--month` # currently fails break
elif re.search(r"^\d{4}-[012]\d", old_filename):
time_stamp = old_filename[:7]
time_stamp_separator = old_filename[7]
old_filename_no_timestamp = old_filename[8:]
# pattern `--short` # currently fails new_filename = "".join([
elif re.search(r"^\d{4}[012]\d[0-3]\d", old_filename): timestamp, # timestamp_separator,
time_stamp = old_filename[:6]
time_stamp_separator = old_filename[6]
old_filename_no_timestamp = old_filename[7:]
new_filename = "".join([time_stamp, # time_stamp_separator,
separator, shlex.split(arg2)[1], separator, separator, shlex.split(arg2)[1], separator,
old_filename_no_timestamp]) old_filename_no_timestamp
])
# is the new file present? # is the new file present?
print("\nnew_filename") # optional check for `pytest -s` print("new_filename") # optional check for `pytest -s`
print(new_filename) print(new_filename)
assert os.path.isfile(new_filename) assert os.path.isfile(new_filename)

View file

@ -1,7 +1,7 @@
# name: test_generator.org # name: test_generator.org
# author: nbehrnd@yahoo.com # author: nbehrnd@yahoo.com
# date: 2022-01-05 (YYYY-MM-DD) # date: 2022-01-05 (YYYY-MM-DD)
# edit: [2024-11-21 Thu] # edit: [2024-11-22 Fri]
# license: GPL3, 2022-2024 # license: GPL3, 2022-2024
# Export the tangled files with C-c C-v t # Export the tangled files with C-c C-v t
@ -39,10 +39,10 @@ python -m pytest
- =-x= stops the sequence after the first failing test - =-x= stops the sequence after the first failing test
- =-s= occasionally provides information e.g., about the tests' criteria - =-s= occasionally provides information e.g., about the tests' criteria
The tests are organized in sets =default=, =prepend=, and =smart=. The tests are organized in sets =default=, =prepend=, and =smart_prepend=.
This allows to selectively run only checks which are about the This allows to selectively run only checks which are about the
results by =appendfilename= in the /default/ mode, /prepend/ mode, results by =appendfilename= in the /default/ mode, /prepend/ mode,
or /smart prepend/ mode alone, e.g. or /smart-prepend/ mode alone, e.g.
#+begin_src shell :tangle no #+begin_src shell :tangle no
python -m pytest -m "prepend" python -m pytest -m "prepend"
@ -148,7 +148,7 @@ pytest-3 test_appendfilename.py -m "default and prepend" -v
# author: nbehrnd@yahoo.com # author: nbehrnd@yahoo.com
# license: GPL v3, 2022. # license: GPL v3, 2022.
# date: 2022-01-05 (YYYY-MM-DD) # date: 2022-01-05 (YYYY-MM-DD)
# edit: [2024-11-21 Thu] # edit: [2024-11-22 Fri]
# #
"""Test pad for functions by appendfilename with pytest. """Test pad for functions by appendfilename with pytest.
@ -406,8 +406,8 @@ def test_smart_prepend(arg1, arg2, arg3):
arg2 the text string to be added arg2 the text string to be added
arg3 the separator (at least in Windows 10, do not use `*` arg3 the separator (at least in Windows 10, do not use `*`
""" """
time_stamp = "" timestamp = ""
# time_stamp_separator = "" # timestamp_separator = ""
old_filename_no_timestamp = "" old_filename_no_timestamp = ""
# create a test file: # create a test file:
@ -428,7 +428,7 @@ def test_smart_prepend(arg1, arg2, arg3):
else: else:
separator = shlex.split(arg3)[1] separator = shlex.split(arg3)[1]
# Time stamps `date2name` provides can be either one of five formats # Timestamps `date2name` provides can be either one of five formats
# #
# YYYY-MM-DDTHH.MM.SS `--withtime` # YYYY-MM-DDTHH.MM.SS `--withtime`
# YYYY-MM-DD default # YYYY-MM-DD default
@ -444,42 +444,39 @@ def test_smart_prepend(arg1, arg2, arg3):
# https://github.com/novoid/appendfilename/issues/15 # https://github.com/novoid/appendfilename/issues/15
# https://github.com/novoid/appendfilename/issues/16 # https://github.com/novoid/appendfilename/issues/16
# pattern `--with-time` patterns = [
if re.search(r"^\d{4}-[012]\d-[0-3]\dT[012]\d\.[0-5]\d\.[0-5]\d", old_filename): r"^\d{4}-[012]\d-[0-3]\dT[012]\d\.[0-5]\d\.[0-5]\d",
time_stamp = old_filename[:19] r"^\d{4}-[012]\d-[0-3]\d",
time_stamp_separator = old_filename[19] r"^\d{4}[012]\d[0-3]\d",
old_filename_no_timestamp = old_filename[20:] r"^\d{4}-[012]\d",
r"^\d{2}[012]\d[0-3]\d"
]
# default pattern for pattern in patterns:
elif re.search(r"^\d{4}-[012]\d-[0-3]\d", old_filename): match = re.search(pattern, old_filename)
time_stamp = old_filename[:10] if match:
time_stamp_separator = old_filename[10] timestamp = re.findall(pattern, old_filename)[0]
old_filename_no_timestamp = old_filename[11:] timestamp_separator = str(old_filename)[len(timestamp)]
old_filename_no_timestamp = old_filename[len(timestamp) + 1:]
# pattern `--compact` # currently fails print("\n\ntest of option smart-prepend:") # `pytest -s` diagnosis
elif re.search(r"^\d{4}[012]\d[0-3]\d", old_filename): print("old_filename:")
time_stamp = old_filename[:8] print(old_filename)
time_stamp_separator = old_filename[8] print("timestamp, timestamp_separator, old_filename_no_timestamp")
old_filename_no_timestamp = old_filename[9:] print(timestamp)
print(timestamp_separator)
print(old_filename_no_timestamp)
# pattern `--month` # currently fails break
elif re.search(r"^\d{4}-[012]\d", old_filename):
time_stamp = old_filename[:7]
time_stamp_separator = old_filename[7]
old_filename_no_timestamp = old_filename[8:]
# pattern `--short` # currently fails new_filename = "".join([
elif re.search(r"^\d{4}[012]\d[0-3]\d", old_filename): timestamp, # timestamp_separator,
time_stamp = old_filename[:6]
time_stamp_separator = old_filename[6]
old_filename_no_timestamp = old_filename[7:]
new_filename = "".join([time_stamp, # time_stamp_separator,
separator, shlex.split(arg2)[1], separator, separator, shlex.split(arg2)[1], separator,
old_filename_no_timestamp]) old_filename_no_timestamp
])
# is the new file present? # is the new file present?
print("\nnew_filename") # optional check for `pytest -s` print("new_filename") # optional check for `pytest -s`
print(new_filename) print(new_filename)
assert os.path.isfile(new_filename) assert os.path.isfile(new_filename)