def OnInit(self): frame = garlicsim_wx.Frame(parent=None, title='GarlicSim', size=(1140, 850)) self.frame = frame self.SetTopWindow(frame) if self.new_gui_project_simpack_name is not None: simpack = \ import_tools.normal_import(self.new_gui_project_simpack_name) wx.CallAfter( functools.partial(self.frame._new_gui_project_from_simpack, simpack)) if self.load_gui_project_file_path is not None: wx.CallAfter( functools.partial(self.frame._open_gui_project_from_path, self.load_gui_project_file_path)) return True
def _get_from_state_class(state_class): ''' Find the simpack that a state class belongs to. Internal use. ''' assert state_class.__name__ == 'State' # remove this limitation short_address = address_tools.describe(state_class, shorten=True) simpack_name = '.'.join(short_address.split('.')[:-1]) simpack = import_tools.normal_import(simpack_name) garlicsim.misc.simpack_grokker.SimpackGrokker(simpack) # Not saving the reference: But it'll get cached because `SimpackGrokker` # is a `CachedType`. return simpack
def get_object_by_address(address, root=None, namespace={}): ''' Get an object by its address. For example: >>> get_object_by_address('email.encoders') <module 'email.encoders' from 'c:\Python27\lib\email\encoders.pyc'> `root` is an object (usually a module) whose attributes will be looked at when searching for the object. `namespace` is a `dict` whose keys will be searched as well. ''' # todo: should know what exception this will raise if the address is bad / # object doesn't exist. if not _address_pattern.match(address): raise ValueError("'%s' is not a legal address." % address) ########################################################################### # Before we start, we do some pre-processing of `root` and `namespace`: # # We are letting the user input (base)strings for `root` and `namespace`, # so if he did that, we'll get the actual objects. if root: # First for `root`: if isinstance(root, basestring): root = get_object_by_address(root) root_short_name = root.__name__.rsplit('.', 1)[-1] if namespace: # And then for `namespace`: if isinstance(namespace, basestring): namespace = get_object_by_address(namespace) parent_object, namespace_dict = _get_parent_and_dict_from_namespace( namespace) else: parent_object, namespace_dict = None, None # Finished pre-processing `root` and `namespace`. # ########################################################################### ########################################################################### # The implementation is recursive: We handle the case of a single-level # address, like 'email'. If we get a multi-level address (i.e. contains a # dot,) like 'email.encoders', we use this function twice, first to get # `email`, and then from it to get `email.encoders`. if '.' not in address: ### Here we solve the basic case of a single-level address: ########### # # # Let's rule out the easy option that the requested object is the root: if root and (address == root_short_name): return root if parent_object: if isinstance(parent_object, types.ModuleType) and \ hasattr(parent_object, '__path__'): # `parent_object` is a package. The wanted object may be a # module. Let's try importing it: import_tools.import_if_exists('.'.join( (parent_object.__name__, address)), silent_fail=True) # Not keeping reference, just importing so we could get later. # We know we have a `namespace_dict` to take the object from, and we # might have a `parent_object` we can take the object from by using # `getattr`. We always have a `namespace_dict`, but not always a # `parent_object`. # # We are going to prefer to do `getattr` from `parent_object`, if one # exists, rather than using `namespace_dict`. This is because some # attributes may not be present on an object's `__dict__`, and we want # to be able to catch them: # The first place we'll try to take the object from is the # `parent_object`. We try this before `namespace_dict` because # `parent_object` may have `__getattr__` or similar magic and our # object might be found through that: if parent_object and hasattr(parent_object, address): return getattr(parent_object, address) # Next is the `namespace_dict`: elif namespace_dict and (address in namespace_dict): return namespace_dict[address] # Last two options: else: try: # It may be a built-in: return eval(address) except Exception: # Or a module: return import_tools.normal_import(address) # # ### Finished solving the basic case of a single-level address. ######## else: # '.' in address ### If we get a composite address, we solve recursively: ############## # # first_object_address, second_object_address = address.rsplit('.', 1) first_object = get_object_by_address(first_object_address, root=root, namespace=namespace) second_object = get_object_by_address(second_object_address, namespace=first_object) return second_object
def get_simpack_selection(self): '''Import the selected simpack and return it.''' string = self.list_box.GetStringSelection() result = import_tools.normal_import(string) return result