def load_from_eggpkg(package, entry_group, entry_name, instance_name=None, logger=None, observer=None): """ Load object graph state by invoking the given package entry point. Returns the root object. package: string Name of package to load from. entry_group: string Name of group. entry_name: string Name of entry point in group. instance_name: string Name for instance loaded. logger: Logger Used for recording progress, etc. observer: callable Called via an :class:`EggObserver`. """ logger = logger or NullLogger() observer = EggObserver(observer, logger) logger.debug('Loading %s from %s in %s...', entry_name, package, os.getcwd()) try: dist = pkg_resources.get_distribution(package) except pkg_resources.DistributionNotFound as exc: logger.error('Distribution not found: %s', exc) raise exc return _load_from_distribution(dist, entry_group, entry_name, instance_name, logger, observer)
def load_from_eggfile(filename, entry_group, entry_name, logger=None, observer=None): """ Extracts files in egg to a subdirectory matching the saved object name. Then loads object graph state by invoking the given entry point. Returns the root object. filename: string Name of egg file. entry_group: string Name of group. entry_name: string Name of entry point in group. logger: Logger Used for recording progress, etc. observer: callable Called via an :class:`EggObserver`. """ logger = logger or NullLogger() observer = EggObserver(observer, logger) logger.debug('Loading %s from %s in %s...', entry_name, filename, os.getcwd()) egg_dir, dist = _dist_from_eggfile(filename, logger, observer) # Just being defensive, '.' is typically in the path. if not '.' in sys.path: # pragma no cover sys.path.append('.') orig_dir = os.getcwd() os.chdir(egg_dir) try: return _load_from_distribution(dist, entry_group, entry_name, None, logger, observer) finally: os.chdir(orig_dir)
def setUp(self): """ Invoked before each test. """ self.observer = Observer() self.egg_observer = EggObserver(self.observer.observe, logging.getLogger('EO'))
class TestCase(unittest.TestCase): """ Test Egg Observer functions. """ def setUp(self): """ Invoked before each test. """ self.observer = Observer() self.egg_observer = EggObserver(self.observer.observe, logging.getLogger('EO')) def tearDown(self): """ Invoked after each test. """ pass def test_normal(self): logging.debug('') logging.debug('test_normal') # Note: this is not a valid ordering of calls. self.egg_observer.analyze('xyzzy') self.egg_observer.add('file1', 0, 0) self.egg_observer.copy('file2', 0.3, 0.5) self.egg_observer.extract('file3', 0.6, 0.75) self.egg_observer.complete('xyzzy') self.egg_observer.exception('oops') def test_exception(self): logging.debug('') logging.debug('test_exception') self.observer.throw_exception = True # Note: this is not a valid ordering of calls. self.egg_observer.analyze('xyzzy') self.egg_observer.add('file1', 0, 0) self.egg_observer.copy('file2', 0.3, 0.5) self.egg_observer.extract('file3', 0.6, 0.75) self.egg_observer.complete('xyzzy') self.egg_observer.exception('oops') def test_abort(self): logging.debug('') logging.debug('test_abort') self.observer.abort = True # Note: this is not a valid ordering of calls. assert_raises(self, "self.egg_observer.analyze('xyzzy')", globals(), locals(), RuntimeError, 'Aborted by observer.') assert_raises(self, "self.egg_observer.add('file1', 0, 0)", globals(), locals(), RuntimeError, 'Aborted by observer.') assert_raises(self, "self.egg_observer.copy('file2', 0.3, 0.5)", globals(), locals(), RuntimeError, 'Aborted by observer.') assert_raises(self, "self.egg_observer.extract('file3', 0.6, 0.75)", globals(), locals(), RuntimeError, 'Aborted by observer.') self.egg_observer.complete('xyzzy') self.egg_observer.exception('oops')