dotfiles.venv package¶
Objectives¶
dotfiles.venv.ipython_config (ipython_config.py):
- Set variables for standard Virtualenv paths in the environment dict
- Define IPython aliases
- Serialize variables and aliases to:
dotfiles.venv.ipython_magics (ipython_magics.py):
- Configure IPython %magic commands
- cd* – change directories (%cdhelp, %cdp, %cdwh, %cdv, %cds, %cdw)
- ds – print dotfiles_status
Configuration¶
Shell¶
For Bash/ZSH, etc/bash/10-bashrc.venv.sh sets:
# ...
_VENV="${__DOTFILES}/etc/ipython/ipython_config.py"
venv() {
$_VENV $@
}
# ...
etc/bash/10-bashrc.venv.sh is sourced by etc/bash/00-bashrc.before.sh, which is sourced by ~/.bashrc (a symlink to ${__DOTFILES}/etc/bashrc created by bootstrap_dotfiles.sh -S).
IPython¶
To configure IPython with venv, ipython_config.py must be symlinked into ~/.ipython/profile_default (and, optionally, ipython_magics.py into ~/.ipython/profile_default/startup/):
# symlink paths relative to ${__DOTFILES}
__DOTFILES="~/.dotfiles"
# working directory (path to the dotfiles repository)
_WRD=${WORKON_HOME}/dotfiles/src/dotfiles
# created by ``bootstrap_dotfiles.sh -S``
# ln -s ${_WRD} ${__DOTFILES}
# ln -s ${__DOTFILES}/etc/bashrc ~/.bashrc
# ln -s ${_WRD}/etc/ipython/ipython_config.py \
# ${__DOTFILES}/etc/ipython/ipython_config.py
# MANUALLY INSTALL for each IPython profile
IPY_PROFILE="profile_default"
ln -s ${__DOTFILES}/etc/ipython/ipython_config.py \
~/.ipython/${IPY_PROFILE}/ipython_config.py
ln -s ${__DOTFILES}/etc/ipython/ipython_magics.py \
~/.ipython/${IPY_PROFILE}/ipython_magics.py
Submodules¶
dotfiles.venv.ipython_config module¶
- class dotfiles.venv.ipython_config.Env(*args, **kwds)[source]¶
Bases: collections.OrderedDict
OrderedDict of variables for/from os.environ.
- classmethod from_environ(environ)[source]¶
Build an Env from a dict (e.g. os.environ)
Parameters: environ (dict) – a dict with variable name keys and values Returns: an Env environment built from the given environ dict Return type: Env
- osenviron_keys = ('__DOCSWWW', '__DOTFILES', 'PAGER', 'PROJECTS', 'USRLOG', 'VIMBIN', 'GVIMBIN', 'MVIMBIN', 'GUIVIMBIN', 'VIMCONF', 'EDITOR_', 'VIRTUAL_ENV', 'VIRTUAL_ENV_NAME', 'WORKON_HOME', 'WORKSPACE', '_APP', '_BIN', '_CFG', '_ETC', '_LIB', '_LOG', '_MNT', '_OPT', '_PYLIB', '_PYSITE', '_SRC', '_SRV', '_VAR', '_WRD', '_WRD_SETUPY', '_WWW', '__SRC', '_src')¶
- paths_to_variables(path_)[source]¶
Replace components of a path string with variables configured in this Env.
Parameters: path (str) – a path string Returns: path string containing ${VARNAME} variables Return type: str
- class dotfiles.venv.ipython_config.IPYMock[source]¶
Bases: object
Provide a few mocked methods for testing
- class dotfiles.venv.ipython_config.Test_Env(methodName='runTest')[source]¶
Bases: unittest.case.TestCase
- class dotfiles.venv.ipython_config.Test_venv(methodName='runTest')[source]¶
Bases: unittest.case.TestCase
- class dotfiles.venv.ipython_config.Venv(virtualenv=None, appname=None, env=None, from_environ=False, open_editors=False, open_terminals=False, dont_reflect=True)[source]¶
Bases: object
A virtual environment configuration generator
- bash_env(output=<open file '<stdout>', mode 'w' at 0x7fe98d19a150>)[source]¶
Generate a source-able script for the environment variables, aliases, and functions defined by the current Venv.
Parameters: output (file-like) – object to print to (print calls .write() and then .flush())
- configure_ipython(*args, **kwargs)[source]¶
Configure IPython with Venv._configure_ipython and user_aliases from self.aliases.items().
Parameters: - args (list) – args for Venv._configure_ipython
- kwargs (dict) – kwargs for Venv._configure_ipython.
- get_user_aliases(dont_reflect=False)[source]¶
Configure env variables and return an OrderedDict of aliases
Parameters: dont_reflect (bool) – Whether to always create aliases and functions referencing $_WRD even if $_WRD doesn’t exist. (default: False) Returns: dict of aliases Return type: OrderedDict
- get_user_aliases_base()[source]¶
Returns: dict of cd command aliases Return type: OrderedDict Note
These do not work in IPython as they run in a subshell. See: dotfiles.venv.ipython_magics.
- static get_virtualenv_path(virtualenv=None, from_environ=False)[source]¶
Get the path to a virtualenv
Parameters: - virtualenv (str) – a path to a virtualenv containing / OR just the name of a virtualenv in $WORKON_HOME
- from_environ (bool) – whether to try and read from os.environ["VIRTUAL_ENV"]
Returns: a path to a virtualenv (for $VIRTUAL_ENV)
Return type: str
- system(cmd=None)[source]¶
Call os.system with the given command string
Parameters: cmd (string) – command string to call os.system with
Raises: - Exception – if cmd is None
- NotImplementedError – if cmd is a tuple
- dotfiles.venv.ipython_config.get_venv_parser()[source]¶
Returns: optparse.OptionParser: options for the commandline interface
- dotfiles.venv.ipython_config.get_workon_home_default()[source]¶
Returns: Best path for a virtualenvwrapper $WORKON_HOME Return type: str
- dotfiles.venv.ipython_config.in_ipython_config()[source]¶
Returns: True if get_ipython is in globals() Return type: bool
- dotfiles.venv.ipython_config.ipython_imports()[source]¶
Default imports for IPython (currently unused)
- dotfiles.venv.ipython_config.ipython_main()[source]¶
Function to call if running within IPython, as determined by in_ipython_config.
- dotfiles.venv.ipython_config.main()[source]¶
Commandline main function called if __name__=="__main__"
Returns: nonzero on error Return type: int
- dotfiles.venv.ipython_config.shell_quote(var)[source]¶
Escape single quotes and add double quotes around a given variable.
Parameters: _str (str) – string to add quotes to Returns: string wrapped in quotes Return type: str Warning
This is not safe for untrusted input and only valid in this context (os.environ).
dotfiles.venv.ipython_magics module¶
IPython %magic commands
- cd aliases
- ds (dotfiles_status)
- dr (dotfiles_reload)
Installation¶
__DOTFILES="~/.dotfiles"
ipython_profile="profile_default"
ln -s ${__DOTFILES}/etc/ipython/ipython_magics.py \
~/.ipython/${ipython_profile}/startup/ipython_magics.py
- dotfiles.venv.ipython_magics.magics_class(cls, *args, **kwargs)¶
- dotfiles.venv.ipython_magics.line_magic(func, *args, **kwargs)¶