All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Public Member Functions | Static Public Member Functions | Public Attributes | Static Public Attributes | List of all members
SerialSubstitution.ProcessorClass Class Reference

Public Member Functions

def __init__
 
def SetOptions
 
def SetColors
 
def Color
 
def RecordPattern
 
def AddFilePattern
 
def AddFileNamePattern
 
def AddFileType
 
def AddRegExPattern
 
def AddRegExRemoveLine
 
def AddSimplePattern
 
def AddWord
 
def AddWarningPattern
 
def AddPattern
 
def MatchFile
 
def SubstituteLine
 
def ProcessFile
 
def ProcessFiles
 
def ProcessDir
 
def __str__
 
def Describe
 

Static Public Member Functions

def CreateTempFile
 

Public Attributes

 name
 
 options
 
 file_filters
 
 patterns
 

Static Public Attributes

tuple context = LineNoContextClass(FilePath, 0)
 
list Content = []
 
int nChanges = 0
 
tuple SourceFile = open(FilePath, 'r')
 
tuple new_line = self.SubstituteLine(line, context)
 
tuple OutputFile = ProcessorClass.CreateTempFile(FilePath)
 
 OutputPath = OutputFile.name
 

Detailed Description

Definition at line 193 of file SerialSubstitution.py.

Constructor & Destructor Documentation

def SerialSubstitution.ProcessorClass.__init__ (   self,
  name 
)
Supported keyword arguments: "options"

Definition at line 194 of file SerialSubstitution.py.

195  def __init__(self, name):
196  """Supported keyword arguments: "options"
197  """
198  self.name = name
199  self.options = None
200  self.file_filters = []
self.patterns = []

Member Function Documentation

def SerialSubstitution.ProcessorClass.__str__ (   self)

Definition at line 402 of file SerialSubstitution.py.

403  def __str__(self): return self.name
def SerialSubstitution.ProcessorClass.AddFileNamePattern (   self,
  name_pattern 
)

Definition at line 229 of file SerialSubstitution.py.

230  def AddFileNamePattern(self, name_pattern):
231  return self.AddFilePattern(R"(.*/)*" + name_pattern)
def SerialSubstitution.ProcessorClass.AddFilePattern (   self,
  pattern 
)

Definition at line 222 of file SerialSubstitution.py.

223  def AddFilePattern(self, pattern):
224  if not pattern.endswith('$'): pattern += "$"
225  match = re.compile(pattern)
226  self.file_filters.append(match)
return self
def SerialSubstitution.ProcessorClass.AddFileType (   self,
  suffixes 
)

Definition at line 232 of file SerialSubstitution.py.

233  def AddFileType(self, *suffixes):
234  for suffix in suffixes: self.AddFileNamePattern(".*\." + suffix)
return self
def SerialSubstitution.ProcessorClass.AddPattern (   self,
  pattern,
  repl,
  exceptions = [] 
)

Definition at line 261 of file SerialSubstitution.py.

262  def AddPattern(self, pattern, repl, exceptions=[]):
263  return self.AddRegExPattern(pattern, repl, exceptions)
264 
def SerialSubstitution.ProcessorClass.AddRegExPattern (   self,
  pattern,
  repl,
  exceptions = [] 
)

Definition at line 238 of file SerialSubstitution.py.

239  def AddRegExPattern(self, pattern, repl, exceptions = []):
240  self.RecordPattern(RegExSubstitutionClass(pattern, repl, exceptions))
return self
def SerialSubstitution.ProcessorClass.AddRegExRemoveLine (   self,
  pattern,
  exceptions = [] 
)

Definition at line 243 of file SerialSubstitution.py.

244  def AddRegExRemoveLine(self, pattern, exceptions = []):
245  self.RecordPattern(RegExDeleteLineClass(pattern, exceptions))
return self
def SerialSubstitution.ProcessorClass.AddSimplePattern (   self,
  pattern,
  repl,
  exceptions = [] 
)

Definition at line 248 of file SerialSubstitution.py.

249  def AddSimplePattern(self, pattern, repl, exceptions = []):
250  self.RecordPattern(ReplacementClass(pattern, repl, exceptions))
return self
def SerialSubstitution.ProcessorClass.AddWarningPattern (   self,
  pattern,
  msg,
  exceptions = [] 
)

Definition at line 256 of file SerialSubstitution.py.

257  def AddWarningPattern(self, pattern, msg, exceptions = []):
258  self.RecordPattern(WarningClass(pattern, msg, exceptions))
return self
def SerialSubstitution.ProcessorClass.AddWord (   self,
  word,
  repl,
  exceptions = [] 
)

Definition at line 253 of file SerialSubstitution.py.

254  def AddWord(self, word, repl, exceptions = []):
255  return self.AddRegExPattern(r'\b' + word + r'\b', repl, exceptions)
def SerialSubstitution.ProcessorClass.Color (   self,
  msg,
  category 
)

Definition at line 214 of file SerialSubstitution.py.

215  def Color(self, msg, category): return Colorize(msg, category, self.options)
def SerialSubstitution.ProcessorClass.CreateTempFile (   FilePath)
static

Definition at line 419 of file SerialSubstitution.py.

420  def CreateTempFile(FilePath):
421  TempPath = os.path.join(
422  tempfile.gettempdir(),
423  tempfile.gettempprefix() + "-" + os.path.basename(FilePath) + ".tmp"
424  )
425  TempFile = open(TempPath, 'w')
return TempFile
open(RACETRACK) or die("Could not open file $RACETRACK for writing")
def SerialSubstitution.ProcessorClass.Describe (   self)

Definition at line 404 of file SerialSubstitution.py.

405  def Describe(self):
406  output = [
407  "Processor '%s' applies %d substitutions" % (self, len(self.patterns))
408  ]
409  for subst in self.patterns:
410  try: output.append(" " + subst.Describe())
411  except AttributeError:
412  output.append(" " + str(subst))
413  except:
414  output.append(" " + repr(subst))
415  # for
return output
def SerialSubstitution.ProcessorClass.MatchFile (   self,
  FilePath 
)

Definition at line 265 of file SerialSubstitution.py.

266  def MatchFile(self, FilePath):
267  if not self.file_filters: return True
268  for pattern in self.file_filters:
269  if pattern.match(FilePath) is None: continue
270  # logging.debug("Matched pattern: '%s'", pattern.pattern)
271  return True
272  # for
return False
def SerialSubstitution.ProcessorClass.ProcessDir (   self,
  DirPath 
)
Returns the number of files processor actually acted on

Definition at line 369 of file SerialSubstitution.py.

370  def ProcessDir(self, DirPath):
371  """Returns the number of files processor actually acted on"""
372  nActions = 0
373  if os.path.isdir(DirPath):
374  for dirpath, dirnames, filenames in os.walk(DirPath):
375  filepaths \
376  = [ os.path.join(dirpath, filename) for filename in filenames ]
377  nChanged = self.ProcessFiles(*filepaths)
378  if nChanged > 0:
379  logging.debug(" processor '%s' changed %d files in '%s'",
380  self.name, nChanged, dirpath
381  )
382  # if
383  nActions += nChanged
384  # for
385  if nActions > 0:
386  ApplyChangesMsg = "changed" if self.options.DoIt else "would change"
387  logging.info("Processor '%s' %s %d files in '%s'",
388  self.name, ApplyChangesMsg, nActions, DirPath
389  )
390  # if nActions
391  else:
392  if self.ProcessFile(DirPath):
393  ApplyChangesMsg = "changed" if self.options.DoIt else "would change"
394  logging.info("Processor '%s' %s file '%s'",
395  self.name, ApplyChangesMsg, DirPath
396  )
397  nActions += 1
398  # if
399  # if ... else
return nActions
def SerialSubstitution.ProcessorClass.ProcessFile (   self,
  FilePath 
)
Returns whether substitutions were performed

Definition at line 309 of file SerialSubstitution.py.

310  def ProcessFile(self, FilePath):
311  """Returns whether substitutions were performed"""
312  if not self.patterns: return False
def SerialSubstitution.ProcessorClass.ProcessFiles (   self,
  files 
)

Definition at line 361 of file SerialSubstitution.py.

362  def ProcessFiles(self, *files):
363  nChanged = 0
364  for FilePath in files:
365  if self.ProcessFile(FilePath): nChanged += 1
366  # for files
return nChanged
def SerialSubstitution.ProcessorClass.RecordPattern (   self,
  pattern 
)

Definition at line 216 of file SerialSubstitution.py.

217  def RecordPattern(self, pattern):
218  pattern.SetOptions(self.options)
self.patterns.append(pattern)
def SerialSubstitution.ProcessorClass.SetColors (   self,
  colors 
)

Definition at line 209 of file SerialSubstitution.py.

210  def SetColors(self, **colors):
211  try: self.options.Colors.update(colors)
except AttributeError: self.options.Colors = colors
def SerialSubstitution.ProcessorClass.SetOptions (   self,
  options 
)

Definition at line 203 of file SerialSubstitution.py.

204  def SetOptions(self, options):
205  self.options = options
206  for pattern in self.patterns: pattern.SetOptions(options)
return self
def SerialSubstitution.ProcessorClass.SubstituteLine (   self,
  line,
  context = None 
)
Returns the very same string if the new line is the same as the old one
or a list of lines to replace line with

Definition at line 275 of file SerialSubstitution.py.

276  def SubstituteLine(self, line, context = None):
277  """Returns the very same string if the new line is the same as the old one
278  or a list of lines to replace line with
279  """
280  if line is None: return line
281 
282  for subst in self.patterns:
283  new_line = subst(line, context)
284  if new_line is line: continue
285 
286  msg = " pattern '%s' matched" % subst
287  if context is not None: msg += " at %s" % context
288  msg += ":"
289  if isinstance(new_line, str):
290  msg += "\n OLD| " + self.Color(line.rstrip('\n'), 'old')
291  msg += "\n NEW| %s" % self.Color(new_line.rstrip('\n'), 'new')
292  elif not new_line:
293  msg += "\n DEL| %s" % self.Color(line.rstrip('\n'), 'old')
294  else:
295  msg += "\n OLD| " + self.Color(line.rstrip('\n'), 'old')
296  for l in new_line:
297  msg += "\n NEW| %s" % self.Color(l.rstrip('\n'), 'new')
298  # if ... else
299  self.options.LogMsg(msg)
300 
301  # if the result if not a single line, we interrupt here;
302  # no particular reason, but we don't need a more complex behaviour
303  if not isinstance(new_line, str): return new_line
304 
305  line = new_line
306  # for
return line

Member Data Documentation

list SerialSubstitution.ProcessorClass.Content = []
static

Definition at line 321 of file SerialSubstitution.py.

tuple SerialSubstitution.ProcessorClass.context = LineNoContextClass(FilePath, 0)
static

Definition at line 319 of file SerialSubstitution.py.

SerialSubstitution.ProcessorClass.file_filters

Definition at line 199 of file SerialSubstitution.py.

SerialSubstitution.ProcessorClass.name

Definition at line 197 of file SerialSubstitution.py.

int SerialSubstitution.ProcessorClass.nChanges = 0
static

Definition at line 322 of file SerialSubstitution.py.

tuple SerialSubstitution.ProcessorClass.new_line = self.SubstituteLine(line, context)
static

Definition at line 326 of file SerialSubstitution.py.

SerialSubstitution.ProcessorClass.options

Definition at line 198 of file SerialSubstitution.py.

tuple SerialSubstitution.ProcessorClass.OutputFile = ProcessorClass.CreateTempFile(FilePath)
static

Definition at line 347 of file SerialSubstitution.py.

SerialSubstitution.ProcessorClass.OutputPath = OutputFile.name
static

Definition at line 348 of file SerialSubstitution.py.

SerialSubstitution.ProcessorClass.patterns

Definition at line 200 of file SerialSubstitution.py.

tuple SerialSubstitution.ProcessorClass.SourceFile = open(FilePath, 'r')
static

Definition at line 323 of file SerialSubstitution.py.


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