import unittest
from .compat.odict import odict as OrderedDict
+from .util.convert import to_string, from_string
DEFAULT_PATHS = [
in turn overrides the developer defaults.
"""
-CONVERT_FROM_STRING = {
- 'string': lambda x: x,
- 'bool': lambda x: x == 'True',
- 'int': lambda x: int(x),
- 'float': lambda x: float(x),
- }
-"""Functions converting strings to values, keyed by type.
-"""
-
-CONVERT_TO_STRING = {
- 'string': lambda x: x,
- 'bool': lambda x: str(x),
- 'int': lambda x: str(x),
- 'float': lambda x: str(x),
- }
-"""Functions converting values to strings, keyed by type.
-"""
-
class Setting (object):
"""An entry (section or option) in HookeConfigParser.
"""
help=None, wrap=True):
self.section = section
self.option = option
- self.value = CONVERT_TO_STRING[type](value)
+ self.value = to_string(value=value, type=type)
self.type = type
self.help = help
self.wrap = wrap
for i,kv in enumerate(items):
key,value = kv
setting = self._default_settings_dict[(section, key)]
- items[i] = (key, CONVERT_FROM_STRING[setting.type](value))
+ items[i] = (key, from_string(value=value, type=setting.type))
return items
def set(self, section, option, value):
"""Set an option."""
setting = self._default_settings_dict[(section, option)]
- value = CONVERT_TO_STRING[setting.type](value)
+ value = to_string(value=value, type=setting.type)
# Can't use super() because RawConfigParser is a classic class
#return super(HookeConfigParser, self).set(section, option, value)
configparser.RawConfigParser.set(self, section, option, value)
help=self.__doc__, plugin=plugin)
def _run(self, hooke, inqueue, outqueue, params):
- data = params['curve'].data[int(params['block'])] # HACK, int() should be handled by ui
+ data = params['curve'].data[params['block']]
f = open(params['output'], 'w')
if params['header'] == True:
help=self.__doc__, plugin=plugin)
def _run(self, hooke, inqueue, outqueue, params):
- params['playlist'].jump(int(params['index'])) # HACK, int() should be handled by ui
+ params['playlist'].jump(params['index'])
class IndexCommand (Command):
"""Print the index of the current curve.
help=self.__doc__, plugin=plugin)
def _run(self, hooke, inqueue, outqueue, params):
- hooke.playlists.jump(int(params['index'])) # HACK, int() should be handled by ui
+ hooke.playlists.jump(params['index'])
class IndexCommand (Command):
"""Print the index of the current playlist.
help=self.__doc__, plugin=plugin)
def _run(self, hooke, inqueue, outqueue, params):
- data = params['curve'].data[int(params['block'])] # HACK, int() should be handled by ui
+ data = params['curve'].data[params['block']]
# HACK? rely on params['curve'] being bound to the local hooke
# playlist (i.e. not a copy, as you would get by passing a
# curve through the queue). Ugh. Stupid queues. As an
new.info['surface detection parameters'] = ps
new[:,-2] = z_data - surface_offset
new[:,-1] = d_data - deflection_offset
- data = params['curve'].data[int(params['block'])] # HACK, int() should be handled by ui
- params['curve'].data[int(params['block'])] = new # HACK, int() should be handled by ui
+ data = params['curve'].data[params['block']]
+ params['curve'].data[params['block']] = new
def find_contact_point(self, curve, z_data, d_data, outqueue=None):
"""Railyard for the `find_contact_point_*` family.
help=self.__doc__, plugin=plugin)
def _run(self, hooke, inqueue, outqueue, params):
- data = params['curve'].data[int(params['block'])] # HACK, int() should be handled by ui
+ data = params['curve'].data[params['block']]
# HACK? rely on params['curve'] being bound to the local hooke
# playlist (i.e. not a copy, as you would get by passing a
# curve through the queue). Ugh. Stupid queues. As an
new.info['columns'].append('deflection (N)')
d_data = data[:,data.info['columns'].index('surface adjusted deflection (m)')]
new[:,-1] = d_data * data.info['spring constant (N/m)']
- params['curve'].data[int(params['block'])] = new # HACK, int() should be handled by ui
+ params['curve'].data[params['block']] = new
class generalvclampCommands(object):
from ..command import CommandExit, Exit, Command, Argument, StoreValue
from ..interaction import Request, BooleanRequest, ReloadUserInterfaceConfig
from ..ui import UserInterface, CommandMessage
+from ..util.convert import from_string
from ..util.encoding import get_input_encoding, get_output_encoding
continue # 'help' is a default OptionParser option
if a.optional == True:
name = name_fn(a.name)
+ type = a.type
+ if type == 'bool':
+ if a.default == True:
+ self.add_option(
+ '--disable-%s' % name, dest=name, default=Default,
+ action='store_false')
+ self.command_opts.append(a)
+ continue
+ elif a.default == False:
+ self.add_option(
+ '--enable-%s' % name, dest=name, default=Default,
+ action='store_true')
+ self.command_opts.append(a)
+ continue
+ else:
+ type = 'string'
+ elif type not in ['string', 'int', 'long', 'choice', 'float',
+ 'complex']:
+ type = 'string'
self.add_option(
- '--%s' % name, dest=name, default=Default)
+ '--%s' % name, dest=name, type=type, default=Default)
self.command_opts.append(a)
else:
self.command_args.append(a)
self.cmd.stdout.write(str(e).lstrip()+'\n')
self.cmd.stdout.write('Failure\n')
return
+ print args
self.cmd.inqueue.put(CommandMessage(self.command, args))
while True:
msg = self.cmd.outqueue.get()
arg_index = 0
for argument in self.parser.command_args:
if argument.count == 1:
- params[argument.name] = args[arg_index]
+ params[argument.name] = from_string(args[arg_index],
+ argument.type)
elif argument.count > 1:
- params[argument.name] = \
- args[arg_index:arg_index+argument.count]
+ params[argument.name] = [
+ from_string(a, argument.type)
+ for a in args[arg_index:arg_index+argument.count]]
else: # argument.count == -1:
- params[argument.name] = args[arg_index:]
+ params[argument.name] = [
+ from_string(a, argument.type) for a in args[arg_index:]]
arg_index += argument.count
return params
--- /dev/null
+# Copyright
+
+"""Type conversion utilities.
+"""
+
+CONVERT_FROM_STRING = {
+ 'string': lambda x: x,
+ 'bool': lambda x: x == 'True',
+ 'int': lambda x: int(x),
+ 'float': lambda x: float(x),
+ }
+"""Functions converting strings to values, keyed by type.
+"""
+
+ANALOGS = {
+ 'file': 'string',
+ 'point': 'int',
+ }
+"""Types that may be treated as other types.
+
+These types may have optional special handling on the UI end
+(e.g. file picker dialogs), but it is not required.
+"""
+
+RAW_TYPES = [
+ 'curve',
+ 'dict',
+ 'driver',
+ 'function',
+ 'object',
+ 'playlist',
+ ]
+"""List of types that should not be converted.
+"""
+
+def to_string(value, type):
+ """Convert `value` from `type` to a unicode string.
+ """
+ type = ANALOGS.get(type, type)
+ if type in RAW_TYPES:
+ return value
+ return unicode(value)
+
+def from_string(value, type):
+ """Convert `value` from a string to `type`.
+ """
+ type = ANALOGS.get(type, type)
+ if type in RAW_TYPES:
+ return value
+ fn = globals()['_string_to_%s' % type]
+ return fn(value)
+
+def _string_to_string(value):
+ return unicode(value)
+
+def _string_to_bool(value):
+ return value == 'True'
+
+def _string_to_int(value):
+ return int(value)
+
+def _string_to_float(value):
+ return float(value)