# Path: src/astrodynamics/utils/web.py
# SPK_DIR = Path(appdirs.user_data_dir, 'spk')
# 
# class SPKDownloadError(Exception):
#     """Raised when SPK download fails."""
# 
# def download_spk(category, kernel, download_dir=None):
#     """Download generic kernel SPK files from
#     http://naif.jpl.nasa.gov/pub/naif/generic_kernels/spk/
# 
#     Parameters:
#         category: asteroids, comets, lagrange_point, planets, satellites, or stations
#         kernel: Kernel name, e.g. `de430` will download `de430.bsp`
#         download_dir: Directory to download file to. By default, this is to a
#                       platform-dependent astrodynamics directory: :py:const:`SPK_DIR`
#     """
#     valid_categories = ['asteroids', 'comets', 'lagrange_point', 'planets',
#                         'satellites', 'stations']
# 
#     if category not in valid_categories:
#         s = 'Invalid category. Valid categories: {}'
#         raise InvalidCategoryError(s.format(', '.join(valid_categories)))
# 
#     # We only wanted the name, strip the extension.
#     if kernel.endswith('.bsp'):
#         kernel = kernel[:-4]
# 
#     urls = [
#         SPK_URL.format(category=category, kernel=quote(kernel)),
#         SPK_OLD_URL.format(category=category, kernel=quote(kernel))
#     ]
# 
#     # Get last path component for filename.
#     # e.g. 'de423_for_mercury_and_venus/de423' => 'de423.bsp'
#     #      'de421' => 'de421.bsp'
#     filename = kernel.split('/')[-1] + '.bsp'
# 
#     if download_dir:
#         download_dir = Path(download_dir)
# 
#         filepath = download_dir / filename
#     else:
#         with suppress_file_exists_error():
#             SPK_DIR.mkdir(parents=True)
# 
#         filepath = SPK_DIR / filename
# 
#     downloaded = False
# 
#     for url in urls:
#         try:
#             download_file_with_progress(url, str(filepath))
#         except HTTPError as exc:
#             if exc.response.status_code != 404:
#                 raise
#         else:
#             print('Kernel downloaded to', filepath)
#             downloaded = True
#             break
# 
#     if not downloaded:
#         s = 'Kernel not found in the following locations:\n{}'
#         raise KernelNotFoundError(s.format('\n'.join(prefix('  ', urls))))


import sys
from docopt import docopt
from .utils import SPK_DIR, SPKDownloadError, download_spk
# coding: utf-8
"""astrodynamics

Usage:
    astrodynamics download_spk [options] <category> <kernel>

Options:
    --download-dir <dir>  Default: {default}

Example:
    astrodynamics download_spk planets de421
"""
from __future__ import absolute_import, division, print_function





def main():
    args = docopt(__doc__.format(default=SPK_DIR))

    if args['download_spk']:
        category = args['<category>']
        kernel = args['<kernel>']
        download_dir = args['--download-dir']
        try:

# Path: src/astrodynamics/utils/web.py
# SPK_DIR = Path(appdirs.user_data_dir, 'spk')
# 
# class SPKDownloadError(Exception):
#     """Raised when SPK download fails."""
# 
# def download_spk(category, kernel, download_dir=None):
#     """Download generic kernel SPK files from
#     http://naif.jpl.nasa.gov/pub/naif/generic_kernels/spk/
# 
#     Parameters:
#         category: asteroids, comets, lagrange_point, planets, satellites, or stations
#         kernel: Kernel name, e.g. `de430` will download `de430.bsp`
#         download_dir: Directory to download file to. By default, this is to a
#                       platform-dependent astrodynamics directory: :py:const:`SPK_DIR`
#     """
#     valid_categories = ['asteroids', 'comets', 'lagrange_point', 'planets',
#                         'satellites', 'stations']
# 
#     if category not in valid_categories:
#         s = 'Invalid category. Valid categories: {}'
#         raise InvalidCategoryError(s.format(', '.join(valid_categories)))
# 
#     # We only wanted the name, strip the extension.
#     if kernel.endswith('.bsp'):
#         kernel = kernel[:-4]
# 
#     urls = [
#         SPK_URL.format(category=category, kernel=quote(kernel)),
#         SPK_OLD_URL.format(category=category, kernel=quote(kernel))
#     ]
# 
#     # Get last path component for filename.
#     # e.g. 'de423_for_mercury_and_venus/de423' => 'de423.bsp'
#     #      'de421' => 'de421.bsp'
#     filename = kernel.split('/')[-1] + '.bsp'
# 
#     if download_dir:
#         download_dir = Path(download_dir)
# 
#         filepath = download_dir / filename
#     else:
#         with suppress_file_exists_error():
#             SPK_DIR.mkdir(parents=True)
# 
#         filepath = SPK_DIR / filename
# 
#     downloaded = False
# 
#     for url in urls:
#         try:
#             download_file_with_progress(url, str(filepath))
#         except HTTPError as exc:
#             if exc.response.status_code != 404:
#                 raise
#         else:
#             print('Kernel downloaded to', filepath)
#             downloaded = True
#             break
# 
#     if not downloaded:
#         s = 'Kernel not found in the following locations:\n{}'
#         raise KernelNotFoundError(s.format('\n'.join(prefix('  ', urls))))


# Path: src/astrodynamics/__main__.py
import sys
from docopt import docopt
from .utils import SPK_DIR, SPKDownloadError, download_spk

# coding: utf-8
"""astrodynamics

Usage:
    astrodynamics download_spk [options] <category> <kernel>

Options:
    --download-dir <dir>  Default: {default}

Example:
    astrodynamics download_spk planets de421
"""
from __future__ import absolute_import, division, print_function





def main():
    args = docopt(__doc__.format(default=SPK_DIR))

    if args['download_spk']:
        category = args['<category>']
        kernel = args['<kernel>']
        download_dir = args['--download-dir']
        try:

            download_spk(category=category, kernel=kernel, download_dir=download_dir)