def __setitem__(self, name, vals): """ Set a parameter """ if name not in self._rd: raise KeyError(name) context = self._pdict.get_context(name) if self._shp is None: # Not initialized: if isinstance(vals, np.ndarray): self._shp = vals.shape elif isinstance(vals, list): self._shp = (len(vals),) else: raise BadRequest('No shape was defined') log.trace('Set shape to %s', self._shp) else: if isinstance(vals, np.ndarray): validate_equal(vals.shape, self._shp, 'Invalid shape on input') elif isinstance(vals, list): validate_equal(len(vals), self._shp[0], 'Invalid shape on input') dom = self.domain paramval = get_value_class(context.param_type, domain_set = dom) paramval[:] = vals paramval.storage._storage.flags.writeable = False self._rd[name] = paramval
def _parent_topics(self, topic_id): nodes = [topic_id] done=False while not done: parents, assocs = self.clients.resource_registry.find_subjects(object=topic_id, predicate=PRED.hasTopic, subject_type=RT.Topic, id_only=True) if not parents: done = True else: validate_equal(len(parents),1, 'Found a topic with more than one parent.') topic_id = parents[0] nodes.append(topic_id) return nodes
def create_topic(self, name='', exchange_point='', parent_topic_id='', description=''): validate_true(exchange_point, 'An exchange point must be provided for the topic') name = name or create_unique_identifier() topic = Topic(name=name, description=description, exchange_point=exchange_point) if parent_topic_id: parent_topic = self.read_topic(parent_topic_id) validate_equal(exchange_point, parent_topic.exchange_point, 'Can not make a sub-topic with a different exchange point') topic_id, rev = self.clients.resource_registry.create(topic) if parent_topic_id: self._associate_topic_with_topic(parent_topic_id, topic_id) return topic_id
def test_validations(self): import pyon.util.arg_check as arg_check with self.assertRaises(BadRequest): arg_check.validate_true(False, 'test') with self.assertRaises(BadRequest): arg_check.validate_equal(3, 4, 'test') with self.assertRaises(BadRequest): arg_check.validate_not_equal(4, 4, 'test') with self.assertRaises(BadRequest): arg_check.validate_false(True, 'test') with self.assertRaises(BadRequest): one = list() two = list() arg_check.validate_is(one, two, 'test') with self.assertRaises(BadRequest): one = list() two = one arg_check.validate_is_not(one, two, 'test') with self.assertRaises(BadRequest): c = None arg_check.validate_is_not_none(c, 'test') with self.assertRaises(BadRequest): one = list([1, 3]) two = 2 arg_check.validate_in(two, one, 'test') with self.assertRaises(BadRequest): one = list([1, 2, 3]) two = 2 arg_check.validate_not_in(two, one, 'test') with self.assertRaises(BadRequest): one = list() arg_check.validate_is_instance(one, dict, 'test') with self.assertRaises(BadRequest): one = list() arg_check.validate_not_is_instance(one, list, 'test')
def _set(self, name, vals): """ Set a parameter """ if name not in self.fields: raise KeyError(name) if vals is None: self._rd[name] = None return context = self._pdict.get_context(name) if self._shp is None and isinstance( context.param_type, (SparseConstantType, ConstantType, ConstantRangeType)): self._shp = (1, ) self._dirty_shape = True elif self._shp is None or self._dirty_shape: if isinstance(vals, np.ndarray): self._shp = (vals.shape[0], ) # Only support 1-d right now elif isinstance(vals, list): self._shp = (len(vals), ) else: raise BadRequest('No shape was defined') log.trace('Set shape to %s', self._shp) if self._dirty_shape: self._dirty_shape = False self._reshape_const() else: if isinstance(vals, np.ndarray): if not vals.shape: raise BadRequest('Invalid shape on input (dimensionless)') validate_equal( vals.shape[0], self._shp[0], 'Invalid shape on input (%s expecting %s)' % (vals.shape, self._shp)) elif isinstance(vals, list): validate_equal(len(vals), self._shp[0], 'Invalid shape on input') dom = self.domain paramval = self.get_paramval(context.param_type, dom, vals) self._rd[name] = paramval
def test_validations(self): import pyon.util.arg_check as arg_check with self.assertRaises(BadRequest): arg_check.validate_true(False,'test') with self.assertRaises(BadRequest): arg_check.validate_equal(3,4,'test') with self.assertRaises(BadRequest): arg_check.validate_not_equal(4,4,'test') with self.assertRaises(BadRequest): arg_check.validate_false(True,'test') with self.assertRaises(BadRequest): one = list() two = list() arg_check.validate_is(one,two,'test') with self.assertRaises(BadRequest): one = list() two = one arg_check.validate_is_not(one,two,'test') with self.assertRaises(BadRequest): c = None arg_check.validate_is_not_none(c,'test') with self.assertRaises(BadRequest): one = list([1,3]) two = 2 arg_check.validate_in(two,one,'test') with self.assertRaises(BadRequest): one = list([1,2,3]) two = 2 arg_check.validate_not_in(two,one,'test') with self.assertRaises(BadRequest): one = list() arg_check.validate_is_instance(one,dict,'test') with self.assertRaises(BadRequest): one = list() arg_check.validate_not_is_instance(one,list,'test')
def _parent_topics(self, topic_id): nodes = [topic_id] done = False while not done: parents, assocs = self.clients.resource_registry.find_subjects( object=topic_id, predicate=PRED.hasTopic, subject_type=RT.Topic, id_only=True) if not parents: done = True else: validate_equal(len(parents), 1, 'Found a topic with more than one parent.') topic_id = parents[0] nodes.append(topic_id) return nodes
def persist_file(self, file_data='', digest='', metadata=None): ds = self.container.datastore_manager.get_datastore( self.datastore_name, DS.DS_PROFILE.FILESYSTEM) validate_is_instance(file_data, basestring, "File or binary data must be a string.") validate_is_instance(metadata, File) if self.list_files(metadata.name + metadata.extension): raise BadRequest('%s already exists.' % metadata.name + metadata.extension) digest_ = sha224(file_data).hexdigest() if digest: validate_equal( digest, digest_, "The provided digest does not match the file's digest. Ensure you are using sha224." ) else: digest = digest_ extension = metadata.extension if '.' in metadata.name: t = metadata.name.split('.') metadata.name, metadata.extension = ('.'.join(t[:-1]), '.' + t[-1]) url = FileSystem.get_hierarchical_url(FS.CACHE, digest, extension) try: with open(url, 'w+b') as f: f.write(file_data) f.close() except Exception: log.exception('Failed to write %s', url) raise BadRequest('Could not successfully write file data') if metadata.name[0] != '/': metadata.name = '/' + metadata.name metadata.url = url metadata.digest = digest metadata.created_date = IonTime().to_string() metadata.modified_date = IonTime().to_string() metadata.size = len(file_data) doc_id, rev_id = ds.create(metadata) return doc_id
def _set(self, name, vals): """ Set a parameter """ if name not in self.fields: raise KeyError(name) if vals is None: self._rd[name] = None return context = self._pdict.get_context(name) if self._shp is None and isinstance(context.param_type, (SparseConstantType, ConstantType, ConstantRangeType)): self._shp = (1,) self._dirty_shape = True elif self._shp is None or self._dirty_shape: if isinstance(vals, np.ndarray): self._shp = (vals.shape[0],) # Only support 1-d right now elif isinstance(vals, list): self._shp = (len(vals),) else: raise BadRequest("No shape was defined") log.trace("Set shape to %s", self._shp) if self._dirty_shape: self._dirty_shape = False self._reshape_const() else: if isinstance(vals, np.ndarray): if not vals.shape: raise BadRequest("Invalid shape on input (dimensionless)") validate_equal( vals.shape[0], self._shp[0], "Invalid shape on input (%s expecting %s)" % (vals.shape, self._shp) ) elif isinstance(vals, list): validate_equal(len(vals), self._shp[0], "Invalid shape on input") # paramval = self.get_paramval(context.param_type, dom, vals) self._rd[name] = vals
def _set(self, name, vals): """ Set a parameter """ if name not in self.fields: raise KeyError(name) if vals is None: self._rd[name] = None return context = self._pdict.get_context(name) if self._shp is None and (isinstance(context.param_type, ConstantType) or isinstance(context.param_type, ConstantRangeType)): self._shp = (1,) self._dirty_shape = True elif self._shp is None or self._dirty_shape: if isinstance(vals, np.ndarray): self._shp = vals.shape elif isinstance(vals, list): self._shp = (len(vals),) else: raise BadRequest('No shape was defined') log.trace('Set shape to %s', self._shp) if self._dirty_shape: self._dirty_shape = False self._reshape_const() else: if isinstance(vals, np.ndarray): if not vals.shape: raise BadRequest('Invalid shape on input (dimensionless)') validate_equal(vals.shape[0], self._shp[0], 'Invalid shape on input (%s expecting %s)' % (vals.shape, self._shp)) elif isinstance(vals, list): validate_equal(len(vals), self._shp[0], 'Invalid shape on input') dom = self.domain paramval = self.get_paramval(context.param_type, dom, vals) self._rd[name] = paramval
def create_topic(self, name='', exchange_point='', parent_topic_id='', description=''): validate_true(exchange_point, 'An exchange point must be provided for the topic') name = name or create_unique_identifier() topic = Topic(name=name, description=description, exchange_point=exchange_point) if parent_topic_id: parent_topic = self.read_topic(parent_topic_id) validate_equal( exchange_point, parent_topic.exchange_point, 'Can not make a sub-topic with a different exchange point') topic_id, rev = self.clients.resource_registry.create(topic) if parent_topic_id: self._associate_topic_with_topic(parent_topic_id, topic_id) return topic_id
def persist_file(self, file_data='', digest='', metadata=None): ds = self.container.datastore_manager.get_datastore(self.datastore_name, DS.DS_PROFILE.FILESYSTEM) validate_is_instance(file_data,basestring, "File or binary data must be a string.") validate_is_instance(metadata,File) if self.list_files(metadata.name + metadata.extension): raise BadRequest('%s already exists.' % metadata.name + metadata.extension) digest_ = sha224(file_data).hexdigest() if digest: validate_equal(digest,digest_,"The provided digest does not match the file's digest. Ensure you are using sha224.") else: digest = digest_ extension = metadata.extension if '.' in metadata.name: t = metadata.name.split('.') metadata.name, metadata.extension = ('.'.join(t[:-1]), '.' + t[-1]) url = FileSystem.get_hierarchical_url(FS.CACHE, digest, extension) try: with open(url,'w+b') as f: f.write(file_data) f.close() except Exception: log.exception('Failed to write %s', url) raise BadRequest('Could not successfully write file data') if metadata.name[0] != '/': metadata.name = '/' + metadata.name metadata.url = url metadata.digest = digest metadata.created_date = IonTime().to_string() metadata.modified_date = IonTime().to_string() metadata.size = len(file_data) doc_id, rev_id = ds.create(metadata) return doc_id
def check_equality(self, a,b): ''' You needed to be sure that two items we're equivalent ''' validate_equal(a,b,'%s != %s' %(str(a), str(b))) return True
def check_equality(self, a, b): ''' You needed to be sure that two items we're equivalent ''' validate_equal(a, b, '%s != %s' % (str(a), str(b))) return True