clog/clog/utils/common.py
theonepath 61c3411faf
Updated utils/common.py
Fixed bug with regex to allow for pathspecs containing a period (.)
character, and amended statement to flake8 W504 definition.
2022-05-12 14:01:06 +01:00

52 lines
1.5 KiB
Python

import os
import re
## get the name of the OS
__OS: str = os.name
# define the path spec pattern depending on OS
__REGEX_PAT = re.compile(r'^(.+)[\\]([^\\]+)\\*$') \
if __OS == "nt" else re.compile(r'^(.+)[\/]([^\/]+)$')
class LogLevel:
"""Enumerator structure to map an integer against a log level.
Higher `int` value means higher severity of level for logging.
"""
NORMAL = 0
PASS = 1
DEBUG = 2
WARN = 3
ERROR = 4
def is_path_spec(path_spec: str) -> bool:
"""Evaluate if a given string is a valid pathspec identifier.
Pathspec evaluation is matched against the following regular
expression patterns:
```plaintext
OS Pathspec
NT: ^(.+)[\\\\]([^\\\\]+)\\*$
Other: ^(.+)[\\/]([^\\/]+)$
OR
Valid names of CWD subdirectories
^[\\w\\d\\-_]+$
```
The following pathspecs are evaluated as valid pathspec identifiers
(OS implicitly implied):
- C:\\User\\user-1\\Documents\\my-project
- $HOME/.local/bin/my_project
- ".\\Python Examples\\my-project1"
The following pathspecs are evaluated as invalid pathspec identifiers
(OS implicitly implied)
- ./this,_is-a(bad)+pathspec
`@Params`: path_spec - `str`
`@Return`: `bool`
"""
## perform a regex match to ensure that the given path is a
## valid pathspec for the system.
return bool(re.match(__REGEX_PAT, path_spec.strip(r"\"'"))
or re.match(r'^[\w\d\-_\.]+$', path_spec.strip(r"\"'")))