11from string
import Formatter
12from pathlib
import Path
19 Used to escape user strings for LaTex.
20 @param some_string which
is escaped
21 @return string escaped version of some_string
23 return some_string.replace(
'\\',
r'\textbackslash').replace(
'_',
r'\_').replace(
'^',
r'\^{}')
26def variable(variable_string):
28 Adds hyphenations after brackets, and for common variables.
29 @param variable_string variable name
30 @return string
with hyphenation hints
for latex
33 '=':
r'=\allowbreak ',
34 '_':
r'\_\allowbreak ',
35 ':':
r':\allowbreak ',
36 '(':
r'(\allowbreak ',
37 'extraInfo':
r'ex\-tra\-In\-fo',
38 'SignalProbability':
r'Sig\-nal\-Prob\-a\-bil\-i\-ty',
39 'cosAngleBetweenMomentumAndVertexVector':
r'cosAngle\-Between\-Momentum\-And\-Vertex\-Vector'}
40 for key, value
in substitutes.items():
41 variable_string = variable_string.replace(key, value)
42 return variable_string
45def decayDescriptor(decay_string):
47 Prettifies the given decay string by using latex-symbols instead of plain-text
48 @param decay_string string containing a decay descriptor
49 @return string latex version of the decay descriptor
51 decay_string = decay_string.replace(':generic',
'')
52 decay_string = decay_string.replace(
':semileptonic',
'$_{SL}$')
53 decay_string = decay_string.replace(
':FSP',
'$_{FSP}$')
54 decay_string = decay_string.replace(
':V0',
'$_{V0}$')
59 (
'gamma',
r'$\gamma$'),
61 (
'anti-p-',
r'$\bar{p}$'),
69 (
'tau+',
r'$\tau^+$'),
70 (
'tau-',
r'$\tau^-$'),
76 (
'J/psi',
r'$J/\psi$'),
77 (
'anti-Lambda_c-',
r'$\Lambda^{-}_{c}$'),
78 (
'anti-Sigma+',
r'$\overline{\Sigma}^{+}$'),
79 (
'anti-Lambda0',
r'$\overline{\Lambda}^{0}$'),
80 (
'anti-D0*',
r'$\overline{D^{0*}}$'),
81 (
'anti-D*0',
r'$\overline{D^{0*}}$'),
82 (
'anti-D0',
r'$\overline{D^0}$'),
83 (
'anti-B0',
r'$\overline{B^0}$'),
84 (
'Sigma+',
r'$\Sigma^{+}$'),
85 (
'Lambda_c+',
r'$\Lambda^{+}_{c}$'),
86 (
'Lambda0',
r'$\Lambda^{0}$'),
95 (
'D_s*+',
r'$D^{+*}_s$'),
96 (
'D_s*-',
r'$D^{-*}_s$'),
100 (
'B_s0',
r'$B^0_s$'),
101 (
'K*0',
r'$K^{0*}$')]
102 tex_string = decay_string
103 for (key, value)
in substitutes:
104 tex_string = tex_string.replace(key, value)
105 return f
'\\texorpdfstring{{{tex_string}}}{{{string(decay_string)}}}'
108def duration(seconds):
110 Converts a duration given in seconds into a nice latex-style duration string
111 @param seconds duration
in seconds
112 @return string describing a duration
in a natural format
114 minutes = int(seconds / 60)
115 hours = int(minutes / 60)
117 ms = int(seconds * 1000) % 1000
118 us = int(seconds * 1000 * 1000) % 1000
119 seconds = int(seconds % 60)
122 string += f
"{hours}h"
124 string += f
"{minutes}m"
125 if seconds != 0
and hours == 0:
126 string += f
"{seconds}s"
127 if ms != 0
and hours == 0
and minutes == 0
and seconds == 0:
129 if us != 0
and hours == 0
and minutes == 0
and seconds == 0
and ms == 0:
130 string += f
"{us}$\\mu$s"
132 if hours == 0
and minutes == 0
and seconds == 0
and ms == 0
and us == 0:
133 string +=
r'$<1\mu$s'
138 """Simple wrapper class to allow accessing dictionary elements via
142 """Remember the dictionary"""
147 """Return any dictionay element as attribute"""
152 """Custom formatter class which allows to substitute a missing value with a
157 Format a single field:
159 * if the field
is None and we have a ``:=`` specification we replace
160 the field
with the default value
and change the spec to
's'
161 *
if the value
is None we replace it
with an empty string
163 then we just run the normal formatter ...
165 if spec.startswith(
"="):
176 Try to get the field as usual but
in case we cannot find it because it
177 either doesn
't exist or it doesn't have the correct attribute/item we
181 return super().
get_field(field_name, args, kwargs)
182 except (KeyError, TypeError):
186def format_filename(template, filename, metadata_json):
188 Format a file name as described
in BELLE2-NOTE-TE-2017-012
191 template (str): the format string
192 filename (str): the name of the file
193 metadata_json (str): a string representation of the file metadata json
196 a string
with the formatted file name
198 filepath = Path(filename)
199 metadata = json.loads(metadata_json)
200 for key, value
in metadata.items():
201 if isinstance(value, dict):
204 result = formatter.format(template, file=filepath, **metadata)
205 result = re.sub(
r'//+',
"/", result)
206 result = re.sub(
r'\s+',
'_', result)