Back to home page

EIC code displayed by LXR

 
 

    


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

0001 #!/usr/bin/env python
0002 """
0003 bashnotes.py
0004 ==============
0005 
0006 Present the notes from bash/RST definition lists with integer keys, 
0007 presenting individual notes or all.
0008 
0009 Usage::
0010 
0011     bashnotes.py 
0012     bashnotes.py 8 9
0013     bashnotes.py 8 9 --bashcmd "scan-;scan-ph-notes"
0014 
0015 """
0016 import sys, re, argparse
0017 from collections import OrderedDict as odict
0018 from opticks.ana.base import _subprocess_output
0019 
0020 class BashNotes(object):
0021     """
0022     Parse the output of bashfunc that is assumed to return 
0023     an RST style definition list keyed with integers. 
0024 
0025     NB must not be blank lines in the note items
0026     """
0027     startptn = re.compile("^(\d*)\s*$")  # integer starting a blank line
0028     blankptn = re.compile("^\s*$")   # blank line
0029     def __init__(self, bashcmd):
0030         out,err = _subprocess_output(["bash","-lc",bashcmd])
0031         assert err == ""
0032         self.d = self.parse(out) 
0033 
0034     def parse(self, out):
0035         #print(out)
0036         d = odict() 
0037         v = None
0038         for line in out.split("\n"):
0039             mitem = self.startptn.match(line)
0040             mend = self.blankptn.match(line)
0041             if mend:break   
0042             pass 
0043             if mitem:
0044                  v = int(mitem.groups()[0]) 
0045                  d[v] = []
0046             else:
0047                  if not v is None: 
0048                      d[v].append(line)  
0049             pass
0050             pass
0051         return d
0052        
0053     def item(self, v):
0054         return "%s\n%s" % (v,"\n".join(self.d[v]))
0055 
0056     def __call__(self, v):
0057         return self.item(v)
0058  
0059     def __str__(self):
0060         return "\n".join([self.item(v) for v in self.d.keys()])
0061 
0062 
0063 if __name__ == '__main__':
0064 
0065     parser = argparse.ArgumentParser(__doc__)
0066     parser.add_argument( "--bashcmd", default="scan-;scan-ph-notes", help="Bash command returning a definition list keyed with integers" )
0067     parser.add_argument( "vers", nargs="*", default=[1], type=int, help="Prefix beneath which to search for OpticksProfile.npy" )
0068     args = parser.parse_args()
0069 
0070     #print(args)
0071  
0072     bn = BashNotes(args.bashcmd)
0073     if len(args.vers) == 0:
0074         print(bn) 
0075     else:
0076         for v in args.vers:
0077             print(bn(v))
0078         pass
0079     pass
0080 
0081