Package logilab :: Package common :: Module fileutils
[frames] | no frames]

Module fileutils

source code

File and file-path manipulation utilities.
Classes
  ProtectedFile
A special file-object class that automatically does a 'chmod +w' when needed.
  UnresolvableError
Exception raised by relative path when it's unable to compute relative path between two paths.
Functions
 
abspath_listdir(path)
Lists path's content using absolute paths.
source code
    path manipulation
str
first_level_directory(path)
Return the first level directory of a path.
source code
bool
is_binary(filename)
Return true if filename may be a binary file, according to it's extension.
source code
str
relative_path(from_file, to_file)
Try to get a relative path from from_file to to_file (path will be absolute if to_file is an absolute file). This function is useful to create link in from_file to to_file. This typical use case is used in this function description.
source code
 
remove_dead_links(directory, verbose=0)
Recursively traverse directory and remove all dead links.
source code
    file manipulation
str
write_open_mode(filename)
Return the write mode that should used to open file.
source code
 
ensure_fs_mode(filepath, desired_mode=S_IWRITE)
Check that the given file has the given mode(s) set, else try to set it.
source code
list
lines(path, comments=None)
Return a list of non empty lines in the file located at path.
source code
list
stream_lines(stream, comments=None)
Return a list of non empty lines in the given stream.
source code
 
export(from_dir, to_dir, blacklist=BASE_BLACKLIST, ignore_ext=IGNORED_EXTENSIONS, verbose=0)
Make a mirror of from_dir in to_dir, omitting directories and files listed in the black list or ending with one of the given extensions.
source code
Variables
    file manipulation
  norm_read = deprecated("use \"open(path, 'U').read()\"")(norm_...
  norm_open = deprecated("use \"open(path, 'U')\"")(norm_open)
Function Details

first_level_directory(path)

source code 

Return the first level directory of a path.

>>> first_level_directory('home/syt/work')
'home'
>>> first_level_directory('/home/syt/work')
'/'
>>> first_level_directory('work')
'work'
>>>
Parameters:
  • path (str) - the path for which we want the first level directory
Returns: str
the first level directory appearing in path

is_binary(filename)

source code 
Return true if filename may be a binary file, according to it's extension.
Parameters:
  • filename (str) - the name of the file
Returns: bool
true if the file is a binary file (actually if it's mime type isn't beginning by text/)

write_open_mode(filename)

source code 
Return the write mode that should used to open file.
Parameters:
  • filename (str) - the name of the file
Returns: str
the mode that should be use to open the file ('w' or 'wb')

ensure_fs_mode(filepath, desired_mode=S_IWRITE)

source code 
Check that the given file has the given mode(s) set, else try to set it.
Parameters:
  • filepath (str) - path of the file
  • desired_mode (int) - ORed flags describing the desired mode. Use constants from the stat module for file permission's modes

relative_path(from_file, to_file)

source code 

Try to get a relative path from from_file to to_file (path will be absolute if to_file is an absolute file). This function is useful to create link in from_file to to_file. This typical use case is used in this function description.

If both files are relative, they're expected to be relative to the same directory.

>>> relative_path( from_file='toto/index.html', to_file='index.html')
'../index.html'
>>> relative_path( from_file='index.html', to_file='toto/index.html')
'toto/index.html'
>>> relative_path( from_file='tutu/index.html', to_file='toto/index.html')
'../toto/index.html'
>>> relative_path( from_file='toto/index.html', to_file='/index.html')
'/index.html'
>>> relative_path( from_file='/toto/index.html', to_file='/index.html')
'../index.html'
>>> relative_path( from_file='/toto/index.html', to_file='/toto/summary.html')
'summary.html'
>>> relative_path( from_file='index.html', to_file='index.html')
''
>>> relative_path( from_file='/index.html', to_file='toto/index.html')
Traceback (most recent call last):
  File "<string>", line 1, in ?
  File "<stdin>", line 37, in relative_path
UnresolvableError
>>> relative_path( from_file='/index.html', to_file='/index.html')
''
>>>
Parameters:
  • from_file (str) - source file (where links will be inserted)
  • to_file (str) - target file (on which links point)
Returns: str
the relative path of to_file from from_file
Raises:

lines(path, comments=None)

source code 
Return a list of non empty lines in the file located at path.
Parameters:
  • path (str) - path to the file
  • comments (str or None) - optional string which can be used to comment a line in the file (i.e. lines starting with this string won't be returned)
Returns: list
a list of stripped line in the file, without empty and commented lines

Warning: at some point this function will probably return an iterator

stream_lines(stream, comments=None)

source code 
Return a list of non empty lines in the given stream.
Parameters:
  • stream (object implementing 'xreadlines' or 'readlines') - file like object
  • comments (str or None) - optional string which can be used to comment a line in the file (i.e. lines starting with this string won't be returned)
Returns: list
a list of stripped line in the file, without empty and commented lines

Warning: at some point this function will probably return an iterator

export(from_dir, to_dir, blacklist=BASE_BLACKLIST, ignore_ext=IGNORED_EXTENSIONS, verbose=0)

source code 
Make a mirror of from_dir in to_dir, omitting directories and files listed in the black list or ending with one of the given extensions.
Parameters:
  • from_dir (str) - directory to export
  • to_dir (str) - destination directory
  • blacklist (list or tuple) - list of files or directories to ignore, default to the content of BASE_BLACKLIST
  • ignore_ext (list or tuple) - list of extensions to ignore, default to the content of IGNORED_EXTENSIONS
  • verbose (bool) - flag indicating whether information about exported files should be printed to stderr, default to False

remove_dead_links(directory, verbose=0)

source code 
Recursively traverse directory and remove all dead links.
Parameters:
  • directory (str) - directory to cleanup
  • verbose (bool) - flag indicating whether information about deleted links should be printed to stderr, default to False

Variables Details

norm_read

Value:
deprecated("use \"open(path, 'U').read()\"")(norm_read)