#!/usr/bin/env python

import argparse
import os
import locale

from pyp2rpmlib import Convertor
from pyp2rpmlib import settings

def main():
    parser = argparse.ArgumentParser(description = 'Convert PyPI package to RPM specfile.')
    parser.add_argument('-n',
                        required = False,
                        help = 'Name of the package on PyPI (ignored for local files).',
                        metavar = 'PYPI_NAME')
    parser.add_argument('-v',
                        required = False,
                        help = 'Version of the package to download (ignored for local files).',
                        metavar = 'VERSION')
    parser.add_argument('-m',
                        required = False,
                        help = 'Where to get metadata from ("pypi" or "local", default: "%s").' % settings.DEFAULT_METADATA_SOURCE,
                        metavar = 'METADATA_SOURCE',
                        choices = ['pypi', 'local'],
                        default = settings.DEFAULT_METADATA_SOURCE)
    parser.add_argument('-s',
                        required = False,
                        help = 'Where to get package from ("pypi" or "/full/path/to/local/file", default: "%s").' % settings.DEFAULT_PKG_SOURCE,
                        metavar = 'PACKAGE_SOURCE',
                        default = settings.DEFAULT_PKG_SOURCE)
    parser.add_argument('-d',
                        required = False,
                        help = 'Where to save the package file (default: "%s")' % settings.DEFAULT_PKG_SAVE_PATH,
                        metavar = 'SAVE_DIR',
                        default = settings.DEFAULT_PKG_SAVE_PATH)
    parser.add_argument('-t',
                        required = False,
                        help = 'Template file (jinja2 format) to render (default: "%s"). Search order is 1) filesystem, 2) default templates.' % settings.DEFAULT_TEMPLATE,
                        metavar = 'TEMPLATE',
                        default = settings.DEFAULT_TEMPLATE)
    parser.add_argument('-b',
                        required = False,
                        help = 'Base Python version to package for (default: "%s").' % settings.DEFAULT_PYTHON_VERSION,
                        metavar = 'BASE_PYTHON',
                        default = settings.DEFAULT_PYTHON_VERSION)
    parser.add_argument('-p',
                        required = False,
                        help = 'Additional Python versions to include in the specfile (e.g -p3 for %%{?with_python3}). Can be specified multiple times.',
                        metavar = 'PYTHON_VERSION',
                        default = [],
                        action = 'append')


    ns = parser.parse_args()
    if ns.__dict__['n'] == None and not os.path.exists(ns.__dict__['s']):
        parser.error('You must specify name of the package (-n) or full path (-s).')

    convertor = Convertor(name = ns.__dict__['n'],
                          version = ns.__dict__['v'],
                          metadata_from = ns.__dict__['m'],
                          source_from = ns.__dict__['s'],
                          save_dir = ns.__dict__['d'],
                          template = ns.__dict__['t'],
                          base_python_version = ns.__dict__['b'],
                          python_versions = ns.__dict__['p'])

    converted = convertor.convert()

    if isinstance(converted, str): # python 3
        print(converted)
    else: # python 2
        print(converted.encode(locale.getpreferredencoding()))

@@ -2007,16 +2007,434 @@
 MPLATE)%0A
+    parser.add_argument('-o',%0A                        required = False,%0A                        help = 'Default distro whose conversion rules to use (default: %22%25s%22). Default templates have their rules associated and ignore this.' %25 settings.DEFAULT_DISTRO,%0A                        metavar = 'DISTRO',%0A                        default = settings.DEFAULT_DISTRO,%0A                        choices = settings.KNOWN_DISTROS)%0A
     pars
@@ -2455,16 +2455,16 @@
 t('-b',%0A
-
         
@@ -3283,16 +3283,130 @@
 -s).')%0A%0A
+    distro = ns.__dict__%5B'o'%5D%0A    if ns.__dict%5B't'%5D in settings.KNOWN_DISTROS:%0A        distro = ns.__dict__%5B't'%5D%0A%0A
     conv
@@ -3671,24 +3671,24 @@
 ict__%5B'd'%5D,%0A
-
             
@@ -3726,24 +3726,67 @@
 ict__%5B't'%5D,%0A
+                          distro = distro,%0A
             

Add an option to specify the distro rules for conversion