From 116a0a3aa39a8690eed48b9c575dac779052d113 Mon Sep 17 00:00:00 2001 From: Norwid Behrnd Date: Thu, 25 Nov 2021 10:12:03 +0100 Subject: [PATCH 1/4] Complete regex grouping The regexes are easier to read if their elements are grouped in parentheses. Some already were organized this way, so this commit only extends the pattern to cover all of them. Pytest does not identify a functional change. --- date2name/__init__.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/date2name/__init__.py b/date2name/__init__.py index d404a11..e8493e7 100755 --- a/date2name/__init__.py +++ b/date2name/__init__.py @@ -31,13 +31,13 @@ FORMATSTRING_MONTH = "%Y-%m" FORMATSTRING_WITHTIME = "%Y-%m-%dT%H.%M.%S" REGEX_PATTERNS = { 'NODATESTAMP': re.compile('^\D'), - 'SHORT': re.compile('^\d{2,2}[01]\d[0123]\d[- _]'), - 'COMPACT': re.compile('^\d{4,4}[01]\d[0123]\d[- _]'), - 'STANDARD': re.compile('^\d{4,4}-[01]\d-[0123]\d[- _]'), - 'MONTH': re.compile('^\d{4,4}-[01]\d[- _]'), - 'WITHTIME_AND_SECONDS': re.compile('^\d{4,4}-[01]\d-[0123]\d([T :_-])([012]\d)([:.-])([012345]\d)([:.-])([012345]\d)[- _.]'), - 'WITHTIME_NO_SECONDS': re.compile('^\d{4,4}-[01]\d-[0123]\d([T :_-])([012]\d)([:.-])([012345]\d)[- _.]'), - 'WITHSECONDS': re.compile('^\d{4,4}-[01]\d-[0123]\d[T :_-][012]\d[:.-][012345]\d[:.-][012345]\d[- _]'), + 'SHORT': re.compile('^(\d{2,2})([01]\d)([0123]\d)([- _])'), + 'COMPACT': re.compile('^(\d{4,4})([01]\d)([0123]\d)([- _])'), + 'STANDARD': re.compile('^(\d{4,4})-([01]\d)-([0123]\d)([- _])'), + 'MONTH': re.compile('^(\d{4,4})-([01]\d)([- _])'), + 'WITHTIME_AND_SECONDS': re.compile('^(\d{4,4})-([01]\d)-([0123]\d)([T :_-])([012]\d)([:.-])([012345]\d)([:.-])([012345]\d)([- _.])'), + 'WITHTIME_NO_SECONDS': re.compile('^(\d{4,4})-([01]\d)-([0123]\d)([T :_-])([012]\d)([:.-])([012345]\d)([- _.])'), + 'WITHSECONDS': re.compile('^(\d{4,4})-([01]\d)-([0123]\d)([T :_-])([012]\d)([:.-])([012345]\d)([:.-])([012345]\d)([- _])'), } MAX_PATHLENGTH = 255 # os.pathconf('/', 'PC_PATH_MAX') may be longer but os.rename() seems to have hard-coded 256 From 51aaed2d014fed9eabbe5205d2f513d096ca503a Mon Sep 17 00:00:00 2001 From: Norwid Behrnd Date: Thu, 25 Nov 2021 10:41:24 +0100 Subject: [PATCH 2/4] redefine month pattern (explicit day truncation) Regexes' grouping with parentheses and the definition of the standard pattern just ahead of the month pattern allowed to spot a plausible cause for the problem to retract all autogenerated stamps. In the present scope of pytest, the additional lookafter now sufficies to enable the retraction of all five stamp pattern. --- date2name/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/date2name/__init__.py b/date2name/__init__.py index e8493e7..dbe29ba 100755 --- a/date2name/__init__.py +++ b/date2name/__init__.py @@ -34,7 +34,7 @@ REGEX_PATTERNS = { 'SHORT': re.compile('^(\d{2,2})([01]\d)([0123]\d)([- _])'), 'COMPACT': re.compile('^(\d{4,4})([01]\d)([0123]\d)([- _])'), 'STANDARD': re.compile('^(\d{4,4})-([01]\d)-([0123]\d)([- _])'), - 'MONTH': re.compile('^(\d{4,4})-([01]\d)([- _])'), + 'MONTH': re.compile('^(\d{4,4})-([01]\d)(?!-[0123]\d)([- _])'), 'WITHTIME_AND_SECONDS': re.compile('^(\d{4,4})-([01]\d)-([0123]\d)([T :_-])([012]\d)([:.-])([012345]\d)([:.-])([012345]\d)([- _.])'), 'WITHTIME_NO_SECONDS': re.compile('^(\d{4,4})-([01]\d)-([0123]\d)([T :_-])([012]\d)([:.-])([012345]\d)([- _.])'), 'WITHSECONDS': re.compile('^(\d{4,4})-([01]\d)-([0123]\d)([T :_-])([012]\d)([:.-])([012345]\d)([:.-])([012345]\d)([- _])'), From 25dda29fb5bb662d7fbe39457a1f85e94ad7079c Mon Sep 17 00:00:00 2001 From: Norwid Behrnd Date: Thu, 25 Nov 2021 11:01:08 +0100 Subject: [PATCH 3/4] Simplify scope of regex As long as years as written with integers of four decimals, \d{4,4} may be substituted by \d{4}, too. --- date2name/__init__.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/date2name/__init__.py b/date2name/__init__.py index dbe29ba..6641958 100755 --- a/date2name/__init__.py +++ b/date2name/__init__.py @@ -31,13 +31,13 @@ FORMATSTRING_MONTH = "%Y-%m" FORMATSTRING_WITHTIME = "%Y-%m-%dT%H.%M.%S" REGEX_PATTERNS = { 'NODATESTAMP': re.compile('^\D'), - 'SHORT': re.compile('^(\d{2,2})([01]\d)([0123]\d)([- _])'), - 'COMPACT': re.compile('^(\d{4,4})([01]\d)([0123]\d)([- _])'), - 'STANDARD': re.compile('^(\d{4,4})-([01]\d)-([0123]\d)([- _])'), - 'MONTH': re.compile('^(\d{4,4})-([01]\d)(?!-[0123]\d)([- _])'), - 'WITHTIME_AND_SECONDS': re.compile('^(\d{4,4})-([01]\d)-([0123]\d)([T :_-])([012]\d)([:.-])([012345]\d)([:.-])([012345]\d)([- _.])'), - 'WITHTIME_NO_SECONDS': re.compile('^(\d{4,4})-([01]\d)-([0123]\d)([T :_-])([012]\d)([:.-])([012345]\d)([- _.])'), - 'WITHSECONDS': re.compile('^(\d{4,4})-([01]\d)-([0123]\d)([T :_-])([012]\d)([:.-])([012345]\d)([:.-])([012345]\d)([- _])'), + 'SHORT': re.compile('^(\d{2})([01]\d)([0123]\d)([- _])'), + 'COMPACT': re.compile('^(\d{4})([01]\d)([0123]\d)([- _])'), + 'STANDARD': re.compile('^(\d{4})-([01]\d)-([0123]\d)([- _])'), + 'MONTH': re.compile('^(\d{4})-([01]\d)(?!-[0123]\d)([- _])'), + 'WITHTIME_AND_SECONDS': re.compile('^(\d{4})-([01]\d)-([0123]\d)([T :_-])([012]\d)([:.-])([012345]\d)([:.-])([012345]\d)([- _.])'), + 'WITHTIME_NO_SECONDS': re.compile('^(\d{4})-([01]\d)-([0123]\d)([T :_-])([012]\d)([:.-])([012345]\d)([- _.])'), + 'WITHSECONDS': re.compile('^(\d{4})-([01]\d)-([0123]\d)([T :_-])([012]\d)([:.-])([012345]\d)([:.-])([012345]\d)([- _])'), } MAX_PATHLENGTH = 255 # os.pathconf('/', 'PC_PATH_MAX') may be longer but os.rename() seems to have hard-coded 256 @@ -99,7 +99,7 @@ parser.add_option("--delimiter", dest="delimiter", metavar='DELIMITER_STRING', 'space, or underscore may result in not recognizing the delimiter ' + 'for further operations such as fixing slightly wrong formatted time-stamps.') parser.add_option("--nocorrections", dest="nocorrections", action="store_true", - help="do not convert existing but slightely wrong formatted date/time-stamps to new format") + help="do not convert existing but slightly wrong formatted date/time-stamps to new format") parser.add_option("-q", "--quiet", dest="quiet", action="store_true", help="do not output anything but just errors on console") parser.add_option("-v", "--verbose", dest="verbose", action="store_true", From 4f3dd2a146dd9fdede14a9df082435975fdc55cd Mon Sep 17 00:00:00 2001 From: nbehrnd Date: Thu, 25 Nov 2021 12:19:14 +0100 Subject: [PATCH 4/4] update README.org (stamp retraction) --- README.org | 1 + 1 file changed, 1 insertion(+) diff --git a/README.org b/README.org index 40477b2..aa24906 100644 --- a/README.org +++ b/README.org @@ -36,6 +36,7 @@ Run "date2name --help" for usage hints such as: : -C, --compact use compact datestamp (YYYYMMDD) : -M, --month use datestamp with year and month (YYYY-MM) : -w, --withtime use datestamp including seconds (YYYY-MM-DDThh.mm.ss) +: -r, --remove remove all known datestamps : -m, --mtime take modification time for datestamp [default] : -c, --ctime take creation time for datestamp : --delimiter overwrite default delimiter