-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.py
More file actions
32 lines (26 loc) · 813 Bytes
/
util.py
File metadata and controls
32 lines (26 loc) · 813 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import re
import yaml
def assert_on_re(name: str, pattern: str):
regex = re.compile(pattern)
def assertion(value: str):
if regex.match(value) is None:
raise ValueError(f'Invalid {name}: {value}')
return assertion
assert_ident = assert_on_re('identifier', r'^[A-Za-z0-9\-_\.:]+$')
assert_name = assert_on_re('name', r'^[A-Za-z0-9\-_\.]+$')
def split_ident(ident: str):
assert_ident(ident)
ids = ident.split(':')
return ids
def join_ident(ids: list):
for name in ids:
assert_name(name)
return ':'.join(ids)
def head_ident(ident: str):
assert_ident(ident)
sep = ident.find(':')
if sep == -1:
sep = len(ident)
return ident[:sep]
def yamldump(obj):
return yaml.dump(obj, default_flow_style=False, allow_unicode=True).strip()