-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathpp_iopluginmanager.py
144 lines (116 loc) · 5.82 KB
/
pp_iopluginmanager.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import os
import imp
import configparser
from pp_utils import Monitor
class IOPluginManager(object):
plugins=[]
def __init__(self):
self.mon=Monitor()
def init(self,pp_dir,pp_profile,widget,callback,pp_home):
self.pp_dir=pp_dir
self.pp_profile=pp_profile
self.pp_home=pp_home
IOPluginManager.plugins=[]
if os.path.exists(self.pp_profile+os.sep+'pp_io_config'):
# read the .cfg files in /pp_io_config in profile registring the I/O plugin
for cfgfile in os.listdir(self.pp_profile+os.sep+'pp_io_config'):
if cfgfile in ('screen.cfg','osc.cfg'):
continue
cfgfilepath = self.pp_profile+os.sep+'pp_io_config'+os.sep+cfgfile
status,message=self.init_config(cfgfile,cfgfilepath,widget,callback)
if status == 'error':
return status,message
#read .cfg file in /pipresents/pp_io_config if file not present in profile then use this one
for cfgfile in os.listdir(self.pp_dir+os.sep+'pp_io_config'):
if cfgfile in ('screen.cfg','osc.cfg'):
continue
if not os.path.exists(self.pp_profile+os.sep+'pp_io_config'+os.sep+cfgfile):
cfgfilepath=self.pp_dir+os.sep+'pp_io_config'+os.sep+cfgfile
status,message=self.init_config(cfgfile,cfgfilepath,widget,callback)
if status == 'error':
return status,message
# print IOPluginManager.plugins
return 'normal','I/O Plugins registered'
def init_config(self,cfgfile,cfgfilepath,widget,callback):
# print cfgfile,cfgfilepath
reason,message,config=self._read(cfgfile,cfgfilepath)
if reason =='error':
self.mon.err(self,'Failed to read '+cfgfile + ' ' + message)
return 'error','Failed to read '+cfgfile + ' ' + message
if config.has_section('DRIVER') is False:
self.mon.err(self,'No DRIVER section in '+cfgfilepath)
return 'error','No DRIVER section in '+cfgfilepath
entry = dict()
#read information from DRIVER section
entry['title']=config.get('DRIVER','title')
if config.get('DRIVER','enabled')=='yes':
if config.has_option('DRIVER','driver-ref'):
entry['driver-ref']=config.get('DRIVER','driver-ref')
else:
entry['driver-ref']=''
driver_name=config.get('DRIVER','module')
driver_path=self.pp_dir+os.sep+'pp_io_plugins'+os.sep+driver_name+'.py'
if not os.path.exists(driver_path):
self.mon.err(self,driver_name + ' Driver not found in ' + driver_path)
return 'error',driver_name + ' Driver not found in ' + driver_path
instance = self._load_plugin_file(driver_name,self.pp_dir+os.sep+'pp_io_plugins')
reason,message=instance.init(cfgfile,cfgfilepath,widget,self.pp_dir,self.pp_home,self.pp_profile,callback)
if reason=='warn':
self.mon.warn(self,message)
return 'error',message
if reason=='error':
self.mon.warn(self,message)
return 'error',message
entry['instance']=instance
self.mon.log(self,message)
IOPluginManager.plugins.append(entry)
return 'normal','I/O Plugins registered'
def start(self):
for entry in IOPluginManager.plugins:
plugin=entry['instance']
if plugin.is_active() is True:
plugin.start()
def terminate(self):
for entry in IOPluginManager.plugins:
plugin=entry['instance']
if plugin.is_active() is True:
plugin.terminate()
self.mon.log(self,'I/O plugin '+entry['title']+ ' terminated')
def get_input(self,key,driver_ref=''):
for entry in IOPluginManager.plugins:
plugin=entry['instance']
# print ('trying ',entry['title'],plugin.is_active())
if plugin.is_active() is True and driver_ref == entry['driver-ref']:
# need to test found in plugin to allow key to match if driver-ref not used
found,value = plugin.get_input(key)
if found is True:
return found,value
# key not found in any plugin
return False,None
def handle_output_event(self,name,param_type,param_values,req_time):
for entry in IOPluginManager.plugins:
plugin=entry['instance']
# print ('trying ',entry['title'],name,param_type,plugin.is_active())
if plugin.is_active() is True:
# print (name,param_type,param_values,req_time)
reason,message= plugin.handle_output_event(name,param_type,param_values,req_time)
if reason == 'error':
# self.mon.err(self,message)
return 'error',message
else:
self.mon.log(self,message)
self.mon.log (self,'All installed I/O plugins scanned')
return 'normal','output scan complete'
def _load_plugin_file(self, name, driver_dir):
fp, pathname,description = imp.find_module(name,[driver_dir])
module_id = imp.load_module(name,fp,pathname,description)
plugin_class = getattr(module_id,name)
return plugin_class()
def _read(self,filename,filepath):
if os.path.exists(filepath):
config = configparser.ConfigParser(inline_comment_prefixes = (';',))
config.read(filepath)
self.mon.log(self,filename+" read from "+ filepath)
return 'normal',filename+' read',config
else:
return 'error',filename+' not found at: '+filepath,None