diff --git a/HISTORY.rst b/HISTORY.rst index b3492c21..7bf75107 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -18,6 +18,7 @@ Release History For more details refer to the following links: http://internetarchive.readthedocs.io/en/latest/quickstart.html?highlight=configure#configuring http://internetarchive.readthedocs.io/en/latest/api.html#configuration +- Added ability to specify your own filepath in ``ia configure`` and ``internetarchive.configure()``. **Bugfixes** diff --git a/docs/source/api.rst b/docs/source/api.rst index e85e41e9..728d9113 100644 --- a/docs/source/api.rst +++ b/docs/source/api.rst @@ -16,7 +16,21 @@ The easiest way to create a config file is with the `configure >> from internetarchive import configure >>> configure('user@example.com', 'password') -Config files are stored in either ``$HOME/.ia`` or ``$HOME/.config/ia.ini`` by default, but other config files can be specified when insantiating an :class:`ArchiveSession` or :class:`Item` object. +Config files are stored in either ``$HOME/.ia`` or ``$HOME/.config/ia.ini`` by default. You can also specify your own path:: + + + >>> from internetarchive import configure + >>> configure('user@example.com', 'password', config_file='/home/jake/.config/ia-alternate.ini') + +Custom config files can be specified when insantiating an :class:`ArchiveSession` object:: + + >>> from internetarchive import get_session + >>> s = get_session(config_file='/home/jake/.config/ia-alternate.ini') + +Or an :class:`Item` object:: + + >>> form internetarchive import get_item + >>> item = get_item('nasa', config_file='/home/jake/.config/ia-alternate.ini') IA-S3 Configuration ~~~~~~~~~~~~~~~~~~~ diff --git a/docs/source/quickstart.rst b/docs/source/quickstart.rst index 67b69f11..73ebf45e 100644 --- a/docs/source/quickstart.rst +++ b/docs/source/quickstart.rst @@ -19,7 +19,7 @@ To automatically create a config file with your Archive.org credentials, you can Config saved to: /home/user/.config/ia.ini -Your config file will be saved to ``$HOME/.config/ia.ini``, or ``$HOME/.ia`` if you do not have a ``.config`` directory in ``$HOME``. +Your config file will be saved to ``$HOME/.config/ia.ini``, or ``$HOME/.ia`` if you do not have a ``.config`` directory in ``$HOME``. Alternatively, you can specify your own path to save the config to via ``ia --config-file '~/.ia-custom-config' configure``. Uploading diff --git a/internetarchive/api.py b/internetarchive/api.py index 844b1ace..d01f1837 100644 --- a/internetarchive/api.py +++ b/internetarchive/api.py @@ -487,7 +487,7 @@ def search_items(query, request_kwargs=request_kwargs) -def configure(username=None, password=None): +def configure(username=None, password=None, config_file=None): """Configure internetarchive with your Archive.org credentials. :type username: str @@ -502,7 +502,7 @@ def configure(username=None, password=None): """ username = input('Email address: ') if not username else username password = getpass('Password: ') if not password else password - config_file_path = config_module.write_config_file(username, password) + config_file_path = config_module.write_config_file(username, password, config_file) print('\nConfig saved to: {0}'.format(config_file_path)) diff --git a/internetarchive/cli/ia.py b/internetarchive/cli/ia.py index cd237566..5609611a 100755 --- a/internetarchive/cli/ia.py +++ b/internetarchive/cli/ia.py @@ -107,8 +107,7 @@ def main(): # Validate args. s = Schema({ six.text_type: bool, - '--config-file': Or(None, lambda f: os.path.exists(f), - error='--config-file should be a readable file.'), + '--config-file': Or(None, str), '': list, '': Or(str, lambda _: 'help'), }) @@ -130,6 +129,12 @@ def main(): ia_module = load_ia_module(args[''][0]) sys.exit(print(ia_module.__doc__.strip(), file=sys.stderr)) + if cmd != 'configure' and args['--config-file']: + if not os.path.isfile(args['--config-file']): + print('--config-file should be a readable file.\n{0}'.format( + printable_usage(__doc__)), file=sys.stderr) + sys.exit(1) + argv = [cmd] + args[''] config = dict() diff --git a/internetarchive/cli/ia_configure.py b/internetarchive/cli/ia_configure.py index e103e69c..e31b865a 100755 --- a/internetarchive/cli/ia_configure.py +++ b/internetarchive/cli/ia_configure.py @@ -38,7 +38,7 @@ def main(argv, session): docopt(__doc__, argv=argv) print("Enter your Archive.org credentials below to configure 'ia'.\n") try: - configure() + configure(config_file=session.config_file) except AuthenticationError as exc: print('\nerror: {0}'.format(str(exc))) sys.exit(1) diff --git a/internetarchive/config.py b/internetarchive/config.py index cc131a20..240598b5 100644 --- a/internetarchive/config.py +++ b/internetarchive/config.py @@ -77,8 +77,8 @@ def get_auth_config(username, password): return auth_config -def write_config_file(username, password): - config_file, config = parse_config_file() +def write_config_file(username, password, config_file=None): + config_file, config = parse_config_file(config_file) auth_config = get_auth_config(username, password) # S3 Keys. diff --git a/internetarchive/session.py b/internetarchive/session.py index 0e1a0e2f..865d9868 100644 --- a/internetarchive/session.py +++ b/internetarchive/session.py @@ -97,6 +97,7 @@ def __init__(self, debug = False if not debug else True self.config = get_config(config, config_file) + self.config_file = config_file self.cookies.update(self.config.get('cookies', {})) self.secure = self.config.get('general', {}).get('secure', True) self.protocol = 'https:' if self.secure else 'http:'