1.1.3.2. gnr.devel.utils – Tools to build command-line tools

Last page update: October 10, 2011

Available functionality:

  • ProgressBar, a class to draw text-based progressbars.
  • AutoDiscovery, a class to auto-detect current instance, site, package and project from GENROPY_xxx environment variables and/or the current working directory.

1.1.3.2.1. Autodiscovery

It is a tool to gather information about projects, instances, sites, packages and commands available in a given GenroPy installation.

It tries to guess the current project, instance, site and package based on the current working directory.

class gnr.devel.utils.AutoDiscovery(config_file='~/.gnr/environment.xml')

Try to guess the current project, package, site and instance.

Environment variables:

GENROPY_PROJECT

The name of the current project.

GENROPY_INSTANCE

The name of the current instance.

GENROPY_SITE

The name of the current site.

GENROPY_PACKAGE

The name of the current package.

We apply these rules:

  1. look in the environment for a GENROPY_PROJECT, GENROPY_SITE, GENROPY_INSTANCE and GENROPY_PACKAGE variables. If anyone is present, we cross-check if they point to the right places. For doing this, we look to the declared places in the configuration (.gnr/environment.xml) and complain if they don’t.
  2. if GENROPY_PROJECT is missing, we look if the current path is inside one of the declared projects in the configuration. If we find one, we assume that it is the current project. If no, we don’t have a current project.
  3. if GENROPY_INSTANCE is missing, we look if the current path is inside one of the instances of the current project or if we are inside one of the declared instances in the configuration. If yes, we assume that is the current instance. If no, we don’t have a current instance.
  4. if GENROPY_SITE is missing... (same as the previous steps)
  5. if GENROPY_PACKAGE is missing... (same as the previous steps)
  6. If we have a project, but we don’t have an instance/site/package -and- the project has only one instance/site/package, then we assume that’s the current one.

Attributes:

current_project

the current project, if found

current_package

the current package, if found

current_instance

the current instance, if found

current_site

the current site, if found

project_packages

all packages in the current project, if a project has been found

project_instances

all instances in the current project, if a project has been found

project_sites

all sites in the current project, if a project has been found

project_commands

all commands in the current project, if a project has been found

all_projects

all projects in this GenroPy installation

all_instances

all instances in this GenroPy installation

all_packages

all packages in this GenroPy installation

all_sites

all sites in this GenroPy installation

all_commands

all commands in this GenroPy installation

config_file

path to the configuration file (e.g. ~/gnr/environment.xml)

class Item(path)

AutoDiscovery‘s attributes contain instances of this class and its subclasses.

They have the following public attributes:

name

the name of this item

path

the absolute path to this item

class AutoDiscovery.Project(path)

Project have these additional methods:

commands()

all commands in this project

instances()

all instances in this project

packages()

all packages in this project

sites()

all sites in this project

AutoDiscovery.report(all=False)

Print a summary of what AutoDiscovery class found

Parameters:all – if False, print only the current project, instance, packages and site. if True, print a full report including all available items in this GenroPy installation

1.1.3.2.2. Utilities

gnr.devel.utils.expandpath(path, full=False)

Expand user home directory (~) and environment variables. Return the expanded path

Parameters:
  • path – the path to expand
  • full – boolean. If True, returns a normalized path (see os.path.normpath)
class gnr.devel.utils.ProgressBar(label, label_width=20, bar_width=40, min_value=0, max_value=100, fd=<open file '<stdout>', mode 'w' at 0x401ed070>, progress_label='')

Provides a text-based progress bar.

Example:

import time
with ProgressBar('ProgressBar test') as pg:
    for n in xrange(333):
        time.sleep(0.01)
        pg.update(n/3.33)
        if n > 233:
            raise Exception, "Something bad happened."
update(value, progress_value=None)

Draws the progress bar.

Here’s how it looks:

Label padded to length     [**********----------------------]  45.12%        (3m 28s passed, 4m 54s remaining)

Table Of Contents

Previous topic

1.1.3.1. gnr.devel.commands

Next topic

1.1.4. gnr.pdf (deprecated)

This Page