diff options
| author | Dennis Fink | 2025-09-28 15:03:45 +0200 |
|---|---|---|
| committer | Dennis Fink | 2025-09-28 15:03:45 +0200 |
| commit | 1360dbee8cbdf273b8c019941fdeb6f93806af18 (patch) | |
| tree | 42725e70dc1e8d6fc7032eb1cedf635de92386ec | |
| parent | c880242739a38ffdc0db4e6b7b15f4340271338d (diff) | |
| download | patternutils-1360dbee8cbdf273b8c019941fdeb6f93806af18.tar.gz patternutils-1360dbee8cbdf273b8c019941fdeb6f93806af18.zip | |
Add parse_regex_flags
Diffstat (limited to '')
| -rw-r--r-- | patternutils/types.py | 1 | ||||
| -rw-r--r-- | patternutils/utils.py | 25 |
2 files changed, 26 insertions, 0 deletions
diff --git a/patternutils/types.py b/patternutils/types.py index 6b2a5ac..cbab184 100644 --- a/patternutils/types.py +++ b/patternutils/types.py @@ -13,4 +13,5 @@ PredefinedExtensions = Literal[TemplateExtensions, RegexExtensions] CommandNames = Literal[PlainCommands, TemplateCommands, RegexCommands] +RegexFlags = Literal["ascii", "a", "ignorecase", "i", "multiline", "m", "dotall", "s"] TemplatePrefer = Optional[Literal["global", "command"]] diff --git a/patternutils/utils.py b/patternutils/utils.py index 22d4123..89b7a05 100644 --- a/patternutils/utils.py +++ b/patternutils/utils.py @@ -1,9 +1,13 @@ import json import os +import re +from collections.abc import Container from typing import Any, Generator, Optional, overload import click +from patternutils.types import RegexFlags + def max_procs() -> int: """Returns the number of CPU cores available, or 1 if unavailable.""" @@ -121,3 +125,24 @@ def walk( except FileNotFoundError: click.secho(f"Directory not found: {directory}", fg="red", err=True) raise SystemExit(1) + + +def parse_regex_flags(flags: Container[RegexFlags]) -> int: + concrete_flags = re.NOFLAG + + if "ascii" in flags or "a" in flags: + concrete_flags |= re.ASCII + + if "ignorecase" in flags or "i" in flags: + concrete_flags |= re.IGNORECASE + + if "multiline" in flags or "m" in flags: + concrete_flags |= re.MULTILINE + + if "dotall" in flags or "s" in flags: + concrete_flags |= re.DOTALL + + if "verbose" in flags or "x" in flags: + concrete_flags |= re.VERBOSE + + return concrete_flags |
