2008-04-05 23:22:12 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
2007-02-28 22:15:20 +01:00
|
|
|
#
|
2009-07-19 01:03:23 +02:00
|
|
|
# Copyright (C) 2007, 2008, 2009 by Jan Dittberner.
|
2007-02-28 22:15:20 +01:00
|
|
|
#
|
|
|
|
# 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; either version 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
|
|
|
|
# USA.
|
|
|
|
#
|
|
|
|
# Version: $Id$
|
2009-08-01 15:33:34 +02:00
|
|
|
import argparse
|
2008-06-06 21:20:18 +02:00
|
|
|
import sys
|
|
|
|
import logging
|
2007-07-02 11:14:47 +02:00
|
|
|
from gnuviechadmin.exceptions import GnuviechadminError
|
2007-02-28 22:15:20 +01:00
|
|
|
|
2008-06-06 21:20:18 +02:00
|
|
|
|
2009-08-01 15:33:34 +02:00
|
|
|
class CliCommand(object):
|
2007-07-02 11:14:47 +02:00
|
|
|
"""
|
2009-08-01 17:10:28 +02:00
|
|
|
Base class for command line interface.
|
2007-07-02 11:14:47 +02:00
|
|
|
|
2009-08-01 17:10:28 +02:00
|
|
|
A specific implementation class must implement the static
|
|
|
|
setup_arparser method and the _execute method.
|
|
|
|
"""
|
2007-07-02 11:14:47 +02:00
|
|
|
|
2009-08-01 17:10:28 +02:00
|
|
|
@staticmethod
|
|
|
|
def setup_argparser(subparsers):
|
|
|
|
"""
|
|
|
|
This static method is used to initialize the command line
|
|
|
|
argument parser.
|
2007-07-02 11:14:47 +02:00
|
|
|
|
2009-08-01 17:10:28 +02:00
|
|
|
The method has to be overridden by subclasses.
|
|
|
|
"""
|
|
|
|
raise NotImplementedError
|
2007-07-02 11:14:47 +02:00
|
|
|
|
|
|
|
|
2009-08-01 17:10:28 +02:00
|
|
|
def _execute(self):
|
|
|
|
"""
|
|
|
|
This method is called when the subcommand of the command is
|
|
|
|
executed.
|
2007-02-28 22:15:20 +01:00
|
|
|
|
2009-08-01 17:10:28 +02:00
|
|
|
The method has to be overridden by subclasses.
|
|
|
|
"""
|
2007-02-28 22:15:20 +01:00
|
|
|
raise NotImplementedError
|
|
|
|
|
2009-07-19 01:03:23 +02:00
|
|
|
def __init__(self, args, config):
|
2009-08-01 17:10:28 +02:00
|
|
|
"""
|
|
|
|
This initializes the command with the given command line
|
|
|
|
arguments and executes it.
|
|
|
|
"""
|
2009-07-19 01:03:23 +02:00
|
|
|
self.config = config
|
2009-08-01 15:33:34 +02:00
|
|
|
self.args = args
|
2007-07-09 08:46:36 +02:00
|
|
|
self.logger = logging.getLogger("%s.%s" % (
|
|
|
|
self.__class__.__module__, self.__class__.__name__))
|
2009-08-01 15:33:34 +02:00
|
|
|
self._execute()
|