#!/usr/bin/env python3
usage = """
    Распечатывает информацию о конкретном дистрибутиве.

    Вывести эту справку и завершиться:
    $ dist -h
    $ dist --help

    Вывести список каталогов с модулями (sys.path):
    $ dist -s
    $ dist --syspath

    Вывести все установленные дистрибутивы:
    $ dist-info

    Вывести сводную информацию о дистрибутиве:
    $ dist-info <дистрибутив>

    Вывести каталог в котором находятся модули пакета:
    $ dist-info <дистрибутив> dist

    Вывести путь к файлу или каталогу с метаинформацией:
    $ dist-info <дистрибутив> egg

    Вывести сокращённую метаинформацию:
    $ dist-info <дистрибутив> meta

    Вывести файлы:
    $ dist-info <дистрибутив> files
    
    Вывести модули:
    $ dist-info [-c|--check] <дистрибутив> modules

    Вывести модули по указанному пути:
    $ dist-info [-c|--check] <путь> mod

    Вывести подмодули модуля (например, io.six - ищется в sys.path):
    $ dist-info [-c|--check] <модуль> mods

    Опция -с, --check импортирует все модули после их распечатки.

"""

from data_printer import p
from dist_info import DistNotFound, metadata, dists, \
    dist_info_paths, files, modules, modules_in_dir, \
    modules_from, imports
import sys


def print_list(l):
    for i in l:
        print(i)


options = {a: 1 for a in sys.argv if a and a[0] == '-'}
av = [a for a in sys.argv if not a or a and a[0] != '-']

try:

    mod = []

    if '-h' in options or '--help' in options:
        print(usage)
        exit(0)
    if '-s' in options or '--syspath' in options:
        print_list(sys.path)
    elif len(av) > 3:
        print(f"Слишком много аргументов", file=sys.stderr)
    elif 1 == len(av):
        print_list(dists())
    elif 2 == len(av):
        dist_dir, egg_dir = dist_info_paths(av[1])
        f = "\n    ".join(files(av[1]))
        m = "\n    ".join(modules(av[1]))
        print(f"""

Файлы:

    {f}

Модули:

    {m}

Каталог с модулями:

    {dist_dir}

Путь к метаинформации:

    {egg_dir}
""")
    elif av[2] == 'egg':
        dist_dir, egg_dir = dist_info_paths(av[1])
        print(egg_dir)
    elif av[2] == 'dist':
        dist_dir, egg_dir = dist_info_paths(av[1])
        print(dist_dir)
    elif av[2] == 'meta':
        dist_dir, egg_dir = dist_info_paths(av[1])
        p(metadata(egg_dir))
    elif av[2] == 'files':
        print_list( files(av[1]) )
    elif av[2] == 'modules':
        mod = modules(av[1])
        print_list( mod )
    elif av[2] == 'mod':
        mod = modules_in_dir(av[1])
        print_list( mod )
    elif av[2] == 'mods':
        mod = modules_from(av[1])
        print_list( mod )
    else:
        print(usage)
        exit(0)

    if ('-c' in options or '--check' in options) and mod:
        imports(mod)
        print("Все модули импортированы успешно", file=sys.stderr)

except DistNotFound as e:
    print(f"Пакет не найден / {e}", file=sys.stderr)
