# Path: coshsh/util.py
# def compare_attr(key, params, strings):
#     if not isinstance(strings, list):
#         strings = [strings]
#     if key in params:
#         if params[key] == None:
#             return False
#         elif [str1 for str1 in strings if re.match(str1, params[key], re.IGNORECASE)]:
#             return True
#     return False
# 
# def substenv(matchobj):
#     if matchobj.group(0).replace('%', '') in os.environ.keys():
#         return os.environ[matchobj.group(0).replace('%', '')]
#     else:
#         return matchobj.group(0)


import sys
import os
import re
import imp
import inspect
import logging
import coshsh
from coshsh.util import compare_attr, substenv
    pass


class Datasource(object):

    my_type = 'datasource'
    class_factory = []

    def __init__(self, **params):
        #print "datasourceinit with", self.__class__
        for key in [k for k in params if k.startswith("recipe_")]:
            setattr(self, key, params[key])
            short = key.replace("recipe_", "")
            if not short in params:
                params[short] = params[key]
        for key in params.keys():
            if isinstance(params[key], basestring):
                params[key] = re.sub('%.*?%', substenv, params[key])
        if self.__class__ == Datasource:
            #print "generic ds", params
            newcls = self.__class__.get_class(params)
            if newcls:
                #print "i rebless anon datasource to", newcls, params
                self.__class__ = newcls
                self.__init__(**params)
            else:
                logger.critical('datasource for %s is not implemented' % params)
                #print "i raise DatasourceNotImplemented"
                raise DatasourceNotImplemented
        else:

# Path: coshsh/util.py
# def compare_attr(key, params, strings):
#     if not isinstance(strings, list):
#         strings = [strings]
#     if key in params:
#         if params[key] == None:
#             return False
#         elif [str1 for str1 in strings if re.match(str1, params[key], re.IGNORECASE)]:
#             return True
#     return False
# 
# def substenv(matchobj):
#     if matchobj.group(0).replace('%', '') in os.environ.keys():
#         return os.environ[matchobj.group(0).replace('%', '')]
#     else:
#         return matchobj.group(0)


# Path: coshsh/datasource.py
import sys
import os
import re
import imp
import inspect
import logging
import coshsh
from coshsh.util import compare_attr, substenv

    pass


class Datasource(object):

    my_type = 'datasource'
    class_factory = []

    def __init__(self, **params):
        #print "datasourceinit with", self.__class__
        for key in [k for k in params if k.startswith("recipe_")]:
            setattr(self, key, params[key])
            short = key.replace("recipe_", "")
            if not short in params:
                params[short] = params[key]
        for key in params.keys():
            if isinstance(params[key], basestring):
                params[key] = re.sub('%.*?%', substenv, params[key])
        if self.__class__ == Datasource:
            #print "generic ds", params
            newcls = self.__class__.get_class(params)
            if newcls:
                #print "i rebless anon datasource to", newcls, params
                self.__class__ = newcls
                self.__init__(**params)
            else:
                logger.critical('datasource for %s is not implemented' % params)
                #print "i raise DatasourceNotImplemented"
                raise DatasourceNotImplemented
        else:

            setattr(self, 'name', params["name"])