Package duplicity :: Module selection :: Class Select
[hide private]
[frames] | no frames]

Class Select

source code

Iterate appropriate Paths in given directory

This class acts as an iterator on account of its next() method. Basically, it just goes through all the files in a directory in order (depth-first) and subjects each file to a bunch of tests (selection functions) in order. The first test that includes or excludes the file means that the file gets included (iterated) or excluded. The default is include, so with no tests we would just iterate all the files in the directory in order.

The one complication to this is that sometimes we don't know whether or not to include a directory until we examine its contents. For instance, if we want to include all the **.py files. If /home/ben/foo.py exists, we should also include /home and /home/ben, but if these directories contain no **.py files, they shouldn't be included. For this reason, a test may not include or exclude a directory, but merely "scan" it. If later a file in the directory gets included, so does the directory.

As mentioned above, each test takes the form of a selection function. The selection function takes a path, and returns:

None - means the test has nothing to say about the related file 0 - the file is excluded by the test 1 - the file is included 2 - the test says the file (must be directory) should be scanned

Also, a selection function f has a variable f.exclude which should be true iff f could potentially exclude some file. This is used to signal an error if the last function only includes, which would be redundant and presumably isn't what the user intends.

Instance Methods [hide private]
 
__init__(self, path)
Initializer, called with Path of root directory
source code
 
set_iter(self)
Initialize generator, prepare to iterate.
source code
 
Iterate(self, path)
Return iterator yielding paths in path
source code
 
Select(self, path)
Run through the selection functions and return dominant val 0/1/2
source code
 
ParseArgs(self, argtuples, filelists)
Create selection functions based on list of tuples
source code
 
parse_catch_error(self, exc)
Deal with selection error exc
source code
 
parse_last_excludes(self)
Exit with error if last selection function isn't an exclude
source code
 
add_selection_func(self, sel_func, add_to_start=None)
Add another selection function at the end or beginning
source code
 
filelist_get_sf(self, filelist_fp, inc_default, filelist_name)
Return selection function by reading list of files
source code
 
filelist_read(self, filelist_fp, include, filelist_name)
Read filelist from fp, return (tuplelist, something_excluded)
source code
 
filelist_parse_line(self, line, include)
Parse a single line of a filelist, returning a pair
source code
 
filelist_pair_match(self, path, pair)
Matches a filelist tuple against a path
source code
 
filelist_globbing_get_sfs(self, filelist_fp, inc_default, list_name)
Return list of selection functions by reading fileobj
source code
 
other_filesystems_get_sf(self, include)
Return selection function matching files on other filesystems
source code
 
regexp_get_sf(self, regexp_string, include)
Return selection function given by regexp_string
source code
 
devfiles_get_sf(self)
Return a selection function to exclude all dev files
source code
 
glob_get_sf(self, glob_str, include)
Return selection function given by glob string
source code
 
present_get_sf(self, filename, include)
Return selection function given by existence of a file in a directory
source code
 
glob_get_filename_sf(self, filename, include)
Get a selection function given a normal filename
source code
 
glob_get_tuple_sf(self, tuple, include)
Return selection function based on tuple
source code
 
glob_get_normal_sf(self, glob_str, include)
Return selection function based on glob_str
source code
 
glob_get_prefix_res(self, glob_str)
Return list of regexps equivalent to prefixes of glob_str
source code
 
glob_to_re(self, pat)
Returned regular expression equivalent to shell glob pat
source code
Class Variables [hide private]
  glob_re = re.compile(r'(?is)(.*[\*\?\[]|ignorecase:)')
Method Details [hide private]

Iterate(self, path)

source code 

Return iterator yielding paths in path

This function looks a bit more complicated than it needs to be because it avoids extra recursion (and no extra function calls for non-directory files) while still doing the "directory scanning" bit.

ParseArgs(self, argtuples, filelists)

source code 

Create selection functions based on list of tuples

The tuples are created when the initial commandline arguments are read. They have the form (option string, additional argument) except for the filelist tuples, which should be (option-string, (additional argument, filelist_fp)).

filelist_get_sf(self, filelist_fp, inc_default, filelist_name)

source code 

Return selection function by reading list of files

The format of the filelist is documented in the man page. filelist_fp should be an (open) file object. inc_default should be true if this is an include list, false for an exclude list. filelist_name is just a string used for logging.

filelist_parse_line(self, line, include)

source code 

Parse a single line of a filelist, returning a pair

pair will be of form (index, include), where index is another tuple, and include is 1 if the line specifies that we are including a file. The default is given as an argument. prefix is the string that the index is relative to.

filelist_pair_match(self, path, pair)

source code 

Matches a filelist tuple against a path

Returns a pair (include, move_on). include is None if the tuple doesn't match either way, and 0/1 if the tuple excludes or includes the path.

move_on is true if the tuple cannot match a later index, and so we should move on to the next tuple in the index.

filelist_globbing_get_sfs(self, filelist_fp, inc_default, list_name)

source code 

Return list of selection functions by reading fileobj

filelist_fp should be an open file object inc_default is true iff this is an include list list_name is just the name of the list, used for logging See the man page on --[include/exclude]-globbing-filelist

glob_get_filename_sf(self, filename, include)

source code 

Get a selection function given a normal filename

Some of the parsing is better explained in filelist_parse_line. The reason this is split from normal globbing is things are a lot less complicated if no special globbing characters are used.

glob_get_normal_sf(self, glob_str, include)

source code 

Return selection function based on glob_str

The basic idea is to turn glob_str into a regular expression, and just use the normal regular expression. There is a complication because the selection function should return '2' (scan) for directories which may contain a file which matches the glob_str. So we break up the glob string into parts, and any file which matches an initial sequence of glob parts gets scanned.

Thanks to Donovan Baarda who provided some code which did some things similar to this.

glob_to_re(self, pat)

source code 

Returned regular expression equivalent to shell glob pat

Currently only the ?, *, [], and ** expressions are supported. Ranges like [a-z] are also currently unsupported. There is no way to quote these special characters.

This function taken with minor modifications from efnmatch.py by Donovan Baarda.