From 6879140db790a23a8782f7200400f2b58a69f6a0 Mon Sep 17 00:00:00 2001 From: Jason Gerard DeRose Date: Thu, 30 Oct 2008 02:20:28 -0600 Subject: Added ipalib.plugins.f_misc with new 'context' Command; moved 'env' Command from cli to f_misc --- ipalib/plugins/f_misc.py | 73 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 ipalib/plugins/f_misc.py (limited to 'ipalib/plugins/f_misc.py') diff --git a/ipalib/plugins/f_misc.py b/ipalib/plugins/f_misc.py new file mode 100644 index 00000000..ff8569b1 --- /dev/null +++ b/ipalib/plugins/f_misc.py @@ -0,0 +1,73 @@ +# Authors: +# Jason Gerard DeRose +# +# Copyright (C) 2008 Red Hat +# see file 'COPYING' for use and warranty information +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License as +# published by the Free Software Foundation; version 2 only +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +""" +Misc frontend plugins. +""" + +from ipalib import api, Command, Param, Bool + + +class env_and_context(Command): + """ + Base class for `env` and `context` commands. + """ + + def run(self, **kw): + if kw.get('server', False) and not self.api.env.in_server: + return self.forward() + return self.execute() + + def output_for_cli(self, ret): + for (key, value) in ret: + print '%s = %r' % (key, value) + + +class env(env_and_context): + """Show environment variables""" + + takes_options = ( + Param('server?', type=Bool(), default=False, + doc='Show environment variables of server', + ), + ) + + def execute(self): + return tuple( + (key, self.api.env[key]) for key in self.api.env + ) + +api.register(env) + + +class context(env_and_context): + """Show request context""" + + takes_options = ( + Param('server?', type=Bool(), default=False, + doc='Show request context in server', + ), + ) + + def execute(self): + return [ + (key, self.api.context[key]) for key in self.api.Context + ] + +api.register(context) -- cgit From 014af24731ff39520a9635694ed99dc9d09669c9 Mon Sep 17 00:00:00 2001 From: Jason Gerard DeRose Date: Wed, 12 Nov 2008 00:46:04 -0700 Subject: Changed calling signature of output_for_cli(); started work on 'textui' backend plugin --- ipalib/plugins/f_misc.py | 48 +++++++++++++++--------------------------------- 1 file changed, 15 insertions(+), 33 deletions(-) (limited to 'ipalib/plugins/f_misc.py') diff --git a/ipalib/plugins/f_misc.py b/ipalib/plugins/f_misc.py index ff8569b1..055e54d7 100644 --- a/ipalib/plugins/f_misc.py +++ b/ipalib/plugins/f_misc.py @@ -24,22 +24,11 @@ Misc frontend plugins. from ipalib import api, Command, Param, Bool -class env_and_context(Command): - """ - Base class for `env` and `context` commands. - """ - - def run(self, **kw): - if kw.get('server', False) and not self.api.env.in_server: - return self.forward() - return self.execute() - - def output_for_cli(self, ret): - for (key, value) in ret: - print '%s = %r' % (key, value) - - -class env(env_and_context): +# FIXME: We should not let env return anything in_server +# when mode == 'production'. This would allow an attacker to see the +# configuration of the server, potentially revealing compromising +# information. However, it's damn handy for testing/debugging. +class env(Command): """Show environment variables""" takes_options = ( @@ -48,26 +37,19 @@ class env(env_and_context): ), ) + def run(self, **kw): + if kw.get('server', False) and not self.api.env.in_server: + return self.forward() + return self.execute() + def execute(self): return tuple( (key, self.api.env[key]) for key in self.api.env ) -api.register(env) - - -class context(env_and_context): - """Show request context""" - - takes_options = ( - Param('server?', type=Bool(), default=False, - doc='Show request context in server', - ), - ) - - def execute(self): - return [ - (key, self.api.context[key]) for key in self.api.Context - ] + def output_for_cli(self, textui, result, **kw): + textui.print_name(self.name) + textui.print_keyval(result) + textui.print_count(result, '%d variable', '%d variables') -api.register(context) +api.register(env) -- cgit From 09161e399a61e2a548e9efb3c3abb2c7b47d5520 Mon Sep 17 00:00:00 2001 From: Jason Gerard DeRose Date: Wed, 12 Nov 2008 01:47:37 -0700 Subject: Command.get_default() will now fill-in None for all missing non-required params --- ipalib/plugins/f_misc.py | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) (limited to 'ipalib/plugins/f_misc.py') diff --git a/ipalib/plugins/f_misc.py b/ipalib/plugins/f_misc.py index 055e54d7..1acf1c99 100644 --- a/ipalib/plugins/f_misc.py +++ b/ipalib/plugins/f_misc.py @@ -31,23 +31,34 @@ from ipalib import api, Command, Param, Bool class env(Command): """Show environment variables""" + takes_args = ('variables*',) + takes_options = ( Param('server?', type=Bool(), default=False, doc='Show environment variables of server', ), ) - def run(self, **kw): - if kw.get('server', False) and not self.api.env.in_server: - return self.forward() - return self.execute() + def run(self, variables, **kw): + if kw['server'] and not self.env.in_server: + return self.forward(variables) + return self.execute(variables) + + def find_keys(self, variables): + for key in variables: + if key in self.env: + yield (key, self.env[key]) - def execute(self): - return tuple( - (key, self.api.env[key]) for key in self.api.env - ) + def execute(self, variables): + if variables is None: + return tuple( + (key, self.env[key]) for key in self.env + ) + return tuple(self.find_keys(variables)) def output_for_cli(self, textui, result, **kw): + if len(result) == 0: + return textui.print_name(self.name) textui.print_keyval(result) textui.print_count(result, '%d variable', '%d variables') -- cgit From f04aaff97c9c8c22b36706f2c6d4de6f23d06b95 Mon Sep 17 00:00:00 2001 From: Jason Gerard DeRose Date: Wed, 12 Nov 2008 09:55:11 -0700 Subject: output_for_cli signature is now output_for_cli(textui, result, *args, **options) --- ipalib/plugins/f_misc.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'ipalib/plugins/f_misc.py') diff --git a/ipalib/plugins/f_misc.py b/ipalib/plugins/f_misc.py index 1acf1c99..05fd6d52 100644 --- a/ipalib/plugins/f_misc.py +++ b/ipalib/plugins/f_misc.py @@ -39,8 +39,8 @@ class env(Command): ), ) - def run(self, variables, **kw): - if kw['server'] and not self.env.in_server: + def run(self, variables, **options): + if options['server'] and not self.env.in_server: return self.forward(variables) return self.execute(variables) @@ -56,7 +56,7 @@ class env(Command): ) return tuple(self.find_keys(variables)) - def output_for_cli(self, textui, result, **kw): + def output_for_cli(self, textui, result, variables, **options): if len(result) == 0: return textui.print_name(self.name) -- cgit From 9de56d43f054bc5e509e38bda1a048e5af6d73d7 Mon Sep 17 00:00:00 2001 From: Jason Gerard DeRose Date: Fri, 14 Nov 2008 21:58:39 -0700 Subject: env plugin now subclasses from RemoteOrLocal --- ipalib/plugins/f_misc.py | 24 ++++++++---------------- 1 file changed, 8 insertions(+), 16 deletions(-) (limited to 'ipalib/plugins/f_misc.py') diff --git a/ipalib/plugins/f_misc.py b/ipalib/plugins/f_misc.py index 05fd6d52..6988d6f8 100644 --- a/ipalib/plugins/f_misc.py +++ b/ipalib/plugins/f_misc.py @@ -21,44 +21,36 @@ Misc frontend plugins. """ -from ipalib import api, Command, Param, Bool +from ipalib import api, LocalOrRemote # FIXME: We should not let env return anything in_server # when mode == 'production'. This would allow an attacker to see the # configuration of the server, potentially revealing compromising # information. However, it's damn handy for testing/debugging. -class env(Command): +class env(LocalOrRemote): """Show environment variables""" takes_args = ('variables*',) - takes_options = ( - Param('server?', type=Bool(), default=False, - doc='Show environment variables of server', - ), - ) - - def run(self, variables, **options): - if options['server'] and not self.env.in_server: - return self.forward(variables) - return self.execute(variables) - - def find_keys(self, variables): + def __find_keys(self, variables): for key in variables: if key in self.env: yield (key, self.env[key]) - def execute(self, variables): + def execute(self, variables, **options): if variables is None: return tuple( (key, self.env[key]) for key in self.env ) - return tuple(self.find_keys(variables)) + return tuple(self.__find_keys(variables)) def output_for_cli(self, textui, result, variables, **options): if len(result) == 0: return + if len(result) == 1: + textui.print_keyval(result) + return textui.print_name(self.name) textui.print_keyval(result) textui.print_count(result, '%d variable', '%d variables') -- cgit From e059591d6b190cbc6c50d0b96e1f63fddb30a934 Mon Sep 17 00:00:00 2001 From: Jason Gerard DeRose Date: Fri, 14 Nov 2008 22:21:36 -0700 Subject: env command now supports * wildcard for searching --- ipalib/plugins/f_misc.py | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) (limited to 'ipalib/plugins/f_misc.py') diff --git a/ipalib/plugins/f_misc.py b/ipalib/plugins/f_misc.py index 6988d6f8..b2f97c71 100644 --- a/ipalib/plugins/f_misc.py +++ b/ipalib/plugins/f_misc.py @@ -21,9 +21,11 @@ Misc frontend plugins. """ +import re from ipalib import api, LocalOrRemote + # FIXME: We should not let env return anything in_server # when mode == 'production'. This would allow an attacker to see the # configuration of the server, potentially revealing compromising @@ -34,16 +36,25 @@ class env(LocalOrRemote): takes_args = ('variables*',) def __find_keys(self, variables): - for key in variables: - if key in self.env: - yield (key, self.env[key]) + keys = set() + for query in variables: + if '*' in query: + pat = re.compile(query.replace('*', '.*') + '$') + for key in self.env: + if pat.match(key): + keys.add(key) + elif query in self.env: + keys.add(query) + return sorted(keys) def execute(self, variables, **options): if variables is None: - return tuple( - (key, self.env[key]) for key in self.env - ) - return tuple(self.__find_keys(variables)) + keys = self.env + else: + keys = self.__find_keys(variables) + return tuple( + (key, self.env[key]) for key in keys + ) def output_for_cli(self, textui, result, variables, **options): if len(result) == 0: @@ -53,6 +64,6 @@ class env(LocalOrRemote): return textui.print_name(self.name) textui.print_keyval(result) - textui.print_count(result, '%d variable', '%d variables') + textui.print_count(result, '%d variables') api.register(env) -- cgit From e7ec4131589d5d387c4257bca76e91a17ad7e1a3 Mon Sep 17 00:00:00 2001 From: Jason Gerard DeRose Date: Sun, 16 Nov 2008 19:50:17 -0700 Subject: Moved plugins command from ipalib.cli to ipalib.plugins.f_misc --- ipalib/plugins/f_misc.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'ipalib/plugins/f_misc.py') diff --git a/ipalib/plugins/f_misc.py b/ipalib/plugins/f_misc.py index b2f97c71..a2f0fa4e 100644 --- a/ipalib/plugins/f_misc.py +++ b/ipalib/plugins/f_misc.py @@ -67,3 +67,23 @@ class env(LocalOrRemote): textui.print_count(result, '%d variables') api.register(env) + + +class plugins(LocalOrRemote): + """Show all loaded plugins""" + + def execute(self, **options): + plugins = sorted(self.api.plugins, key=lambda o: o.plugin) + return tuple( + (p.plugin, p.bases) for p in plugins + ) + + def output_for_cli(self, textui, result, **options): + textui.print_name(self.name) + for (plugin, bases) in result: + textui.print_indented( + '%s: %s' % (plugin, ', '.join(bases)) + ) + textui.print_count(result, '%d plugin loaded', '%s plugins loaded') + +api.register(plugins) -- cgit