Back to index

enigmail  1.4.3
Public Member Functions | Public Attributes | Static Private Attributes
pymake.data.Pattern Class Reference

List of all members.

Public Member Functions

def __init__
def ismatchany
def ispattern
def __hash__
def __eq__
def gettarget
def hasslash
def match
def resolve
def subst
def __repr__
def __str__

Public Attributes

 data

Static Private Attributes

tuple __slots__ = ('data')
tuple _backre = re.compile(r'[%\\]')

Detailed Description

A pattern is a string, possibly with a % substitution character. From the GNU make manual:

'%' characters in pattern rules can be quoted with precending backslashes ('\'). Backslashes that
would otherwise quote '%' charcters can be quoted with more backslashes. Backslashes that
quote '%' characters or other backslashes are removed from the pattern before it is compared t
file names or has a stem substituted into it. Backslashes that are not in danger of quoting '%'
characters go unmolested. For example, the pattern the\%weird\\%pattern\\ has `the%weird\' preceding
the operative '%' character, and 'pattern\\' following it. The final two backslashes are left alone
because they cannot affect any '%' character.

This insane behavior probably doesn't matter, but we're compatible just for shits and giggles.

Definition at line 348 of file data.py.


Constructor & Destructor Documentation

def pymake.data.Pattern.__init__ (   self,
  s 
)

Definition at line 365 of file data.py.

00365 
00366     def __init__(self, s):
00367         r = []
00368         i = 0
00369         while i < len(s):
00370             c = s[i]
00371             if c == '\\':
00372                 nc = s[i + 1]
00373                 if nc == '%':
00374                     r.append('%')
00375                     i += 1
00376                 elif nc == '\\':
00377                     r.append('\\')
00378                     i += 1
00379                 else:
00380                     r.append(c)
00381             elif c == '%':
00382                 self.data = (''.join(r), s[i+1:])
00383                 return
00384             else:
00385                 r.append(c)
00386             i += 1
00387 
00388         # This is different than (s,) because \% and \\ have been unescaped. Parsing patterns is
00389         # context-sensitive!
00390         self.data = (''.join(r),)


Member Function Documentation

def pymake.data.Pattern.__eq__ (   self,
  o 
)

Definition at line 400 of file data.py.

00400 
00401     def __eq__(self, o):
00402         assert isinstance(o, Pattern)
00403         return self.data == o.data

Definition at line 397 of file data.py.

00397 
00398     def __hash__(self):
00399         return self.data.__hash__()

Definition at line 461 of file data.py.

00461 
00462     def __repr__(self):
00463         return "<Pattern with data %r>" % (self.data,)

Definition at line 465 of file data.py.

00465 
00466     def __str__(self):
00467         if not self.ispattern():
00468             return self._backre.sub(r'\\\1', self.data[0])
00469 
00470         return self._backre.sub(r'\\\1', self.data[0]) + '%' + self.data[1]

Here is the call graph for this function:

Definition at line 404 of file data.py.

00404 
00405     def gettarget(self):
00406         assert not self.ispattern()
00407         return self.data[0]

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 408 of file data.py.

00408 
00409     def hasslash(self):
00410         return self.data[0].find('/') != -1 or self.data[1].find('/') != -1

Definition at line 391 of file data.py.

00391 
00392     def ismatchany(self):
00393         return self.data == ('','')

Definition at line 394 of file data.py.

00394 
00395     def ispattern(self):
00396         return len(self.data) == 2

Here is the caller graph for this function:

def pymake.data.Pattern.match (   self,
  word 
)
Match this search pattern against a word (string).

@returns None if the word doesn't match, or the matching stem.
      If this is a %-less pattern, the stem will always be ''

Definition at line 411 of file data.py.

00411 
00412     def match(self, word):
00413         """
00414         Match this search pattern against a word (string).
00415 
00416         @returns None if the word doesn't match, or the matching stem.
00417                       If this is a %-less pattern, the stem will always be ''
00418         """
00419         d = self.data
00420         if len(d) == 1:
00421             if word == d[0]:
00422                 return word
00423             return None
00424 
00425         d0, d1 = d
00426         l1 = len(d0)
00427         l2 = len(d1)
00428         if len(word) >= l1 + l2 and word.startswith(d0) and word.endswith(d1):
00429             if l2 == 0:
00430                 return word[l1:]
00431             return word[l1:-l2]
00432 
00433         return None

Here is the caller graph for this function:

def pymake.data.Pattern.resolve (   self,
  dir,
  stem 
)

Definition at line 434 of file data.py.

00434 
00435     def resolve(self, dir, stem):
00436         if self.ispattern():
00437             return dir + self.data[0] + stem + self.data[1]
00438 
00439         return self.data[0]

Here is the call graph for this function:

Here is the caller graph for this function:

def pymake.data.Pattern.subst (   self,
  replacement,
  word,
  mustmatch 
)
Given a word, replace the current pattern with the replacement pattern, a la 'patsubst'

@param mustmatch If true and this pattern doesn't match the word, throw a DataError. Otherwise
         return word unchanged.

Definition at line 440 of file data.py.

00440 
00441     def subst(self, replacement, word, mustmatch):
00442         """
00443         Given a word, replace the current pattern with the replacement pattern, a la 'patsubst'
00444 
00445         @param mustmatch If true and this pattern doesn't match the word, throw a DataError. Otherwise
00446                          return word unchanged.
00447         """
00448         assert isinstance(replacement, str)
00449 
00450         stem = self.match(word)
00451         if stem is None:
00452             if mustmatch:
00453                 raise DataError("target '%s' doesn't match pattern" % (word,))
00454             return word
00455 
00456         if not self.ispattern():
00457             # if we're not a pattern, the replacement is not parsed as a pattern either
00458             return replacement
00459 
00460         return Pattern(replacement).resolve('', stem)

Here is the call graph for this function:


Member Data Documentation

tuple pymake.data.Pattern.__slots__ = ('data') [static, private]

Definition at line 363 of file data.py.

tuple pymake.data.Pattern._backre = re.compile(r'[%\\]') [static, private]

Definition at line 464 of file data.py.

Definition at line 381 of file data.py.


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