Sphinx logo

config – configuration and setup

The config module contains functions to manage and access the persistent astropysics configuration. It also includes utilities to install recommended packages and set up the ipython environment.

Configuration files can be found in the directory returned by get_config_dir(), typically a subdirectory ‘.astropysics’ of the user’s home directory. The format for the files is that of the ` configobj<http://www.voidspace.org.uk/python/configobj.html>`_ package, although for most files this is as simple as:

name1=value
#maybe a comment
name2 = anothervalue

Classes and Inheritance Structure

Inheritance diagram of astropysics.config

Module API

exception astropysics.config.DownloadError

Bases: exceptions.Exception

exception astropysics.config.InstallError

Bases: exceptions.Exception

class astropysics.config.PackageInstaller(name, importmod=None, version=None, buildargs='', instargs='', extrainfo=None, verbose=True)

Bases: HTMLParser.HTMLParser

Represents a python package to be downloaded and installed.

Parameters:
  • name – The name of the package.
  • importmod – The module name to import to test if the pacakge is installed. If None, will be assumed to match name
  • version (str) – A version request for finding the package on PyPI, such as ‘0.2’,’>0.1’ (greater than 0.1), or ‘<0.3’. Can also be None to get the most recent version. If the PyPI entry only has a download link, this is ignored.
  • buildargs – Arguments to be given to the “python setup.py build [args]” stage. Can be either a string or a sequence of strings.
  • instargs – Arguments to be given to the “python setup.py install [args]” stage. Can be either a string or a sequence of strings.
  • extrainfo (str) – A string with additional information about the pacakge (to be shown if the user requests it in the install tool). Can be None to indicate no extra info.
  • verbose (bool) – If True, information will be printed to standard out about steps in the install process.

Subclassing

If a package needs some additional install steps, the postInstall() and preInstall() methods can be overridden (default does nothing). If the package isn’t in PyPI, the getURL() method should be overridden to return the necessary URL.

download(dldir=None, overwrite=False)

Attempt to download this package

Parameters:
  • dldir (str) – The directory to download to. If None, the standard configuration directory will be used.
  • overwrite (bool) – If True, downloaded package archives will be overwritten instead of being re-used.
Returns:

The full path to the downloaded file.

Raises DownloadError:
 

If the pacakge could not be located

getUrl()

Returns the URL to download to get this package. Override this to get a URL from somewhere other than PyPI.

Returns:(url,fn) where url is the URL to the source distribution, and fn is the filename to use locally if None, the end of the URL will be used)
handle_starttag(tag, attrs)
install(dldir=None, overwrite=False, instprefix='')

Download the package, if necessary, and install it.

Parameters:
  • dldir (str) – The directory to download to. If None, the standard configuration directory will be used.
  • overwrite (bool) – If True, downloaded package archives will be overwritten instead of being re-used.
  • instprefix (str) – A command line prefix (before “python”) to be used in the install step. Most often this is ‘sudo’ on certain oses.
Raises InstallError:
 

If the install fails (postInstall() will be called immediately before).

isInstalled()

Test if the package is installed.

Returns:True if the module is installed, otherwise False.
postInstall(idir, success)

Subclasses can override this method to do something after building and installing occurs. Only called if install succeeds.

Parameters:
  • idir (str) – The path to the directory in which the package is built.
  • success (bool) – If True, the install was sucessful. Otherwise, it failed.
preInstall(idir)

Subclasses can override this method to do something before building and installing occurs.

Parameters:idir (str) – The path to the directory in which the package is built.
exception astropysics.config.VersionError

Bases: exceptions.Exception

astropysics.config.add_project(name, dir=None, scriptfile=None)

Add a new project to the project registry.

Parameters:
  • name (str) – The name of the project.
  • dir (str) – The name of the directory associated with the project or None to use name, relative to the current directory. If the directory dos not exist, it will be created.
  • scriptfile (str) – The name of the file with the main runnable python code for the project, relative to the dir directory, or None to use the name. If the script does not exist, it will be created with an analysis template file (see project_template.py).
Returns:

A 2-tuple (projectdir,projectscriptfilename)

Raises IOError:

If something is wrong with the file or directory.

astropysics.config.del_project(name)

Unregisters the project with the specified name.

astropysics.config.get_config(name)

Returns a dictionary-like object that has the configuration information for the specified configuration file. To save the configuration data if it is modified, call write() on the object.

Parameters:

name (str) – The name of the configuration file (without any path). The file will be searched for in/written to the config directory.

Returns:

A ConfigObj object with the configuration information.

Raises:
  • ValueError – If the name is invalid.
  • astropysics.external.configobj.ConfigObjError – If the file exists and it is an invalid format.
astropysics.config.get_config_dir(create=True)

Returns the astropysics configuration directory name.

Parameters:create (bool) – If True, the directory will be created if it doesn’t exist.
Returns:The absolute path to the config directory as a string
astropysics.config.get_data_dir(create=True)

Returns the directory name for data that astropysics downloads. See :func`astropysics.utils.io.get_data` to work with data in this directory.

Parameters:create (bool) – If True, the directory will be created if it doesn’t exist.
Returns:The absolute path to the data directory as a string.
astropysics.config.get_projects()

Returns all registered projects and their assoiciated directories and script files.

Returns:A dictionary where the keys are the project names and the values are 2-tuples (projectdir,projectscriptfilename)
astropysics.config.run_install_tool(sudo='auto')

Starts the console-based interactive installation tool.

Parameters:sudo – Determines whether or not the install step is prefixed by ‘sudo’. If True, sudo will be prefixed, if False, no prefix will be used. If ‘auto’, the platform will be examined to try to determine if sudo is necessary. If ‘toggleauto’, the opposite of whatever ‘auto’ gives will be used.

Note

The ‘auto’ option for sudo is nowhere close to perfect - it’s pretty much just a list of common platforms that require it... if you’re on an uncommon platform, you will probably have to set it manually.

astropysics.config.run_ipython_setup()

Runs the console-based ipython setup tool.

Table Of Contents

Previous topic

utils – utility classes and functions

Next topic

GUI applications

This Page