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
44 """
45 Return plugin object if CLI option is activated and method exists
46
47 @param method: name of plugin's method we're calling
48 @type method: string
49
50 @returns: list of plugins with `method`
51
52 """
53 all_plugins = []
54 for plugin_obj in self.plugins:
55 plugin = plugin_obj()
56 plugin.configure(self.options, None)
57 if plugin.enabled:
58 if not hasattr(plugin, method):
59 plugin = None
60 else:
61 all_plugins.append(plugin)
62 return all_plugins
63
65 '''Set log level according to command-line options'''
66 if self.options.verbose:
67 self.log.setLevel(logging.INFO)
68 if self.options.debug:
69 self.log.setLevel(logging.DEBUG)
70
72 '''
73 Print doap as n3, rdf/xml, plain text or using serialization plugin
74
75 @param doap_xml: DOAP in RDF/XML serialization
76 @type doap_xml: text
77
78 @rtype: None
79 @return: Just displays DOAP
80
81 '''
82
83
84 plugins = self.get_plugin('serialize')
85 if len(plugins) == 0:
86 serializer = None
87 else:
88
89 serializer = plugins[0].serialize
90 if self.options.write:
91 filename = self.options.write
92 else:
93 filename = None
94 print_doap(doap_xml, serializer=serializer, filename=filename,
95 color=not self.options.no_color)
96
98 '''
99 Return active search plugin callable
100
101 @rtype: callable
102 @returns: A callable object that fetches for DOAP
103 '''
104 plugins = self.get_plugin('search')
105 if len(plugins) == 1:
106 return plugins[0].search
107
109 '''
110 Run doapfiend command
111
112 Find the active plugin that has a 'search' method and run it,
113 then output the DOAP with print_doap, using the active plugin
114 with a 'serializer' method.
115
116
117 @rtype: int
118 @returns: 0 success or 1 failure
119
120 '''
121 opt_parser = self.setup_opt_parser()
122 (self.options, remaining_args) = opt_parser.parse_args()
123 if remaining_args:
124 opt_parser.print_help()
125 self.set_log_level()
126
127 if self.options.doapfiend_version:
128 return doapfiend_version()
129
130 if self.options.no_color:
131 for this in COLOR:
132 COLOR[this] = '\x1b[0m'
133 search_func = self.get_search_plugin()
134 if search_func:
135 doap = search_func()
136 if doap:
137 if self.options.follow:
138
139
140 self.print_doap(doap)
141 return follow_homepages(doap)
142 elif self.options.show_links:
143 return show_links(doap)
144 else:
145 return self.print_doap(doap)
146 else:
147 opt_parser.print_help()
148 return 1
149
150
152 '''
153 Setup the optparser
154
155 @rtype: opt_parser.OptionParser
156 @return: Option parser
157
158 '''
159 usage = 'usage: %prog [options]'
160 opt_parser = optparse.OptionParser(usage=usage)
161 group_search = optparse.OptionGroup(opt_parser,
162 'Search options',
163 'Options for searching for DOAP')
164
165 opt_parser.add_option('--version', action='store_true',
166 dest='doapfiend_version', default=False,
167 help='Show doapfiend version and exit.')
168
169 opt_parser.add_option('-P', '--http-proxy', action='store',
170 dest='proxy', default=False,
171 help='Specify http proxy URL if you use one.')
172
173 group_output = optparse.OptionGroup(opt_parser,
174 'Output options',
175 'Choose these options to change default output behavior')
176
177 group_output.add_option('--debug', action='store_true',
178 dest= 'debug', default=False,
179 help='Show debugging information')
180
181 group_output.add_option('-f', '--follow-links', action='store_true',
182 dest='follow', default=False,
183 help='Search for and show additional DOAP.',
184 metavar='FILENAME')
185
186 group_output.add_option('-s', '--show-links', action='store_true',
187 dest='show_links', default=False,
188 help='Search for and show links to additional DOAP.',
189 metavar='FILENAME')
190
191 group_output.add_option('-w', '--write', action='store',
192 dest='write', default=False,
193 help='Write DOAP to a file instead of displaying it.',
194 metavar='FILENAME')
195
196 group_output.add_option('-b', '--brief', action='store_true',
197 dest='brief', default=False, help='Show less output.')
198
199 group_output.add_option('-C', '--no-color', action='store_true',
200 dest='no_color', default=False,
201 help="Don't use color in output")
202
203 group_output.add_option('-v', '--verbose', action='store_true',
204 dest='verbose', default=False, help="Show more output")
205
206
207 for plugcls in self.plugins:
208 plug = plugcls()
209 plug.add_options(opt_parser, group_output, group_search)
210 opt_parser.add_option_group(group_search)
211 opt_parser.add_option_group(group_output)
212 return opt_parser
213
214
216 '''Print doapfiend version'''
217 print VERSION
218
219
221 '''Let's do it.'''
222 my_doapfiend = DoapFiend()
223 return my_doapfiend.run()
224
225
226 if __name__ == '__main__':
227 sys.exit(main())
228