3
3
import datetime
4
4
import fnmatch
5
5
import os
6
+ import pathlib
6
7
import platform
7
8
import re
8
9
from dataclasses import dataclass
@@ -106,6 +107,7 @@ def __init__(self, expression: str, message: str):
106
107
"time" : datetime .time ,
107
108
"timedelta" : datetime .timedelta ,
108
109
"timezone" : datetime .timezone ,
110
+ "Path" : pathlib .Path ,
109
111
}
110
112
111
113
@@ -115,14 +117,27 @@ class Expression:
115
117
116
118
expr : str = field (
117
119
description = """\
118
- Condition to evaluate. This must be a Python "eval" expression.
120
+ Expression to evaluate. This must be a Python "eval" expression.
119
121
For security reasons, only certain expressions and functions are allowed.
120
122
121
123
Examples:
122
124
```toml
123
125
expr = "re.match(r'^\\ d+$', environ.get('TEST_VAR', ''))"
124
126
expr = "platform.system() == 'Linux'"
127
+ expr = "Path.cwd() / 'app'"
125
128
```
129
+
130
+ Allowed global names (the name and the corresponding python module/name):
131
+
132
+ - `environ` -> os.environ
133
+ - `re` -> re
134
+ - `platform` -> platform
135
+ - `datetime` -> datetime.datetime
136
+ - `date` -> datetime.date
137
+ - `time` -> datetime.time
138
+ - `timedelta` -> datetime.timedelta
139
+ - `timezone` -> datetime.timezone
140
+ - `Path` -> pathlib.Path
126
141
""" ,
127
142
no_default = True ,
128
143
)
@@ -159,6 +174,8 @@ class Condition:
159
174
if = "re.match(r'^\\ d+$', environ.get('TEST_VAR', ''))"
160
175
if = "platform.system() == 'Linux'"
161
176
```
177
+
178
+ see also `expr` for allowed global names.
162
179
""" ,
163
180
alias = "if" ,
164
181
no_default = True ,
0 commit comments