Compat Module

browsepy.compat.isexec(path)[source]

Check if given path points to an executable file.

Parameters:path (str) – file path
Returns:True if executable, False otherwise
Return type:bool
browsepy.compat.pathconf(path, os_name='posix', isdir_fnc=<function isdir>, pathconf_fnc=<built-in function pathconf>, pathconf_names={'PC_ALLOC_SIZE_MIN': 18, 'PC_ASYNC_IO': 10, 'PC_CHOWN_RESTRICTED': 6, 'PC_FILESIZEBITS': 13, 'PC_LINK_MAX': 0, 'PC_MAX_CANON': 1, 'PC_MAX_INPUT': 2, 'PC_NAME_MAX': 3, 'PC_NO_TRUNC': 7, 'PC_PATH_MAX': 4, 'PC_PIPE_BUF': 5, 'PC_PRIO_IO': 11, 'PC_REC_INCR_XFER_SIZE': 14, 'PC_REC_MAX_XFER_SIZE': 15, 'PC_REC_MIN_XFER_SIZE': 16, 'PC_REC_XFER_ALIGN': 17, 'PC_SOCK_MAXBUF': 12, 'PC_SYMLINK_MAX': 19, 'PC_SYNC_IO': 9, 'PC_VDISABLE': 8})[source]

Get all pathconf variables for given path.

Parameters:path (str) – absolute fs path
Returns:dictionary containing pathconf keys and their values (both str)
Return type:dict
browsepy.compat.pathparse(value, sep=':', os_sep='/')[source]

Get enviroment PATH directories as list.

This function cares about spliting, escapes and normalization of paths across OSes.

Parameters:
  • value (str) – path string, as given by os.environ[‘PATH’]
  • sep (str) – PATH separator, defaults to os.pathsep
  • os_sep (str) – OS filesystem path separator, defaults to os.sep
Yields:

every path

Ytype:

str

browsepy.compat.pathsplit(value, sep=':')[source]

Get enviroment PATH elements as list.

This function only cares about spliting across OSes.

Parameters:
  • value (str) – path string, as given by os.environ[‘PATH’]
  • sep (str) – PATH separator, defaults to os.pathsep
Yields:

every path

Ytype:

str

browsepy.compat.re_escape(pattern, chars=frozenset({'|', ')', '#', '\\', '.', '(', '-', '{', '}', '+', '^', '?', '*', '[', '$', ']'}))[source]

Escape all special regex characters in pattern. Logic taken from regex module.

Parameters:pattern – regex pattern to escape
Returns:escaped pattern
Return type:str
browsepy.compat.usedoc(other)[source]

Decorator which copies __doc__ of given object into decorated one.

Usage:

>>> def fnc1():
...     """docstring"""
...     pass
>>> @usedoc(fnc1)
... def fnc2():
...     pass
>>> fnc2.__doc__
'docstring'collections.abc.D
Parameters:other (any) – anything with a __doc__ attribute
Returns:decorator function
Return type:callable
browsepy.compat.FS_ENCODING = sys.getfilesystemencoding()

Detected filesystem encoding: ie. utf-8.

browsepy.compat.PY_LEGACY = sys.version_info < (3, )

True on Python 2, False on newer.

browsepy.compat.ENV_PATH = ('/usr/local/bin', '/usr/bin', ... )
browsepy.compat.ENV_PATHEXT = ('.exe', '.bat', ... ) if os.name == 'nt' else ('',)

List of paths where commands are located, taken and processed from PATH environment variable. Used by which().

browsepy.compat.TRUE_VALUES = frozenset({'true', 'yes', '1', 'enable', 'enabled', True, 1})

Values which should be equivalent to True, used by getdebug()

browsepy.compat.FileNotFoundError = OSError if PY_LEGACY else FileNotFoundError

Convenience python exception type reference.

browsepy.compat.range = xrange if PY_LEGACY else range

Convenience python builtin function reference.

browsepy.compat.filter = itertools.ifilter if PY_LEGACY else filter

Convenience python builtin function reference.

browsepy.compat.basestring = basestring if PY_LEGACY else str

Convenience python type reference.

browsepy.compat.unicode = unicode if PY_LEGACY else str

Convenience python type reference.

browsepy.compat.scandir = scandir.scandir or os.walk

New scandir, either from scandir module or Python3.6+ os module.

browsepy.compat.walk = scandir.walk or os.walk[source]

New walk, either from scandir module or Python3.6+ os module.

browsepy.compat.pathconf(path)[source]

Get all pathconf variables for given path.

Parameters:path (str) – absolute fs path
Returns:dictionary containing pathconf keys and their values (both str)
Return type:dict
browsepy.compat.isexec(path)[source]

Check if given path points to an executable file.

Parameters:path (str) – file path
Returns:True if executable, False otherwise
Return type:bool
browsepy.compat.which(name, env_path=ENV_PATH, is_executable_fnc=isexec, path_join_fnc=os.path.join)[source]

Get command absolute path.

Parameters:
  • name (str) – name of executable command
  • env_path (list of str) – OS environment executable paths, defaults to autodetected
  • is_executable_fnc (Callable) – callable will be used to detect if path is executable, defaults to isexec
  • path_join_fnc (Callable) – callable will be used to join path components
  • os_name (str) – os name, defaults to os.name
Returns:

absolute path

Return type:

str or None

browsepy.compat.getdebug(environ=os.environ, true_values=TRUE_VALUES)[source]

Get if app is expected to be ran in debug mode looking at environment variables.

Parameters:environ (collections.abc.Mapping) – environment dict-like object
Returns:True if debug contains a true-like string, False otherwise
Return type:bool
browsepy.compat.deprecated(func_or_text, environ=os.environ)[source]

Decorator used to mark functions as deprecated. It will result in a warning being emmitted hen the function is called.

Usage:

>>> @deprecated
... def fnc():
...     pass

Usage (custom message):

>>> @deprecated('This is deprecated')
... def fnc():
...     pass
Parameters:
Returns:

nested decorator or new decorated function (depending on params)

Return type:

callable

browsepy.compat.usedoc(other)[source]

Decorator which copies __doc__ of given object into decorated one.

Usage:

>>> def fnc1():
...     """docstring"""
...     pass
>>> @usedoc(fnc1)
... def fnc2():
...     pass
>>> fnc2.__doc__
'docstring'collections.abc.D
Parameters:other (any) – anything with a __doc__ attribute
Returns:decorator function
Return type:callable
browsepy.compat.fsdecode(path, os_name=os.name, fs_encoding=FS_ENCODING, errors=None)[source]

Decode given path.

Parameters:
  • path (bytes or str) – path will be decoded if using bytes
  • os_name (str) – operative system name, defaults to os.name
  • fs_encoding (str) – current filesystem encoding, defaults to autodetected
Returns:

decoded path

Return type:

str

browsepy.compat.fsencode(path, os_name=os.name, fs_encoding=FS_ENCODING, errors=None)[source]

Encode given path.

Parameters:
  • path (bytes or str) – path will be encoded if not using bytes
  • os_name (str) – operative system name, defaults to os.name
  • fs_encoding (str) – current filesystem encoding, defaults to autodetected
Returns:

encoded path

Return type:

bytes

browsepy.compat.getcwd(fs_encoding=FS_ENCODING, cwd_fnc=os.getcwd)[source]

Get current work directory’s absolute path. Like os.getcwd but garanteed to return an unicode-str object.

Parameters:
  • fs_encoding (str) – filesystem encoding, defaults to autodetected
  • cwd_fnc (Callable) – callable used to get the path, defaults to os.getcwd
Returns:

path

Return type:

str

browsepy.compat.re_escape(pattern, chars="()[]{}?*+|^$\.-#")[source]

Escape all special regex characters in pattern. Logic taken from regex module.

Parameters:pattern – regex pattern to escape
Returns:escaped pattern
Return type:str
browsepy.compat.pathsplit(value, sep=os.pathsep)[source]

Get enviroment PATH elements as list.

This function only cares about spliting across OSes.

Parameters:
  • value (str) – path string, as given by os.environ[‘PATH’]
  • sep (str) – PATH separator, defaults to os.pathsep
Yields:

every path

Ytype:

str

browsepy.compat.pathparse(value, sep=os.pathsep, os_sep=os.sep)[source]

Get enviroment PATH directories as list.

This function cares about spliting, escapes and normalization of paths across OSes.

Parameters:
  • value (str) – path string, as given by os.environ[‘PATH’]
  • sep (str) – PATH separator, defaults to os.pathsep
  • os_sep (str) – OS filesystem path separator, defaults to os.sep
Yields:

every path

Ytype:

str