def init_ports(self): if self.hub_port == self.port: raise TraitError("The hub and proxy cannot both listen on port %i" % self.port) if self.hub_port == self.proxy_api_port: raise TraitError("The hub and proxy API cannot both listen on port %i" % self.hub_port) if self.proxy_api_port == self.port: raise TraitError("The proxy's public and API ports cannot both be %i" % self.port)
def _signature_scheme_changed(self, name, old, new): if not new.startswith('hmac-'): raise TraitError("signature_scheme must start with 'hmac-', got %r" % new) hash_name = new.split('-', 1)[1] try: self.digest_mod = getattr(hashlib, hash_name) except AttributeError: raise TraitError("hashlib has no such attribute: %s" % hash_name)
def _notebook_dir_changed(self, name, old, new): """do a bit of validation of the notebook dir""" if os.path.exists(new) and not os.path.isdir(new): raise TraitError("notebook dir %r is not a directory" % new) if not os.path.exists(new): self.log.info("Creating notebook dir %s", new) try: os.mkdir(new) except: raise TraitError("Couldn't create notebook dir %r" % new)
def _notebook_dir_changed(self, name, old, new): """Do a bit of validation of the notebook dir.""" if not os.path.isabs(new): # If we receive a non-absolute path, make it absolute. self.notebook_dir = os.path.abspath(new) return if os.path.exists(new) and not os.path.isdir(new): raise TraitError("notebook dir %r is not a directory" % new) if not os.path.exists(new): self.log.info("Creating notebook dir %s", new) try: os.mkdir(new) except: raise TraitError("Couldn't create notebook dir %r" % new)
def _checkpoint_dir_changed(self, name, old, new): """do a bit of validation of the checkpoint dir""" if not os.path.isabs(new): # If we receive a non-absolute path, make it absolute. abs_new = os.path.abspath(new) self.checkpoint_dir = abs_new return if os.path.exists(new) and not os.path.isdir(new): raise TraitError("checkpoint dir %r is not a directory" % new) if not os.path.exists(new): self.log.info("Creating checkpoint dir %s", new) try: os.mkdir(new) except: raise TraitError("Couldn't create checkpoint dir %r" % new)
def _min_validate(self, min, trait): """Enforce min <= value <= max""" if min > self.max: raise TraitError("Setting min > max") if min > self.value: self.value = min return min
def _max_validate(self, max, trait): """Enforce min <= value <= max""" if max < self.min: raise TraitError("setting max < min") if max < self.value: self.value = max return max
def _notebook_dir_changed(self, name, old, new): """Do a bit of validation of the notebook dir.""" if not os.path.isabs(new): # If we receive a non-absolute path, make it absolute. self.notebook_dir = os.path.abspath(new) return if not os.path.exists(new) or not os.path.isdir(new): raise TraitError("notebook dir %r is not a directory" % new)
def _figure_formats_changed(self, name, old, new): from IPython.core.pylabtools import select_figure_formats if 'jpg' in new or 'jpeg' in new: if not pil_available(): raise TraitError("Requires PIL/Pillow for JPG figures") if self.shell is None: return else: select_figure_formats(self.shell, new)
def _db_changed(self, name, old, new): """validate the db, since it can be an Instance of two different types""" connection_types = (DummyDB, ) if sqlite3 is not None: connection_types = (DummyDB, sqlite3.Connection) if not isinstance(new, connection_types): msg = "%s.db must be sqlite3 Connection or DummyDB, not %r" % \ (self.__class__.__name__, new) raise TraitError(msg)
def validate(self, obj, value): # cast to proper unicode value = super(FilenameExtension, self).validate(obj, value) # check that it starts with a dot if value and not value.startswith('.'): msg = "FileExtension trait '{}' does not begin with a dot: {!r}" raise TraitError(msg.format(self.name, value)) return value
def validate(self, obj, value): # this just checks instance val = super(NDArray, self).validate(obj, value) # maybe check dtype castable # TODO exact dtype match dt = self.get_metadata('dtype') if dt: if not can_cast(dtype(dt), value.dtype): msg = 'Expected type castable with %r, received %r' msg %= (dtype(dt), value.dtype) raise TraitError(msg) # maybe check shape shape = self.get_metadata('shape') if shape: # TODO implement implicit transpose, shape of (-1, 3), value.shape(3, -1), transpose it. # TODO implement exact shape mode, len(shape) == len(value.shape) if len(shape) > len(value.shape): msg = 'Expected at least %d dimensions, received %d' msg %= (len(shape), len(value.shape)) raise t.TraitError(msg) # broadcast mode, ignore 1s and start from right if self.get_metadata('bcast'): for i, (e, v) in enumerate( zip(reversed(shape), reversed(value.shape))): if not (e == -1 or (e == 1 or v == 1) or e == v): msg = 'Expected axis %d to have dim %d, received %d' msg %= (len(value.shape) - i, e, v) raise TraitError(msg) # normal, ignore -1 and start from left else: for i, (e, v) in enumerate(zip(shape, value.shape)): if not (e == -1 or e == v): msg = 'Expected axis %d to have dim %d, received %d' msg %= (i, e, v) raise TraitError(msg) return val
def _notebook_dir_changed(self, name, old, new): """Do a bit of validation of the notebook dir.""" if not os.path.isabs(new): # If we receive a non-absolute path, make it absolute. self.notebook_dir = os.path.abspath(new) return if not os.path.isdir(new): raise TraitError("No such notebook dir: %r" % new) # setting App.notebook_dir implies setting notebook and kernel dirs as well self.config.FileContentsManager.root_dir = new self.config.MappingKernelManager.root_dir = new
def error(self, obj, value, reason=None): if reason is None: return super(FilePath, self).error(obj, value) if reason == 'exist': e = "The '%s' trait of %s instance must exist, but '%s' does not exist on disk." elif reason == 'file': e = "The '%s' trait of %s instance must be a file, '%s' is not." elif reason == 'dir': e = "The '%s' trait of %s instance must be a dir, but '%s' is not." elif reason == 'extension': e = "The '%s' trait of %s instance must end with {0}, but '%s' does not.".format( self._extension) raise TraitError(e % (self.name, class_of(obj), value))
def _pre_save_hook_changed(self, name, old, new): if new and isinstance(new, string_types): self.pre_save_hook = import_item(self.pre_save_hook) elif new: if not callable(new): raise TraitError("pre_save_hook must be callable")
def _hub_prefix_changed(self, name, old, new): if new == '/': raise TraitError("'/' is not a valid hub prefix") if not new.startswith(self.base_url): self.hub_prefix = url_path_join(self.base_url, new)
def _figure_formats_changed(self, name, old, new): if 'jpg' in new or 'jpeg' in new: if not pil_available(): raise TraitError("Requires PIL/Pillow for JPG figures") self._update_figure_formatters()
def _value_names_changed(self, name, old, new): if not self._in_values_changed: raise TraitError("value_names is a read-only proxy to values.keys(). Use the values dict instead.")
def _values_readonly_changed(self, name, old, new): if not self.values_lock.locked(): raise TraitError( "`.%s` is a read-only trait. Use the `.values` tuple instead." % name)
def _bar_validate(self, value, trait): if value: raise TraitError('foobar') return value
def _b_validate(self, value, trait): if value != 0: raise TraitError('Only 0 is a valid value') return value
def _value_validate(self, value, trait): if self.parity == 'even' and value % 2: raise TraitError('Expected an even number') if self.parity == 'odd' and (value % 2 == 0): raise TraitError('Expected an odd number') return value
def validate(self, obj, value): if self.min and V(value) < V(self.min): raise TraitError("bad version: %s < %s" % (value, self.min)) if self.max and (V(value) > V(self.max)): raise TraitError("bad version: %s > %s" % (value, self.max))
def _quality_changed(self, name, old, new): if new < 10 or new > 100: raise TraitError("figure JPEG quality must be in [10-100] range.")