Biskit :: LocalPath :: LocalPath :: Class LocalPath
[hide private]
[frames] | no frames]

Class LocalPath

source code


Encapsulate a file name that might look differently in different environments depending on environment settings. The file name has an absolute (original) variant but parts of it can be looked up from environment or Biskit settings variables (if the original path doesn't exist in the current environment).

Creating a LocalPath:

Creating a LocalPath is simple. By default, LocalPath takes a normal filename and browses the local Biskit.settings and run-time environment for variables that can replace part of that path.

Variable values must be at least 3 char long and contain at least one '/' in order to avoid substitution of single letters or '/a' etc. $PYTHONPATH, $PATH and $PWD are ignored.

Variables from Biskit.settings (thus also .biskit/settings.cfg) have priority over normal environment variables both during the construction of LocalPath instances and during the reconstruction of a locally valid path.

Using a LocalPath:

LocalPath tries behaving like the simple absolute path string when it comes to slicing etc:
 l = LocalPath( '{/home/raik|$USER}/data/x.txt' )
 l[:] == '/home/raik/data/x.txt' == l.local()
 l[-4:] == '.txt'
 str( l ) == l.local()

 l.formatted() == '{/home/raik|$USER}/data/x.txt'



To Do: input: allow multiple or overlapping substitutions output: only substitute what is necessary until path exists

Instance Methods [hide private]
  __init__(self, path=None, checkEnv=1, minLen=3, maxSub=1, absolute=1, resolveLinks=0, **vars)
Create a new environment-dependent path from either a list of fragments and their substitution variable names or from a path or from a formatted string (not implemented).
  __setstate__(self, state)
called for unpickling the object.
str get_local(self, existing=0)
Return a valid, absolute path.
str local(self, existing=0, force=1)
Cached variant of get_local.
str formatted(self)
Get a string representation that describes the original path and all possible substitutions by environment variables.
str original(self)
Get the original path (also non-absolute) that is used if the file exists or if there are no environment variables for any of the substitutions.
  set(self, v, checkEnv=1, minLen=3, maxSub=1, absolute=1, resolveLinks=0, **vars)
Assign a new file name.
  set_fragments(self, *fragments)
Set new path from list of path fragments and their possible environment variable substitutions.
  set_string(self, s)
Set a new path and its substitutable parts from a formatted string.
  absfile(self, fname, resolveLinks=0)
  set_path(self, fname, minLen=3, absolute=1, resolveLinks=0, maxSub=1, **vars)
Set a new path and try to identify settings/environment variables that could substitute parts of it.
1|0 exists(self)
Check if path exists
any load(self)
Try to unpickle an object from the currently valid path.
str dump(self, o)
Try to pickle an object to the currently valid path.
  __find_subpath(self, path, subpath)
[ (str, str) ] __substitute(self, fragments, name, value)
Look in all not yet substituted fragments for parts that can be substituted by value and, if successful, create a new fragment
int __is_path(self, o, minLen=3)
Check whether an object is a path string (existing or not).
[ (str,str) ] __path_vars(self, d, minLen=3, vars={}, exclude=[])
@see __paths_in_settings and __paths_in_env
[ (str,str) ] __paths_in_settings(self, minLen=3, vars={}, exclude=[])
Get all setting variables looking like a path, sorted by length
[ (str,str) ] __paths_in_env(self, minLen=3, vars={}, exclude=[])
Get all environment variables with at least one '/' sorted by length.
[ (str,str) ] get_substitution_pairs(self, minLen=3, vars={}, exclude=[])
Get all variable/value pairs that are available for path substitutions.
  get_substitution_dict(self, minLen=3, vars={}, exclude=[])
str __str__(self)
Returns Same as local().
str __repr__(self)
Returns formatted output (Python representation)
int __len__(self)
Time costly when repeated many times.
  __getslice__(self, a, b)
  __getitem__(self, i)
  __eq__(self, other)
supports this == other -> 0|1
  __ne__(self, other)
supports this != other -> 0|1
  __hash__(self)
if __eq__ or __cmp__ are defined hash has to be defined too, otherwise the objects cannot be used as keys in dictionaries (needed for Complex- ModelRegistry).

Inherited from object: __delattr__, __getattribute__, __new__, __reduce__, __reduce_ex__, __setattr__


Class Variables [hide private]
  ex_fragment = re.compile('\{([a-zA-Z0-9_~/ ]+)\|\$([a-zA-Z0-9_]+)\}')
  ex_minpath = re.compile('/|~[a-zA-Z0-9_~/ ]+')
  exclude_vars = ['PWD', 'OLDPWD', 'PYTHONPATH', 'PATH']

Properties [hide private]

Inherited from object: __class__


Method Details [hide private]

__init__(self, path=None, checkEnv=1, minLen=3, maxSub=1, absolute=1, resolveLinks=0, **vars)
(Constructor)

source code 

Create a new environment-dependent path from either a list of fragments and their substitution variable names or from a path or from a formatted string (not implemented). A path will be analyzed to substitute as big chunks as possible by environment variables. ~ and ../../ will be expanded both in the given path and in the environment variables.
Parameters:
  • path ([ (str, str) ] OR str) - path(s)
  • checkEnv (1|0) - look for substitution values among environment variables (default 1)
  • absolute (1|0) - normalize file name [1]
  • resolveLinks (1|0) - resolve symbolic links [0]
  • maxSub (int) - maximal number of substitutions [1]
  • vars (param=value) - additional param=value pairs with suggested substitutors
Overrides: object.__init__

__setstate__(self, state)

source code 

called for unpickling the object.

get_local(self, existing=0)

source code 

Return a valid, absolute path. Either the existing original or with all substitutions for which environment variables exist. This function is time consuming (absfile - os.realpath is the culprit).
Parameters:
  • existing (0|1) - don't return a non-existing path
Returns: str
valid absolute path in current environment
Raises:
  • LocalPathError - if existing==1 and no existing path can be constructed via environment variables

local(self, existing=0, force=1)

source code 

Cached variant of get_local. Return a valid, absolute path. Either the existing original or with all substitutions for which environment variables exist. This function is time consuming (absfile - os.realpath is the culprit).
Parameters:
  • existing (0|1) - don't return a non-existing path [0]
  • force (0|1) - override cached value [0]
Returns: str
valid absolute (not necessarily existing) path in current environment
Raises:
  • LocalPathError - if existing==1 and no existing path can be constructed via environment variables

formatted(self)

source code 

Get a string representation that describes the original path and all possible substitutions by environment variables.
Returns: str
formated path e.g. {/home/raik|$USER}/data/x.txt'

original(self)

source code 

Get the original path (also non-absolute) that is used if the file exists or if there are no environment variables for any of the substitutions.
Returns: str
original path

set(self, v, checkEnv=1, minLen=3, maxSub=1, absolute=1, resolveLinks=0, **vars)

source code 

Assign a new file name. checkEnv, minLen, resolve*, maxSub are only considered for path name input.
Parameters:
  • v ([ (str,str) ] OR str) - fragment tuples or path or custom-formatted string
  • checkEnv (0|1) - {/x/y|$xy}/z.txt' )
  • minLen (int) - mininal length of environment variables to consider [3]
  • absolute (1|0) - normalize file name [1]
  • resolveLinks (1|0) - resolve symbolic links [0]
  • maxSub (int) - maximal number of substitutions [1]
  • vars (param=value) - additional param=value pairs with suggested substitutors

set_fragments(self, *fragments)

source code 

Set new path from list of path fragments and their possible environment variable substitutions. Fragments that can not be substituted are given as (str, None).
Parameters:
  • fragments ([ (str, str), (str, None), .. ]) - list of fragment tuples

set_string(self, s)

source code 

Set a new path and its substitutable parts from a formatted string.
Parameters:
  • s (str) - {/home/raik|$USER}/data/test.txt
Raises:
  • PathError - formatted input is not yet implemented

absfile(self, fname, resolveLinks=0)

source code 

set_path(self, fname, minLen=3, absolute=1, resolveLinks=0, maxSub=1, **vars)

source code 

Set a new path and try to identify settings/environment variables that could substitute parts of it.
Parameters:
  • fname (str) - relative or absolute file name
  • minLen (int) - minimal length of string o to be counted as path
  • absolute (1|0) - normalize file name [1]
  • resolveLinks (1|0) - resolve symbolic links [0]
  • maxSub (int) - maximal number of substitutions [1]
  • vars (param=value) - additional param=value pairs with suggested substitutors

exists(self)

source code 

Check if path exists
Returns: 1|0
1 if if current path exists

load(self)

source code 

Try to unpickle an object from the currently valid path.
Returns: any
unpickled object
Raises:
  • IOError - if file can not be found

dump(self, o)

source code 

Try to pickle an object to the currently valid path.
Returns: str
the absolute path to which o was pickled

__find_subpath(self, path, subpath)

source code 

__substitute(self, fragments, name, value)

source code 

Look in all not yet substituted fragments for parts that can be substituted by value and, if successful, create a new fragment
Parameters:
  • fragments ([ (str, str) ]) - fragment tuples
  • name (str) - substitution variable name
  • value (str) - susbtitution value in current environment
Returns: [ (str, str) ]
fragment tuples

__is_path(self, o, minLen=3)

source code 

Check whether an object is a path string (existing or not).
Parameters:
  • minLen (int) - minimal length of string o to be counted as path
Returns: int
1|0

__path_vars(self, d, minLen=3, vars={}, exclude=[])

source code 

@see __paths_in_settings and __paths_in_env
Returns: [ (str,str) ]
[ (variable name, path) ] sorted by length of value

__paths_in_settings(self, minLen=3, vars={}, exclude=[])

source code 

Get all setting variables looking like a path, sorted by length
Parameters:
  • minLen (int) - minimal path length [3]
  • vars (param=value) - alternative param=value pairs to consider instead of environment
Returns: [ (str,str) ]
[ (variable name, value) ] sorted by length of value

__paths_in_env(self, minLen=3, vars={}, exclude=[])

source code 

Get all environment variables with at least one '/' sorted by length.
Parameters:
  • minLen (int) - minimal path length [3]
  • vars (param=value) - alternative param=value pairs to consider instead of environment
Returns: [ (str,str) ]
[ (variable name, value) ] sorted by length of value

get_substitution_pairs(self, minLen=3, vars={}, exclude=[])

source code 

Get all variable/value pairs that are available for path substitutions.
Parameters:
  • minLen (int) - minimal path length [3]
  • vars (param=value) - additional param=value pairs to consider
Returns: [ (str,str) ]
[ (variable name, value) ] sorted by priority (mostly length of value)

get_substitution_dict(self, minLen=3, vars={}, exclude=[])

source code 

__str__(self)
(Informal representation operator)

source code 

str(x)
Returns: str
Same as local(). string representation for print and str()
Overrides: object.__str__

__repr__(self)
(Representation operator)

source code 

repr(x)
Returns: str
formatted output (Python representation)
Overrides: object.__repr__

__len__(self)
(Length operator)

source code 

Time costly when repeated many times.
Returns: int
length of file name in current environment

__getslice__(self, a, b)
(Slicling operator)

source code 

__getitem__(self, i)
(Indexing operator)

source code 

__eq__(self, other)
(Equality operator)

source code 

supports this == other -> 0|1

__ne__(self, other)

source code 

supports this != other -> 0|1

__hash__(self)
(Hashing function)

source code 

if __eq__ or __cmp__ are defined hash has to be defined too, otherwise the objects cannot be used as keys in dictionaries (needed for Complex- ModelRegistry).
Returns:
int
Overrides: object.__hash__

Class Variable Details [hide private]

ex_fragment

Value:
re.compile('\{([a-zA-Z0-9_~/ ]+)\|\$([a-zA-Z0-9_]+)\}')                
      

ex_minpath

Value:
re.compile('/|~[a-zA-Z0-9_~/ ]+')                                      
      

exclude_vars

Value:
['PWD', 'OLDPWD', 'PYTHONPATH', 'PATH']