Commits

Sebastian Rahlf  committed 375f55d

API accepts path to config file or config as dict (addresses #45).

  • Participants
  • Parent commits 4e1c03b

Comments (0)

Files changed (2)

File amazonproduct/api.py

     TIMEOUT = 5 #: timeout in seconds
 
     def __init__(self, access_key_id=None, secret_access_key=None, locale=None,
-                 associate_tag=None, processor='amazonproduct.processors.objectify'):
+             associate_tag=None, processor='amazonproduct.processors.objectify',
+             cfg=None):
         """
         .. versionchanged:: 0.2.6
            Passing parameters ``access_key_id``, ``secret_access_key`` and
         :param processor: module containing result processing functions. Look
         in package ``amazonproduct.processors`` for values.
         """
-        if not (access_key_id is None and secret_access_key is None
-        and associate_tag is None):
+
+        if any([access_key_id, secret_access_key, associate_tag]):
             warnings.warn('Please use a config file!', DeprecationWarning,
                 stacklevel=2)
 
         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()
+            # 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])

File docs/source/config.rst

 ``AWS_ACCESS_KEY_ID`` and ``AWS_SECRET_ACCESS_KEY``).
 
 
-Config files
-------------
+Using files
+-----------
 
-Upon initialisation, the API looks for configuration files (similar to the
-`boto config`_, where I have borrowed the idea from) in the following
+To use a config file, pass its path to the API::
+
+    import amazonproduct
+    api = amazonproduct.API(cfg='~/my-config-file')
+
+If no path was specified, the API looks for configuration files in the following
 locations and in the following order:
 
-* any `boto config`_ files ``/etc/boto.cfg`` and ``~/.boto``
 * ``/etc/amazon-product-api.cfg`` for site-wide settings that all users on
   this machine will use
 * ``~/.amazon-product-api`` for user-specific settings
 
 The options are merged into a single, in-memory configuration that is available.
-Files from this wrapper will always take precedence over boto config files!
 
 The following sections and options are currently recognized within the config
 file.
         secret_key = <your secret key>
         associate_tag = <your associate id>
 
-    .. note:: Stating the obivious: Your access key is *not* ``<your access
+    .. note:: Stating the obvious: Your access key is *not* ``<your access
        key>`` but something like ``10RZZJBK6YBQASX213G2``.
 
-.. _boto config: http://code.google.com/p/boto/wiki/BotoConfig
+
+Using config dict
+-----------------
+
+If you need to configure the API at runtime you can also pass the config values
+as dict::
+
+    import amazonproduct
+    config = {
+        'access_key': 'ABCDEFG1234X',
+        'secret_key': 'Ydjkei78HdkffdklieAHDJWE3134',
+        'associate_tag': 'redtoad-10',
+        'locale': 'us'
+    }
+    api = amazonproduct.API(cfg=config)
 
 
 Environment variables
 ``AWS_LOCALE``
     Your API locale
 
+.. important:: Environment variables will always take precedence over values
+   from config files *but not from config dict*!
+
 
 Order of precedence
 -------------------
 
 * Parameters specified by environment variables
 * User-specific parameters from ``~/.amazon-product-api``
-* Site-wide parameters from ``/etc/amazon-product-api.cfg`` for settings that all users on
-* `boto config`_ files
 
 The following table gives an overview which values can be defined where:
 
-=============  ======================  =====================
-config file    boto config             environment variable
-=============  ======================  =====================
-access_key     aws_access_key_id       AWS_ACCESS_KEY_ID
-secret_key     aws_secret_access_key   AWS_SECRET_ACCESS_KEY
-associate_tag                          AWS_ASSOCIATE_TAG
-locale                                 AWS_LOCALE
-=============  ======================  =====================
+=============  =====================
+config file    environment variable
+=============  =====================
+access_key     AWS_ACCESS_KEY_ID
+secret_key     AWS_SECRET_ACCESS_KEY
+associate_tag  AWS_ASSOCIATE_TAG
+locale         AWS_LOCALE
+=============  =====================