ATLAS Offline Software
han_lark_tester.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3 
4 # More detailed syntax checking for han files
5 
6 from __future__ import print_function
7 
8 import lark, sys
9 from AthenaCommon.Utils.unixtools import find_datafile
10 
11 class T(lark.Transformer):
12  def __default_token__(self,tok):
13  print(tok)
14  return tok
15  def referenceblock(self, tok):
16  locations = [_ for _ in tok if isinstance(_, lark.Tree) and _.data == 'location']
17  if len(locations) > 1:
18  raise ValueError(f'More than one location given for reference {tok[0].children[0]}')
19  if len(locations) > 0:
20  locs = locations[0].children[0].split(',')
21  else:
22  locs = [_.children[0] for _ in tok if isinstance(_, lark.Tree) and _.data == 'file']
23  afspaths = [_ for _ in locs if _.startswith('/afs')]
24  if any(afspaths):
25  raise ValueError('A reference location for a production han configuration is given with an AFS path\n'
26  f'Offending path is {",".join(afspaths)} of reference {tok[0].children[0]}')
27  eospaths = [_ for _ in locs if _.startswith('/eos')]
28  xrootdpaths = [_ for _ in locs if _.startswith('root://')]
29  if len(eospaths) != len(xrootdpaths):
30  raise ValueError(f'Backup xrootd locations must be given for references with EOS paths for reference {tok[0].children[0]}\n'
31  f'Given EOS paths are {",".join(eospaths)}\n'
32  f'Given xrootd paths are {",".join(xrootdpaths)}')
33  return tok
34  pass
35 
36 grammarfile = find_datafile('DataQualityInterfaces/han_def.lark')
37 if grammarfile is None:
38  raise OSError(f'Unable to find file parser configuration file')
39 grammar = open(grammarfile).read()
40 parser = lark.Lark(grammar, parser='lalr', lexer='contextual', transformer=T())
41 
42 infile = open(sys.argv[1]).read()
43 try:
44  tree = parser.parse(infile)
45  print('Tree done')
46  print(tree.pretty())
47 except Exception as e:
48  print('ERROR:')
49  print(e)
50  raise e
51  sys.exit(1)
read
IovVectorMap_t read(const Folder &theFolder, const SelectionCriterion &choice, const unsigned int limit=10)
Definition: openCoraCool.cxx:569
han_lark_tester.T
Definition: han_lark_tester.py:11
han_lark_tester.T.referenceblock
def referenceblock(self, tok)
Definition: han_lark_tester.py:15
han_lark_tester.T.__default_token__
def __default_token__(self, tok)
Definition: han_lark_tester.py:12
Trk::open
@ open
Definition: BinningType.h:40
Muon::print
std::string print(const MuPatSegment &)
Definition: MuonTrackSteering.cxx:28
python.Utils.unixtools.find_datafile
def find_datafile(fname, pathlist=None, access=os.R_OK)
pathresolver-like helper function --------------------------------------—
Definition: unixtools.py:67
Trk::split
@ split
Definition: LayerMaterialProperties.h:38