Beispiel #1
0
	def __contains__(self, name):

		if not isinstance(name, cistr):
			try:
				name = cistr(name)
			except AttributeError:
				return False
		return name in self.__items__
Beispiel #2
0
	def __getitem__(self, name):

		if not isinstance(name, cistr):
			name = cistr(name)
		try:
			return self.__items__[name]
		except KeyError:
			raise osexception(u'No item named "%s"' % name)
Beispiel #3
0
	def __getitem__(self, name):

		if not isinstance(name, cistr):
			name = cistr(name)
		try:
			return self.__items__[name]
		except KeyError:
			raise osexception(u'No item named "%s"' % name)
Beispiel #4
0
	def __contains__(self, name):

		if not isinstance(name, cistr):
			try:
				name = cistr(name)
			except AttributeError:
				return False
		return name in self.__items__
Beispiel #5
0
	def valid_name(self, item_type, suggestion=None):

		"""
		desc:
			Generates a unique name that is valid and resembles the desired
			name.

		arguments:
			item_type:
				desc:	The type of the item to suggest a name for.
				type:	unicode

		keywords:
			suggestion:
				desc:	The desired name, or None to choose a name based on the
						item's type.
				type:	[unicode, NoneType]

		returns:
			desc:	A unique name.
			type:	unicode

		example: |
			valid_name = items.valid_name(u'sketchpad', u'an invalid name')
		"""

		if suggestion is None:
			name = u'new_%s' % item_type
		else:
			name = self.experiment.syntax.sanitize(suggestion, strict=True,
				allow_vars=False)
			# Empty names are not allowed, and we fall back to the item type
			if not name:
				name = item_type
			# Names cannot start with a number, and are therefore prefixed by
			# an underscore.
			elif name[0].isnumeric():
				name = u'_' + name
		_name = name
		invalid_names = list(self) + INVALID_NAMES
		i = 1
		while _name in invalid_names:
			_name = u'%s_%d' % (name, i)
			i += 1
		return cistr(_name)
Beispiel #6
0
	def valid_name(self, item_type, suggestion=None):

		"""
		desc:
			Generates a unique name that is valid and resembles the desired
			name.

		arguments:
			item_type:
				desc:	The type of the item to suggest a name for.
				type:	unicode

		keywords:
			suggestion:
				desc:	The desired name, or None to choose a name based on the
						item's type.
				type:	[unicode, NoneType]

		returns:
			desc:	A unique name.
			type:	unicode
		"""

		if suggestion is None:
			name = u'new_%s' % item_type
		else:
			name = self.experiment.syntax.sanitize(suggestion, strict=True,
				allow_vars=False)
			# Empty names are not allowed, and we fall back to the item type
			if not name:
				name = item_type
			# Names cannot start with a number, and are therefore prefixed by
			# an underscore.
			elif name[0].isnumeric():
				name = u'_' + name
		_name = name
		i = 1
		while _name in self:
			_name = u'%s_%d' % (name, i)
			i += 1
		return cistr(_name)
Beispiel #7
0
	def new(self, _type, name=None, script=None, allow_rename=True):

		"""
		desc:
			Creates a new item.

		arguments:
			_type:
				desc:	The item type.
				type:	unicode

		keywords:
			name:
				desc:	The item name, or None to choose a unique name based
						on the item type.
				type:	[unicode, NoneType]
			script:
				desc:	A definition script, or None to start with a blank item.
				type:	[unicode, NoneType]
			allow_rename:
				desc:	Indicates whether OpenSesame can use a different name
						from the one that is provided as `name` to avoid
						duplicate names etc.
				type:	bool

		returns:
			desc:	The newly generated item.
			type:	item

		example: |
			items.new(u'sketchpad', name=u'my_sketchpad')
			items[u'my_sketchpad'].prepare()
			items[u'my_sketchpad'].run()
		"""

		debug.msg(u'creating %s' % _type)
		if allow_rename:
			name = self.valid_name(_type, suggestion=name)
		else:
			name = cistr(name)
		if plugins.is_plugin(_type):
			# Load a plug-in
			try:
				item = plugins.load_plugin(
					_type, name, self.experiment, script,
					self.experiment.item_prefix()
				)
			except Exception as e:
				raise osexception(
					u"Failed to load plugin '%s'" % _type,
					exception=e
				)
			self.__items__[name] = item
		else:
			# Load one of the core items
			debug.msg(
				u"loading core item '%s' from '%s'" % (
					_type,
					self.experiment.module_container()
				)
			)
			item_module = __import__(
				u'%s.%s' % (
					self.experiment.module_container(),
					_type
				),
				fromlist=[u'dummy']
			)
			item_class = getattr(item_module, _type)
			item = item_class(name, self.experiment, script)
			self.__items__[name] = item
		return item