import glob
import logging
import os
import plistlib
import subprocess
import zipfile

import click


logger = logging.getLogger(__name__)

project_root = os.path.realpath('.')
project_name = os.path.basename(project_root)
archive_path = os.path.join(project_root, project_name + '.alfredworkflow')


@click.group()
@click.option('-v', '--verbose', count=True)
def main(verbose):
    logging.basicConfig(level=(logging.WARNING - 10 * verbose))


@main.command()
def build():
    '''Build Alfred Extension'''
    logger.info('Reading %s', project_root)
    logger.info('Building %s', project_name)

    with zipfile.ZipFile(archive_path, 'a') as zfp:
        # git-ls-files will let us take advantage of .gitignore to ignore
        # files that we do not want to save in our extension
        for path in subprocess.check_output(['git', 'ls-files']).strip().split('\n'):
            logger.info('Adding %s', path)
            zfp.write(path, os.path.basename(path))

    click.echo('Built workflow ' + archive_path)


@main.command()
@click.pass_context
def install(ctx):
    '''Open Alfred Extension in Alfred'''
    ctx.forward(build)
    click.launch(archive_path)


@main.command()
def list():
    click.echo('Listing alfred extensions')

    sync_folder = subprocess.check_output([
        'defaults',
        'read',
        'com.runningwithcrayons.Alfred-Preferences',
        'syncfolder'
    ]).strip()
    logger.info('Sync Folder: %s', sync_folder)
    workflow_glob = os.path.join(
        os.path.expanduser(sync_folder),
        'Alfred.alfredpreferences',
        'workflows',
        '**',
        'info.plist',
    )
    logger.info('Search Glob: %s', workflow_glob)

    for path in glob.iglob(workflow_glob):
        plist = plistlib.readPlist(path)
        print 'Name:', plist.get('name')
        print 'Description', plist.get('description')
        print 'BundleID', plist.get('bundleid')
        print 'Category', plist.get('category')
        print 'Creator', plist.get('createdby')
        print 'URL', plist.get('webaddress')
        print 'Path', path
        print 


@main.command()
def info():
    import pprint
    plist = plistlib.readPlist('info.plist')
    print 'Name:', plist.get('name')
    print 'Description', plist.get('description')
    print 'BundleID', plist.get('bundleid')
    print 'Category', plist.get('category')
    print 'Creator', plist.get('createdby')
    print 'URL', plist.get('webaddress')
    pprint.pprint(plist)
    print 

@@ -1175,24 +1175,68 @@
 n.command()%0A
+@click.option('-f', '--full', is_flag=True)%0A
 def list():%0A
@@ -1232,16 +1232,20 @@
 ef list(
+full
 ):%0A    c
@@ -1282,16 +1282,41 @@
 nsions')
+%0A    click.echo('-' * 80)
 %0A%0A    sy
@@ -1844,33 +1844,24 @@
        print
- 'Name:',
  plist.get('
@@ -1862,24 +1862,25 @@
 .get('name')
+,
 %0A        pri
@@ -1879,35 +1879,25 @@
      print '
-Description
+%5B
 ', plist.get
@@ -1902,70 +1902,103 @@
 et('
-description')%0A        print 'BundleID', plist.get('bundleid')%0A
+bundleid'), '%5D'%0A        if full:%0A            print 'Description', plist.get('description')%0A    
     
@@ -2041,32 +2041,36 @@
 egory')%0A        
+   
+ 
 print 'Creator',
@@ -2097,24 +2097,28 @@
 y')%0A        
+    
 print 'URL',
@@ -2146,24 +2146,28 @@
 s')%0A        
+  
+  
 print 'Path'
@@ -2173,16 +2173,20 @@
 ', path%0A
+    
         

Add --full option to support a more condensed output