2 from pkgutil
import walk_packages
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']
17 class Command(object):
23 self
.parser
= optparse
.OptionParser(
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':
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
)
41 @ Parses common options to initialize colly's core(.py) object
44 class CsvCommand(Command
):
45 usage
= "%prog FILE(S) [OPTIONS]"
49 super(CsvCommand
, self
).__init
__()
51 self
.parser
.add_option('-H', '--headings',
54 callback
=split_callback
,
57 help='Comma separated headings for CSV columns')
59 self
.parser
.add_option('-i', '--index',
62 callback
=split_callback
,
65 help='Define index column on CSV, shortcut to using -H pk,etc.')
69 self.parser.add_option('-z', '--lazy',
72 help='Will make assumptions rather than throw errors')
74 self.parser.add_option('-i', '--index',
77 help='Column to set as the index (pk)')
79 self.parser.add_option('-f', '--format',
82 help='Output format [default: JSON]')
84 self.parser.add_option('-c', '--compress',
87 help='Column to set as the index (pk)')
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')
102 kwargs
['headings'] = options
.headings
[n
]
107 kwargs
['headings'] = [] # leave out index for now
113 self
.objects
.append(Collate(csv_file
, **kwargs
))
116 self
.run(options
, args
)
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
:
132 __import__(full_name
)
136 def load_all_commands():
137 for name
in command_names():
141 names
= set((pkg
[1] for pkg
in walk_packages(path
=__path__
)))