"""This script handles fred files

"If in doubt, name it Fred"
"""


from __future__ import print_function
import os
import sys
import argparse
from bdb import BdbQuit

import dotsite as site


__version__ = '0.1.0'


class ScriptError(NotImplementedError):
    pass


def run_args(args, methods):
    """Run any methods eponymous with args"""
    if not args:
        return False
    valuable_args = {k for k, v in args.__dict__.items() if v}
    arg_methods = {methods[a] for a in valuable_args if a in methods}
    for method in arg_methods:
        method(args)


def version(_):
    print('%s %s' % (os.path.basename(sys.argv[0]), __version__))
    raise SystemExit


def Use_debugger(_args):
    try:
        import pudb as pdb
    except ImportError:
        import pdb
    pdb.set_trace()


def parse_args(methods):
    """Parse out command line arguments"""
    parser = argparse.ArgumentParser(description=__doc__.splitlines()[0])
    parser.add_argument('directories', metavar='items', type=str, nargs='*',
                        help='Only look for fred files in these directories')
    parser.add_argument('-d', '--debug', action='store_true',
                        help='Debug the first fred.py with pudb')
    parser.add_argument('-e', '--edit', action='store_true',
                        help='Edit the freds with vim')
    parser.add_argument('-l', '--list', action='store_true',
                        help='Use long listing')
    parser.add_argument('-r', '--remove', action='store_true',
                        help='Remove the freds')
    parser.add_argument('-p', '--python', action='store_true',
                        help='Run the first fred.py script')
    parser.add_argument('-s', '--shell', action='store_true',
                        help='Run the first fred.sh script')
    parser.add_argument('-v', '--version', action='store_true',
                        help='Show version')
    parser.add_argument('-U', '--Use_debugger', action='store_true',
                        help='Run this script with pdb (or pudb if available)')
    args = parser.parse_args()
    run_args(args, methods)
    return args


def dirs():
    return ('.', '~/tmp', '/tmp')


def freds():
    exts = ('', '.py', '.sh', '.txt', '.now')
    return [str('%s/fred%s' % (d,e)) for d in dirs() for e in exts]


def paths():
    return [site.paths.path(_) for _ in freds()]


def files():
    return [_ for _ in paths() if _.isfile()]


def substantial():
    return set(_ for _ in files() if _.size)


def insubstantial():
    return [_ for _ in files() if not _.size]


def purge_insubstantial():
    [_.remove() for _ in insubstantial()]


def as_path(fred):
    string = str(fred)
    if not ' ' in string:
        return string
    return '"%s"' % string


def as_paths(freds):
    return [as_path(_) for _ in freds]


def script(args):
    purge_insubstantial()
    command = 'ls'
    if args.directories:
        global dirs
        dirs = lambda : args.directories
    freds = substantial()
    if args.debug:
        freds = [_ for _ in freds if _.ext == '.py'][:1]
        if not freds:
            raise ScriptError('No pythonic freds found')
        command = 'pudb'
    elif args.list:
        command = 'ls -l'
    elif args.edit:
        command = 'vim -p'
    elif args.remove:
        command = 'rm -vrf'
    elif args.python:
        freds = [_ for _ in freds if _.ext == '.py'][:1]
        if not freds:
            raise ScriptError('No pythonic freds found')
        command = 'mython'
    elif args.shell:
        freds = [_ for _ in freds if _.ext == '.sh'][:1]
        if not freds:
            raise ScriptError('No bashable freds found')
        command = 'bash -x'
    else:
        print(' '.join(freds))
        return True
    if not freds:
        raise ScriptError('No freds found')
    print('%s %s' % (command, ' '.join(as_paths(freds))))
    return True


def main():
    """Run the script"""
    try:
        args = parse_args(globals())
        return os.EX_OK if script(args) else not os.EX_OK
    except BdbQuit:
        pass
    except SystemExit as e:
        return e.code
    except ScriptError as e:
        print(e, file=sys.stderr)
        return not os.EX_OK
    except Exception, e:  # pylint: disable=broad-except
        if __version__[0] < '1':
            raise
        print(e, file=sys.stderr)
        return not os.EX_OK
    return os.EX_OK


if __name__ == '__main__':
    sys.exit(main())

@@ -2279,16 +2279,17 @@
 s' %25 (d,
+ 
 e)) for 
@@ -2701,15 +2701,15 @@
  if 
-not ' '
+' ' not
  in 
@@ -2780,20 +2780,22 @@
 s_paths(
-fred
+string
 s):%0A    
@@ -2822,20 +2822,22 @@
 or _ in 
-fred
+string
 s%5D%0A%0A%0Adef
@@ -2940,16 +2940,60 @@
 bal dirs
+ # pylint: disable=global-variable-undefined
 %0A       
@@ -3006,17 +3006,16 @@
 = lambda
- 
 : args.d
@@ -3029,24 +3029,29 @@
 ies%0A    fred
+_file
 s = substant
@@ -3079,32 +3079,37 @@
 ug:%0A        fred
+_file
 s = %5B_ for _ in 
@@ -3104,32 +3104,37 @@
 %5B_ for _ in fred
+_file
 s if _.ext == '.
@@ -3153,32 +3153,37 @@
      if not fred
+_file
 s:%0A            r
@@ -3420,32 +3420,37 @@
 on:%0A        fred
+_file
 s = %5B_ for _ in 
@@ -3445,32 +3445,37 @@
 %5B_ for _ in fred
+_file
 s if _.ext == '.
@@ -3494,32 +3494,37 @@
      if not fred
+_file
 s:%0A            r
@@ -3627,16 +3627,21 @@
     fred
+_file
 s = %5B_ f
@@ -3652,16 +3652,21 @@
  in fred
+_file
 s if _.e
@@ -3693,32 +3693,37 @@
      if not fred
+_file
 s:%0A            r
@@ -3831,16 +3831,21 @@
 oin(fred
+_file
 s))%0A    
@@ -3875,16 +3875,21 @@
 not fred
+_file
 s:%0A     
@@ -3979,16 +3979,21 @@
 ths(fred
+_file
 s))))%0A  

Remove pylint warnings