Exemplo n.º 1
0
def test_guess_method():
    assert 'IInt' in guess_interface(1)

    class NotRegisteredClass(object):
        pass

    assert guess_interface(NotRegisteredClass()) == []
Exemplo n.º 2
0
    def set_attribute(self, name, value, interface=None, label=None, constraints=None):
        attribute_names = [a["name"] for a in self._attributes]
        try:
            attribute = self._attributes[attribute_names.index(name)]
        except ValueError:
            if interface is None:
                from openalea.core.service.interface import guess_interface

                interfaces = guess_interface(value)
                if len(interfaces):
                    interface = interfaces[0]
            self._attributes.append(
                dict(name=name, value=value, interface=interface, label=label, constraints=constraints)
            )
            self.notify_listeners(("world_object_attribute_changed", (self, None, self._attributes[-1])))
        else:
            from copy import copy

            old_attribute = copy(attribute)
            if interface is not None:
                attribute["interface"] = interface
            if label is not None:
                attribute["label"] = label
            if constraints:
                attribute["constraints"] = constraints
            attribute["value"] = value
            if attribute["value"] != old_attribute["value"]:
                self.notify_listeners(("world_object_attribute_changed", (self, old_attribute, attribute)))
Exemplo n.º 3
0
def Widget(option_name, value):
    """
    :return: create a widget which permit to edit the value with a label
    """
    # TODO: Currently an evaluation is used to guess value type.
    # This approach may fails for complex data
    # We need to improve option type management, for example by providing an associated map "option_name -> type".
    # We should also add constraints on values
    try:
        eval_value = ast.literal_eval(value)
    except (ValueError, SyntaxError):
        eval_value = value

    inames = guess_interface(eval_value)
    if len(inames):
        iname = inames[0]
    else:
        iname = 'IStr'

    # Dirty hack to handle int constraints on font size.
    if 'font' in option_name and iname == 'IInt':
        iname = new_interface(iname, min=5, max=200)

    control = Control(option_name, iname, eval_value)
    editor = qt_editor(control)
    return control, editor
Exemplo n.º 4
0
 def set_attribute(self,
                   name,
                   value,
                   interface=None,
                   label=None,
                   constraints=None):
     attribute_names = [a['name'] for a in self._attributes]
     try:
         attribute = self._attributes[attribute_names.index(name)]
     except ValueError:
         if interface is None:
             from openalea.core.service.interface import guess_interface
             interfaces = guess_interface(value)
             if len(interfaces):
                 interface = interfaces[0]
         self._attributes.append(
             dict(name=name,
                  value=value,
                  interface=interface,
                  label=label,
                  constraints=constraints))
         self.notify_listeners(('world_object_attribute_changed',
                                (self, None, self._attributes[-1])))
     else:
         old_attribute = copy(attribute)
         if interface is not None:
             attribute['interface'] = interface
         if label is not None:
             attribute['label'] = label
         if constraints:
             attribute['constraints'] = constraints
         attribute['value'] = value
         if attribute['value'] != old_attribute['value']:
             self.notify_listeners(('world_object_attribute_changed',
                                    (self, old_attribute, attribute)))
Exemplo n.º 5
0
def set_interface(input_obj):
    if input_obj.interface is None:
        if isinstance(input_obj.default, str):
            try:
                default_eval = eval(input_obj.default)
                input_obj.interface = guess_interface(default_eval)
            except SyntaxError:
                input_obj.interface = guess_interface(input_obj.default)
        else:
            input_obj.interface = guess_interface(input_obj.default)
    else:
        try:
            input_obj.interface = interface_class(input_obj.interface)
        except ValueError:
            input_obj.interface = guess_interface(input_obj.default)
    if input_obj.interface == []:
        input_obj.interface = None
    elif isinstance(input_obj.interface, list):
        input_obj.interface = input_obj.interface[0]
Exemplo n.º 6
0
def set_interface(input_obj):
    if input_obj.interface is None:
        if isinstance(input_obj.default, str):
            try:
                default_eval = eval(input_obj.default)
                input_obj.interface = guess_interface(default_eval)
            except SyntaxError:
                input_obj.interface = guess_interface(input_obj.default)
        else:
            input_obj.interface = guess_interface(input_obj.default)
    else:
        try:
            input_obj.interface = interface_class(input_obj.interface)
        except ValueError:
            input_obj.interface = guess_interface(input_obj.default)
    if input_obj.interface == []:
        input_obj.interface = None
    elif isinstance(input_obj.interface, list):
        input_obj.interface = input_obj.interface[0]
Exemplo n.º 7
0
 def set_attribute(self, name, value, interface=None, alias=None, constraints=None):
     attribute_names = [a['name'] for a in self._attributes]
     try:
         attribute = self._attributes[attribute_names.index(name)]
     except ValueError:
         if interface is None:
             from openalea.core.service.interface import guess_interface
             interfaces = guess_interface(value)
             if len(interfaces):
                 interface = interfaces[0]
         self._attributes.append(dict(name=name, value=value, interface=interface, alias=alias, constraints=constraints))
         self.notify_listeners(('world_object_attribute_changed', (self, None, self._attributes[-1])))
     else:
         from copy import copy
         old_attribute = copy(attribute)
         if interface is not None:
             attribute['interface'] = interface
         if alias is not None:
             attribute['alias'] = alias
         if constraints:
             attribute['constraints'] = constraints
         attribute['value'] = value
         if attribute['value'] != old_attribute['value']:
             self.notify_listeners(('world_object_attribute_changed', (self, old_attribute, attribute)))
 def test_guess_interface(self):
     assert 'IInt' in guess_interface(1)
     assert 'IFloat' in guess_interface(1.)
Exemplo n.º 9
0
def import_lpy_controls(filepath):
    if not path(filepath).isfile():
        return

    control = dict()

    f = open(filepath, "r")
    script = f.read()
    f.close()

    if script is None:
        script = ""
    beginTag = LpyParsing.InitialisationBeginTag
    if not beginTag in script:
        return str(script), control
    else:
        txts = str(script).split(beginTag)
        new_script = txts[0]
        context_to_translate = txts[1]
        context = Lsystem().context()
        context.initialiseFrom(beginTag + context_to_translate)

    managers = get_managers()
    visualparameters = []
    scalars = []
    functions = []
    curves = []
    geoms = []

    lpy_code_version = 1.0
    if context.has_key("__lpy_code_version__"):
        lpy_code_version = context["__lpy_code_version__"]
    if context.has_key("__scalars__"):
        scalars_ = context["__scalars__"]
        scalars = [ProduceScalar(v) for v in scalars_]
    if context.has_key("__functions__") and lpy_code_version <= 1.0:
        functions = context["__functions__"]
        for n, c in functions:
            c.name = n
        functions = [c for n, c in functions]
        funcmanager = managers["Function"]
        geoms += [(funcmanager, func) for func in functions]
    if context.has_key("__curves__") and lpy_code_version <= 1.0:
        curves = context["__curves__"]
        for n, c in curves:
            c.name = n
        curves = [c for n, c in curves]
        curvemanager = managers["Curve2D"]
        geoms += [(curvemanager, curve) for curve in curves]
    if context.has_key("__parameterset__"):
        for panelinfo, objects in context["__parameterset__"]:
            for typename, obj in objects:
                visualparameters.append((managers[typename], obj))

    for scalar in scalars:
        control[unicode(scalar.name)] = scalar.value
    for (manager, geom) in geoms:
        if geom != list():
            control[geom.getName()] = geom
    for (manager, geom) in visualparameters:
        if geom != list():
            control[geom.getName()] = geom

    new_controls = []
    for name, value in control.items():
        interfaces = guess_interface(value)
        if interfaces:
            new_controls.append(Control(name, interfaces[0], value))

    try:
        control["color map"] = to_color(context.turtle.getColorList())
    except AttributeError:
        pass
    else:
        new_controls.append(Control("color map", "IColorList", control["color map"]))

    for control in new_controls:
        register_control(control)
Exemplo n.º 10
0
def import_lpy_controls(filepath):
    if not path(filepath).isfile():
        return

    control = dict()

    f = open(filepath, 'r')
    script = f.read()
    f.close()

    if script is None: script = ""
    beginTag = LpyParsing.InitialisationBeginTag
    if not beginTag in script:
        return str(script), control
    else:
        txts = str(script).split(beginTag)
        new_script = txts[0]
        context_to_translate = txts[1]
        context = Lsystem().context()
        context.initialiseFrom(beginTag + context_to_translate)

    managers = get_managers()
    visualparameters = []
    scalars = []
    functions = []
    curves = []
    geoms = []

    lpy_code_version = 1.0
    if context.has_key('__lpy_code_version__'):
        lpy_code_version = context['__lpy_code_version__']
    if context.has_key('__scalars__'):
        scalars_ = context['__scalars__']
        scalars = [ProduceScalar(v) for v in scalars_]
    if context.has_key('__functions__') and lpy_code_version <= 1.0:
        functions = context['__functions__']
        for n, c in functions:
            c.name = n
        functions = [c for n, c in functions]
        funcmanager = managers['Function']
        geoms += [(funcmanager, func) for func in functions]
    if context.has_key('__curves__') and lpy_code_version <= 1.0:
        curves = context['__curves__']
        for n, c in curves:
            c.name = n
        curves = [c for n, c in curves]
        curvemanager = managers['Curve2D']
        geoms += [(curvemanager, curve) for curve in curves]
    if context.has_key('__parameterset__'):
        for panelinfo, objects in context['__parameterset__']:
            for typename, obj in objects:
                visualparameters.append((managers[typename], obj))

    for scalar in scalars:
        control[unicode(scalar.name)] = scalar.value
    for (manager, geom) in geoms:
        if geom != list():
            control[geom.getName()] = geom
    for (manager, geom) in visualparameters:
        if geom != list():
            control[geom.getName()] = geom

    new_controls = []
    for name, value in control.items():
        interfaces = guess_interface(value)
        if interfaces:
            new_controls.append(Control(name, interfaces[0], value))

    try:
        control["color map"] = to_color(context.turtle.getColorList())
    except AttributeError:
        pass
    else:
        new_controls.append(
            Control("color map", 'IColorList', control["color map"]))

    for control in new_controls:
        register_control(control)
Exemplo n.º 11
0
 def test_guess_interface(self):
     assert 'IInt' in guess_interface(1)
     assert 'IFloat' in guess_interface(1.)