forked from pcdshub/lcls-naming-czar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathczar.py
50 lines (33 loc) · 1.58 KB
/
czar.py
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import re
pattern = re.compile(r'^(?P<functionalTaxon>(?P<functionalClass>\w{2})(?P<functionalIncrement>\d{1,})(?P<beamPath>\w{1,}\d{0,}\w{0,})):((?P<fungibleTaxon>[\w\d_]{3,}):|)(?P<component>\w{3,}?):?(?P<componentIncrement>\d*)($|:(?P<auxiliaryPV>.{3,})$)')
def describe(Name):
'''
Returns a dictionary of all the elements of a well formed name.
Raises a ValueError if the name doesn't conform (ie. doesn't match the regex).
### Increments
May come with zero padding. Strip and convert as you like.
'''
match = re.search(pattern, Name)
if match is None:
raise ValueError
NameDict = match.groupdict()
return NameDict
def name(NameDict):
'''
Returns a properly formatted device name from a dictionary.
Dictionary must include all elements of a name, omitted increments will be set to 01. <todo>
'''
theName = """{functionalClass}{functionalIncrement}{beamPath}:{fungibleTaxon}:{component}:{componentIncrement}:{auxiliaryPV}""".format(
**NameDict
)
if re.search(pattern, theName) == None:
raise ValueError
return theName
def human_readable(Name):
'''Produce a human-readable description of the name'''
NameDict = describe(Name)
humanReadableDescription = '''This name refers to the {componentIncrementTranslated} {componentTaxonTranslated}'''.format(
**NameDict
)
#This will take a bit more work to really pull off
return humanReadableDescription