import click
import pathlib
import prettytable
import toleo
import xdg.BaseDirectory
import yaml


class SoftwareTable(prettytable.PrettyTable):
    def add_software_row(self, pkg, src):
        if src.version > pkg.version:
            status = 'UPDATE'
        else:
            status = 'OK'
        self.add_row([pkg.name, src.name,
                      pkg.version, src.version,
                      status])


def read_config(collection='default'):
    xdg_config_home = pathlib.Path(xdg.BaseDirectory.xdg_config_home)
    config_dir = xdg_config_home / 'toleo'
    config_file = (config_dir / collection).with_suffix('.yaml')
    if config_file.is_file():
        with config_file.open() as f:
            return yaml.load(f)
    else:
        raise FileNotFoundError('cannot read {}'.format(config_file))


@click.command()
@click.option('--collection', '-c', default='default')
# @click.option('--upstream-only', '-u', 'action', flag_value='upstream')
# @click.option('--repo-only', '-r', 'action', flag_value='repo')
# @click.option('--verbose', '-v', count=True)
# @click.option('--path-override', envvar='TOLEO_CONFIG_HOME')
# @click.option('--limit', '-l')
def cli(collection):
    ''' Entry point for application. '''
    table = SoftwareTable(['PACKAGE', 'PROJECT', 'AUR', 'UPSTREAM', 'STATUS'])

    for name in ['click', 'docker-py', 'pluginbase']:
        aur = toleo.AurPackage('python2-' + name.rstrip('-py'), upstream=name)
        pypi = toleo.PypiSoftware(name)
        table.add_software_row(aur, pypi)

    table.add_software_row(toleo.AurPackage('supernova'),
                           toleo.PypiSoftware('supernova'))

    mint_x_icons_pkg = toleo.AurPackage('mint-x-icons')
    mint_x_icons_src = toleo.GenericSoftware('mint-x-icons',
        url='http://packages.linuxmint.com/pool/main/m/mint-x-icons/')
    table.add_software_row(mint_x_icons_pkg, mint_x_icons_src)

    mint_x_theme_pkg = toleo.AurPackage('mint-x-theme', upstream='mint-themes')
    mint_x_theme_src = toleo.GenericSoftware('mint-themes',
        url='http://packages.linuxmint.com/pool/main/m/mint-themes/')
    table.add_software_row(mint_x_theme_pkg, mint_x_theme_src)

    autokey_data_xdg_pkg = toleo.AurPackage('autokey-data-xdg', upstream='autokey')
    autokey_data_xdg_src = toleo.GenericSoftware('autokey',
        url='https://code.google.com/p/autokey/downloads/list')
    table.add_software_row(autokey_data_xdg_pkg, autokey_data_xdg_src)

    print(table)

@@ -886,291 +886,8 @@
 t')%0A
-# @click.option('--upstream-only', '-u', 'action', flag_value='upstream')%0A# @click.option('--repo-only', '-r', 'action', flag_value='repo')%0A# @click.option('--verbose', '-v', count=True)%0A# @click.option('--path-override', envvar='TOLEO_CONFIG_HOME')%0A# @click.option('--limit', '-l')%0A
 def 
@@ -976,14 +976,11 @@
 e(%5B'
-PACKAG
+NAM
 E', 
@@ -995,1176 +995,34 @@
 ', '
-AUR', 'UPSTREAM', 'STATUS'%5D)%0A%0A    for name in %5B'click', 'docker-py', 'pluginbase'%5D:%0A        aur = toleo.AurPackage('python2-' + name.rstrip('-py'), upstream=name)%0A        pypi = toleo.PypiSoftware(name)%0A        table.add_software_row(aur, pypi)%0A%0A    table.add_software_row(toleo.AurPackage('supernova'),%0A                           toleo.PypiSoftware('supernova'))%0A%0A    mint_x_icons_pkg = toleo.AurPackage('mint-x-icons')%0A    mint_x_icons_src = toleo.GenericSoftware('mint-x-icons',%0A        url='http://packages.linuxmint.com/pool/main/m/mint-x-icons/')%0A    table.add_software_row(mint_x_icons_pkg, mint_x_icons_src)%0A%0A    mint_x_theme_pkg = toleo.AurPackage('mint-x-theme', upstream='mint-themes')%0A    mint_x_theme_src = toleo.GenericSoftware('mint-themes',%0A        url='http://packages.linuxmint.com/pool/main/m/mint-themes/')%0A    table.add_software_row(mint_x_theme_pkg, mint_x_theme_src)%0A%0A    autokey_data_xdg_pkg = toleo.AurPackage('autokey-data-xdg', upstream='autokey')%0A    autokey_data_xdg_src = toleo.GenericSoftware('autokey',%0A        url='https://code.google.com/p/autokey/downloads/list')%0A    table.add_software_row(autokey_data_xdg_pkg, autokey_data_xdg_src
+REPO', 'SOURCE', 'STATUS'%5D
 )%0A%0A 

remove legacy code