Esempio n. 1
0
 def execute(self, _name, *args, **options):
     try:
         if _name not in self.Command:
             raise CommandError(name=_name)
         return self.Command[_name](*args, **options)
     except PublicError:
         raise
     except Exception as e:
         logger.exception('non-public: %s: %s', e.__class__.__name__,
                          str(e))
         raise InternalError()
     finally:
         destroy_context()
Esempio n. 2
0
 def run(self, command_name, **options):
     command_name = from_cli(command_name)
     if command_name not in self.Command:
         raise CommandError(name=command_name)
     params = self.Command[command_name].options
     out = [('Parameter','LDAP attribute'),
            ('=========','==============')]
     mcl = len(out[0][0])
     for param in params():
         if param.exclude and 'webui' in param.exclude:
             continue
         out.append((param.cli_name, param.param_spec))
         mcl = max(mcl,len(param.cli_name))
     for item in out:
         print(to_cli(item[0]).ljust(mcl)+' : '+item[1])
Esempio n. 3
0
 def execute(self, _name, *args, **options):
     error = None
     try:
         if _name not in self.Command:
             raise CommandError(name=_name)
         result = self.Command[_name](*args, **options)
     except PublicError as e:
         error = e
     except Exception as e:
         self.exception('non-public: %s: %s', e.__class__.__name__, str(e))
         error = InternalError()
     destroy_context()
     if error is None:
         return result
     assert isinstance(error, PublicError)
     raise error  #pylint: disable=E0702
Esempio n. 4
0
    def wsgi_execute(self, environ):
        result = None
        error = None
        _id = None
        lang = os.environ['LANG']
        name = None
        args = ()
        options = {}

        e = None
        if not 'HTTP_REFERER' in environ:
            return self.marshal(result, RefererError(referer='missing'), _id)
        if not environ['HTTP_REFERER'].startswith(
                'https://%s/ipa' % self.api.env.host) and not self.env.in_tree:
            return self.marshal(result,
                                RefererError(referer=environ['HTTP_REFERER']),
                                _id)
        try:
            if ('HTTP_ACCEPT_LANGUAGE' in environ):
                lang_reg_w_q = environ['HTTP_ACCEPT_LANGUAGE'].split(',')[0]
                lang_reg = lang_reg_w_q.split(';')[0]
                lang_ = lang_reg.split('-')[0]
                if '-' in lang_reg:
                    reg = lang_reg.split('-')[1].upper()
                else:
                    reg = lang_.upper()
                os.environ['LANG'] = '%s_%s' % (lang_, reg)
            if (environ.get('CONTENT_TYPE', '').startswith(self.content_type)
                    and environ['REQUEST_METHOD'] == 'POST'):
                data = read_input(environ)
                (name, args, options, _id) = self.unmarshal(data)
            else:
                (name, args, options, _id) = self.simple_unmarshal(environ)
            if name in self._system_commands:
                result = self._system_commands[name](self, *args, **options)
            elif name not in self.Command:
                raise CommandError(name=name)
            else:
                result = self.Command[name](*args, **options)
        except PublicError, e:
            if self.api.env.debug:
                self.debug('WSGI wsgi_execute PublicError: %s',
                           traceback.format_exc())
            error = e
Esempio n. 5
0
    def get_command(self, argv):
        """Given CLI arguments, return the Command to use

        On incorrect invocation, prints out a help message and returns None
        """
        if len(argv) == 0:
            self.Command.help(outfile=sys.stderr)
            print(file=sys.stderr)
            print('Error: Command not specified', file=sys.stderr)
            sys.exit(2)
        (key, argv) = (argv[0], argv[1:])
        name = from_cli(key)
        if name not in self.Command and len(argv) == 0:
            try:
                self.Command.help(unicode(key), outfile=sys.stderr)
            except HelpError:
                pass
        if name not in self.Command or self.Command[name].NO_CLI:
            raise CommandError(name=key)
        cmd = self.Command[name]
        return cmd