def test_configuration(self): composition = aspect.compose( config1, config2(config2='preconfigured'), config3(config3='preconfigured'), ) composition = composition( config3='composition', ) self.assertEqual(composition(C)().config1, 'default') self.assertEqual(composition(C()).config1, 'default') self.assertEqual(composition(C)().config2, 'preconfigured') self.assertEqual(composition(C()).config2, 'preconfigured') self.assertEqual(composition(C)().config3, 'composition') self.assertEqual(composition(C()).config3, 'composition') self.assertEqual( composition(C, config3='application')().config3, 'application' ) self.assertEqual( composition(C(), config3='application').config3, 'application' ) self.assertEqual( composition(C, config3='application')( config3='instantiation' ).config3, 'instantiation' )
def test(self): class a1(Aspect): a = 1 b = aspect.default(1) c = aspect.default(1) d = aspect.default(1) e = aspect.default(1) class a2(Aspect): a = 2 b = 2 c = aspect.default(2) d = aspect.default(2) class a3(Aspect): a = 3 b = 3 c = 3 a123 = a1(a2(a3)) a12 = a1(a2) a12_3 = a12(a3) a23 = a2(a3) a1_23 = a1(a23) class C(object): pass composition = compose(a1, a2, a3) #self.assertEqual(composition.__name__, 'a1:a2:a3') self.assertEqual(composition.__name__, 'AspectComposition')
def test_inheritance(self): composition = aspect.compose( child1, child2, ) self.assertEqual(composition(C)().base, 'base') self.assertEqual(composition(C)().child, 'child1') self.assertEqual(composition(C()).base, 'base') self.assertEqual(composition(C()).child, 'child1')
def test_default_overwrite_plumb_plumb(self): composition = aspect.compose( plumb, plumb, overwrite1, default1, ) self.assertEqual(composition(C)().f(), "plumb-plumb-overwrite1") self.assertEqual(composition(C()).f(), "plumb-plumb-overwrite1") self.assertEqual(composition(D)().f(), "plumb-plumb-overwrite1") self.assertEqual(composition(D()).f(), "plumb-plumb-overwrite1")
def test_default_default_plumb_plumb(self): composition = aspect.compose( plumb, plumb, default2, default1, ) self.assertEqual(composition(C)().f(), "plumb-plumb-C") self.assertEqual(composition(C()).f(), "plumb-plumb-C") self.assertEqual(composition(D)().f(), "plumb-plumb-default1") self.assertEqual(composition(D()).f(), "plumb-plumb-default1")
class ipdb_DELETE(Aspect): @aspect.plumb def DELETE(_next, self, **kw): import ipdb ipdb.set_trace() response = _next(**kw) ipdb.set_trace() return response ipdb_all = aspect.compose( ipdb__call__, ipdb_GET, ipdb_POST, ipdb_PUT, ipdb_DELETE, ) class log_call(Aspect): @aspect.plumb def __call__(_next, self, **kw): vars = { 'url_with_query': urllib.unquote(kw['url_with_query']), 'method': kw['method'] } log.info('%(method)s %(url_with_query)s' % vars + ' %r', dict((k, v) for k, v in kw.items() if k not in ('method', 'url_with_query')))
@aspect.plumb def keys(_next, self): return merge_dicts(self.children, self.get()).keys() def get_url(self): return self.url_pattern.format(**self.parameters) \ if self.url_pattern else None def merge_dicts(*dicts): return dict(chain(*(d.iteritems() for d in dicts))) hydra_node = aspect.compose( cache_get, traverse_get, hydra_json_get, apply_map_spec ) class Node(object): def keys(self): raise AttributeError def __getitem__(self, key): raise KeyError HydraNode = hydra_node(Node)
class ipdb_DELETE(Aspect): @aspect.plumb def DELETE(_next, self, **kw): import ipdb ipdb.set_trace() response = _next(**kw) ipdb.set_trace() return response ipdb_all = aspect.compose( ipdb__call__, ipdb_GET, ipdb_POST, ipdb_PUT, ipdb_DELETE, ) class log_call(Aspect): @aspect.plumb def __call__(_next, self, **kw): log.info( '%(method)s %(url_with_query)s' % kw + ' %r', dict((k, v) for k, v in kw.items() if k not in ('method', 'url_with_query'))) return _next(**kw)