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__ (self, name)
 
def SetOptions (self, options)
 
def SetColors (self, colors)
 
def Color (self, msg, category)
 
def RecordPattern (self, pattern)
 
def AddFilePattern (self, pattern)
 
def AddFileNamePattern (self, name_pattern)
 
def AddFileType (self, suffixes)
 
def AddRegExPattern (self, pattern, repl, exceptions=[])
 
def AddRegExRemoveLine (self, pattern, exceptions=[])
 
def AddSimplePattern (self, pattern, repl, exceptions=[])
 
def AddWord (self, word, repl, exceptions=[])
 
def AddWarningPattern (self, pattern, msg, exceptions=[])
 
def AddPattern (self, pattern, repl, exceptions=[])
 
def MatchFile (self, FilePath)
 
def SubstituteLine (self, line, context=None)
 
def ProcessFile (self, FilePath)
 
def ProcessFiles (self, files)
 
def ProcessDir (self, DirPath)
 
def __str__ (self)
 
def Describe (self)
 

Static Public Member Functions

def CreateTempFile (FilePath)
 

Public Attributes

 name
 
 options
 
 file_filters
 
 patterns
 

Static Public Attributes

 context = LineNoContextClass(FilePath, 0)
 
list Content = []
 
int nChanges = 0
 
 SourceFile = open(FilePath, 'r')
 
 new_line = self.SubstituteLine(line, context)
 
 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.

Member Function Documentation

def SerialSubstitution.ProcessorClass.__str__ (   self)

Definition at line 402 of file SerialSubstitution.py.

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

Definition at line 229 of file SerialSubstitution.py.

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

Definition at line 222 of file SerialSubstitution.py.

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

Definition at line 232 of file SerialSubstitution.py.

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

Definition at line 261 of file SerialSubstitution.py.

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

Definition at line 238 of file SerialSubstitution.py.

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

Definition at line 243 of file SerialSubstitution.py.

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

Definition at line 248 of file SerialSubstitution.py.

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

Definition at line 256 of file SerialSubstitution.py.

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

Definition at line 253 of file SerialSubstitution.py.

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

Definition at line 214 of file SerialSubstitution.py.

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

Definition at line 419 of file SerialSubstitution.py.

419  def CreateTempFile(FilePath):
420  TempPath = os.path.join(
421  tempfile.gettempdir(),
422  tempfile.gettempprefix() + "-" + os.path.basename(FilePath) + ".tmp"
423  )
424  TempFile = open(TempPath, 'w')
425  return TempFile
int open(const char *, int)
Opens a file descriptor.
def SerialSubstitution.ProcessorClass.Describe (   self)

Definition at line 404 of file SerialSubstitution.py.

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

Definition at line 265 of file SerialSubstitution.py.

265  def MatchFile(self, FilePath):
266  if not self.file_filters: return True
267  for pattern in self.file_filters:
268  if pattern.match(FilePath) is None: continue
269  # logging.debug("Matched pattern: '%s'", pattern.pattern)
270  return True
271  # for
272  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.

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

Definition at line 309 of file SerialSubstitution.py.

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

Definition at line 361 of file SerialSubstitution.py.

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

Definition at line 216 of file SerialSubstitution.py.

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

Definition at line 209 of file SerialSubstitution.py.

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

Definition at line 203 of file SerialSubstitution.py.

203  def SetOptions(self, options):
204  self.options = options
205  for pattern in self.patterns: pattern.SetOptions(options)
206  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.

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

Member Data Documentation

list SerialSubstitution.ProcessorClass.Content = []
static

Definition at line 321 of file SerialSubstitution.py.

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.

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.

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.

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: