def op_data(self, content, headers, msg): """ @brief: this method is invoked when data arrives off an AMQP Queue @param content Message object which could be DAP, Dictionary or String @param headers Ignored @param msg Used to route the reply, otherwise ignored """ logging.info(self.__class__.__name__ + ', MSG Received: ' + str(headers)) logging.info(self.__class__.__name__ + '; Calling data process!') # Keep track of how many messages you got self.receive_cnt[headers.get('receiver')] += 1 # Unpack the message and save it to disk! datamessage = dataobject.DataObject.decode(content) if isinstance(datamessage, DAPMessageObject): dataset = dap_tools.dap_msg2ds(datamessage) # Call preserve DAP data fname = self.params['filename'] name = os.path.basename(fname) dataset.name = name path = fname.replace(name, '') logging.info('name: ' + name) logging.info('fname:' + fname) logging.info('path:' + path) retval = self._save_dap_dataset(dataset, path) logging.info("retval from _save_dap_dataset is:" + str(retval)) if retval == 1: raise RuntimeError("Archive file does not exist") if retval == 2: raise RuntimeError("Problem with NCA handler") elif isinstance(datamessage, (DictionaryMessageObject, StringMessageObject)): data = datamessage.data fname = self.params['filename'] logging.info("Writing Dictionary or String to " + fname) f = open(fname, "w") f.write(data) f.close() #Call preserve dict or string #self._save_string_dataset(data,fname=self.params['filename']) else: raise RuntimeError( "Persister Service received an incompatible message.") # Later - these will be sent to a historical log for the dataset... notification = datamessage.notification timestamp = datamessage.timestamp
def get_parent(self, parent_klass): """Return one member of self._parents of class 'parent_klass'. Raises an exception if there are multiple matches or no matches.""" parents_filtered = [p for p in self._parents if isinstance(p, parent_klass)] if not parents_filtered: raise RuntimeError("No parents of class %s" % parent_klass) elif len(parents_filtered) > 1: raise RuntimeError("Multiple parents of class %s" % parent_klass) else: return parents_filtered[0]
def read_disputed(path): disp_file = os.path.join(path, "metadata.disputed") if not os.path.exists(disp_file): raise RuntimeError('No metadata.tags file at %s' % path) try: disp_str = open(disp_file, 'r').read() disputed_list = eval(disp_str) except Exception as e: raise RuntimeError("Cannot deserialize set of tags %s at %s: e" % (disp_str, path, e)) return disputed_list
def read_tags(path): tag_file = os.path.join(path, "metadata.tags") if not os.path.exists(tag_file): raise RuntimeError('No metadata.tags file at %s' % path) try: tag_str = open(tag_file, 'r').read() tags = eval(tag_str) except Exception as e: raise RuntimeError("Cannot deserialize set of tags %s at %s: e" % (tag_str, path, e)) return {item[0]: item[1] for item in tags}
def set_display_text(self, text): """Used to display text on the display, max 14 characters, NOTE: does *not* set display mode, you need to do it yourself""" if len(text) > 14: raise RuntimeError("Max text length is 14 characters") if ('"' in text and "'" in text): raise RuntimeError( "Text may only contain either single or double quotes, not both" ) if '"' in text: return self.scpi.send_command("DISP:TEXT '%s'" % text, False) return self.scpi.send_command('DISP:TEXT "%s"' % text, False)
def read_geometry(path, simplified=False): ext = "simple_geom" if simplified else "geom" geom_file = os.path.join(path, "metadata." + ext) if not os.path.exists(geom_file): raise RuntimeError('No metadata geometry file at %s' % geom_file) try: wkb = open(geom_file, 'r').read() geom = wkblib.loads(wkb, hex=True) except Exception as e: raise RuntimeError("Failed to load geometry", e.message, geom_file) return geom
def attrs_to_id_tuple(cls, attrs, null_missing): """Serialized ID for use in StorageResourceRecord.storage_id_str""" identifier_val = [] for f in cls._meta.identifier.id_fields: if not f in cls._meta.storage_attributes: raise RuntimeError("Invalid attribute %s named in identifier for %s" % (f, cls)) if f in attrs: identifier_val.append(attrs[f]) else: if cls._meta.storage_attributes[f].optional or null_missing: identifier_val.append(None) else: raise RuntimeError("Missing ID attribute '%s'" % f) return tuple(identifier_val)
def chain(self, keys=None): if self._key_list is None and keys is None: raise RuntimeError('No key found') if keys is None: keys = self._key_list sql = 'select `{0}` from `{1}` where {2}'.format( '`,`'.join(self._columns), self._table_name, ' and '.join(['`{0}`=%({0})s'.format(key) for key in keys])) if self._order_by is not None: sql = sql + ' order by `{0}`'.format('`,`'.join(self._order_by)) sql = sql + ' limit 1 ' try: dbc = DBConnector.connect() c = dbc.cursor(dictionary=True) c.execute(sql, (self.get_keys(keys))) r = c.fetchone() if r == None: return False self.set_value(r) return True except Exception as e: print e return False finally: c.close() dbc.close()
def set_display_mode(self, mode): """Set the display mode, valied values are NORM and TEXT""" mode = mode.upper() if not mode in ('NORM', 'TEXT'): raise RuntimeError( "Invalid mode %s, valid ones are NORM and TEXT" % mode) return self.scpi.send_command("DISP:MODE %s" % mode, False)
def _Darwin_init(): """\ Sets functions that retrieves time based on system monotonic timer. For mac os x (Darwin kernel) this is based on mach_absolute_time() """ import ctypes try: libc = ctypes.CDLL('libc.dylib', use_errno=True) except OSError: # We may be running afoul of OSX 10.11 System Integrity Protection. # Try by full and real pathname libc = ctypes.CDLL('/usr/lib/libSystem.dylib', use_errno=True) mach_absolute_time = libc.mach_absolute_time mach_timebase_info = libc.mach_timebase_info nanosleep = libc.nanosleep class mach_timebase_info_t(ctypes.Structure): _fields_ = [ ('numer', ctypes.c_uint32), ('denom', ctypes.c_uint32) ] class timespec(ctypes.Structure): _fields_ = [ ('tv_sec', ctypes.c_long), ('tv_nsec', ctypes.c_long) ] # set return type to match that of the function (ctypes assumes int32) mach_absolute_time.restype = ctypes.c_uint64 # set the argument type to match that of the function mach_timebase_info.argtypes = [ctypes.POINTER(mach_timebase_info_t)] nanosleep.argtypes = [ctypes.POINTER(timespec), ctypes.POINTER(timespec)] # fetch timebase info to calculate the multiplier/divider factor tb_info = mach_timebase_info_t() retval = mach_timebase_info(ctypes.pointer(tb_info)) if retval != 0: raise RuntimeError("Failure calling mach_timebase_info - return code %d" % retval) dividerSecs = 1e9 * tb_info.denom / tb_info.numer dividerNanos = tb_info.denom / tb_info.numer dividerMicros = 1000 * tb_info.denom / tb_info.numer def time(): return mach_absolute_time() / dividerSecs def timeNanos(): return mach_absolute_time() / dividerNanos def timeMicros(): return mach_absolute_time() / dividerMicros def sleep(t): if t<=0: return ts = timespec() ts.tv_sec = int(t) ts.tv_nsec = int((t % 1) * 1000000000) retval = nanosleep(ctypes.pointer(ts), None) if retval: raise InterruptedException("Signal interrupted sleep") _bind(time, timeNanos, timeMicros, sleep)
def __init__(self, address=None): # load dll if needed init() self.last = "" # if no address specified, find first device automatically if address is None: addresses = get_addresses() if len(addresses) > 0: address = addresses[0] else: raise RuntimeError("No laser connected") # assume address is a USBAddress object, otherwise treat as # the device ID try: id = address.id except AttributeError: try: id = int(address) except (TypeError, ValueError): raise TypeError("Expected a USBAddress or int for address") self.devid = ctypes.c_long(id) self.buff = ctypes.create_string_buffer(64) self.n = ctypes.c_ulong(0) self.verbose = True
def __set__(self, obj, image): image_info = getattr(obj, self.prop_name, {}) if not image: image_info["source"] = "" image_info["image"] = fife.GuiImage() image_info["image"]._source = "" self._getSetter(obj)(None) elif isinstance(image, str): image_info["source"] = image # to catch or not to catch ... # we just let the NotFound exception trickle here. # depedning on complains we can catch and print a warning. image_info["image"] = get_manager().loadImage(image) image_info["image"].source = image self._getSetter(obj)(image_info["image"]) elif isinstance(image, fife.GuiImage): # FIXME - this trickery with the hidden annotation # with an _source attribute isn't really clean. # Is it even necessary image_info["source"] = getattr(image, "source", "") image_info["image"] = image if image_info["source"]: image_info["image"] = get_manager().loadImage( image_info["source"]) self._getSetter(obj)(image_info["image"]) else: attribute_name = "%s.%s" % (obj.__class__.__name__, self.name) error_message = "%s only accepts GuiImage and python strings, not '%s'" raise RuntimeError(error_message % (attribute_name, repr(image))) setattr(obj, self.prop_name, image_info)
def build(self, *new_vargs, **new_kwargs): """ Actually build an instance of the requested histogram object. Arguments passed to this method will be combined with those passed to the initialisation method. If new_kwargs contains the keyword labels, this will be used to format the name and title strings of the produced histogram, and then removed from new_kwargs Parameters: new_vargs -- appended to vargs that were passed to the factory's init new_kwargs -- updates the kwargs that were passed to the factory's init """ if self.build_err: raise RuntimeError(self.build_err) vargs = deepcopy(self.vargs) + new_vargs kwargs = deepcopy(self.kwargs) kwargs.update(new_kwargs) if "labels" in kwargs: for attr in ["title", "name"]: if attr in kwargs: new_attr = kwargs[attr].format(**kwargs["labels"]) kwargs[attr] = new_attr if "labels" in kwargs: kwargs.pop('labels') hist = self.hist_class(*vargs, **kwargs) return hist
def get_network_layer_output(model, dataInput, layerNum, **kwargs): """ :param model: :param dataInput: :param layerNum: :param kwargs: :return: """ get_output = K.function( [model.layers[0].input, K.learning_phase()], [model.layers[layerNum].output]) phase = kwargs.get('phase', None) if phase is None or phase == 'test': # output in test mode = 0 layer_output = get_output([dataInput, 0])[0] elif phase == 'train': # output in train mode = 1 layer_output = get_output([dataInput, 1])[0] else: raise RuntimeError("invalid phase passed to get_network_layer_output") return layer_output
def set_palette(palette): if palette in __known_root_pallettes: gStyle.SetPalette(getattr(ROOT, "k" + palette)) palette = root_palette else: raise RuntimeError("Unknown palette requested: " + palette) return palette
def uncache(image, test, exclusive=False): def testlayer(layer): return filter(test, commands(layer)) layers = list(hierarchy(image)) if not layers: raise RuntimeError("No layers found") if len(layers[0]["RepoTags"]) > 1: raise LayersException("Image has multiple tags", layers[:1]) limit = indexIf(testlayer, layers) if limit is None: raise RuntimeError("No matching layer") if not exclusive: limit += 1 strip = layers[:limit] if not strip: raise RuntimeError("No layers to uncache") ancestors = filter(lambda i: i["RepoTags"], strip[1:]) if ancestors: raise LayersException("Tagged ancestors are preventing uncaching", ancestors) anchor = strip[-1]["Parent"] if anchor: execute(["docker", "tag", anchor[:14], "uncache:keep"]) execute(["docker", "rmi", image]) if dry_run: return remaining = filter(lambda i: exists(i["Id"]), strip) if remaining: raise LayersException("Some layers were not pruned", remaining)
def enableBeep(self, enable=True): """ BEEP<Boolean> """ self.serial.write(b'BEEP%d\n' % int(enable)) err = self.getError() if err != b'No Error.': raise RuntimeError(err)
def enableOutput(self, enable=True): """ OUT<Boolean> """ self.serial.write(b'OUT%d\n' % int(enable)) err = self.getError() if err != b'No Error.': raise RuntimeError(err)
def selectIndependentMode(self): """ TRACK<NR1> """ self.serial.write(b'TRACK0\n') err = self.getError() if err != b'No Error.': raise RuntimeError(err)
def selectTrackingParallelMode(self): """ TRACK<NR1> """ self.serial.write(b'TRACK2\n') err = self.getError() if err != b'No Error.': raise RuntimeError(err)
def isValidMemory(self, memory): """ Check if the given memory number is valid or not. Only memory 1 to 2 are allowed. """ if not (memory <= 0 or 4 < memory): raise RuntimeError('Invalid memory number: %d was given.' % memory) return True
def set_remote_mode(self, state=True): """RS232 only, prevent accidental button mashing on the fron panel, this switches between SYSTem:REMote and SYSTem:LOCal according to state, this overrides previous value set with set_rwlock""" from scpi.transports import rs232 if not isinstance(self.scpi.transport, rs232): from exceptions import RuntimeError raise RuntimeError("Only usable with RS232 transports") if state: return self.scpi.send_command("SYST:REM", False) return self.scpi.send_command("SYST:LOC", False)
def __init__(self, **root_info_data): if not root_info_data: raise RuntimeError( "Tree should be initialised with root node data") self.update_hash_queue = set() self.root = Node(0, self.update_hash_queue, _depth=0, **root_info_data) self.root._parent = self.root self._last_pk = 0 self._pk_to_node_mapper = {0: self.root}
def _is_valid(self): # [todo] Check there are no empty files. conf_file = join(self.base_dir, 'nagios.cfg') if not exists(self.bin): raise RuntimeError("Can not find nagios binary: {0}".format( self.bin)) cmd = '%s -v %s > /dev/null 2>&1' % (self.bin, conf_file) return run(cmd) == 0
def isValidChannel(self, channel): """ Check if the given channel number is valid or not. Only channel 1 and 2 are allowed. """ if not (channel == 1 or channel == 2): raise RuntimeError('Invalid channel number: %d was given.' % channel) return True
def isValidChannel(self, channel): """ Check if the given channel number is valid or not. Only channels 1 to 4 are allowed. """ if not (1 <= channel <= 4): raise RuntimeError('Invalid channel number: %d was given.' % channel) return True
def getError(self): """ ERR? """ self.serial.write(b'ERR?\n') ret = self.serial.readline(eol=self.eol) if ret != b'': return ret[:-len(self.eol)] else: raise RuntimeError('Cannot read error message')
def saveSetting(self, memory): """ SAV<NR1> """ self.isValidMemory(memory) self.serial.write(b'SAV%d\n' % memory) err = self.getError() if err != b'No Error.': raise RuntimeError(err)
def setVoltage(self, channel, voltage): """ VSET<X>:<NR2> """ self.isValidChannel(channel) self.serial.write(b'VSET%d:%.3f\n' % (channel, voltage)) err = self.getError() if err != b'No Error.': raise RuntimeError(err)
def setCurrent(self, channel, current): """ ISET<X>:<NR2> """ self.isValidChannel(channel) self.serial.write(b'ISET%d:%.3f\n' % (channel, current)) err = self.getError() if err != b'No Error.': raise RuntimeError(err)
def __init__(self, message, failure_code): # Call the base class constructor with the parameters it needs RuntimeError.__init__(self, message) self.failure_code = failure_code