Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-09 07:48:49

0001 #!/usr/bin/env python
0002 """
0003 OpticksPhoton.py
0004 =================
0005 
0006 Formerly OpticksFlags.py
0007 
0008 Used from optickscore/CMakeLists.txt
0009 
0010 """
0011 import os, re, logging, argparse, json
0012 
0013 ## json_save_ duplicates opticks.ana.base
0014 ## to make this script self contained
0015 ## as this is used from the okc- build
0016 
0017 def makedirs_(path):
0018     pdir = os.path.dirname(path)
0019     if not os.path.exists(pdir):
0020         os.makedirs(pdir)
0021     pass
0022     return path
0023 
0024 expand_ = lambda path:os.path.expandvars(os.path.expanduser(path))
0025 json_load_ = lambda path:json.load(open(expand_(path), "r"))
0026 json_save_ = lambda path, d:json.dump(d, open(makedirs_(expand_(path)),"w"))
0027 
0028 
0029 
0030 log = logging.getLogger(__name__)
0031 
0032 class OpticksPhoton(object):
0033     pfx = "    static constexpr const char* _"
0034     ptn = re.compile(r"^(?P<key>\S*)\s*=\s*\"(?P<val>\S{2})\"\s*;\s*$")
0035 
0036     @classmethod
0037     def Flag2Abbrev(cls, lines):
0038         """
0039         Apply pattern to lines starting with the prefix
0040         and match to extract the flag name and abbreviation.
0041 
0042             BULK_SCATTER      = "SC" ;
0043 
0044         """
0045         d = dict()
0046         for line in lines:
0047             select = line.startswith(cls.pfx)
0048             log.debug( " select %d line %s " % (select, line ))
0049             if not select: continue
0050             line = line[len(cls.pfx):]
0051             m = cls.ptn.match(line)
0052             if not m:
0053                log.debug("failed to match %s " % line )
0054                continue
0055             pass
0056             g = m.groupdict()
0057             k, v = g["key"], g["val"]
0058             d[k] = v
0059             log.debug( " k %20s v %s " % (k,v ))
0060         pass
0061         return d
0062 
0063     def __repr__(self):
0064         return "\n".join([" %2s : %s " % (kv[1], kv[0]) for kv in self.flag2abbrev.items()])
0065 
0066     def __init__(self, hh_path):
0067         hh_path = os.path.expandvars(hh_path)
0068         hh_lines = open(hh_path, "r").readlines()
0069         log.debug(" hh_path %s hh_lines %d " % (hh_path, len(hh_lines)) )
0070         self.flag2abbrev = self.Flag2Abbrev(hh_lines)
0071 
0072 
0073 
0074 if __name__ == '__main__':
0075 
0076     parser = argparse.ArgumentParser(__doc__)
0077     default_path = "$OPTICKS_HOME/sysrap/OpticksPhoton.hh"
0078     parser.add_argument(     "path",  nargs="?", help="Path to input OpticksPhoton.hh", default=default_path )
0079     parser.add_argument(     "--jsonpath", default=None, help="When a path is provided an json file will be written to it." )
0080     parser.add_argument(     "--quiet", action="store_true", default=False, help="Skip dumping" )
0081     parser.add_argument(     "--level", default="info", help="logging level" )
0082     args = parser.parse_args()
0083 
0084     fmt = '[%(asctime)s] p%(process)s {%(pathname)s:%(lineno)d} %(levelname)s - %(message)s'
0085     logging.basicConfig(level=getattr(logging,args.level.upper()), format=fmt)
0086 
0087     flags = OpticksPhoton(args.path)
0088 
0089     if not args.quiet:
0090         print(flags)
0091     pass
0092     if not args.jsonpath is None:
0093         log.info("writing flag2abbrev to jsonpath %s " % args.jsonpath)
0094         json_save_(args.jsonpath, flags.flag2abbrev )
0095     pass
0096 
0097 
0098