From 1360dbee8cbdf273b8c019941fdeb6f93806af18 Mon Sep 17 00:00:00 2001 From: Dennis Fink Date: Sun, 28 Sep 2025 15:03:45 +0200 Subject: Add parse_regex_flags --- patternutils/types.py | 1 + patternutils/utils.py | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+) 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 -- cgit v1.3.1