LSSTApplications  10.0-2-g4f67435,11.0.rc2+1,11.0.rc2+12,11.0.rc2+3,11.0.rc2+4,11.0.rc2+5,11.0.rc2+6,11.0.rc2+7,11.0.rc2+8
LSSTDataManagementBasePackage
Public Member Functions | Public Attributes | List of all members
lsst.daf.butlerUtils.fsScanner.FsScanner Class Reference
Inheritance diagram for lsst.daf.butlerUtils.fsScanner.FsScanner:

Public Member Functions

def __init__
 
def getFields
 
def isNumeric
 
def isInt
 
def isFloat
 
def processPath
 

Public Attributes

 globString
 
 fields
 
 reString
 

Detailed Description

Class to scan a filesystem location for paths matching a template.

Decomposes the resulting paths into fields and passes them to a callback
function.

Definition at line 33 of file fsScanner.py.

Constructor & Destructor Documentation

def lsst.daf.butlerUtils.fsScanner.FsScanner.__init__ (   self,
  pathTemplate 
)
Constructor.  Takes the path template, which should be in the form
of a Python string with named format substitution specifications.
Such a template would be suitable for generating a path given a set of
fields in a dictionary.  Does not handle hex (%x or %X).

Example:
    %(field)s/%(visit)d/%(exposure)d/raw-%(visit)d-e%(exposure)03d-c%(ccd)03d-a%(amp)03d.fits
    
Note that fields may appear multiple times; the second and subsequent
appearances of such fields will have "_{number}" appended to them to
disambiguate, although it is typically assumed that they will all be
identical.

Definition at line 40 of file fsScanner.py.

40 
41  def __init__(self, pathTemplate):
42  """Constructor. Takes the path template, which should be in the form
43  of a Python string with named format substitution specifications.
44  Such a template would be suitable for generating a path given a set of
45  fields in a dictionary. Does not handle hex (%x or %X).
46 
47  Example:
48  %(field)s/%(visit)d/%(exposure)d/raw-%(visit)d-e%(exposure)03d-c%(ccd)03d-a%(amp)03d.fits
49 
50  Note that fields may appear multiple times; the second and subsequent
51  appearances of such fields will have "_{number}" appended to them to
52  disambiguate, although it is typically assumed that they will all be
53  identical.
54  """
55 
56  # Change template into a globbable path specification.
57  fmt = re.compile(r'%\((\w+)\).*?([dioueEfFgGcrs])')
58  self.globString = fmt.sub('*', pathTemplate)
59 
60  # Change template into a regular expression.
61  last = 0
62  self.fields = {}
63  self.reString = ""
64  n = 0
65  pos = 0
66  for m in fmt.finditer(pathTemplate):
67  fieldName = m.group(1)
68  if self.fields.has_key(fieldName):
69  fieldName += "_%d" % (n,)
70  n += 1
71 
72  prefix = pathTemplate[last:m.start(0)]
73  last = m.end(0)
74  self.reString += prefix
75 
76  if m.group(2) in 'crs':
77  fieldType = str
78  self.reString += r'(?P<' + fieldName + '>.+?)'
79  elif m.group(2) in 'eEfFgG':
80  fieldType = float
81  self.reString += r'(?P<' + fieldName + '>[\d.eE+-]+?)'
82  else:
83  fieldType = int
84  self.reString += r'(?P<' + fieldName + '>[\d+-]+?)'
85 
86  self.fields[fieldName] = dict(pos=pos, fieldType=fieldType)
87  pos += 1
88 
89  self.reString += pathTemplate[last:]

Member Function Documentation

def lsst.daf.butlerUtils.fsScanner.FsScanner.getFields (   self)
Return the list of fields that will be returned from matched
paths, in order.

Definition at line 90 of file fsScanner.py.

90 
91  def getFields(self):
92  """Return the list of fields that will be returned from matched
93  paths, in order."""
94 
95  fieldList = ["" for i in xrange(len(self.fields))]
96  for f in self.fields.keys():
97  fieldList[self.fields[f]['pos']] = f
98  return fieldList
def lsst.daf.butlerUtils.fsScanner.FsScanner.isFloat (   self,
  name 
)
Return true if the given field contains an float.

Definition at line 109 of file fsScanner.py.

110  def isFloat(self, name):
111  """Return true if the given field contains an float."""
112 
113  return self.fields[name]['fieldType'] == float
def lsst.daf.butlerUtils.fsScanner.FsScanner.isInt (   self,
  name 
)
Return true if the given field contains an integer.

Definition at line 104 of file fsScanner.py.

105  def isInt(self, name):
106  """Return true if the given field contains an integer."""
107 
108  return self.fields[name]['fieldType'] == int
def lsst.daf.butlerUtils.fsScanner.FsScanner.isNumeric (   self,
  name 
)
Return true if the given field contains a number.

Definition at line 99 of file fsScanner.py.

99 
100  def isNumeric(self, name):
101  """Return true if the given field contains a number."""
102 
103  return self.fields[name]['fieldType'] in (float, int)
def lsst.daf.butlerUtils.fsScanner.FsScanner.processPath (   self,
  location,
  callback 
)
Scan a given path location with the given callback function.

Definition at line 114 of file fsScanner.py.

115  def processPath(self, location, callback):
116  """Scan a given path location with the given callback function."""
117 
118  curdir = os.getcwd()
119  os.chdir(location)
120  pathList = glob.glob(self.globString)
121  for path in pathList:
122  m = re.search(self.reString, path)
123  if m:
124  dataId = m.groupdict()
125  for f in self.fields.keys():
126  if self.isInt(f):
127  dataId[f] = int(dataId[f])
128  elif self.isFloat(f):
129  dataId[f] = float(dataId[f])
130  callback(path, dataId)
131  else:
132  print >> sys.stderr, "Warning: unmatched path: %s" % (path,)
133  os.chdir(curdir)

Member Data Documentation

lsst.daf.butlerUtils.fsScanner.FsScanner.fields

Definition at line 61 of file fsScanner.py.

lsst.daf.butlerUtils.fsScanner.FsScanner.globString

Definition at line 57 of file fsScanner.py.

lsst.daf.butlerUtils.fsScanner.FsScanner.reString

Definition at line 62 of file fsScanner.py.


The documentation for this class was generated from the following file: