initial checkin
[grml-etc.git] / etc / skel / .wmi / szs2.1 / szs.py
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3 """
4 COPYRIGHT:
5
6 Copyright 2004  Norman Köhring <nkoehring-at-web-dot-de>
7
8
9 LICENSE:
10
11 This program and all of its modules are free software;
12 you can redistribute it and/or modify it under the terms of the
13 GNU General Public License as published by the Free Software Foundation;
14 either version 2 of the License, or (at your option) any later version.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24
25 For more information read the file LICENSE
26 """
27
28
29 import sys, szstoolbox
30 from commands import getstatusoutput
31 from time import strftime,sleep,time
32 from os.path import exists as fexists
33                 
34
35 # initial variables
36 progname = 'Python SZS '
37 progver = '1st'
38
39 class moduleclass:
40         """ class for importing the modules and get there outputs
41             Usage: It should be enough to create an instance... """
42
43         def __init__(self):
44                 """ initialize all the things in modules """
45
46                 x = str(cfgM.read('modules')).strip().split(',')
47                 self.mods=[]
48                 msg.debug('Module: %s' % str(x), 3)
49                 for mod in x:
50                         try:
51                                 self.mods.append(__import__(mod, globals(), locals(), []))
52                         except ImportError:
53                                 msg.debug("Can't find %s. Maybe you dispelled it?" % mod, 2)
54
55                         except ValueError:
56                                 msg.debug("No modules to load.", 2)
57                                 x = []
58
59                         except:
60                                 raise
61
62
63
64         def txtsnbars(self):
65                 x = []
66                 y = []
67 #               lst = []
68                 for mod in self.mods:
69                         try:
70                                 lst = mod.main()
71                                 lst[0] = lst[0].strip()
72                                 if lst[0] != '':
73                                         x.append(lst[0])
74                                 
75                                 y.extend(lst[1:])
76                                         
77                         except:
78                                 raise
79                 
80                 return [x,y]
81
82
83
84 class init:
85         """ this class contains all function for the script initialization """
86
87         def __init__(self):
88                 """ yes, also init needs an init! """
89
90                 init.classes(self)
91                 init.args(self)
92                 init.vars(self)
93
94
95
96         def classes(self):
97                 """ initialize the classes """
98
99                 global cfg
100                 global cfgG
101                 global cfgM
102                 global msg
103                 global modules
104                 global xtra
105                 msg = szstoolbox.MSG()
106                 cfg = szstoolbox.CFG('')
107                 cfgM = cfg('main')
108                 modules = moduleclass()
109                 xtra = szstoolbox.XTRA()
110
111
112
113         def args(self):
114                 """ initializes the arguments """
115
116                 if len(sys.argv) >= 2:
117                         if sys.argv[1] in ('-?','-h','--help'):
118                                 msg.usage()
119                                 sys.exit(0)
120                         else:
121                                 print 'No parameters, sorry...'
122                                 print '-?, -h or --help for help'
123                                 sys.exit(0)
124         
125
126
127         def vars(self):
128                 """ sets the variables """
129
130                 global welcome
131                 global text
132                 global prog             
133                 global seperator                
134                 welcome = cfgM.read('welcome').strip()
135                 prog = [str(cfgM.read('prog_txt')).strip(),str(cfgM.read('prog_bar')).strip()]
136                 if not fexists(prog[0].split(' ')[0]):
137                         msg.debug(str(prog[0])+' doesn\'t not exists, switched it to `echo `',2)
138                         prog[0] = 'echo '
139                 else:
140                         msg.debug('Using '+str(prog[0])+' to display text',3)
141
142                 if not fexists(prog[1].split(' ')[0]):
143                         msg.debug(str(prog[1])+' doesn\'t not exists, switched it to `echo `',2)
144                         prog[1] = 'echo '
145                 else:
146                         msg.debug('Using \''+str(prog[1])+'\' to display bars',3)
147
148                 seperator = cfg('global').read('seperator')
149
150
151
152 def main():
153         """ the main function: nothing special. it only runs the commands and maybe return the errors """
154         
155         if 'welcome' in globals() and welcome <> '!NONE':
156                 if time() - szstoolbox.starttime <= 3:
157                         now = welcome
158                 else:
159                         now = ''
160
161         txtsnbars = modules.txtsnbars()
162
163 #       for i,j in txtsnbars:
164         txts = seperator.join(txtsnbars[0]).strip()
165         bars = ','.join(txtsnbars[1]).strip(',')
166         
167         cmdtxt = str(prog[0]+' \''+now+' '+txts+'\'')
168         cmdbar = str(prog[1]+' \''+bars+'\'')
169
170         error = getstatusoutput(cmdtxt)
171         if error[0] == 0: 
172                 error = getstatusoutput(cmdbar)
173         return error
174
175
176
177 if __name__ == "__main__":
178         init()
179         error_nr = 0
180         error_msg = 'noerror'
181         while error_nr == 0:
182                 try:
183                         error = main()
184                         error_nr = error[0]
185                         error_msg = error[1]
186                         sleep(szstoolbox.interval)
187                 except KeyboardInterrupt:
188                         sys.exit('goodbye')
189                 except:
190                         raise
191                         sys.exit("Terminated cause above error(s)") # eigentlich ist diese Zeile hirnlos :-S
192
193
194         print 'Error...'
195 #       print 'Nr: '+str(error_nr)+' Msg:'+error_msg
196         print str(error)
197         sys.exit('error')