From 2fd607d5f415788b1025e5aef5155c611c7d70bc Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Wed, 22 Mar 2017 15:49:45 +0100 Subject: [PATCH] Adding application properties 'simulate' and 'force' to pp_lib/app.py and pp_lib/mk_home_app.py --- pp_lib/app.py | 70 +++++++++++++++++++++++++++++++++++++++++-- pp_lib/mk_home_app.py | 40 ++----------------------- 2 files changed, 70 insertions(+), 40 deletions(-) diff --git a/pp_lib/app.py b/pp_lib/app.py index 2d756af..f63ed9c 100644 --- a/pp_lib/app.py +++ b/pp_lib/app.py @@ -33,7 +33,7 @@ from .colored import ColoredFormatter, colorstr from .obj import PpBaseObjectError, PpBaseObject -__version__ = '0.3.1' +__version__ = '0.3.2' LOG = logging.getLogger(__name__) @@ -102,6 +102,8 @@ class PpApplication(PpBaseObject): """ self._quiet = False + self._force = False + self._simulate = False self.env = {} """ @@ -233,6 +235,46 @@ class PpApplication(PpBaseObject): def quiet(self, value): self._quiet = bool(value) + # ----------------------------------------------------------- + @property + def force(self): + """Forced execution of the application.""" + return self._force + + @force.setter + def force(self, value): + self._force = bool(value) + + # ----------------------------------------------------------- + @property + def simulate(self): + """Simulation mode, nothing is really done.""" + return self._simulate + + @simulate.setter + def simulate(self, value): + self._simulate = bool(value) + + # ----------------------------------------------------------- + @property + def show_force_opt(self): + """Flag, whether the command line option '--force' should be shown.""" + return getattr(self, '_show_force_opt', False) + + @show_force_opt.setter + def show_force_opt(self, value): + self._show_force_opt = bool(value) + + # ----------------------------------------------------------- + @property + def show_simulate_opt(self): + """Flag, whether the command line option '--simulate' should be shown.""" + return getattr(self, '_show_simulate_opt', False) + + @show_simulate_opt.setter + def show_simulate_opt(self, value): + self._show_simulate_opt = bool(value) + # ------------------------------------------------------------------------- def exit(self, retval=-1, msg=None, trace=False): """ @@ -297,6 +339,8 @@ class PpApplication(PpBaseObject): res['exit_value'] = self.exit_value res['usage'] = self.usage res['quiet'] = self.quiet + res['force'] = self.force + res['simulate'] = self.simulate res['description'] = self.description res['argparse_epilog'] = self.argparse_epilog res['argparse_prefix_chars'] = self.argparse_prefix_chars @@ -392,8 +436,8 @@ class PpApplication(PpBaseObject): """ - if self.verbose > 1: - LOG.info("executing pre_run() ...") + if self.simulate: + LOG.warn("Simulation mode - nothing is really done.") # ------------------------------------------------------------------------- def _run(self): @@ -521,6 +565,20 @@ class PpApplication(PpBaseObject): help='Silent execution, only warnings and errors are emitted.', ) + if self.show_force_opt: + general_group.add_argument( + "-f", "--force", + action="store_true", dest="force", + help="Forced execution of this application", + ) + + if self.show_simulate_opt: + general_group.add_argument( + "-s", "--simulate", + action="store_true", dest="simulate", + help="Simulation af all actions, nothing is really done.", + ) + general_group.add_argument( "-h", "--help", action='help', @@ -581,6 +639,12 @@ class PpApplication(PpBaseObject): else: self._terminal_has_colors = self.terminal_can_color() + if getattr(self.args, 'force', False): + self.force = True + + if getattr(self.args, 'simulate', False): + self.simulate = True + # ------------------------------------------------------------------------- def perform_arg_parser(self): """ diff --git a/pp_lib/mk_home_app.py b/pp_lib/mk_home_app.py index b8cc9b3..ef1acce 100644 --- a/pp_lib/mk_home_app.py +++ b/pp_lib/mk_home_app.py @@ -40,7 +40,7 @@ from .merge import merge_structure from .ldap_app import PpLdapAppError, PpLdapApplication -__version__ = '0.4.3' +__version__ = '0.4.4' LOG = logging.getLogger(__name__) @@ -72,7 +72,7 @@ class PpMkHomeApp(PpLdapApplication): self.chroot_homedir = self.default_chroot_homedir self.home_root_abs = self.default_home_root self.home_root_rel = os.path.relpath(self.home_root_abs, os.sep) - self.simulate = False + self.show_simulate_opt = True self.user_entries = [] self.users = {} self.home_root_real = os.path.join(self.chroot_homedir, self.home_root_rel) @@ -93,38 +93,6 @@ class PpMkHomeApp(PpLdapApplication): self.initialized = True - # ------------------------------------------------------------------------- - def init_arg_parser(self): - """ - Method to initiate the argument parser. - - This method should be explicitely called by all init_arg_parser() - methods in descendant classes. - """ - - super(PpMkHomeApp, self).init_arg_parser() - - self.arg_parser.add_argument( - "-s", "--simulate", - action='store_true', - dest='simulate', - help="Simulation af all actions, nothing is really done." - ) - - # ------------------------------------------------------------------------- - def perform_arg_parser(self): - """ - Public available method to execute some actions after parsing - the command line parameters. - - Descendant classes may override this method. - """ - - super(PpMkHomeApp, self).perform_arg_parser() - - if self.args.simulate: - self.simulate = True - # ------------------------------------------------------------------------- def perform_config(self): @@ -203,7 +171,6 @@ class PpMkHomeApp(PpLdapApplication): """ - super(PpMkHomeApp, self).pre_run() if os.geteuid(): msg = "Only root may execute this application." LOG.error(msg) @@ -227,8 +194,7 @@ class PpMkHomeApp(PpLdapApplication): LOG.error(msg) self.exit(1) - if self.simulate: - LOG.warn("Simulation mode - nothing is really done.") + super(PpMkHomeApp, self).pre_run() # ------------------------------------------------------------------------- def _run(self): -- 2.39.5