from..Unitsimport_Units# Need to import so that we have GeneralUnitfrom._Baseimport*importatexitas_atexit_wrap_functions=[]_base_wrap=wrap@_atexit.registerdef_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]defwrap(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. """ifisinstance(value,bool):returnBooleanProperty(value)elifisinstance(value,int)orisinstance(value,float):returnNumberProperty(value)# is this a unit?try:u=_Units.GeneralUnit(value)ifnotu.is_dimensionless():returnwrap(u)exceptException:passifisinstance(value,str):returnStringProperty(value)elifisinstance(value,list):# do the number liststry:returnIntegerArrayProperty(value)exceptException:passtry:returnDoubleArrayProperty(value)exceptException:pass# do the string listis_strings=Trueforiteminvalue:ifnotisinstance(item,str):is_strings=Falsebreakelse:try:u=_Units.GeneralUnit(item)ifnotu.is_dimensionless():is_strings=FalsebreakexceptException:passifis_strings:try:returnStringArrayProperty(value)exceptException:passelif_Units.TempBaseintype(value).mro():# this is a temperaturereturnwrap(_Units.GeneralUnit(value))forfuncin_wrap_functions:try:returnfunc(value)exceptException:passtry:return_base_wrap(value)except:pass# if this is a dictionary, then wrap as a Properties objectiftype(value)isdict:p=Properties()forkey,valueinvalue.items():p[key]=valuereturnpelse:returnPropertyList(value)
_original_wrap=wrapdef_add_wrap_function(func):_wrap_functions.append(func)return_original_wrap# cludgy quick fix for an anaconda install_getBundledLibDir=getBundledLibDirdefgetBundledLibDir():try:return_getBundledLibDir()except:return"%s/lib"%getInstallDir()def__set_property__(obj,key,property):try:returnobj.__setProperty__(key,property)exceptExceptionase:ife.__class__.__name__=="ArgumentError":returnobj.__setProperty__(key,wrap(property))else:raiseedef__getitem__(props,i):try:returnprops.__orig_getitem__(i)exceptExceptionase:ife.__class__.__name__=="ArgumentError":key=props.propertyKeys()[i]val=props[key]return(key,val)else:raiseedef__properties_values__(props):vals=[]forkeyinprops.propertyKeys():vals.append(props.property(key))returnvalsdef__properties_items__(props):items=[]forkeyinprops.propertyKeys():items.append((key,props.property(key)))returnitemsProperties.__setProperty__=Properties.setPropertyProperties.setProperty=__set_property__Properties.__orig_getitem__=Properties.__getitem__Properties.__getitem__=__getitem__Properties.__setitem__=__set_property__Properties.keys=Properties.propertyKeysProperties.values=__properties_values__Properties.items=__properties_items__