Skip to content
This repository has been archived by the owner on Mar 29, 2024. It is now read-only.

Commit

Permalink
improved imports with demos converted to use them
Browse files Browse the repository at this point in the history
  • Loading branch information
twobraids committed Dec 29, 2011
1 parent 4474113 commit 791f3be
Show file tree
Hide file tree
Showing 10 changed files with 99 additions and 94 deletions.
6 changes: 4 additions & 2 deletions configman/__init__.py
Expand Up @@ -36,7 +36,7 @@
#
# ***** END LICENSE BLOCK *****

__version__ = '1.0'
__version__ = '1.0.1'

# Having these here makes it possible to easily import once configman is
# installed.
Expand All @@ -45,9 +45,11 @@
# from configman import Namespace, ConfigurationManager
#

from .config_manager import ConfigurationManager
from .config_manager import ConfigurationManager, RequiredConfig
from .namespace import Namespace

from .converters import class_converter, regex_converter, timedelta_converter


# constants used to refer to Value Source concepts generically
from config_file_future_proxy import ConfigFileFutureProxy
Expand Down
55 changes: 30 additions & 25 deletions configman/converters.py
Expand Up @@ -42,7 +42,10 @@
import types
import inspect

import datetime_util as dtu
from .datetime_util import datetime_from_ISO_string as datetime_converter
from .datetime_util import date_from_ISO_string as date_converter

import datetime_util


#------------------------------------------------------------------------------
Expand Down Expand Up @@ -187,18 +190,19 @@ def regex_converter(input_str):
compiled_regexp_type = type(re.compile(r'x'))

#------------------------------------------------------------------------------
from_string_converters = {int: int,
float: float,
str: str,
unicode: unicode,
bool: boolean_converter,
datetime.datetime: dtu.datetime_from_ISO_string,
datetime.date: dtu.date_from_ISO_string,
datetime.timedelta: timedelta_converter,
type: class_converter,
types.FunctionType: class_converter,
compiled_regexp_type: regex_converter,
}
from_string_converters = {
int: int,
float: float,
str: str,
unicode: unicode,
bool: boolean_converter,
datetime.datetime: datetime_converter,
datetime.date: date_converter,
datetime.timedelta: timedelta_converter,
type: class_converter,
types.FunctionType: class_converter,
compiled_regexp_type: regex_converter,
}


#------------------------------------------------------------------------------
Expand All @@ -215,18 +219,19 @@ def py_obj_to_str(a_thing):


#------------------------------------------------------------------------------
to_string_converters = {int: str,
float: str,
str: str,
unicode: unicode,
bool: lambda x: 'True' if x else 'False',
datetime.datetime: dtu.datetime_to_ISO_string,
datetime.date: dtu.date_to_ISO_string,
datetime.timedelta: dtu.timedelta_to_str,
type: py_obj_to_str,
types.FunctionType: py_obj_to_str,
compiled_regexp_type: lambda x: x.pattern,
}
to_string_converters = {
int: str,
float: str,
str: str,
unicode: unicode,
bool: lambda x: 'True' if x else 'False',
datetime.datetime: datetime_util.datetime_to_ISO_string,
datetime.date: datetime_util.date_to_ISO_string,
datetime.timedelta: datetime_util.timedelta_to_str,
type: py_obj_to_str,
types.FunctionType: py_obj_to_str,
compiled_regexp_type: lambda x: x.pattern,
}


#------------------------------------------------------------------------------
Expand Down
16 changes: 7 additions & 9 deletions demo/advanced_demo2.py
Expand Up @@ -48,9 +48,7 @@
import contextlib
import threading

import configman as cm
import configman.config_manager as config_man

from configman import RequiredConfig, ConfigurationManager, Namespace

#------------------------------------------------------------------------------
class FakeDatabaseConnection():
Expand Down Expand Up @@ -84,13 +82,13 @@ def rollback(self):


#==============================================================================
class PGTransaction(config_man.RequiredConfig):
class PGTransaction(RequiredConfig):
"""a configman complient class for setup of a Postgres transaction"""
#--------------------------------------------------------------------------
# configman parameter definition section
# here we're setting up the minimal parameters required for connecting
# to a database.
required_config = cm.Namespace()
required_config = Namespace()
required_config.add_option(
name='database_host',
default='localhost',
Expand Down Expand Up @@ -235,7 +233,7 @@ def transaction_factory(config, local_config, args):

if __name__ == "__main__":

definition_source = cm.Namespace()
definition_source = Namespace()
# setup the option that will specify which database connection/transaction
# factory will be used. Condfig man will query the class for additional
# config options for the database connection parameters.
Expand All @@ -252,9 +250,9 @@ def transaction_factory(config, local_config, args):
function=transaction_factory
)

c = cm.ConfigurationManager(definition_source,
app_name='demo4',
app_description=__doc__)
c = ConfigurationManager(definition_source,
app_name='demo4',
app_description=__doc__)
with c.context() as config:
print "\n**** First query will succeed"
with config.db_transaction() as trans:
Expand Down
21 changes: 10 additions & 11 deletions demo/advanced_demo3.py
Expand Up @@ -81,8 +81,7 @@
import time
import socket

import configman as cm
import configman.config_manager as config_man
from configman import RequiredConfig, ConfigurationManager, Namespace


#------------------------------------------------------------------------------
Expand Down Expand Up @@ -140,13 +139,13 @@ def rollback(self):


#==============================================================================
class Postgres(config_man.RequiredConfig):
class Postgres(RequiredConfig):
"""a configman compliant class for setup of Postgres transactions"""
#--------------------------------------------------------------------------
# configman parameter definition section
# here we're setting up the minimal parameters required for connecting
# to a database.
required_config = cm.Namespace()
required_config = Namespace()
required_config.add_option(
name='database_host',
default='localhost',
Expand Down Expand Up @@ -333,8 +332,8 @@ def transaction_factory(config, local_config, args):


#==============================================================================
class TransactionExecutor(config_man.RequiredConfig):
required_config = cm.Namespace()
class TransactionExecutor(RequiredConfig):
required_config = Namespace()
# setup the option that will specify which database connection/transaction
# factory will be used. Config man will query the class for additional
# config options for the database connection parameters.
Expand Down Expand Up @@ -363,7 +362,7 @@ def do_transaction(self, function, *args, **kwargs):
#==============================================================================
class TransactionExecutorWithBackoff(TransactionExecutor):
# back off times
required_config = cm.Namespace()
required_config = Namespace()
required_config.add_option('backoff_delays',
default=[2, 4, 6, 10, 15],
doc='delays in seconds between retries',
Expand Down Expand Up @@ -427,14 +426,14 @@ def query2(conn):

#==============================================================================
if __name__ == "__main__":
definition_source = cm.Namespace()
definition_source = Namespace()
definition_source.add_option('transaction_executor_class',
default=TransactionExecutorWithBackoff,
doc='a class that will execute transactions')

c = cm.ConfigurationManager(definition_source,
app_name='advanced_demo_3',
app_description=__doc__)
c = ConfigurationManager(definition_source,
app_name='advanced_demo_3',
app_description=__doc__)

with c.context() as config:
# the configuration has a class that can execute transactions
Expand Down
10 changes: 5 additions & 5 deletions demo/demo1.py
Expand Up @@ -45,7 +45,7 @@
# we run the application.

import sys
import configman as cm
from configman import ConfigurationManager, Namespace


# the following three functions are the business logic of the application.
Expand All @@ -63,7 +63,7 @@ def upper(x):
# create the definitions for the parameters that are to come from
# the command line or config file. First we create a container called a
# namespace for the configuration parameters.
definition_source = cm.Namespace()
definition_source = Namespace()
# now we start adding options to the container. This first option
# defines on the command line '--text' and '-t' swiches. For configuration
# files, this defines a top level entry of 'text' and assigns the value
Expand All @@ -87,9 +87,9 @@ def upper(x):
# any values loaded from a config file specified by the --admin.conf command
# line switch, values from the os environment and finally overrides from the
# commandline.
c = cm.ConfigurationManager(definition_source,
app_name='demo1',
app_description=__doc__)
c = ConfigurationManager(definition_source,
app_name='demo1',
app_description=__doc__)

# fetch the DOM-like instance that gives access to the configuration info
config = c.get_config()
Expand Down
8 changes: 4 additions & 4 deletions demo/demo1j.py
Expand Up @@ -46,7 +46,7 @@
# we run the application.

import sys
import configman as cm
from configman import ConfigurationManager


# the following three functions are the business logic of the application.
Expand Down Expand Up @@ -74,9 +74,9 @@ def upper(x):
# any values loaded from a config file specified by the --admin.conf command
# line switch, values from the os environment and finally overrides from the
# commandline.
c = cm.ConfigurationManager(definition_source,
app_name='demo1j',
app_description=__doc__)
c = ConfigurationManager(definition_source,
app_name='demo1j',
app_description=__doc__)

# fetch the DOM-like instance that gives access to the configuration info
config = c.get_config()
Expand Down
19 changes: 10 additions & 9 deletions demo/demo2.py
Expand Up @@ -47,8 +47,9 @@
# application is specified in configuration. The last line of the file invokes
# the action.

import configman as cm
import configman.converters as conv
from configman import ConfigurationManager, Namespace
from configman import environment, command_line
from configman.converters import class_converter


# the following three functions are the business logic of the application.
Expand All @@ -74,7 +75,7 @@ def action_converter(action):
return action_dispatch[action]
except KeyError:
try:
f = conv.class_converter(action)
f = class_converter(action)
except Exception:
raise Exception("'%s' is not a valid action" % action)
if f in action_dispatch.values():
Expand All @@ -83,7 +84,7 @@ def action_converter(action):

# create the definitions for the parameters that are to come from
# the command line or config file.
definition_source = cm.Namespace()
definition_source = Namespace()
definition_source.add_option('text',
'Socorro Forever',
'the text input value',
Expand All @@ -108,17 +109,17 @@ def action_converter(action):
# config file of their own. After reading the hard coded config file, the
# ConfigurationManager will apply values it got from the environment and then,
# finally, apply values that it gets from the command line.
value_sources = ('demo2.ini', cm.environment, cm.command_line)
value_sources = ('demo2.ini', environment, command_line)
# the value_sources sequence can contian any object that is a derivation of the
# type collections.Mapping, a module, or instances of any of the registered
# handlers. cm.environment is just an alias for os.environ. cm.command_line is
# an alias for the 'getopt' module, a registerd handler.

# set up the manager with the definitions and values
c = cm.ConfigurationManager(definition_source,
value_sources,
app_name='demo2',
app_description=__doc__)
c = ConfigurationManager(definition_source,
value_sources,
app_name='demo2',
app_description=__doc__)

# fetch the DotDict version of the values
config = c.get_config()
Expand Down
7 changes: 3 additions & 4 deletions demo/demo3.py
Expand Up @@ -56,20 +56,19 @@
# 5) the app class defines a parameterless 'main' function that executes the
# business logic of the application

import configman as cm
import configman.config_manager as config_man
from configman import RequiredConfig, Namespace


# the following class embodies the business logic of the application.
class Demo3App(config_man.RequiredConfig):
class Demo3App(RequiredConfig):

app_name = 'demo3_app'
app_version = '0.1'
app_description = __doc__

# create the definitions for the parameters that are to come from
# the command line or config file.
required_config = cm.Namespace()
required_config = Namespace()
required_config.add_option('text', 'Socorro Forever', 'the input value',
short_form='t')

Expand Down
17 changes: 8 additions & 9 deletions demo/dyn_app.py
Expand Up @@ -46,37 +46,36 @@
# another. It offers fake versions of Postgres, MySQL and HBase as the
# data sources and sinks.

import configman as cm
import configman.config_manager as config_man
import configman.converters as conv
from configman import RequiredConfig, Namespace
from configman.converters import class_converter


# the following class embodies the business logic of the application.
class DynApp(config_man.RequiredConfig):
class DynApp(RequiredConfig):

app_name = 'dyn'
app_version = '0.1'
app_description = __doc__

# create the definitions for the parameters that are to come from
# the command line or config file.
required_config = cm.Namespace()
required_config = Namespace()
# we're going to have two namespaces, one for the source and another
# for the destination. We use separate namespaces to avoid name
# collisions. For example, both the source and destination are going
# to have 'hostname' and we don't want to mix them up.
required_config.source = s = cm.Namespace()
required_config.destination = d = cm.Namespace()
required_config.source = s = Namespace()
required_config.destination = d = Namespace()
# when the data source class is loaded, it will bring in more
# configuration parameters gleaned from the loaded class itself.
s.add_option('storage', 'data_store.CannedDataSource',
'the class to handle database interaction for input',
short_form='s',
from_string_converter=conv.class_converter)
from_string_converter=class_converter)
d.add_option('storage', 'data_store.CannedDataSink',
'the class to handle database interaction for output',
short_form='d',
from_string_converter=conv.class_converter)
from_string_converter=class_converter)

def __init__(self, config):
super(DynApp, self).__init__()
Expand Down

0 comments on commit 791f3be

Please sign in to comment.