def _getByIndex(self, item, index): location = self._validLocation (item) insts = self.getByClass(location.cls) if insts: try: return self._res[insts[index].location] except IndexError: raise ObjectNotFoundException("Couldn't found %s instance #%d." % (location, index)) else: raise ObjectNotFoundException("Couldn't found %s." % location)
def remove(self, location): """ Remove the object pointed by 'location' from the system stopping it before if needed. @param location: The object to remove. @type location: Location,str @raises ObjectNotFoundException: When te request object or the Manager was not found. @return: retuns True if sucessfull. False otherwise. @rtype: bool """ if location not in self.resources: raise ObjectNotFoundException("Location %s was not found." % location) self.stop(location) resource = self.resources.get(location) self.adapter.disconnect(resource.instance) self.resources.remove(location) return True
def _get (self, item): location = self._validLocation (item) if location in self: ret = filter(lambda x: x == location, self.keys()) return self._res[ret[0]] else: raise ObjectNotFoundException("Couldn't found %s." % location)
def _get(self, item): location = self._validLocation(item) locations = [x.location for x in self.getByClass(location.cls)] if location in locations: ret = filter(lambda x: x == location, self.keys()) return self._res[ret[0]] else: raise ObjectNotFoundException("Couldn't find %s." % location)
def start(self, location): """ Start the object pointed by 'location'. @param location: The object to start. @type location: Location or str @raises ObjectNotFoundException: When te request object or the Manager was not found. @raises ChimeraObjectException: Internal error on managed (user) object. @return: retuns True if sucessfull. False otherwise. @rtype: bool """ if location not in self.resources: raise ObjectNotFoundException("Location %s was not found." % location) log.info("Starting %s." % location) resource = self.resources.get(location) if resource.instance.getState() == State.RUNNING: return True try: resource.instance.__start__() except Exception: log.exception("Error running %s __start__ method." % location) raise ChimeraObjectException("Error running %s __start__ method." % location) try: # FIXME: thread exception handling # ok, now schedule object main in a new thread log.info("Running %s. __main___." % location) loop = threading.Thread(target=resource.instance.__main__) loop.setName(str(resource.location) + ".__main__") loop.setDaemon(True) loop.start() resource.instance.__setstate__(State.RUNNING) resource.created = time.time() resource.loop = loop return True except Exception, e: log.exception("Error running %s __main__ method." % location) resource.instance.__setstate__(State.STOPPED) raise ChimeraObjectException("Error running %s __main__ method." % location)
def stop(self, location): """ Stop the object pointed by 'location'. @param location: The object to stop. @type location: Location or str @raises ObjectNotFoundException: When the requested object or the Manager was not found. @raises ChimeraObjectException: Internal error on managed (user) object. @return: retuns True if sucessfull. False otherwise. @rtype: bool """ if location not in self.resources: raise ObjectNotFoundException("Location %s was not found." % location) log.info("Stopping %s." % location) resource = self.resources.get(location) try: # stop control loop if resource.loop and resource.loop.isAlive(): resource.instance.__abort_loop__() try: resource.loop.join() except KeyboardInterrupt: # ignore Ctrl+C on shutdown pass if resource.instance.getState() != State.STOPPED: resource.instance.__stop__() resource.instance.__setstate__(State.STOPPED) return True except Exception, e: log.exception( "Error running %s __stop__ method. Exception follows..." % location) raise ChimeraObjectException("Error running %s __stop__ method." % location)
def lookup(name): if not Simbad.__client: Simbad.__client = Client(Simbad.WSDL) client = Simbad.__client if name in Simbad.__cache: return Simbad.__cache[name] res = client.service.sesame(name, 'x', True) target = Simbad._parseSesame(res) if not target: raise ObjectNotFoundException("Couldn't find %s on SIMBAD" % name) Simbad.__cache[name] = target return target
def getProxy(self, location, name='0', host=None, port=None, lazy=False): """ Get a proxy for the object pointed by location. The given location can contain index instead of names, e.g. '/Object/0' to get objects when you don't know their names. location can also be a class. getProxy will return an instance named 'name' at the given host/port (or on the current manager, if None given). host and port parameters determines which Manager we will lookup for location/instance. If None, look at this Manager. host/port is only used when location is a class, otherwise, host and port are determined by location itself. lazy parameter determines if Manager will try to locate the selected Manager at host/port and ask them for a valid object/instance. If False, Manager just return an proxy for the selected parameters but can't guarantee that the returned Proxy have an active object bounded. For objects managed by this own Manager, lazy is always False. @param location: Object location or class. @type location: Location or class @param name: Instance name. @type name: str @param host: Manager's hostname. @type host: str @param port: Manager's port. @type port: int @param lazy: Manager's laziness (check for already bound objects on host/port Manager) @type lazy: bool @raises NotValidChimeraObjectException: When a object which doesn't inherites from ChimeraObject is given in location. @raises ObjectNotFoundException: When te request object or the Manager was not found. @raises InvalidLocationException: When the requested location s invalid. @return: Proxy for selected object. @rtype: Proxy """ if not location: raise ObjectNotFoundException("Couldn't find an object at the" " given location %s" % location) if not isinstance(location, StringType) and not isinstance( location, Location): if issubclass(location, ChimeraObject): location = Location(cls=location.__name__, name=name, host=host or self.getHostname(), port=port or self.getPort()) else: raise NotValidChimeraObjectException( "Can't get a proxy from non ChimeraObject's descendent object (%s)." % location) else: location = Location(location, host=host or self.getHostname(), port=port or self.getPort()) # who manages this location? if self._belongsToMe(location): ret = self.resources.get(location) if not ret: raise ObjectNotFoundException("Couldn't found an object at the" " given location %s" % location) p = Proxy(uri=ret.uri) if lazy: return p else: p.ping() return p else: if lazy: return Proxy(location) else: # contact other manager try: other = Proxy(location=MANAGER_LOCATION, host=location.host or host, port=location.port or port) except Pyro.errors.URIError, e: raise InvalidLocationException( "Invalid remote location given. '%s' (%s)." % (location, str(e))) if not other.ping(): raise ObjectNotFoundException( "Can't contact %s manager at %s." % (location, other.URI.address)) proxy = other.getProxy(location) if not proxy: raise ObjectNotFoundException( "Couldn't find an object at the" " given location %s" % location) else: return proxy
def process(self, exposure): self.log.debug('Acquiring proxies...') telescope = self.getManager().getProxy(self['telescope']) dome = self.getManager().getProxy(self['dome']) camera = self.getManager().getProxy(self['camera']) filterwheel = self.getManager().getProxy(self['filterwheel']) observation = exposure.observation if observation == None: raise ObjectNotFoundException( 'Unable to find associated observation') program = observation.program if program == None: raise ObjectNotFoundException('Unable to find associated program') self.log.debug('Attempting to slew telescope to ' + observation.targetPos.__str__()) telescope.slewToRaDec(observation.targetPos) self.log.debug('Setting filter to ' + str(exposure.filter) + '...') filterwheel.setFilter(str(exposure.filter)) while (telescope.isSlewing() or dome.notSyncWithTel()): self.log.debug('Waiting for slew to finish. Dome: ' + dome.isSlewing().__str__() + '; Tel:' + telescope.isSlewing().__str__()) time.sleep(1) self.log.debug('Telescope Slew Complete') #FIXME: filterwheel doesn't respond properly! #while (str(filterwheel.getFilter()) != str(exposure.filter)): # self.log.debug('Waiting for filterwheel to finish. Current: ' + filterwheel.getFilter().__str__() + '; Wanted: ' + exposure.filter.__str__()) # filterwheel.setFilter(str(exposure.filter)) # time.sleep(1) if (str(filterwheel.getFilter()) != str(exposure.filter)): self.log.warning( 'Filterwheel didn\'t behave as expected. Current: ' + filterwheel.getFilter().__str__() + '; Wanted: ' + exposure.filter.__str__()) self.log.debug('Filter set') self.log.debug('Generating exposure request..') if exposure.shutterOpen: shutter = Shutter.OPEN else: shutter = Shutter.CLOSE ir = ImageRequest(exptime=exposure.duration, shutter=shutter, frames=exposure.frames, type=str(exposure.imageType), filename=exposure.filename) ir.headers += [ ('OBJECT', str(observation.targetName), 'Object name'), ('TRGT_RA', observation.targetPos.ra.__str__(), 'Target RA'), ('TRGT_DEC', observation.targetPos.dec.__str__(), 'Target DEC'), ('PROGRAM', str(program.caption), 'Program Name'), ('PROG_PI', str(program.pi), 'Program\'s PI') ] ir.metadatapre = [ self.hostPort + self['telescope'], self.hostPort + self['camera'], self.hostPort + self['filterwheel'], self.hostPort + self['focuser'], self.hostPort + self['dome'], self.hostPort + self['site'] ] self.log.info('Running exposure ' + str(ir)) try: camera.expose(ir) except Exception, e: print ''.join(Pyro.util.getPyroTraceback(e))