Major; refactored to use CsvCommand for initializing collate objects.
[colly.git] / colly / commands / __init__.py
bloba1431f276a6c7a44ecbc7d2527243da18129b24c
1 import sys
2 from pkgutil import walk_packages
3 import logging
4 import optparse
6 from colly.optbase import parser
7 from colly.core import Collate
9 __all__ = ['command_dict', 'Command', 'load_command',
10 'load_all_commands', 'command_names']
12 command_dict = {}
14 ''' Base command
15 '''
17 class Command(object):
18 name = None
19 usage = None
21 def __init__(self):
22 assert self.name
23 self.parser = optparse.OptionParser(
24 usage=self.usage,
25 prog='%s %s' % ("colly", self.name),
26 version=parser.version)
27 for option in parser.option_list:
28 if not option.dest or option.dest == 'help':
29 # -h, --version, etc
30 continue
31 self.parser.add_option(option)
32 command_dict[self.name] = self
34 def main(self, initial_options, args):
35 options, args = self.parser.parse_args(args)
36 self.run(options, args)
39 ''' CSV base command
41 @ Parses common options to initialize colly's core(.py) object
42 '''
44 class CsvCommand(Command):
45 usage = "%prog FILE(S) [OPTIONS]"
46 objects = []
48 def __init__(self):
49 super(CsvCommand, self).__init__()
51 self.parser.add_option('-H', '--headings',
52 dest='headings',
53 action='callback',
54 callback=split_callback,
55 default=[],
56 type='str',
57 help='Comma separated headings for CSV columns')
59 self.parser.add_option('-i', '--index',
60 dest='index',
61 action='callback',
62 callback=split_callback,
63 default=[],
64 type='str',
65 help='Define index column on CSV, shortcut to using -H pk,etc.')
67 '''
68 TODO:
69 self.parser.add_option('-z', '--lazy',
70 dest='lazy',
71 action='store_true',
72 help='Will make assumptions rather than throw errors')
74 self.parser.add_option('-i', '--index',
75 dest='pk',
76 action='store',
77 help='Column to set as the index (pk)')
79 self.parser.add_option('-f', '--format',
80 dest='format',
81 action='store',
82 help='Output format [default: JSON]')
84 self.parser.add_option('-c', '--compress',
85 dest='compress',
86 action='store_true',
87 help='Column to set as the index (pk)')
88 '''
90 def main(self, initial_options, args): #:! overwrites Command.main
91 options, args = self.parser.parse_args(args)
93 if options.headings and options.index:
94 self.parser.error('Options HEADINGS and INDEX are mutually exclusive')
96 n = 0
97 for csv_file in args:
98 kwargs = {}
100 if options.headings:
101 try:
102 kwargs['headings'] = options.headings[n]
103 except IndexError:
104 break
105 elif options.index:
106 try:
107 kwargs['headings'] = [] # leave out index for now
108 except IndexError:
109 break
110 else:
111 pass
113 self.objects.append(Collate(csv_file, **kwargs))
114 n += 1
116 self.run(options, args)
118 # Utils
120 def split_callback(option, opt, value, parser):
121 ''' Callback, extra option parsing
123 value_list = map(lambda v: v.split(','), value.split(':'))
124 setattr(parser.values, option.dest, value_list)
127 def load_command(name):
128 full_name = 'colly.commands.%s' % name
129 if full_name in sys.modules:
130 return
131 try:
132 __import__(full_name)
133 except ImportError:
134 pass
136 def load_all_commands():
137 for name in command_names():
138 load_command(name)
140 def command_names():
141 names = set((pkg[1] for pkg in walk_packages(path=__path__)))
142 return list(names)