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:
    • IPython configuration (variables, aliases)
    • Bash/ZSH configuration (variables, aliases, functions):
      • venv -E --bash
      • venv dotfiles --bash
    • JSON (variables, aliases) [--print]
      • venv -E --json
      • venv dotfiles json

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
set_standard_paths(env, prefix)[source]

Set variables for standard paths in the environment

Parameters:prefix (str) – a path prefix (e.g. $VIRTUAL_ENV or $PREFIX)

see:

class dotfiles.venv.ipython_config.IPYMock[source]

Bases: object

Provide a few mocked methods for testing

magic(*args, **kwargs)[source]
system(*args, **kwargs)[source]
class dotfiles.venv.ipython_config.Test_Env(methodName='runTest')[source]

Bases: unittest.case.TestCase

test_Env()[source]
test_Env_from_environ()[source]
class dotfiles.venv.ipython_config.Test_venv(methodName='runTest')[source]

Bases: unittest.case.TestCase

setUp()[source]
test_venv()[source]
test_venv_appname()[source]
test_venv_from_null_environ()[source]
test_venv_with_environ()[source]
test_venv_without_environ()[source]
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

asdict()[source]
Returns:OrderedDict(env=self.env, aliases=self.aliases)
Return type:OrderedDict
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.
configure_sys()[source]
Returns:sys.path list from _configure_sys.
Return type:list
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

open_editors()[source]

Run self._edit_project_cmd

open_terminals()[source]

Run self._open_terminals_cmd

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
to_json(indent=None)[source]
Parameters:indent (int) – number of spaces with which to indent JSON output
Returns:json.dumps(self.asdict())
Return type:str
classmethod workon_project(virtualenv, **kwargs)[source]
Parameters:
  • virtualenv (str) – a path to a virtualenv containing / OR just the name of a virtualenv in $WORKON_HOME
  • kwargs (dict) – kwargs to pass to Venv (see Venv.__init__)
Returns:

an intialized Venv

Return type:

Venv

dotfiles.venv.ipython_config.get_DEFAULT_ALIASES(platform=None)[source]
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_config.shell_varquote(str_)[source]

Add doublequotes and shell variable brackets to a string

Parameters:str (str) – string to varquote (e.g. VIRTUAL_ENV)
Returns:“${VIRTUAL_ENV}”
Return type:str

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)
class dotfiles.venv.ipython_magics.VenvMagics[source]

Bases: object

cd(envvar, line)[source]

Change directory

Parameters:
  • envvar (str) – os.environ variable name
  • line (str) – path to append to envvar
cdb(line)[source]

cdb() – cd ${_BIN}/${@}

cde(line)[source]

cde() – cd ${_ETC}/${@}

cdh(line)[source]

cdh() – cd ${HOME}/${@}

cdl(line)[source]

cdl() – cd ${_LIB}/${@}

cdlog(line)[source]

cdlog() – cd ${_LOG}/${@}

cdp(line)[source]

cdp() – cd ${PROJECT_HOME}/${@}

cdph(line)[source]

cdph() – cd ${PROJECT_HOME}/${@}

cdpylib(line)[source]

cdpylib() – cd ${PYLIB)/${@}

cdpysite(line)[source]

cdpysite() – cd ${PYSITE}/${@}

cdsitepackages(line)[source]

cdsitepackages() – cd ${_PYSITE}/${@}

cds(line)[source]

cds() – cd ${_SRC}/${@}

cdv(line)[source]

cdv() – cd ${VIRTUAL_ENV}/${@}

cdve(line)[source]

cdve() – cd ${VIRTUAL_ENV}/${@}

cdvirtualenv(line)[source]

cdvirtualenv() – cd ${VIRTUAL_ENV}/${@}

cdvar(line)[source]

cdvar() – cd ${_VAR}/${@}

cdw(line)[source]

cdw() – cd ${_WRD}/${@}

cdwrd(line)[source]

cdwrd() – cd ${_WRD}/${@}

cdwrk(line)[source]

cdwrk() – cd ${WORKON_HOME}/${@}

cdwh(line)[source]

cdwh() – cd ${WORKON_HOME}/${@}

cdww(line)[source]

cdww() – cd ${_WWW}/${@}

cdwww(line)[source]

cdwww() – cd ${_WWW}/${@}

cdhelp(line)[source]

cdhelp() – list cd commands

dotfiles_status(line)[source]

dotfiles_status() – print dotfiles_status() .

ds(line)[source]

ds() – print dotfiles_status() .

dotfiles_reload(line)[source]

dotfiles_reload() – print NotImplemented

dr(line)[source]

dr() – print NotImplemented [dotfiles_reload()]

dotfiles.venv.ipython_magics.main()[source]

Register VenvMagics with IPython