1
2
3 '''
4
5 cli.py
6 ======
7
8 Command-line tool for querying, serializing and displaying DOAP
9
10 Author: Rob Cakebread <rob@doapspace.org>
11
12 License : BSD-2
13
14 '''
15
16 __docformat__ = 'epytext'
17 __revision__ = '$Revision: $'[11:-1].strip()
18
19
20 import sys
21 import logging
22 import optparse
23
24 from doapfiend.plugins import load_plugins
25 from doapfiend.utils import COLOR
26 from doapfiend.__init__ import __version__ as VERSION
27 from doapfiend.doaplib import print_doap, follow_homepages, show_links
28
29
31
32 '''`DoapFiend` class'''
33
35 '''Initialize attributes, set logger'''
36 self.doap = None
37 self.options = None
38 self.log = logging.getLogger('doapfiend')
39 self.log.addHandler(logging.StreamHandler())
40
41 self.plugins = list(load_plugins(others=True))
42 self.serializer = None
43
45 """
46 Return plugin object if CLI option is activated and method exists
47
48 @param method: name of plugin's method we're calling
49 @type method: string
50
51 @returns: list of plugins with `method`
52
53 """
54 all_plugins = []
55 for plugin_obj in self.plugins:
56 plugin = plugin_obj()
57 plugin.configure(self.options, None)
58 if plugin.enabled:
59 if not hasattr(plugin, method):
60 plugin = None
61 else:
62 all_plugins.append(plugin)
63 return all_plugins
64
66 '''Set log level according to command-line options'''
67 if self.options.verbose:
68 self.log.setLevel(logging.INFO)
69 elif self.options.quiet:
70 self.log.setLevel(logging.ERROR)
71 elif self.options.debug:
72 self.log.setLevel(logging.DEBUG)
73 else:
74 self.log.setLevel(logging.WARN)
75
77 '''
78 Print doap as n3, rdf/xml, plain text or using serialization plugin
79
80 @param doap_xml: DOAP in RDF/XML serialization
81 @type doap_xml: text
82
83 @rtype: None
84 @return: Just displays DOAP
85
86 '''
87 if self.options.write:
88 filename = self.options.write
89 else:
90 filename = None
91 print_doap(doap_xml, serializer=self.serializer, filename=filename,
92 color=not self.options.no_color)
93
95 '''
96 Return active search plugin callable
97
98 @rtype: callable
99 @returns: A callable object that fetches for DOAP
100 '''
101 plugins = self.get_plugin('search')
102 if len(plugins) == 1:
103 return plugins[0].search
104
106 '''
107 Run doapfiend command
108
109 Find the active plugin that has a 'search' method and run it,
110 then output the DOAP with print_doap, using the active plugin
111 with a 'serializer' method.
112
113
114 @rtype: int
115 @returns: 0 success or 1 failure
116
117 '''
118 opt_parser = self.setup_opt_parser()
119 (self.options, remaining_args) = opt_parser.parse_args()
120 self.set_serializer()
121 if not self.serializer and remaining_args:
122 opt_parser.print_help()
123 return 1
124 self.set_log_level()
125
126 if self.options.doapfiend_version:
127 return doapfiend_version()
128
129 if self.options.no_color:
130 for this in COLOR:
131 COLOR[this] = '\x1b[0m'
132 search_func = self.get_search_plugin()
133 if search_func:
134 doap_xml = search_func()
135 if doap_xml:
136 if self.options.follow:
137
138
139
140
141
142
143 self.print_doap(doap_xml)
144 return follow_homepages(doap_xml)
145 elif self.options.show_links:
146 return show_links(doap_xml)
147 else:
148 return self.print_doap(doap_xml)
149 else:
150 opt_parser.print_help()
151 return 1
152
154 '''
155 Find all plugins that are enabled on the command-line and have a
156 `serialize` method. If none are enabled, default to plain text
157 '''
158 plugins = self.get_plugin('serialize')
159 if len(plugins) == 0:
160 self.serializer = None
161 else:
162
163 self.serializer = plugins[0].serialize
164
166 '''
167 Setup the optparser
168
169 @rtype: opt_parser.OptionParser
170 @return: Option parser
171
172 '''
173 usage = 'usage: %prog [options]'
174 opt_parser = optparse.OptionParser(usage=usage)
175 group_search = optparse.OptionGroup(opt_parser,
176 'Search options',
177 'Options for searching for DOAP')
178
179 opt_parser.add_option('--version', action='store_true',
180 dest='doapfiend_version', default=False,
181 help='Show doapfiend version and exit.')
182
183 opt_parser.add_option('-P', '--http-proxy', action='store',
184 dest='proxy', default=False,
185 help='Specify http proxy URL if you use one.')
186
187 group_output = optparse.OptionGroup(opt_parser,
188 'Output options',
189 'Choose these options to change default output behavior')
190
191 group_output.add_option('--debug', action='store_true',
192 dest= 'debug', default=False,
193 help='Show debugging information')
194
195 group_output.add_option('-f', '--follow-links', action='store_true',
196 dest='follow', default=False,
197 help='Search for and show additional DOAP.',
198 metavar='FILENAME')
199
200 group_output.add_option('-s', '--show-links', action='store_true',
201 dest='show_links', default=False,
202 help='Search for and show links to additional DOAP.',
203 metavar='FILENAME')
204
205 group_output.add_option('-w', '--write', action='store',
206 dest='write', default=False,
207 help='Write DOAP to a file instead of displaying it.',
208 metavar='FILENAME')
209
210 group_output.add_option('-C', '--no-color', action='store_true',
211 dest='no_color', default=False,
212 help="Don't use color in output")
213
214 group_output.add_option('-q', '--quiet', action='store_true',
215 dest='quiet', default=False, help="Show less output")
216
217 group_output.add_option('-v', '--verbose', action='store_true',
218 dest='verbose', default=False, help="Show more output")
219
220
221 for plugcls in self.plugins:
222 plug = plugcls()
223 plug.add_options(opt_parser, group_output, group_search)
224 opt_parser.add_option_group(group_search)
225 opt_parser.add_option_group(group_output)
226 return opt_parser
227
228
230 '''Print doapfiend version'''
231 print VERSION
232
233
235 '''Let's do it.'''
236 my_doapfiend = DoapFiend()
237 return my_doapfiend.run()
238
239
240 if __name__ == '__main__':
241 sys.exit(main())
242