4 from string
import Formatter
5 from pathlib
import Path
8 from basf2
import B2ERROR
11 def string(some_string):
13 Used to escape user strings for LaTex.
14 @param some_string which is escaped
15 @return string escaped version of some_string
17 return some_string.replace(
'\\',
r'\textbackslash').replace(
'_',
r'\_').replace(
'^',
r'\^{}')
20 def variable(variable_string):
22 Adds hyphenations after brackets, and for common variables.
23 @param variable_string variable name
24 @return string with hyphenation hints for latex
27 '=':
r'=\allowbreak ',
28 '_':
r'\_\allowbreak ',
29 ':':
r':\allowbreak ',
30 '(':
r'(\allowbreak ',
31 'extraInfo':
r'ex\-tra\-In\-fo',
32 'SignalProbability':
r'Sig\-nal\-Prob\-a\-bil\-i\-ty',
33 'cosAngleBetweenMomentumAndVertexVector':
r'cosAngle\-Between\-Momentum\-And\-Vertex\-Vector'}
34 for key, value
in substitutes.items():
35 variable_string = variable_string.replace(key, value)
36 return variable_string
39 def decayDescriptor(decay_string):
41 Prettifies the given decay string by using latex-symbols instead of plain-text
42 @param decay_string string containing a decay descriptor
43 @return string latex version of the decay descriptor
45 decay_string = decay_string.replace(
':generic',
'')
46 decay_string = decay_string.replace(
':semileptonic',
'$_{SL}$')
47 decay_string = decay_string.replace(
':FSP',
'$_{FSP}$')
48 decay_string = decay_string.replace(
':V0',
'$_{V0}$')
53 (
'gamma',
r'$\gamma$'),
55 (
'anti-p-',
r'$\bar{p}$'),
63 (
'tau+',
r'$\tau^+$'),
64 (
'tau-',
r'$\tau^-$'),
70 (
'J/psi',
r'$J/\psi$'),
71 (
'anti-Lambda_c-',
r'$\Lambda^{-}_{c}$'),
72 (
'anti-Sigma+',
r'$\overline{\Sigma}^{+}$'),
73 (
'anti-Lambda0',
r'$\overline{\Lambda}^{0}$'),
74 (
'anti-D0*',
r'$\overline{D^{0*}}$'),
75 (
'anti-D*0',
r'$\overline{D^{0*}}$'),
76 (
'anti-D0',
r'$\overline{D^0}$'),
77 (
'anti-B0',
r'$\overline{B^0}$'),
78 (
'Sigma+',
r'$\Sigma^{+}$'),
79 (
'Lambda_c+',
r'$\Lambda^{+}_{c}$'),
80 (
'Lambda0',
r'$\Lambda^{0}$'),
89 (
'D_s*+',
r'$D^{+*}_s$'),
90 (
'D_s*-',
r'$D^{-*}_s$'),
96 tex_string = decay_string
97 for (key, value)
in substitutes:
98 tex_string = tex_string.replace(key, value)
99 return '\\texorpdfstring{%s}{%s}' % (tex_string, string(decay_string))
102 def duration(seconds):
104 Converts a duration given in seconds into a nice latex-style duration string
105 @param seconds duration in seconds
106 @return string describing a duration in a natural format
108 minutes = int(seconds / 60)
109 hours = int(minutes / 60)
111 ms = int(seconds * 1000) % 1000
112 us = int(seconds * 1000 * 1000) % 1000
113 seconds = int(seconds % 60)
116 string +=
"%dh" % (hours)
118 string +=
"%dm" % (minutes)
119 if seconds != 0
and hours == 0:
120 string +=
"%ds" % (seconds)
121 if ms != 0
and hours == 0
and minutes == 0
and seconds == 0:
122 string +=
"%dms" % (ms)
123 if us != 0
and hours == 0
and minutes == 0
and seconds == 0
and ms == 0:
124 string +=
r"%d$\mu$s" % (us)
126 if hours == 0
and minutes == 0
and seconds == 0
and ms == 0
and us == 0:
127 string +=
r'$<1\mu$s'
132 """Simple wrapper class to allow accessing dictionary elements via
136 """Remember the dictionary"""
141 """Return any dictionay element as attribute"""
146 """Custom formatter class which allows to substitute a missing value with a
151 Format a single field:
153 * if the field is None and we have a ``:=`` specification we replace
154 the field with the default value and change the spec to 's'
155 * if the value is None we replace it with an empty string
157 then we just run the normal formatter ...
159 if spec.startswith(
"="):
170 Try to get the field as usual but in case we cannot find it because it
171 either doesn't exist or it doesn't have the correct attribute/item we
175 return super().
get_field(field_name, args, kwargs)
176 except (KeyError, TypeError):
180 def format_filename(template, filename, metadata_json):
182 Format a file name as described in BELLE2-NOTE-TE-2017-012
185 template (str): the format string
186 filename (str): the name of the file
187 metadata_json (str): a string representation of the file metadata json
190 a string with the formatted file name
192 filepath = Path(filename)
193 metadata = json.loads(metadata_json)
194 for key, value
in metadata.items():
195 if isinstance(value, dict):
198 result = formatter.format(template, file=filepath, **metadata)
199 result = re.sub(
r'//+',
"/", result)
200 result = re.sub(
r'\s+',
'_', result)