pmxbot / pmxbot /

from __future__ import unicode_literals

import yaml

# from jaraco.util.dictlib
class ItemsAsAttributes(object):
	Mix-in class to enable a mapping object to provide items as

	>>> C = type(str('C'), (dict, ItemsAsAttributes), dict())
	>>> i = C()
	>>> i['foo'] = 'bar'

	# natural attribute access takes precedence
	>>> = 'henry'

	# but as you might expect, the mapping functionality is preserved.
	>>> i['foo']

	# A normal attribute error should be raised if an attribute is
	#  requested that doesn't exist.
	>>> i.missing
	Traceback (most recent call last):
	AttributeError: 'C' object has no attribute 'missing'

	It also works on dicts that customize __getitem__
	>>> missing_func = lambda self, key: 'missing item'
	>>> C = type(str('C'), (dict, ItemsAsAttributes), dict(__missing__ = missing_func))
	>>> i = C()
	>>> i.missing
	u'missing item'
	u'missing item'
	def __getattr__(self, key):
			return getattr(super(ItemsAsAttributes, self), key)
		except AttributeError as e:
			# attempt to get the value from the mapping (return self[key])
			#  but be careful not to lose the original exception context.
			noval = object()
			def _safe_getitem(cont, key, missing_result):
					return cont[key]
				except KeyError:
					return missing_result
			result = _safe_getitem(self, key, noval)
			if result is not noval:
				return result
			# raise the original exception, but use the original class
			#  name, not 'super'.
			e.message = e.message.replace('super',
				self.__class__.__name__, 1)
			e.args = (e.message,)

class ConfigDict(ItemsAsAttributes, dict):
	def from_yaml(cls, filename):
		with open(filename) as f:
			return cls(yaml.load(f))
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.