00001
00002
00003
00004
00005 '''rtshell
00006
00007 Copyright (C) 2009-2014
00008 Geoffrey Biggs
00009 RT-Synthesis Research Group
00010 Intelligent Systems Research Institute,
00011 National Institute of Advanced Industrial Science and Technology (AIST),
00012 Japan
00013 All rights reserved.
00014 Licensed under the Eclipse Public License -v 1.0 (EPL)
00015 http://www.opensource.org/licenses/eclipse-1.0.txt
00016
00017 Implementation of the command to manage component configuration.
00018
00019 '''
00020
00021
00022 import optparse
00023 import os
00024 import os.path
00025 import rtctree.exceptions
00026 import rtctree.tree
00027 import rtctree.path
00028 import rtctree.utils
00029 import sys
00030 import traceback
00031
00032 import path
00033 import rtshell
00034 import rts_exceptions
00035
00036
00037 def is_hidden(name):
00038 hidden_prefix = '__'
00039 hidden_suffix = '__'
00040 return name.startswith(hidden_prefix) and name.endswith(hidden_suffix)
00041
00042
00043 def format_conf_set(set_name, set, is_active, use_colour, long):
00044 result = []
00045 indent = 0
00046 if long:
00047 tag = '-'
00048 else:
00049 tag = '+'
00050 if is_active:
00051 title = tag + rtctree.utils.build_attr_string(['bold', 'green'],
00052 supported=use_colour) + set_name + '*' + \
00053 rtctree.utils.build_attr_string('reset', supported=use_colour)
00054 if set.description:
00055 title += ' ({0})'.format(set.description)
00056 else:
00057 title = tag + rtctree.utils.build_attr_string('bold',
00058 supported=use_colour) + set_name + \
00059 rtctree.utils.build_attr_string('reset', supported=use_colour)
00060 if set.description:
00061 title += ' ({0})'.format(set.description)
00062 result.append(title)
00063
00064 if long:
00065 params = set.data.keys()
00066 if params:
00067 params.sort()
00068 padding = len(max(params, key=len)) + 2
00069 indent += 2
00070 for param in params:
00071 result.append('{0}{1}{2}'.format(''.ljust(indent),
00072 param.ljust(padding), set.data[param]))
00073 indent -= 2
00074
00075 return result
00076
00077
00078 def format_conf_sets(sets, active_set_name, all, use_colour, long):
00079 result = []
00080 set_keys = [k for k in sets.keys() if not is_hidden(k) or all]
00081 set_keys.sort()
00082 for set_name in set_keys:
00083 result += format_conf_set(set_name, sets[set_name],
00084 set_name == active_set_name, use_colour, long)
00085 return result
00086
00087
00088 def get_comp(cmd_path, full_path, tree=None):
00089 path, port = rtctree.path.parse_path(full_path)
00090 if port:
00091
00092 raise rts_exceptions.NotAComponentError(cmd_path)
00093 if not path[-1]:
00094
00095 raise rts_exceptions.NoSuchObjectError(cmd_path)
00096
00097 if not tree:
00098 tree = rtctree.tree.RTCTree(paths=path, filter=[path])
00099
00100 comp = tree.get_node(path)
00101 if not comp:
00102 raise rts_exceptions.NoSuchObjectError(cmd_path)
00103 if comp.is_zombie:
00104 raise rts_exceptions.ZombieObjectError(cmd_path)
00105 if not comp.is_component:
00106 raise rts_exceptions.NotAComponentError(cmd_path)
00107 return tree, comp
00108
00109
00110
00111 def print_conf_sets(cmd_path, full_path, options, tree=None):
00112 use_colour = rtctree.utils.colour_supported(sys.stdout)
00113 tree, comp = get_comp(cmd_path, full_path, tree)
00114
00115 if options.set_name:
00116 if is_hidden(options.set_name) and not options.all:
00117 raise rts_exceptions.NoConfSetError(options.set_name)
00118 if not options.set_name in comp.conf_sets:
00119 raise rts_exceptions.NoConfSetError(options.set_name)
00120 return format_conf_set(options.set_name,
00121 comp.conf_sets[options.set_name],
00122 options.set_name == comp.active_conf_set_name,
00123 use_colour, options.long)
00124 else:
00125 return format_conf_sets(comp.conf_sets,
00126 comp.active_conf_set_name, options.all,
00127 use_colour, options.long)
00128
00129
00130 def set_conf_value(param, new_value, cmd_path, full_path, options, tree=None):
00131 tree, comp = get_comp(cmd_path, full_path, tree)
00132
00133 if not options.set_name:
00134 options.set_name = comp.active_conf_set_name
00135 if is_hidden(options.set_name) and not options.all:
00136 raise rts_exceptions.NoConfSetError(options.set_name)
00137 comp.set_conf_set_value(options.set_name, param, new_value)
00138 if options.set_name == comp.active_conf_set_name:
00139
00140
00141 comp.activate_conf_set(options.set_name)
00142
00143
00144 def get_conf_value(param, cmd_path, full_path, options, tree=None):
00145 tree, comp = get_comp(cmd_path, full_path, tree)
00146
00147 if not options.set_name:
00148 options.set_name = comp.active_conf_set_name
00149 if is_hidden(options.set_name) and not options.all:
00150 raise rts_exceptions.NoConfSetError(options.set_name)
00151
00152 if not options.set_name in comp.conf_sets:
00153 raise rts_exceptions.NoConfSetError(options.set_name)
00154 if not param in comp.conf_sets[options.set_name].data:
00155 raise rtctree.exceptions.NoSuchConfParamError(param)
00156 return [comp.conf_sets[options.set_name].data[param]]
00157
00158
00159 def activate_set(cmd_path, full_path, options, tree=None):
00160 if is_hidden(options.set_name) and not options.all:
00161 raise rts_exceptions.NoConfSetError(options.set_name)
00162 tree, comp = get_comp(cmd_path, full_path, tree)
00163 if not options.set_name in comp.conf_sets:
00164 raise rts_exceptions.NoConfSetError(options.set_name)
00165 comp.activate_conf_set(options.set_name)
00166
00167
00168 def main(argv=None, tree=None):
00169 usage = '''Usage: %prog <path> [options] [command] [args]
00170 Display and edit configuration parameters and sets.'''
00171 version = rtshell.RTSH_VERSION
00172 parser = optparse.OptionParser(usage=usage, version=version)
00173 parser.add_option('-a', '--all', dest='all', action='store_true',
00174 default=False,
00175 help='Do not ignore hidden sets. [Default: %default]')
00176 parser.add_option('-l', dest='long', action='store_true', default=False,
00177 help='Show more information. [Default: %default]')
00178 parser.add_option('-s', '--set', dest='set_name', action='store',
00179 default='', help='Choose the configuration set to manipulate. '\
00180 '[Default: %default]')
00181 parser.add_option('-v', '--verbose', dest='verbose', action='store_true',
00182 default=False,
00183 help='Output verbose information. [Default: %default]')
00184
00185 if argv:
00186 sys.argv = [sys.argv[0]] + argv
00187 try:
00188 options, args = parser.parse_args()
00189 except optparse.OptionError, e:
00190 print >>sys.stderr, 'OptionError:', e
00191 return 1, []
00192
00193 if not args:
00194 print >>sys.stderr, usage
00195 return 1, []
00196 elif len(args) == 1:
00197 cmd_path = args[0]
00198 cmd = 'list'
00199 args = args[1:]
00200 else:
00201 cmd_path = args[0]
00202 cmd = args[1]
00203 args = args[2:]
00204 full_path = path.cmd_path_to_full_path(cmd_path)
00205
00206 result = []
00207 try:
00208 if cmd == 'list':
00209
00210 result = print_conf_sets(cmd_path, full_path, options, tree)
00211 elif cmd == 'set':
00212
00213 if len(args) == 2:
00214 param = args[0]
00215 new_value = args[1]
00216 else:
00217 print >>sys.stderr, usage
00218 return 1, []
00219 set_conf_value(param, new_value, cmd_path, full_path, options,
00220 tree)
00221 elif cmd == 'get':
00222
00223 if len(args) == 1:
00224 param = args[0]
00225 else:
00226 print >>sys.stderr, usage
00227 return 1, []
00228 result = get_conf_value(param, cmd_path, full_path, options, tree)
00229 elif cmd == 'act':
00230 if len(args) != 0 or options.set_name == '':
00231 print >>sys.stderr, usage
00232 return 1, []
00233 activate_set(cmd_path, full_path, options, tree)
00234 else:
00235 print >>sys.stderr, usage
00236 return 1, []
00237 except Exception, e:
00238 if options.verbose:
00239 traceback.print_exc()
00240 print >>sys.stderr, '{0}: {1}'.format(os.path.basename(sys.argv[0]), e)
00241 return 1, []
00242 return 0, result
00243
00244
00245
00246