示例#1
0
    def __init__(self,
                 access_key_id=None,
                 secret_access_key=None,
                 locale=None,
                 associate_tag=None,
                 processor='amazonproduct.processors.objectify',
                 cfg=None):
        """
        .. versionchanged:: 0.2.6
           Passing parameters ``access_key_id``, ``secret_access_key`` and
           ``associate_tag`` directly to the constructor will be removed in one
           of the next releases. See :ref:`config` for alternatives.

        :param access_key_id: AWS access key ID (deprecated).
        :param secret_access_key: AWS secret key (deprecated).
        :param associate_tag: Amazon Associates tracking id (deprecated).
        :param locale: localise results by using one value from ``LOCALES``.
        :param processor: module containing result processing functions. Look
        in package ``amazonproduct.processors`` for values.
        """

        if any([access_key_id, secret_access_key, associate_tag]):
            warnings.warn('Please use a config file!',
                          DeprecationWarning,
                          stacklevel=2)

        self.access_key = access_key_id
        self.secret_key = secret_access_key
        self.associate_tag = associate_tag
        self.locale = locale

        if not all(getattr(self, key, False) for key in REQUIRED_KEYS):
            # load missing valued from config file
            if cfg is None or isinstance(cfg, (str, unicode)):
                cfg = load_config(cfg)
            for key in REQUIRED_KEYS:
                if getattr(self, key, '???') is None and cfg.get(key, None):
                    setattr(self, key, cfg[key])

        try:
            self.host = HOSTS[self.locale]
        except KeyError:
            raise UnknownLocale(locale)

        # GAE does not allow timeouts to be specified manually
        if not running_on_gae():
            socket.setdefaulttimeout(self.TIMEOUT)

        # instantiate processor class
        if isinstance(processor, str):
            self._processor_module = processor
            self.processor = load_class('%s.Processor' % processor)()
        else:
            self._processor_module = processor.__class__.__name__
            self.processor = processor

        self.last_call = datetime(1970, 1, 1)
        self.debug = 0  # set to 1 if you want to see HTTP headers
def test_load_config(configfiles, monkeypatch):
    configfiles.load_from_string(DUMMY_CONFIG)
    monkeypatch.setenv('AWS_LOCALE', 'OS VARIABLE')

    cfg = utils.load_config()
    assert set(cfg.keys()) == set([
        'access_key', 'secret_key', 'associate_tag', 'locale'])

    assert cfg['access_key'] == 'global boto value'
    assert cfg['secret_key'] == 'local boto value'
    assert cfg['associate_tag'] is None
    assert cfg['locale'] == 'OS VARIABLE'
示例#3
0
def test_load_config(configfiles, monkeypatch):
    configfiles.load_from_string(DUMMY_CONFIG)
    monkeypatch.setenv('AWS_LOCALE', 'OS VARIABLE')

    cfg = utils.load_config()
    assert set(cfg.keys()) == set([
        'access_key', 'secret_key', 'associate_tag', 'locale'])

    assert cfg['access_key'] == 'global cfg value'
    assert cfg['secret_key'] == 'local cfg value'
    assert cfg['associate_tag'] is None
    assert cfg['locale'] == 'OS VARIABLE'
示例#4
0
    def __init__(self, access_key_id=None, secret_access_key=None, locale=None,
             associate_tag=None, processor='amazonproduct.processors.objectify',
             cfg=None):
        """
        .. versionchanged:: 0.2.6
           Passing parameters ``access_key_id``, ``secret_access_key`` and
           ``associate_tag`` directly to the constructor will be removed in one
           of the next releases. See :ref:`config` for alternatives.

        :param access_key_id: AWS access key ID (deprecated).
        :param secret_access_key: AWS secret key (deprecated).
        :param associate_tag: Amazon Associates tracking id (deprecated).
        :param locale: localise results by using one value from ``LOCALES``.
        :param processor: module containing result processing functions. Look
        in package ``amazonproduct.processors`` for values.
        """

        if any([access_key_id, secret_access_key, associate_tag]):
            warnings.warn('Please use a config file!', DeprecationWarning,
                stacklevel=2)

        self.access_key = access_key_id
        self.secret_key = secret_access_key
        self.associate_tag = associate_tag
        self.locale = locale

        if not all(getattr(self, key, False) for key in REQUIRED_KEYS):
            # load missing valued from config file
            if cfg is None or isinstance(cfg, (str, unicode)):
                cfg = load_config(cfg)
            for key in REQUIRED_KEYS:
                if getattr(self, key, '???') is None and cfg.get(key, None):
                    setattr(self, key, cfg[key])

        try:
            self.host = HOSTS[self.locale]
        except KeyError:
            raise UnknownLocale(locale)

        # GAE does not allow timeouts to be specified manually
        if not running_on_gae():
            socket.setdefaulttimeout(self.TIMEOUT)

        # instantiate processor class
        if isinstance(processor, str):
            self._processor_module = processor
            self.processor = load_class('%s.Processor' % processor)()
        else:
            self._processor_module = processor.__class__.__name__
            self.processor = processor

        self.last_call = datetime(1970, 1, 1)
        self.debug = 0  # set to 1 if you want to see HTTP headers
def test_specific_config_file_overrides_all_but_os_variables(configfiles, monkeypatch):
    configfiles.load_from_string(DUMMY_CONFIG)
    monkeypatch.setenv('AWS_LOCALE', 'OS VARIABLE')

    path = configfiles.tmpdir.join(os.path.expanduser('~/my-config')).strpath
    cfg = utils.load_config(path)
    assert set(cfg.keys()) == set([
        'access_key', 'secret_key', 'associate_tag', 'locale'])

    assert cfg['secret_key'] == 'CUSTOM CONFIG OVERRIDES ALL!'
    assert cfg['access_key'] is None
    assert cfg['associate_tag'] is None
    assert cfg['locale'] == 'OS VARIABLE'
示例#6
0
def test_specific_config_file_overrides_all_but_os_variables(configfiles, monkeypatch):
    configfiles.load_from_string(DUMMY_CONFIG)
    monkeypatch.setenv('AWS_LOCALE', 'OS VARIABLE')

    path = configfiles.tmpdir.join(os.path.expanduser('~/my-config')).strpath
    cfg = utils.load_config(path)
    assert set(cfg.keys()) == set([
        'access_key', 'secret_key', 'associate_tag', 'locale'])

    assert cfg['secret_key'] == 'CUSTOM CONFIG OVERRIDES ALL!'
    assert cfg['access_key'] is None
    assert cfg['associate_tag'] is None
    assert cfg['locale'] == 'OS VARIABLE'
示例#7
0
    def __init__(self, access_key_id=None, secret_access_key=None, locale=None,
                 associate_tag=None, processor=None):
        """
        .. versionchanged:: 0.2.6
           Passing parameters ``access_key_id``, ``secret_access_key`` and
           ``associate_tag`` directly to the constructor will be removed in one
           of the next releases. See :ref:`config` for alternatives.

        :param access_key_id: AWS access key ID (deprecated).
        :param secret_key_id: AWS secret key (deprecated).
        :param associate_tag: Amazon Associates tracking id (deprecated).
        :param locale: localise results by using one value from ``LOCALES``.
        :param processor: result processing function (``None`` if unsure).
        """
        if not (access_key_id is None and secret_access_key is None
        and associate_tag is None):
            warnings.warn('Please use a config file!', DeprecationWarning,
                stacklevel=2)

        self.access_key = access_key_id
        self.secret_key = secret_access_key
        self.associate_tag = associate_tag
        self.locale = locale

        # load missing valued from config file
        if not all(getattr(self, key, False) for key in REQUIRED_KEYS):
            cfg = load_config()
            for key in REQUIRED_KEYS:
                if getattr(self, key, '???') is None and cfg.get(key, None):
                    setattr(self, key, cfg[key])

        try:
            self.host = HOSTS[self.locale]
        except KeyError:
            raise UnknownLocale(locale)


        # GAE does not allow timeouts to be specified manually
        if not running_on_gae():
            socket.setdefaulttimeout(self.TIMEOUT)

        self.last_call = datetime(1970, 1, 1)
        self.debug = 0 # set to 1 if you want to see HTTP headers

        if processor is not None:
            self.processor = processor
        else:
            self.processor = Processor()
def test_load_config(configfiles, monkeypatch):
    configfiles.load_from_string(DUMMY_CONFIG)
    for key in [
            'AWS_ACCESS_KEY', 'AWS_SECRET_ACCESS_KEY', 'AWS_ASSOCIATE_TAG',
            'AWS_LOCALE'
    ]:
        monkeypatch.delenv(key, raising=False)
    monkeypatch.setenv('AWS_LOCALE', 'OS VARIABLE')

    cfg = utils.load_config()
    assert set(cfg.keys()) == set(
        ['access_key', 'secret_key', 'associate_tag', 'locale'])

    assert cfg['access_key'] == 'global cfg value'
    assert cfg['secret_key'] == 'local cfg value'
    assert cfg['associate_tag'] is None
    assert cfg['locale'] == 'OS VARIABLE'
    'lxml.etree',
    'xml.etree.cElementTree',
    'xml.etree.ElementTree',
    'cElementTree',
    'elementtree.ElementTree',
    'elementtree.ElementTree'
]

#: Result processors to test with.
TESTABLE_PROCESSORS = {
    'objectify': objectify.Processor,
    #    'minidom': minidom.Processor,
}
# add ElementTree implementations
for mod in ELEMENTTREE_IMPLEMENTATIONS:
    TESTABLE_PROCESSORS[mod] = etree.Processor(module=mod)

def get_config_value(key, default=None):
    """
    Loads value from config.py or from environment variable or return default
    (in that order).
    """
    try:
        return getattr(_config, key)
    except AttributeError:
        return os.environ.get(key, default)

_config = load_config()
AWS_KEY = _config.get('access_key', '')
SECRET_KEY = _config.get('secret_key', '')
#    'xml.etree.cElementTree',
#    'xml.etree.ElementTree',
#    'cElementTree',
#    'elementtree.ElementTree',
#    'elementtree.ElementTree'
#]

#: Result processors to test with.
TESTABLE_PROCESSORS = {
    'objectify': 'amazonproduct.processors.objectify',
    'etree': 'amazonproduct.processors.etree',
    'elementtree': 'amazonproduct.processors.elementtree',
    #    'minidom': 'amazonproduct.processors.minidom',
}


def get_config_value(key, default=None):
    """
    Loads value from config.py or from environment variable or return default
    (in that order).
    """
    try:
        return getattr(_config, key)
    except AttributeError:
        return os.environ.get(key, default)


_config = load_config()
AWS_KEY = _config.get('access_key', '')
SECRET_KEY = _config.get('secret_key', '')