Source code for sire.legacy.Base

from ..Units import _Units  # Need to import so that we have GeneralUnit
from ._Base import *

import atexit as _atexit

_wrap_functions = []

_base_wrap = wrap


@_atexit.register
def _cleanup():
    """
    This function is called when Python exits - this will call the
    clean_up() function in the C++ code to ensure that all memory is
    freed up and all temporary files deleted, threads stopped,
    network connections closed etc
    """
    clean_up()


[docs] def wrap(value): """Wrap the passed value into a :class:`~sire.base.Property` object. This works recursively, wrapping all items in a container, such that the returned value is derived from :class:`~sire.base.Property` and can be passed to the C++ code in sire. Note that you normally don't need to call this yourself, as wrapping is handled automatically. """ if isinstance(value, bool): return BooleanProperty(value) elif isinstance(value, int) or isinstance(value, float): return NumberProperty(value) # is this a unit? try: u = _Units.GeneralUnit(value) if not u.is_dimensionless(): return wrap(u) except Exception: pass if isinstance(value, str): return StringProperty(value) elif isinstance(value, list): # do the number lists try: return IntegerArrayProperty(value) except Exception: pass try: return DoubleArrayProperty(value) except Exception: pass # do the string list is_strings = True for item in value: if not isinstance(item, str): is_strings = False break else: try: u = _Units.GeneralUnit(item) if not u.is_dimensionless(): is_strings = False break except Exception: pass if is_strings: try: return StringArrayProperty(value) except Exception: pass elif _Units.TempBase in type(value).mro(): # this is a temperature return wrap(_Units.GeneralUnit(value)) for func in _wrap_functions: try: return func(value) except Exception: pass try: return _base_wrap(value) except: pass # if this is a dictionary, then wrap as a Properties object if type(value) is dict: p = Properties() for key, value in value.items(): p[key] = value return p else: return PropertyList(value)
_original_wrap = wrap def _add_wrap_function(func): _wrap_functions.append(func) return _original_wrap # cludgy quick fix for an anaconda install _getBundledLibDir = getBundledLibDir def getBundledLibDir(): try: return _getBundledLibDir() except: return "%s/lib" % getInstallDir() def __set_property__(obj, key, property): try: return obj.__setProperty__(key, property) except Exception as e: if e.__class__.__name__ == "ArgumentError": return obj.__setProperty__(key, wrap(property)) else: raise e def __getitem__(props, i): try: return props.__orig_getitem__(i) except Exception as e: if e.__class__.__name__ == "ArgumentError": key = props.propertyKeys()[i] val = props[key] return (key, val) else: raise e def __properties_values__(props): vals = [] for key in props.propertyKeys(): vals.append(props.property(key)) return vals def __properties_items__(props): items = [] for key in props.propertyKeys(): items.append((key, props.property(key))) return items Properties.__setProperty__ = Properties.setProperty Properties.setProperty = __set_property__ Properties.__orig_getitem__ = Properties.__getitem__ Properties.__getitem__ = __getitem__ Properties.__setitem__ = __set_property__ Properties.keys = Properties.propertyKeys Properties.values = __properties_values__ Properties.items = __properties_items__