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 SingleAssignDict, AttributeDict
27 name = cmdtype.__name__
28 if name.islower():
29 self[name] = cmdtype
30 return cmdtype
31 else:
32 return None
33 cmdregy = CommandRegistry()
41
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
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
85 self.env = env
86 self.op = op
87 self._opargs = None
88
89 @property
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
109
110 @property
112 import sys, os
113 import solvcon
114 from solvcon.helper import info
115
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
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
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
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
146 self.env.command = self
147
148 return self._opargs
149
151 raise NotImplementedError
152
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