PHP Classes

File: test/py/test.py

Recommend this page to a friend!
  Classes of Nikos M.   PHP Publish Subscribe   test/py/test.py   Download  
File: test/py/test.py
Role: Auxiliary data
Content type: text/plain
Description: Auxiliary data
Class: PHP Publish Subscribe
Register and call handlers of events by name
Author: By
Last change:
Date: 2 months ago
Size: 2,188 bytes
 

Contents

Class file image Download
#!/usr/bin/env python import pprint import os, sys DIR = os.path.dirname(os.path.abspath(__file__)) def import_module(name, path): #import imp #try: # mod_fp, mod_path, mod_desc = imp.find_module(name, [path]) # mod = getattr( imp.load_module(name, mod_fp, mod_path, mod_desc), name ) #except ImportError as exc: # mod = None # sys.stderr.write("Error: failed to import module ({})".format(exc)) #finally: # if mod_fp: mod_fp.close() #return mod import importlib.util, sys spec = importlib.util.spec_from_file_location(name, path+name+'.py') mod = importlib.util.module_from_spec(spec) sys.modules[name] = mod spec.loader.exec_module(mod) return getattr(mod, name) # import the PublishSubscribe.py (as a) module, probably you will want to place this in another dir/package PublishSubscribe = import_module('PublishSubscribe', os.path.join(DIR, '../../src/py/')) if not PublishSubscribe: print ('Could not load the PublishSubscribe Module') sys.exit(1) else: pass print('PublishSubscribe.VERSION = ' + PublishSubscribe.VERSION + "\n") def _log(evt): print(pprint.pformat({ 'topic' : evt.topic, 'originalTopic' : evt.originalTopic, 'tags' : evt.tags, 'namespaces' : evt.namespaces, 'timestamp' : evt.timestamp }, 4)) print(pprint.pformat(evt.data, 4)) def handler1(evt): print('Handler1' + "\n") _log(evt) evt.next() # event abort #evt.abort() # stop bubble propagation #evt.propagate(False) # stop propagation on same event #evt.stop() #return False def handler2(evt): print('Handler2' + "\n") _log(evt) evt.next() def handler3(evt): print('Handler3' + "\n") _log(evt) evt.next() def handler4(evt): print('Handler4' + "\n") _log(evt) evt.next() (PublishSubscribe() .on('Topic1/SubTopic11#Tag1#Tag2', handler1) .on1('Topic1/SubTopic11#Tag1#Tag2@NS1', handler2) .on('Topic1/SubTopic11#Tag1#Tag2@NS1@NS2', handler3) .off('@NS1@NS2') .pipeline('Topic1/SubTopic11#Tag2#Tag1', {'key1': 'value1'}) .pipeline('Topic1/SubTopic11#Tag2#Tag1@NS1', {'key1': 'value1'}) )