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 = search_func()
135 if doap:
136 if self.options.follow:
137
138
139 self.print_doap(doap)
140 return follow_homepages(doap)
141 elif self.options.show_links:
142 return show_links(doap)
143 else:
144 return self.print_doap(doap)
145 else:
146 opt_parser.print_help()
147 return 1
148
150 '''
151 Find all plugins that are enabled on the command-line and have a
152 `serialize` method. If none are enabled, default to plain text
153 '''
154 plugins = self.get_plugin('serialize')
155 if len(plugins) == 0:
156 self.serializer = None
157 else:
158
159 self.serializer = plugins[0].serialize
160
162 '''
163 Setup the optparser
164
165 @rtype: opt_parser.OptionParser
166 @return: Option parser
167
168 '''
169 usage = 'usage: %prog [options]'
170 opt_parser = optparse.OptionParser(usage=usage)
171 group_search = optparse.OptionGroup(opt_parser,
172 'Search options',
173 'Options for searching for DOAP')
174
175 opt_parser.add_option('--version', action='store_true',
176 dest='doapfiend_version', default=False,
177 help='Show doapfiend version and exit.')
178
179 opt_parser.add_option('-P', '--http-proxy', action='store',
180 dest='proxy', default=False,
181 help='Specify http proxy URL if you use one.')
182
183 group_output = optparse.OptionGroup(opt_parser,
184 'Output options',
185 'Choose these options to change default output behavior')
186
187 group_output.add_option('--debug', action='store_true',
188 dest= 'debug', default=False,
189 help='Show debugging information')
190
191 group_output.add_option('-f', '--follow-links', action='store_true',
192 dest='follow', default=False,
193 help='Search for and show additional DOAP.',
194 metavar='FILENAME')
195
196 group_output.add_option('-s', '--show-links', action='store_true',
197 dest='show_links', default=False,
198 help='Search for and show links to additional DOAP.',
199 metavar='FILENAME')
200
201 group_output.add_option('-w', '--write', action='store',
202 dest='write', default=False,
203 help='Write DOAP to a file instead of displaying it.',
204 metavar='FILENAME')
205
206 group_output.add_option('-C', '--no-color', action='store_true',
207 dest='no_color', default=False,
208 help="Don't use color in output")
209
210 group_output.add_option('-q', '--quiet', action='store_true',
211 dest='quiet', default=False, help="Show less output")
212
213 group_output.add_option('-v', '--verbose', action='store_true',
214 dest='verbose', default=False, help="Show more output")
215
216
217 for plugcls in self.plugins:
218 plug = plugcls()
219 plug.add_options(opt_parser, group_output, group_search)
220 opt_parser.add_option_group(group_search)
221 opt_parser.add_option_group(group_output)
222 return opt_parser
223
224
226 '''Print doapfiend version'''
227 print VERSION
228
229
231 '''Let's do it.'''
232 my_doapfiend = DoapFiend()
233 return my_doapfiend.run()
234
235
236 if __name__ == '__main__':
237 sys.exit(main())
238