Vega strike Python Modules doc  0.5.1
Documentation of the " Modules " folder of Vega strike
 All Data Structures Namespaces Files Functions Variables
fnmatch Namespace Reference

Functions

def fnmatch
 
def filter
 
def fnmatchcase
 
def translate
 

Variables

list __all__ = ["fnmatch","fnmatchcase","translate"]
 
dictionary _cache = {}
 

Detailed Description

Filename matching with shell patterns.

fnmatch(FILENAME, PATTERN) matches according to the local convention.
fnmatchcase(FILENAME, PATTERN) always takes case in account.

The functions operate by translating the pattern into a regular
expression.  They cache the compiled regular expressions for speed.

The function translate(PATTERN) returns a regular expression
corresponding to PATTERN.  (It does not compile it.)

Function Documentation

def fnmatch.filter (   names,
  pat 
)
Return the subset of the list NAMES that match PAT

Definition at line 40 of file fnmatch.py.

References pre.match(), and translate().

40 
41 def filter(names, pat):
42  """Return the subset of the list NAMES that match PAT"""
43  import os,posixpath
44  result=[]
45  pat=os.path.normcase(pat)
46  if not _cache.has_key(pat):
47  res = translate(pat)
48  _cache[pat] = re.compile(res)
49  match=_cache[pat].match
50  if os.path is posixpath:
51  # normcase on posix is NOP. Optimize it away from the loop.
52  for name in names:
53  if match(name):
54  result.append(name)
55  else:
56  for name in names:
57  if match(os.path.normcase(name)):
58  result.append(name)
59  return result
def fnmatch.fnmatch (   name,
  pat 
)
Test whether FILENAME matches PATTERN.

Patterns are Unix shell style:

*       matches everything
?       matches any single character
[seq]   matches any character in seq
[!seq]  matches any char not in seq

An initial period in FILENAME is not special.
Both FILENAME and PATTERN are first case-normalized
if the operating system requires it.
If you don't want this, use fnmatchcase(FILENAME, PATTERN).

Definition at line 19 of file fnmatch.py.

References fnmatchcase().

19 
20 def fnmatch(name, pat):
21  """Test whether FILENAME matches PATTERN.
22 
23  Patterns are Unix shell style:
24 
25  * matches everything
26  ? matches any single character
27  [seq] matches any character in seq
28  [!seq] matches any char not in seq
29 
30  An initial period in FILENAME is not special.
31  Both FILENAME and PATTERN are first case-normalized
32  if the operating system requires it.
33  If you don't want this, use fnmatchcase(FILENAME, PATTERN).
34  """
35 
36  import os
37  name = os.path.normcase(name)
38  pat = os.path.normcase(pat)
39  return fnmatchcase(name, pat)
def fnmatch.fnmatchcase (   name,
  pat 
)
Test whether FILENAME matches PATTERN, including case.

This is a version of fnmatch() which doesn't case-normalize
its arguments.

Definition at line 60 of file fnmatch.py.

References pre.match(), and translate().

60 
61 def fnmatchcase(name, pat):
62  """Test whether FILENAME matches PATTERN, including case.
63 
64  This is a version of fnmatch() which doesn't case-normalize
65  its arguments.
66  """
67 
68  if not _cache.has_key(pat):
69  res = translate(pat)
70  _cache[pat] = re.compile(res)
71  return _cache[pat].match(name) is not None
def fnmatch.translate (   pat)
Translate a shell PATTERN to a regular expression.

There is no way to quote meta-characters.

Definition at line 72 of file fnmatch.py.

References pydoc.replace().

72 
73 def translate(pat):
74  """Translate a shell PATTERN to a regular expression.
75 
76  There is no way to quote meta-characters.
77  """
78 
79  i, n = 0, len(pat)
80  res = ''
81  while i < n:
82  c = pat[i]
83  i = i+1
84  if c == '*':
85  res = res + '.*'
86  elif c == '?':
87  res = res + '.'
88  elif c == '[':
89  j = i
90  if j < n and pat[j] == '!':
91  j = j+1
92  if j < n and pat[j] == ']':
93  j = j+1
94  while j < n and pat[j] != ']':
95  j = j+1
96  if j >= n:
97  res = res + '\\['
98  else:
99  stuff = pat[i:j].replace('\\','\\\\')
100  i = j+1
101  if stuff[0] == '!':
102  stuff = '^' + stuff[1:]
103  elif stuff[0] == '^':
104  stuff = '\\' + stuff
105  res = '%s[%s]' % (res, stuff)
106  else:
107  res = res + re.escape(c)
108  return res + "$"

Variable Documentation

list __all__ = ["fnmatch","fnmatchcase","translate"]

Definition at line 15 of file fnmatch.py.

dictionary _cache = {}

Definition at line 17 of file fnmatch.py.