Exemplo n.º 1
0
 def __init__(self, connection, container, cache=None):
     self.__dict__["_connection"] = connection
     self.__dict__["_container"] = container
     self.__dict__["_cache"] = cache or AttrDict(
     )  # local, non-persistent cache for computed and read-in values
     # cache-able objects
     self._cache._report = None
     self._cache._logfile = None
     self._cache._configurator = None
Exemplo n.º 2
0
def get_mock_config(filelist=None, initdict=None, kwargs=None):
    flags = AttrDict()
    flags.DEBUG = 0
    flags.VERBOSE = 0
    cf = AttrDict()
    cf.flags = flags
    if filelist:
        for f in filelist:
            if os.path.isfile(f):
                gb = globals()
                exec(compile(open(f).read(), f, 'exec'), gb, cf)
    if type(initdict) is dict:
        cf.update(initdict)
    if type(kwargs) is dict:
        cf.update(kwargs)
    return cf
Exemplo n.º 3
0
 def close(self):
     """close cached objects and clean up.
 """
     d = self.__dict__["_cache"]
     self.__dict__["_cache"] = AttrDict()
     for obj in d.values():
         try:
             d.close()
         except:
             pass
     self.__dict__["_connection"] = None
     self.__dict__["_container"] = None
Exemplo n.º 4
0
def GetConfig(extrafiles=None,
              initdict=None,
              clientconfig=None,
              initializer=None):
    """Primary configuration constructor.

  Returns a RootContainer instance containing configuration parameters.
  An extra dictionary may be merged in with the 'initdict' parameter.  And
  finally, extra options may be added with keyword parameters when calling
  this. It also merges in configuration values found in the user-private
  "~/.droidrc" configuration file.

  """
    SOURCES = []
    # user-private configuration overrides
    SOURCES.append(os.path.join(os.environ["HOME"], ".droidrc"))

    if type(extrafiles) is str:
        extrafiles = [extrafiles]
    if extrafiles:
        FILES = SOURCES + extrafiles
    else:
        FILES = SOURCES
    db = GetClient(clientconfig)
    if not db:
        raise ValueError, "Unable to get storage. Check your configuration."
    cache = AttrDict()
    cache["Container"] = AttrDict
    cf = db.getRoot(cache, initializer)
    # copy default flags to cache so they don't get altered in persistent
    # storage.
    cache.flags = cf["flags"].copy()
    # merge in extra configuration files.
    for f in FILES:
        cf.mergefile(f)
    # merge in extra dictionary, if provided.
    if type(initdict) is dict:
        cf.evalupdate(initdict)
    return cf
Exemplo n.º 5
0
def get_config(extrafiles=None, initdict=None, **kwargs):
    """get_config([extrafiles=], [initdict=], [**kwargs])
Returns a RootContainer instance containing configuration parameters.
An extra dictionary may be merged in with the 'initdict' parameter.
And finally, extra options may be added with keyword parameters when calling
this.  """
    from pycopia import socket
    from pycopia import basicconfig
    SOURCES = []
    SOURCES.append(os.path.join(os.environ["HOME"], ".pycopiarc"))

    dbcf = basicconfig.get_config("storage.conf")

    if type(extrafiles) is str:
        extrafiles = [extrafiles]
    if extrafiles:
        FILES = SOURCES + extrafiles
    else:
        FILES = SOURCES
    try:  # try getting the server. If not available, try
        # opening the data file directly.
        db = get_client(dbcf)
    except socket.SocketError:
        db = get_database(dbcf)
    cache = AttrDict()
    cf = db.get_root(cache)
    # copy default flags to cache so the don't get altered
    cache.flags = cf["flags"].copy()
    # initialize cache items to default values, so they can be altered at runtime.
    cf.update(cf["default"])
    for f in FILES:
        if os.path.isfile(f):
            cf.mergefile(f)
    if type(initdict) is dict:
        cf.evalupdate(initdict)
    cf.update(kwargs)
    return cf
Exemplo n.º 6
0
def get_config(extrafiles=None, initdict=None, **kwargs):
    """get_config([extrafiles=], [initdict=], [**kwargs])
Returns a RootContainer instance containing configuration parameters.
An extra dictionary may be merged in with the 'initdict' parameter.
And finally, extra options may be added with keyword parameters when calling
this.  """
    from pycopia import socket
    from pycopia import basicconfig
    SOURCES = []
    SOURCES.append(os.path.join(os.environ["HOME"], ".pycopiarc"))

    dbcf = basicconfig.get_config("storage.conf")

    if type(extrafiles) is str:
        extrafiles = [extrafiles]
    if extrafiles:
        FILES = SOURCES + extrafiles
    else:
        FILES = SOURCES
    try: # try getting the server. If not available, try
         # opening the data file directly.
        db = get_client(dbcf)
    except socket.SocketError:
        db = get_database(dbcf)
    cache = AttrDict()
    cf = db.get_root(cache)
    # copy default flags to cache so the don't get altered
    cache.flags = cf["flags"].copy()
    # initialize cache items to default values, so they can be altered at runtime.
    cf.update(cf["default"])
    for f in FILES:
        if os.path.isfile(f):
            cf.mergefile(f)
    if type(initdict) is dict:
        cf.evalupdate(initdict)
    cf.update(kwargs)
    return cf
Exemplo n.º 7
0
def get_config(storageurl=None, _extrafiles=None, initdict=None, **kwargs):
    """Get primary configuration.

    Returns a RootContainer instance containing configuration parameters.  An
    extra dictionary may be merged in with the 'initdict' parameter.  And
    finally, extra options may be added with keyword parameters when calling
    this.
    """
    models.connect(storageurl)
    files = []

    if type(_extrafiles) is str:
        _extrafiles = [_extrafiles]
    if _extrafiles:
        files.extend(_extrafiles)
    try:
        rootnode = config.get_root()
    except models.OperationalError:
        logging.exception_warning(
            "Could not connect to database. Configuration not available.")
        return get_mock_config(files, initdict, kwargs)
    cache = AttrDict()
    flags = AttrDict()
    # copy flag values to cache so changes don't persist.
    flagsnode = Config.select().where(
        (Config.parent == rootnode) & (Config.name == "flags")).get()
    for valnode in flagsnode.children:
        flags[valnode.name] = valnode.value
    cache.flags = flags
    cf = RootContainer(rootnode, cache)
    for f in files:
        if os.path.isfile(f):
            cf.mergefile(f)
    if type(initdict) is dict:
        cf.evalupdate(initdict)
    cf.update(kwargs)
    return cf
Exemplo n.º 8
0
def get_config(storageurl=None, _extrafiles=None, initdict=None, **kwargs):
    """Get primary configuration.

    Returns a RootContainer instance containing configuration parameters.  An
    extra dictionary may be merged in with the 'initdict' parameter.  And
    finally, extra options may be added with keyword parameters when calling
    this.
    """
    models.connect(storageurl)
    files = []

    if type(_extrafiles) is str:
        _extrafiles = [_extrafiles]
    if _extrafiles:
        files.extend(_extrafiles)
    try:
        rootnode = config.get_root()
    except models.OperationalError:
        logging.exception_warning(
            "Could not connect to database. Configuration not available.")
        return get_mock_config(files, initdict, kwargs)
    cache = AttrDict()
    flags = AttrDict()
    # copy flag values to cache so changes don't persist.
    flagsnode = Config.select().where((Config.parent == rootnode)
                                      & (Config.name == "flags")).get()
    for valnode in flagsnode.children:
        flags[valnode.name] = valnode.value
    cache.flags = flags
    cf = RootContainer(rootnode, cache)
    for f in files:
        if os.path.isfile(f):
            cf.mergefile(f)
    if type(initdict) is dict:
        cf.evalupdate(initdict)
    cf.update(kwargs)
    return cf
Exemplo n.º 9
0
 def __init__(self, connection, container, cache=None):
     self.__dict__["_connection"] = connection
     self.__dict__["_container"] = container
     # local, non-persistent cache for computed and temporary variables
     self.__dict__["_cache"] = cache or AttrDict()
Exemplo n.º 10
0
def get_mock_config(filelist=None, initdict=None, kwargs=None):
    flags = AttrDict()
    flags.DEBUG = 0
    flags.VERBOSE = 0
    cf = AttrDict()
    cf.flags = flags
    if filelist:
        for f in filelist:
            if os.path.isfile(f):
                gb = globals()
                exec(compile(open(f).read(), f, 'exec'), gb, cf)
    if type(initdict) is dict:
        cf.update(initdict)
    if type(kwargs) is dict:
        cf.update(kwargs)
    return cf