1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 """
20 Supporting functionalities and structures for UI commands.
21 """
22
23 from .gendata import TypeNameRegistry
32 cmdregy = CommandRegistry()
40
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
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
84 self.env = env
85 self.op = op
86 self._opargs = None
87
88 @property
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
108
109 @property
111 import sys, os
112 import solvcon
113 from solvcon.helper import info
114
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
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
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
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
145 self.env.command = self
146
147 return self._opargs
148
150 raise NotImplementedError
151
153 test.__test__ = False
154 import os
155 import nose
156 path = os.path.dirname(__file__)
157 nose.run(defaultTest=path)
158
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