Merge branch 'master' into rebuild_pytest

This commit is contained in:
nbehrnd 2024-10-31 11:45:27 +01:00 committed by GitHub
commit 09903e82fb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 122 additions and 80 deletions

View file

@ -1,11 +1,11 @@
# GNU Make file for the automation of pytest for appendfilename. # GNU Make file for the automation of pytest for appendfilename
# #
# While the test script is written for Python 3.9.2, you might need to # While the test script is written for Python 3.9.2, it depends on
# adjust the following instruction once in case your OS includes # your installation of pytest (and in case of Linux, the authors of
# pytest for legacy Python 2 side by side to Python 3, or only hosts # your distribution) if pytest for Python 3 is invoked either by
# pytest for Python 3. The tests in script test_appendfilename.py are # pytest, or pytest-3. In some distributions, pytest actually may
# set up to work with pytest for Python 3; dependent on your # invoke pyest for legacy Python 2; the tests in test_date2name.py
# installation, which may be named pytest-3, or (again) pytest. # however are incompatible to this.
# #
# Put this file like test_appendfilename.py in the root folder of # Put this file like test_appendfilename.py in the root folder of
# appendfilename fetched from PyPi or GitHub. Then run # appendfilename fetched from PyPi or GitHub. Then run
@ -17,5 +17,5 @@
# right after the first test failing, use the -x flag to the # right after the first test failing, use the -x flag to the
# instructions on the CLI in addition to the verbosity flag to (-v). # instructions on the CLI in addition to the verbosity flag to (-v).
# pytest -v test_appendfilename.py # only pytest for Python 3 is present # pytest -v test_appendfilename.py # the pattern by pytest's manual
pytest-3 -v test_appendfilename.py # pytest if Python 2 and Python 3 coexist pytest-3 -v test_appendfilename.py # the alternative pattern (e.g., Debian 12)

View file

@ -1,6 +1,6 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
PROG_VERSION = u"Time-stamp: <2022-01-04 17:25:15 vk>" PROG_VERSION = u"Time-stamp: <2024-08-29 19:02:13 vk>"
# TODO: # TODO:
# * fix parts marked with «FIXXME» # * fix parts marked with «FIXXME»
@ -76,6 +76,8 @@ FILENAME_COMPONENT_LOWERCASE_BLACKLIST = ['img', 'eine', 'einem', 'eines', 'fuer
# initial CV with strings that are provided for tab completion in any case (whitelist) # initial CV with strings that are provided for tab completion in any case (whitelist)
INITIAL_CONTROLLED_VOCABULARY = ['Karl', 'Graz', 'LaTeX', 'specialL', 'specialP'] INITIAL_CONTROLLED_VOCABULARY = ['Karl', 'Graz', 'LaTeX', 'specialL', 'specialP']
DEBUG_SEPARATOR = ''
parser = OptionParser(usage=USAGE) parser = OptionParser(usage=USAGE)
parser.add_option("-t", "--text", dest="text", parser.add_option("-t", "--text", dest="text",
@ -288,6 +290,7 @@ def handle_file(filename, text, dryrun):
assert(isinstance(filename, str)) assert(isinstance(filename, str))
num_errors = 0 num_errors = 0
new_filename = ''
if os.path.isdir(filename): if os.path.isdir(filename):
logging.warning("Skipping directory \"%s\" because this tool only processes file names." % filename) logging.warning("Skipping directory \"%s\" because this tool only processes file names." % filename)
@ -309,15 +312,27 @@ def handle_file(filename, text, dryrun):
try: try:
if options.prepend: if options.prepend:
logging.debug('options.prepend is set with ' + DEBUG_SEPARATOR + str(os.path.dirname(filename)) + DEBUG_SEPARATOR +
str(text) + DEBUG_SEPARATOR + str(separator()) + DEBUG_SEPARATOR +
str(old_basename) + DEBUG_SEPARATOR + str(tags_with_extension))
new_filename = os.path.join(os.path.dirname(filename), text + separator() + old_basename + tags_with_extension) new_filename = os.path.join(os.path.dirname(filename), text + separator() + old_basename + tags_with_extension)
elif options.smartprepend: elif options.smartprepend:
match = re.match(WITHTIME_AND_SECONDS_PATTERN, filename) match = re.match(WITHTIME_AND_SECONDS_PATTERN, filename)
logging.debug('options.smartprepend is set with ' + DEBUG_SEPARATOR + str(os.path.dirname(filename)) + DEBUG_SEPARATOR +
str(text) + DEBUG_SEPARATOR + str(separator()) + DEBUG_SEPARATOR + str(old_basename) + DEBUG_SEPARATOR + str(tags_with_extension))
logging.debug('options.smartprepend is set with ' + DEBUG_SEPARATOR + str(type(os.path.dirname(filename))) + DEBUG_SEPARATOR +
str(type(text)) + DEBUG_SEPARATOR + str(type(separator())) + DEBUG_SEPARATOR + str(type(old_basename)) + DEBUG_SEPARATOR + str(type(tags_with_extension)))
if not match: if not match:
logging.debug('can\'t find a date/time-stamp, doing a simple prepend') logging.debug('can\'t find a date/time-stamp, doing a simple prepend')
new_filename = os.path.join(os.path.dirname(filename), text + separator() + old_basename + tags_with_extension) new_filename = os.path.join(os.path.dirname(filename), text + separator() + old_basename + tags_with_extension)
else: else:
logging.debug('date/time-stamp found, insert text between date/time-stamp and rest') logging.debug('date/time-stamp found, insert text between date/time-stamp and rest')
logging.debug('options.smartprepend is set with ' + DEBUG_SEPARATOR + str(os.path.dirname(filename)) + DEBUG_SEPARATOR +
str(match.group(1)) + DEBUG_SEPARATOR + str(match.group(len(match.groups()))) + DEBUG_SEPARATOR)
logging.debug('options.smartprepend is set with ' + DEBUG_SEPARATOR + str(type(os.path.dirname(filename))) + DEBUG_SEPARATOR +
str(type(match.group(1))) + DEBUG_SEPARATOR + str(type(match.group(len(match.groups())))) + DEBUG_SEPARATOR)
new_filename = os.path.join(os.path.dirname(filename), match.group(1) + separator() + text + separator() + match.group(len(match.groups()))) new_filename = os.path.join(os.path.dirname(filename), match.group(1) + separator() + text + separator() + match.group(len(match.groups())))
logging.debug('new_filename is now: ' + new_filename)
else: else:
new_filename = os.path.join(os.path.dirname(filename), old_basename + separator() + text + tags_with_extension) new_filename = os.path.join(os.path.dirname(filename), old_basename + separator() + text + tags_with_extension)
except: except:

View file

@ -5,7 +5,7 @@
# license: GPL v3, 2022. # license: GPL v3, 2022.
# date: 2022-01-05 (YYYY-MM-DD) # date: 2022-01-05 (YYYY-MM-DD)
# edit: [2024-10-31 Thu] # edit: [2024-10-31 Thu]
#
"""Test pad for functions by appendfilename with pytest. """Test pad for functions by appendfilename with pytest.
Written for Python 3.9.9 and pytest 6.2.4 for Python 3 as provided by Written for Python 3.9.9 and pytest 6.2.4 for Python 3 as provided by

View file

@ -22,24 +22,24 @@
* Deployment * Deployment
On a computer with Python 3 only, the recommended call on the CLI to The programmatic tests are set up for pytest for Python 3. It
run the tests is either one of the following instructions (you might however depends on your installation (and in case of Linux, the
need to add the executable bit): authors of your Linux distribution ([[https://github.com/pytest-dev/pytest/discussions/9481][reference]])) if this utility may
be started by =pytest= (e.g., the pattern in pytest's manual), or by
=pytest-3= by either one of the pattern below:
#+begin_src bash :tangle no #+begin_src bash :tangle no
python pytest -v test_appendfilename.py pytest -v test_appendfilename.py
./Makefile pytest-3 -v test_appendfilename.py
#+end_src #+end_src
In case the computer you use equally includes an installation of As of writing, the later pattern is the to be used e.g., in Linux
legacy Python 2 side-by-side to Python 3, you must explicitly call Debian 12/bookworm (branch testing) to discern pytest (for
for the later branch of the two. Depending on your OS, this contemporary Python 3) from pytest (for legacy Python 2).
requires an adjustment of the command issued. In Linux Debian
12/bookworm, branch testing, for example,
#+begin_src bash :tangle no The =Makefile= this =org= file provides for convenience running
python3 pytest-3 -v test_appendfilename.py these tests assumes the later syntax pattern. (It might be
#+end_src necessary to provide the executable bit to activate the Makefile.)
* Setup of Emacs * Setup of Emacs
@ -56,38 +56,38 @@
permission. permission.
#+begin_src emacs-lisp :tangle no #+begin_src emacs-lisp :tangle no
;; support these languages at all: ;; support these languages at all:
(org-babel-do-load-languages (org-babel-do-load-languages
'org-babel-load-languages 'org-babel-load-languages
'((emacs-lisp . t) '((emacs-lisp . t)
(org . t) (org . t)
(shell . t) (shell . t)
(python . t))) (python . t)))
;; enable syntax highlighting: ;; enable syntax highlighting:
(setq org-src-fontify-natively t) (setq org-src-fontify-natively t)
;; adjust indentations, set tabs as explicit 4 spaces: ;; adjust indentations, set tabs as explicit 4 spaces:
(setq-default indent-tabs-mode nil) (setq-default indent-tabs-mode nil)
(setq default-tab-width 4) (setq default-tab-width 4)
(setq custom-tab-width 4) (setq custom-tab-width 4)
(setq-default python-indent-offset custom-tab-width) (setq-default python-indent-offset custom-tab-width)
(setq org-edit-src-content-indentation 0) (setq org-edit-src-content-indentation 0)
(setq org-src-tab-acts-natively t) (setq org-src-tab-acts-natively t)
(setq org-src-preserve-indentation t) (setq org-src-preserve-indentation t)
;; some comfort functions Suenkler mentions: ;; some comfort functions Suenkler mentions:
(delete-selection-mode 1) (delete-selection-mode 1)
(defalias 'yes-or-no-p 'y-or-n-p) (defalias 'yes-or-no-p 'y-or-n-p)
(show-paren-mode 1) (show-paren-mode 1)
(setq show-paren-style 'parenthesis) (setq show-paren-style 'parenthesis)
(column-number-mode nil) (column-number-mode nil)
(setq org-src-fontify-natively t) (setq org-src-fontify-natively t)
#+end_src #+end_src
#+RESULTS: #+RESULTS:
@ -108,14 +108,14 @@
Makefile. Makefile.
#+BEGIN_SRC makefile :tangle Makefile #+BEGIN_SRC makefile :tangle Makefile
# GNU Make file for the automation of pytest for appendfilename. # GNU Make file for the automation of pytest for appendfilename
# #
# While the test script is written for Python 3.9.2, you might need to # While the test script is written for Python 3.9.2, it depends on
# adjust the following instruction once in case your OS includes # your installation of pytest (and in case of Linux, the authors of
# pytest for legacy Python 2 side by side to Python 3, or only hosts # your distribution) if pytest for Python 3 is invoked either by
# pytest for Python 3. The tests in script test_appendfilename.py are # pytest, or pytest-3. In some distributions, pytest actually may
# set up to work with pytest for Python 3; dependent on your # invoke pyest for legacy Python 2; the tests in test_date2name.py
# installation, which may be named pytest-3, or (again) pytest. # however are incompatible to this.
# #
# Put this file like test_appendfilename.py in the root folder of # Put this file like test_appendfilename.py in the root folder of
# appendfilename fetched from PyPi or GitHub. Then run # appendfilename fetched from PyPi or GitHub. Then run
@ -127,8 +127,8 @@
# right after the first test failing, use the -x flag to the # right after the first test failing, use the -x flag to the
# instructions on the CLI in addition to the verbosity flag to (-v). # instructions on the CLI in addition to the verbosity flag to (-v).
# pytest -v test_appendfilename.py # only pytest for Python 3 is present # pytest -v test_appendfilename.py # the pattern by pytest's manual
pytest-3 -v test_appendfilename.py # pytest if Python 2 and Python 3 coexist pytest-3 -v test_appendfilename.py # the alternative pattern (e.g., Debian 12)
#+end_src #+end_src
** Building a pytest.ini ** Building a pytest.ini
@ -259,7 +259,9 @@ def test_pattern_s1(arg1, arg2, arg3):
#+begin_src python :tangle no #+begin_src python :tangle no
@pytest.mark.prepend @pytest.mark.prepend
@pytest.mark.parametrize("arg1", ["test.txt", "2021-12-31_test.txt", @pytest.mark.parametrize("arg1", ["test.txt", "2021-12-31_test.txt",
"2021-12-31T18.48.22_test.txt"]) "2021-12-31T18.48.22_test.txt",
"20211231_test.txt", "2012-12_test.txt",
"211231_test.txt"])
@pytest.mark.parametrize("arg2", ["-t book", "-t book_shelf", @pytest.mark.parametrize("arg2", ["-t book", "-t book_shelf",
"--text book", "--text book_shelf"]) "--text book", "--text book_shelf"])
@pytest.mark.parametrize("arg3", [" ", "!", "@", "#", "$", "%", "*", "_", "+", @pytest.mark.parametrize("arg3", [" ", "!", "@", "#", "$", "%", "*", "_", "+",
@ -299,7 +301,8 @@ def test_pattern_s2(arg1, arg2, arg3, arg4):
#+begin_src python :tangle no #+begin_src python :tangle no
@pytest.mark.smart @pytest.mark.smart
@pytest.mark.parametrize("arg1", ["test.txt", "2021-12-31_test.txt", @pytest.mark.parametrize("arg1", ["test.txt", "2021-12-31_test.txt",
"2021-12-31T18.48.22_test.txt"]) "2021-12-31T18.48.22_test.txt", "20211231_test.txt",
"2021-12_test.txt", "211231_test.txt"])
@pytest.mark.parametrize("arg2", ["-t book", "-t book_shelf", @pytest.mark.parametrize("arg2", ["-t book", "-t book_shelf",
"--text book", "--text book_shelf"]) "--text book", "--text book_shelf"])
@pytest.mark.parametrize("arg3", [" " , "#", "!", "@", "#", "$", "%", "*", "_", "+", @pytest.mark.parametrize("arg3", [" " , "#", "!", "@", "#", "$", "%", "*", "_", "+",
@ -323,29 +326,53 @@ def test_pattern_s3_02(arg1, arg2, arg3):
# analysis section: # analysis section:
old_filename = str(arg1) old_filename = str(arg1)
# test pattern issued by date2name vs. other pattern
# default (YYYY-MM-DD)
# --withtime (YYYY-MM-DDTHH.MM.SS)
# --compact (YYYYMMDD)
# --month (YYYY-MM)
# --short (YYMMDD)
if (re.search("^\d{4}-[012]\d-[0-3]\d_", old_filename) or
re.search('^\d{4}-[012]\d-[0-3]\dT[012]\d\.[0-5]\d\.[0-5]\d_', old_filename) or
re.search("^\d{4}[012]\d[0-3]\d_", old_filename) or
re.search("^\d{4}-[012]\d_", old_filename) or
re.search("^\d{2}[012]\d[0-3]\d_", old_filename)):
if re.search("^\d{4}-\d{2}-\d{2}_", old_filename): if re.search("^\d{4}-\d{2}-\d{2}_", old_filename):
# if (running date2name in default mode) then .true. # if (running date2name in default mode) then .true.
time_stamp = old_filename[:10] time_stamp = old_filename[:10]
time_stamp_separator = old_filename[10] time_stamp_separator = old_filename[10]
file_extension = old_filename.split(".")[-1] file_extension = old_filename.split(".")[-1]
old_filename_no_timestamp = old_filename[11:] old_filename_no_timestamp = old_filename[11:]
stem_elements = old_filename_no_timestamp.split(".")[:-1]
stem = ".".join(stem_elements)
new_filename = "".join([time_stamp, arg3, text, arg3, stem, str("."), file_extension])
assert os.path.isfile(new_filename)
os.remove(new_filename)
assert os.path.isfile(new_filename) is False
elif re.search('^\d{4}-\d{2}-\d{2}T\d{2}\.\d{2}\.\d{2}_', old_filename): elif re.search('^\d{4}-\d{2}-\d{2}T\d{2}\.\d{2}\.\d{2}_', old_filename):
# if (running date2name --withtime) then .true. # if (running date2name --withtime) then .true.
time_stamp = old_filename[:19] time_stamp = old_filename[:19]
time_stamp_separator = old_filename[19] time_stamp_separator = old_filename[19]
file_extension = old_filename.split(".")[-1] file_extension = old_filename.split(".")[-1]
old_filename_no_timestamp = old_filename[20:] old_filename_no_timestamp = old_filename[20:]
elif re.search("^\d{4}\d{2}\d{2}_", old_filename):
# if (running date2name --compact) then .true.
time_stamp = old_filename[:8]
time_stamp_separator = old_filename[8]
file_extension = old_filename.split(".")[-1]
old_filename_no_timestamp = old_filename[9:]
elif re.search("^\d{4}-\d{2}_", old_filename):
# if (running date2name --month) then .true.
time_stamp = old_filename[:7]
time_stamp_separator = old_filename[7]
file_extension = old_filename.split(".")[-1]
old_filename_no_timestamp = old_filename[8:]
elif re.search("^\d{4}\d{2}\d{2}_", old_filename):
# if (running date2name --short) then .true.
time_stamp = old_filename[:6]
time_stamp_separator = old_filename[6]
file_extension = old_filename.split(".")[-1]
old_filename_no_timestamp = old_filename[7:]
stem_elements = old_filename_no_timestamp.split(".")[:-1] stem_elements = old_filename_no_timestamp.split(".")[:-1]
stem = ".".join(stem_elements) stem = ".".join(stem_elements)