aboutsummaryrefslogtreecommitdiff
path: root/thcolor/_color.py
diff options
context:
space:
mode:
Diffstat (limited to 'thcolor/_color.py')
-rwxr-xr-xthcolor/_color.py55
1 files changed, 35 insertions, 20 deletions
diff --git a/thcolor/_color.py b/thcolor/_color.py
index 9e62072..55795d2 100755
--- a/thcolor/_color.py
+++ b/thcolor/_color.py
@@ -9,12 +9,14 @@
from enum import Enum as _Enum
from warnings import warn as _warn
+_gg_no_re = False
+
try:
import regex as _re
except ImportError:
- _warn("could not import regex, trying the default `re` module.",
+ _warn("could not import regex, text parsing is disabled.",
RuntimeWarning)
- import re as _re
+ _gg_no_re = True
from ._ref import Reference as _Reference
from ._angle import Angle as _Angle
@@ -35,20 +37,32 @@ __all__ = ["Color"]
# Decoding utilities.
# ---
-_ColorPattern = _re.compile(r"""
- (
- ((?P<agl_val>-? ([0-9]+\.?|[0-9]*\.[0-9]+)) \s*
- (?P<agl_typ>deg|grad|rad|turn))
- | ((?P<per>[0-9]+(\.[0-9]*)? | \.[0-9]+) \s* \%)
- | (?P<num>[0-9]+(\.[0-9]*)? | \.[0-9]+)
- | (?P<ncol>[RYGCBM] [0-9]{0,2} (\.[0-9]*)?)
- | (\# (?P<hex>[0-9a-f]{3} | [0-9a-f]{4} | [0-9a-f]{6} | [0-9a-f]{8}))
- | ((?P<name>[a-z]([a-z0-9\s_-]*[a-z0-9_-])?)
- ( \s* \( \s* (?P<arg> (?0)? ) \s* \) )?)
- )
-
- \s* ((?P<sep>[,/\s]) \s* (?P<nextargs> (?0))?)?
-""", _re.VERBOSE | _re.I | _re.M)
+_color_pattern = None
+
+def _get_color_pattern():
+ global _color_pattern
+
+ if _color_pattern is None:
+ if _gg_no_re:
+ raise ImportError("text parsing is disabled until you install " \
+ "the 'regex' module, e.g. via `pip install regex`.")
+
+ _color_pattern = _re.compile(r"""
+ (
+ ((?P<agl_val>-? ([0-9]+\.?|[0-9]*\.[0-9]+)) \s*
+ (?P<agl_typ>deg|grad|rad|turn))
+ | ((?P<per>[0-9]+(\.[0-9]*)? | \.[0-9]+) \s* \%)
+ | (?P<num>[0-9]+(\.[0-9]*)? | \.[0-9]+)
+ | (?P<ncol>[RYGCBM] [0-9]{0,2} (\.[0-9]*)?)
+ | (\# (?P<hex>[0-9a-f]{3} | [0-9a-f]{4} | [0-9a-f]{6} | [0-9a-f]{8}))
+ | ((?P<name>[a-z]([a-z0-9\s_-]*[a-z0-9_-])?)
+ ( \s* \( \s* (?P<arg> (?0)? ) \s* \) )?)
+ )
+
+ \s* ((?P<sep>[,/\s]) \s* (?P<nextargs> (?0))?)?
+ """, _re.VERBOSE | _re.I | _re.M)
+
+ return _color_pattern
# ---
# Color initialization varargs utilities.
@@ -582,7 +596,7 @@ class Color:
# Get the arguments.
args = recurse(column + match.start('arg'),
- _ColorPattern.fullmatch(match['arg']))
+ _get_color_pattern().fullmatch(match['arg']))
# Get the function and call it with the arguments.
@@ -631,7 +645,8 @@ class Color:
# Get the following arguments and check.
args = recurse(column + match.start('nextargs'),
- _ColorPattern.fullmatch(match['nextargs'] or ""))
+ _get_color_pattern().fullmatch(match['nextargs'] \
+ or ""))
try:
assert len(args) >= 2
@@ -671,7 +686,7 @@ class Color:
return (argument(column, value),) \
+ recurse(column + match.start('nextargs'),
- _ColorPattern.fullmatch(match['nextargs'] or ""))
+ _get_color_pattern().fullmatch(match['nextargs'] or ""))
# Strip the expression.
@@ -682,7 +697,7 @@ class Color:
# Match the expression (and check it as a whole directly).
- match = _ColorPattern.fullmatch(expr)
+ match = _get_color_pattern().fullmatch(expr)
if match is None:
raise _ColorExpressionDecodingError("expression parsing failed")