1 # Copyright(c) 2009, Gentoo Foundation
3 # Copyright 2010 Brian Dolbec <brian.dolbec@gmail.com>
4 # Copyright(c) 2010, Gentoo Foundation
5 # Distributed under the terms of the GNU General Public License v2
9 """Analyse Base Module class to hold common module operation functions
12 from __future__ import print_function
14 __docformat__ = 'epytext'
20 from getopt import gnu_getopt, GetoptError
22 import gentoolkit.pprinter as pp
23 from gentoolkit.formatters import format_options
24 from gentoolkit.base import mod_usage
27 class ModuleBase(object):
28 """Analyse base module class to parse module options print module help, etc.."""
31 self.module_name = None
33 self.formatted_options = None
34 self.short_opts = None
38 self.need_queries = True
41 def print_help(self, with_description=True):
42 """Print description, usage and a detailed help message.
44 @type with_description: bool
45 @param with_description: if true, print module's __doc__ string
50 print(__doc__.strip())
54 for line in self.warning:
55 sys.stderr.write(pp.warn(line))
57 print(mod_usage(mod_name=self.module_name, arg=self.arg_spec, optional=self.arg_option))
59 print(pp.command("options"))
60 print(format_options( self.formatted_options ))
61 if self.formatted_args:
63 print(pp.command(self.arg_spec))
64 print(format_options(self.formatted_args))
67 def parse_module_options(self, module_opts):
68 """Parse module options and update self.options"""
70 opts = (x[0] for x in module_opts)
71 posargs = (x[1] for x in module_opts)
72 for opt, posarg in zip(opts, posargs):
73 if opt in ('-h', '--help'):
76 opt_name, opt_type, opt_setting = self.module_opts[opt]
77 if opt_type == 'boolean':
78 self.options[opt_name] = opt_setting
79 elif opt_type == 'int':
84 err = "Module option %s requires integer (got '%s')"
85 sys.stdout.write(pp.error(err % (opt,posarg)))
87 self.print_help(with_description=False)
89 self.options[opt_name] = val
90 if self.options['quiet']:
91 self.options['verbose'] = False
93 def validate_query(self, query, depth=0):
94 """check that the query meets the modules TargetSpec
95 If not it attempts to reduce it to a valid TargetSpec
96 or prints the help message and exits
101 query = list(set(self.arg_options).intersection(query))
102 #print "reduced query =", query
103 query = self.validate_query(query, depth+1)
104 if isinstance(query, list):
106 if query not in self.arg_options:
109 "Error starting module. Incorrect or No TargetSpec specified!"
111 print("query = ", query)
117 def main_setup(self, input_args):
118 """Parse input and prepares the program"""
121 module_opts, queries = gnu_getopt(input_args, self.short_opts, self.long_opts)
122 except GetoptError as err:
123 sys.stderr.write(pp.error("Module %s" % err))
125 self.print_help(with_description=False)
127 self.parse_module_options(module_opts)
128 if self.need_queries and not queries:
134 # vim: set ts=4 sw=4 tw=79: