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 TypeNameRegistry 
24 25 -class CommandRegistry(TypeNameRegistry):
26 - def register(self, cmdtype):
27 name = cmdtype.__name__ 28 if name.islower(): 29 return super(CommandRegistry, self).register(cmdtype) 30 else: 31 return None
32 cmdregy = CommandRegistry() # registry singleton.
33 34 -class CommandMeta(type):
35 - def __new__(cls, name, bases, namespace):
36 newcls = super(CommandMeta, cls).__new__(cls, name, bases, namespace) 37 # register. 38 cmdregy.register(newcls) 39 return newcls
40
41 -class Command(object):
42 """ 43 Command line parameters. 44 45 @cvar min_args: minimal length of command line arguments to a command. 46 @ctype min_args: int 47 @ivar env: environmental object. 48 @itype env: solvcon.conf.Solvcon 49 @ivar op: overall option parser object. 50 @itype op: optparse.OptionParser 51 @ivar opg_global: group for global options. 52 @itype opg_global: optparse.OptionGroup 53 @ivar _opargs: tuple for storing options and arguments. 54 @itype _opargs: tuple 55 @ivar _usage: generic usage string for the UI. 56 @itype _usage: str 57 """ 58 59 __metaclass__ = CommandMeta 60 61 min_args = 0 62
63 - def __init__(self, env):
64 from optparse import OptionParser, OptionGroup 65 from . import __version__ 66 67 self._usage = '%prog command <args> ... <ops> ...' 68 69 op = OptionParser(usage=self._usage, version=__version__) 70 71 opg = OptionGroup(op, 'Global') 72 opg.add_option('--print-solvcon', action='store_true', 73 dest='print_solvcon', default=False, 74 help='Print the solvcon package in use.', 75 ) 76 opg.add_option('--print-project-dir', action='store_true', 77 dest='print_project_dir', default=False, 78 help='Print the the project directory.', 79 ) 80 op.add_option_group(opg) 81 self.opg_global = opg 82 83 # set to self. 84 self.env = env 85 self.op = op 86 self._opargs = None
87 88 @property
89 - def command_description(self):
90 cmdstrs = sorted(cmdregy.keys()) 91 cmdmaxlen = max(len(c) for c in cmdstrs) 92 idt1 = 2 93 nsep = 1 94 idt2 = idt1 + cmdmaxlen + nsep 95 descriptions = [] 96 for cmdstr in cmdstrs: 97 cmdcls = cmdregy[cmdstr] 98 desc = cmdcls.__doc__.strip() 99 desc = desc.split('\n')[0].strip() 100 descriptions.append(''.join([' '*idt1, 101 ('%%-%ds'%cmdmaxlen) % cmdstr, ' '*nsep, desc])) 102 description = '\n'.join(descriptions) 103 return '\n'.join(['Command:', description])
104 105 @property
106 - def usage(self):
107 return self._usage
108 109 @property
110 - def opargs(self):
111 import sys, os 112 import solvcon 113 from solvcon.helper import info 114 # get the options and arguments. 115 if not self._opargs: 116 self.op.usage = self.usage 117 ops, args = self.op.parse_args() 118 narg = len(args) 119 if narg >= 1: 120 args = args[1:] 121 else: 122 args = [] 123 self._opargs = ops, args 124 # include project path. 125 flag_setproj = True 126 for path in sys.path: 127 if self.env.projdir == os.path.abspath(path): 128 flag_setproj = False 129 break 130 if flag_setproj: 131 sys.path.insert(0, self.env.projdir) 132 # output general information. 133 if ops.print_solvcon: 134 info('*** Use the solvcon package located at "%s".\n' % \ 135 solvcon.__file__) 136 if ops.print_project_dir: 137 info('*** Project is located at "%s".\n' % self.env.projdir) 138 ops, args = self._opargs 139 # test for number of arguments. 140 if len(args) < self.min_args: 141 info('Number of arguments is less than the sufficient: %d\n' % \ 142 self.min_args) 143 sys.exit(0) 144 # set self to env. 145 self.env.command = self 146 # return the options and arguments. 147 return self._opargs
148
149 - def __call__(self):
150 raise NotImplementedError
151
152 -def test():
153 test.__test__ = False 154 import os 155 import nose 156 path = os.path.dirname(__file__) 157 nose.run(defaultTest=path)
158
159 -def go():
160 """ 161 Command runner. 162 """ 163 import sys 164 from . import command 165 from .conf import env, use_application 166 for modname in env.modnames: 167 use_application(modname) 168 narg = len(sys.argv) 169 if narg >= 2 and not sys.argv[1].startswith('-'): 170 cmdcls = cmdregy.get(sys.argv[1], None) 171 else: 172 cmdcls = None 173 if cmdcls == None: 174 cmdcls = cmdregy['help'] 175 cmd = cmdcls(env) 176 cmd()
177