Package solvcon :: Module cmdutil
[hide private]
[frames] | no frames]

Source Code for Module solvcon.cmdutil

  1  # -*- coding: UTF-8 -*- 
  2  # 
  3  # Copyright (C) 2008-2010 Yung-Yu Chen <yyc@solvcon.net>. 
  4  # 
  5  # This program is free software; you can redistribute it and/or modify 
  6  # it under the terms of the GNU General Public License as published by 
  7  # the Free Software Foundation; either version 2 of the License, or 
  8  # (at your option) any later version. 
  9  # 
 10  # This program is distributed in the hope that it will be useful, 
 11  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 12  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 13  # GNU General Public License for more details. 
 14  # 
 15  # You should have received a copy of the GNU General Public License along 
 16  # with this program; if not, write to the Free Software Foundation, Inc., 
 17  # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 
 18   
 19  """ 
 20  Supporting functionalities and structures for UI commands. 
 21  """ 
 22   
 23  from .gendata import SingleAssignDict, AttributeDict 
24 25 -class CommandRegistry(SingleAssignDict, AttributeDict):
26 - def register(self, cmdtype):
27 name = cmdtype.__name__ 28 if name.islower(): 29 self[name] = cmdtype 30 return cmdtype 31 else: 32 return None
33 cmdregy = CommandRegistry() # registry singleton.
34 35 -class CommandMeta(type):
36 - def __new__(cls, name, bases, namespace):
37 newcls = super(CommandMeta, cls).__new__(cls, name, bases, namespace) 38 # register. 39 cmdregy.register(newcls) 40 return newcls
41
42 -class Command(object):
43 """ 44 Command line parameters. 45 46 @cvar min_args: minimal length of command line arguments to a command. 47 @ctype min_args: int 48 @ivar env: environmental object. 49 @itype env: solvcon.conf.Solvcon 50 @ivar op: overall option parser object. 51 @itype op: optparse.OptionParser 52 @ivar opg_global: group for global options. 53 @itype opg_global: optparse.OptionGroup 54 @ivar _opargs: tuple for storing options and arguments. 55 @itype _opargs: tuple 56 @ivar _usage: generic usage string for the UI. 57 @itype _usage: str 58 """ 59 60 __metaclass__ = CommandMeta 61 62 min_args = 0 63
64 - def __init__(self, env):
65 from optparse import OptionParser, OptionGroup 66 from . import __version__ 67 68 self._usage = '%prog command <args> ... <ops> ...' 69 70 op = OptionParser(usage=self._usage, version=__version__) 71 72 opg = OptionGroup(op, 'Global') 73 opg.add_option('--print-solvcon', action='store_true', 74 dest='print_solvcon', default=False, 75 help='Print the solvcon package in use.', 76 ) 77 opg.add_option('--print-project-dir', action='store_true', 78 dest='print_project_dir', default=False, 79 help='Print the the project directory.', 80 ) 81 op.add_option_group(opg) 82 self.opg_global = opg 83 84 # set to self. 85 self.env = env 86 self.op = op 87 self._opargs = None
88 89 @property
90 - def command_description(self):
91 cmdstrs = sorted(cmdregy.keys()) 92 cmdmaxlen = max(len(c) for c in cmdstrs) 93 idt1 = 2 94 nsep = 1 95 idt2 = idt1 + cmdmaxlen + nsep 96 descriptions = [] 97 for cmdstr in cmdstrs: 98 cmdcls = cmdregy[cmdstr] 99 desc = cmdcls.__doc__.strip() 100 desc = desc.split('\n')[0].strip() 101 descriptions.append(''.join([' '*idt1, 102 ('%%-%ds'%cmdmaxlen) % cmdstr, ' '*nsep, desc])) 103 description = '\n'.join(descriptions) 104 return '\n'.join(['Command:', description])
105 106 @property
107 - def usage(self):
108 return self._usage
109 110 @property
111 - def opargs(self):
112 import sys, os 113 import solvcon 114 from solvcon.helper import info 115 # get the options and arguments. 116 if not self._opargs: 117 self.op.usage = self.usage 118 ops, args = self.op.parse_args() 119 narg = len(args) 120 if narg >= 1: 121 args = args[1:] 122 else: 123 args = [] 124 self._opargs = ops, args 125 # include project path. 126 flag_setproj = True 127 for path in sys.path: 128 if self.env.projdir == os.path.abspath(path): 129 flag_setproj = False 130 break 131 if flag_setproj: 132 sys.path.insert(0, self.env.projdir) 133 # output general information. 134 if ops.print_solvcon: 135 info('*** Use the solvcon package located at "%s".\n' % \ 136 solvcon.__file__) 137 if ops.print_project_dir: 138 info('*** Project is located at "%s".\n' % self.env.projdir) 139 ops, args = self._opargs 140 # test for number of arguments. 141 if len(args) < self.min_args: 142 info('Number of arguments is less than the sufficient: %d\n' % \ 143 self.min_args) 144 sys.exit(0) 145 # set self to env. 146 self.env.command = self 147 # return the options and arguments. 148 return self._opargs
149
150 - def __call__(self):
151 raise NotImplementedError
152
153 -def go():
154 """ 155 Command runner. 156 """ 157 import sys 158 from . import command 159 from .conf import env, use_application 160 for modname in env.modnames: 161 use_application(modname) 162 narg = len(sys.argv) 163 if narg >= 2 and not sys.argv[1].startswith('-'): 164 cmdcls = cmdregy.get(sys.argv[1], None) 165 else: 166 cmdcls = None 167 if cmdcls == None: 168 cmdcls = cmdregy['help'] 169 cmd = cmdcls(env) 170 cmd()
171