Skip to content

Commit

Permalink
Added support for generating custom configs.
Browse files Browse the repository at this point in the history
- Added ability to specify your own filepath in ``ia configure`` and
  ``internetarchive.configure()``.
  • Loading branch information
jjjake committed May 16, 2016
1 parent f167781 commit bc94d1c
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 9 deletions.
1 change: 1 addition & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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**

Expand Down
16 changes: 15 additions & 1 deletion docs/source/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,21 @@ The easiest way to create a config file is with the `configure <internetarchive.
>>> from internetarchive import configure
>>> configure('[email protected]', '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('[email protected]', '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
~~~~~~~~~~~~~~~~~~~
Expand Down
2 changes: 1 addition & 1 deletion docs/source/quickstart.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions internetarchive/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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))


Expand Down
9 changes: 7 additions & 2 deletions internetarchive/cli/ia.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
'<args>': list,
'<command>': Or(str, lambda _: 'help'),
})
Expand All @@ -130,6 +129,12 @@ def main():
ia_module = load_ia_module(args['<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['<args>']

config = dict()
Expand Down
2 changes: 1 addition & 1 deletion internetarchive/cli/ia_configure.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
4 changes: 2 additions & 2 deletions internetarchive/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions internetarchive/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:'
Expand Down

0 comments on commit bc94d1c

Please sign in to comment.