Commits

Sebastian Rahlf committed 4e1c03b

Use specific config file path.

Comments (0)

Files changed (3)

amazonproduct/utils.py

     'locale',
 ]
 
-BOTO_FILES = [
-    '/etc/boto.cfg',
-    '~/.boto',
-]
 CONFIG_FILES = [
     '/etc/amazon-product-api.cfg',
     '~/.amazon-product-api'
 ]
 
 
-def load_boto_config():
+def load_file_config(path=None):
     """
-    Loads config dict from a boto config file [#]_ found in ``/etc/boto.cfg``
-    or ``~/.boto``. A boto config file looks like this::
-
-        [Credentials]
-        aws_access_key_id = <your access key>
-        aws_secret_access_key = <your secret key>
-
-    Note that the only config section that will be used in ``Credentials``.
-
-    .. _#: http://code.google.com/p/boto/wiki/BotoConfig
-    """
-    config = SafeConfigParser()
-    config.read([os.path.expanduser(path) for path in BOTO_FILES])
-
-    mapper = {
-        'access_key': 'aws_access_key_id',
-        'secret_key': 'aws_secret_access_key',
-    }
-    return dict(
-        (key, config.get('Credentials', boto))
-        for key, boto in mapper.items()
-        if config.has_option('Credentials', boto)
-    )
-
-
-def load_file_config():
-    """
-    Loads dict from config files ``/etc/amazon-product-api.cfg`` or
-    ``~/.amazon-product-api``. ::
+    Loads configuration from file with following content::
 
         [Credentials]
         access_key = <your access key>
         associate_tag = <your associate tag>
         locale = us
 
+    :param path: path to config file. If not specified, locations
+    ``/etc/amazon-product-api.cfg`` and ``~/.amazon-product-api`` are tried.
     """
     config = SafeConfigParser()
-    config.read([os.path.expanduser(path) for path in CONFIG_FILES])
+    if path is None:
+        config.read([os.path.expanduser(path) for path in CONFIG_FILES])
+    else:
+        config.read(path)
 
     if not config.has_section('Credentials'):
         return {}
     return dict(
         (key, os.environ.get(val))
         for key, val in mapper.items()
-        if os.environ.has_key(val)
+        if val in os.environ
     )
 
 
-def load_config():
+def load_config(path=None):
     """
     Returns a dict with API credentials which is loaded from (in this order):
 
       ``AWS_ASSOCIATE_TAG`` and ``AWS_LOCALE``
     * Config files ``/etc/amazon-product-api.cfg`` or ``~/.amazon-product-api``
       where the latter may add or replace values of the former.
-    * A boto config file [#]_ found in ``/etc/boto.cfg`` or ``~/.boto``.
 
     Whatever is found first counts.
 
             'locale': 'uk'
         }
 
-    .. _#: http://code.google.com/p/boto/wiki/BotoConfig
+    :param path: path to config file.
     """
-    config = load_boto_config()
-    config.update(load_file_config())
+    config = load_file_config(path)
     config.update(load_environment_config())
 
     # substitute None for all values not found

tests/conftest.py

         self.files += [p.strpath]
 
     _REG = re.compile(r'^#\s*file:\s+(.+?)\n', re.DOTALL | re.MULTILINE)
+
     def load_from_string(self, content):
         """
         Creates config files from string which is split up into file blocks and
         written to temporary files.
         """
-        last = 0 # end of the last matching '# file: XXX'
-        path = None # path of the last matching '# file: XXX'
+        last = 0  # end of the last matching '# file: XXX'
+        path = None  # path of the last matching '# file: XXX'
         for m in self._REG.finditer(content):
             if path is not None:
                 self.add_file(content[last:m.start()], path)
 
     def prepend_tmppath(dir, files):
         return [tmpdir.join(os.path.expanduser(fn)).strpath for fn in files]
-    monkeypatch.setattr(utils, 'BOTO_FILES',
-        prepend_tmppath(tmpdir, utils.BOTO_FILES))
     monkeypatch.setattr(utils, 'CONFIG_FILES',
         prepend_tmppath(tmpdir, utils.CONFIG_FILES))
 

tests/test_utils.py

+import os
 import pytest
 import types
 
 from amazonproduct import utils
 from amazonproduct.processors import etree, minidom
 
-def test_load_global_boto_config(configfiles):
-    configfiles.add_file('''
-        [Credentials]
-        aws_access_key_id = ABCDEFGH12345
-        aws_secret_access_key = abcdegf43''', path='/etc/boto.cfg')
-
-    cfg = utils.load_boto_config()
-    assert cfg['access_key'] == 'ABCDEFGH12345'
-    assert cfg['secret_key'] == 'abcdegf43'
-    assert len(cfg) == 2
-
-def test_load_local_boto_config(configfiles):
-    configfiles.add_file('''
-        [Credentials]
-        aws_access_key_id = ABCDEFGH12345
-        aws_secret_access_key = zhgsdds8''', path='~/.boto')
-
-    cfg = utils.load_boto_config()
-    assert cfg['access_key'] == 'ABCDEFGH12345'
-    assert cfg['secret_key'] == 'zhgsdds8'
-    assert len(cfg) == 2
-
-def test_load_partial_boto_config(configfiles):
-    configfiles.add_file('''
-        [Credentials]
-        aws_access_key_id = ABCDEFGH12345''', path='~/.boto')
-
-    cfg = utils.load_boto_config()
-    assert cfg['access_key'] == 'ABCDEFGH12345'
-    assert len(cfg) == 1
-
 
 def test_load_global_file_config(configfiles):
     configfiles.add_file('''
     assert cfg['secret_key'] == 'zhgsdds8'
     assert len(cfg) == 2
 
+
 def test_load_local_file_config(configfiles):
     configfiles.add_file('''
         [Credentials]
 
 
 DUMMY_CONFIG = """
-# file: /etc/boto.cfg
+# file: /etc/amazon-product-api.cfg
 [Credentials]
-aws_access_key_id = global boto value
-aws_secret_access_key = global boto value
-
-# file: ~/.boto
-[Credentials]
-aws_access_key_id_wrongly_written = local boto value
-aws_secret_access_key = local boto value
+access_key = global cfg value
+secret_key = global cfg value
 
 # file: ~/.amazon-product-api
 [Credentials]
+secret_key = local cfg value
 locale = de
+
+# file: ~/my-config
+[Credentials]
+secret_key = CUSTOM CONFIG OVERRIDES ALL!
 """
 
+
 def test_load_config(configfiles, monkeypatch):
     configfiles.load_from_string(DUMMY_CONFIG)
     monkeypatch.setenv('AWS_LOCALE', 'OS VARIABLE')
     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['access_key'] == 'global cfg value'
+    assert cfg['secret_key'] == 'local cfg value'
+    assert cfg['associate_tag'] is None
+    assert cfg['locale'] == 'OS VARIABLE'
+
+
+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'