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 """General 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
25 from gentoolkit import CONFIG
27 class ModuleBase(object):
28 """E-app 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
39 self.saved_verbose = None
42 def print_help(self, with_description=True):
43 """Print description, usage and a detailed help message.
45 @type with_description: bool
46 @param with_description: if true, print module's __doc__ string
51 print(__doc__.strip())
55 for line in self.warning:
56 sys.stderr.write(pp.warn(line))
58 print(mod_usage(mod_name=self.module_name, arg=self.arg_spec, optional=self.arg_option))
60 print(pp.command("options"))
61 print(format_options( self.formatted_options ))
62 if self.formatted_args:
64 print(pp.command(self.arg_spec))
65 print(format_options(self.formatted_args))
68 def parse_module_options(self, module_opts):
69 """Parse module options and update self.options"""
71 opts = (x[0] for x in module_opts)
72 posargs = (x[1] for x in module_opts)
73 for opt, posarg in zip(opts, posargs):
74 if opt in ('-h', '--help'):
77 opt_name, opt_type, opt_setting = self.module_opts[opt]
78 if opt_type == 'boolean':
79 self.options[opt_name] = opt_setting
80 elif opt_type == 'int':
85 err = "Module option %s requires integer (got '%s')"
86 sys.stdout.write(pp.error(err % (opt,posarg)))
88 self.print_help(with_description=False)
90 self.options[opt_name] = val
92 def set_quiet(self, quiet):
93 """sets the class option["quiet"] and option["verbose"] accordingly"""
94 if quiet == self.options['quiet']:
96 if self.saved_verbose:
98 verbose = self.options['verbose']
99 self.options['verbose'] = self.saved_verbose
100 self.saved_verbose = verbose
102 self.saved_verbose = self.options['verbose']
103 self.options['verbose'] = False
104 self.options['quiet'] = quiet
107 def validate_query(self, query, depth=0):
108 """check that the query meets the modules TargetSpec
109 If not it attempts to reduce it to a valid TargetSpec
110 or prints the help message and exits
115 query = list(set(self.arg_options).intersection(query))
116 #print "reduced query =", query
117 query = self.validate_query(query, depth+1)
118 if isinstance(query, list):
120 if query not in self.arg_options:
123 "Error starting module. Incorrect or No TargetSpec specified!"
125 print("query = ", query)
131 def main_setup(self, input_args):
132 """Parse input and prepares the program"""
135 module_opts, queries = gnu_getopt(input_args, self.short_opts, self.long_opts)
136 except GetoptError as err:
137 sys.stderr.write(pp.error("Module %s" % err))
139 self.print_help(with_description=False)
141 self.parse_module_options(module_opts)
142 if self.need_queries and not queries:
148 # vim: set ts=4 sw=4 tw=79: