Changeset 3413 for pykota/trunk/bin/warnpykota
- Timestamp:
- 09/27/08 22:02:37 (16 years ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
pykota/trunk/bin/warnpykota
r3411 r3413 9 9 # the Free Software Foundation, either version 3 of the License, or 10 10 # (at your option) any later version. 11 # 11 # 12 12 # This program is distributed in the hope that it will be useful, 13 13 # but WITHOUT ANY WARRANTY; without even the implied warranty of 14 14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 15 # GNU General Public License for more details. 16 # 16 # 17 17 # You should have received a copy of the GNU General Public License 18 18 # along with this program. If not, see <http://www.gnu.org/licenses/>. … … 25 25 notifications to users or groups who have reached the limit of their 26 26 printing quota.""" 27 27 28 28 import sys 29 29 import os … … 41 41 from pykota.tool import PyKotaTool 42 42 43 class WarnPyKota(PyKotaTool) : 43 class WarnPyKota(PyKotaTool) : 44 44 """A class for warnpykota.""" 45 45 def sendMessage(self, adminmail, touser, fullmessage) : 46 46 """Sends an email message containing headers to some user.""" 47 47 smtpserver = self.smtpserver 48 try : 48 try : 49 49 server = smtplib.SMTP(smtpserver) 50 except socket.error, msg : 50 except socket.error, msg : 51 51 self.printInfo(_("Impossible to connect to SMTP server : %(smtpserver)s") \ 52 52 % locals(), \ … … 55 55 try : 56 56 server.sendmail(adminmail, [touser], fullmessage) 57 except smtplib.SMTPException, answer : 57 except smtplib.SMTPException, answer : 58 58 for (k, v) in answer.recipients.items() : 59 59 errormsg = v[0] … … 63 63 "error") 64 64 server.quit() 65 65 66 66 def sendMessageToUser(self, admin, adminmail, user, subject, message) : 67 67 """Sends an email message to a user.""" … … 76 76 msg["Date"] = email.Utils.formatdate(localtime=True) 77 77 self.sendMessage(adminmail, usermail, msg.as_string()) 78 78 79 79 def sendMessageToAdmin(self, adminmail, subject, message) : 80 80 """Sends an email message to the Print Quota administrator.""" … … 86 86 msg["To"] = adminmail 87 87 self.sendMessage(adminmail, adminmail, msg.as_string()) 88 88 89 89 def warnGroupPQuota(self, grouppquota) : 90 90 """Checks a group quota and send messages if quota is exceeded on current printer.""" … … 98 98 if group.LimitBy in ("noquota", "nochange") : 99 99 action = "ALLOW" 100 else : 100 else : 101 101 action = self.checkGroupPQuota(grouppquota) 102 102 if action.startswith("POLICY_") : … … 111 111 if mailto != "EXTERNAL" : 112 112 self.sendMessageToUser(admin, adminmail, user, _("Print Quota Exceeded"), self.config.getHardWarn(printername)) 113 else : 113 else : 114 114 self.externalMailTo(arguments, action, user, printer, self.config.getHardWarn(printername)) 115 elif action == "WARN" : 115 elif action == "WARN" : 116 116 adminmessage = _("Print Quota low for group %(groupname)s on printer %(printername)s") % locals() 117 117 self.printInfo(adminmessage) 118 118 if mailto in [ "BOTH", "ADMIN" ] : 119 119 self.sendMessageToAdmin(adminmail, _("Print Quota"), adminmessage) 120 if group.LimitBy and (group.LimitBy.lower() == "balance") : 120 if group.LimitBy and (group.LimitBy.lower() == "balance") : 121 121 message = self.config.getPoorWarn() 122 else : 122 else : 123 123 message = self.config.getSoftWarn(printername) 124 124 if mailto in [ "BOTH", "USER", "EXTERNAL" ] : … … 126 126 if mailto != "EXTERNAL" : 127 127 self.sendMessageToUser(admin, adminmail, user, _("Print Quota Exceeded"), message) 128 else : 128 else : 129 129 self.externalMailTo(arguments, action, user, printer, message) 130 return action 131 130 return action 131 132 132 def warnUserPQuota(self, userpquota) : 133 133 """Checks a user quota and send him a message if quota is exceeded on current printer.""" … … 139 139 adminmail = self.config.getAdminMail(printername) 140 140 (mailto, arguments) = self.config.getMailTo(printername) 141 141 142 142 if user.LimitBy in ("noquota", "nochange") : 143 143 action = "ALLOW" … … 149 149 if mailto != "EXTERNAL" : 150 150 self.sendMessageToUser(admin, adminmail, user, _("Printing denied."), message) 151 else : 151 else : 152 152 self.externalMailTo(arguments, action, user, printer, message) 153 153 if mailto in [ "BOTH", "ADMIN" ] : … … 157 157 if action.startswith("POLICY_") : 158 158 action = action[7:] 159 159 160 160 if action == "DENY" : 161 161 adminmessage = _("Print Quota exceeded for user %(username)s on printer %(printername)s") % locals() … … 165 165 if mailto != "EXTERNAL" : 166 166 self.sendMessageToUser(admin, adminmail, user, _("Print Quota Exceeded"), message) 167 else : 167 else : 168 168 self.externalMailTo(arguments, action, user, printer, message) 169 169 if mailto in [ "BOTH", "ADMIN" ] : 170 170 self.sendMessageToAdmin(adminmail, _("Print Quota"), adminmessage) 171 elif action == "WARN" : 171 elif action == "WARN" : 172 172 adminmessage = _("Print Quota low for user %(username)s on printer %(printername)s") % locals() 173 173 self.printInfo(adminmessage) 174 174 if mailto in [ "BOTH", "USER", "EXTERNAL" ] : 175 if user.LimitBy and (user.LimitBy.lower() == "balance") : 175 if user.LimitBy and (user.LimitBy.lower() == "balance") : 176 176 message = self.config.getPoorWarn() 177 else : 177 else : 178 178 message = self.config.getSoftWarn(printername) 179 if mailto != "EXTERNAL" : 179 if mailto != "EXTERNAL" : 180 180 self.sendMessageToUser(admin, adminmail, user, _("Print Quota Low"), message) 181 else : 181 else : 182 182 self.externalMailTo(arguments, action, user, printer, message) 183 183 if mailto in [ "BOTH", "ADMIN" ] : 184 184 self.sendMessageToAdmin(adminmail, _("Print Quota"), adminmessage) 185 return action 186 185 return action 186 187 187 def main(self, ugnames, options) : 188 188 """Warn users or groups over print quota.""" … … 192 192 # no username, means all usernames 193 193 ugnames = [ "*" ] 194 else : 194 else : 195 195 # not a PyKota administrator 196 196 # warns only the current user … … 203 203 if user.Exists : 204 204 ugnames = [ g.Name for g in self.storage.getUserGroups(user) ] 205 else : 205 else : 206 206 ugnames = [ ] 207 207 else : 208 208 ugnames = [ username ] 209 209 210 210 printername = options.printer 211 211 printers = self.storage.getMatchingPrinters(printername) … … 231 231 if not done : 232 232 alreadydone[user.Name] = (action in ('WARN', 'DENY')) 233 234 if __name__ == "__main__" : 233 234 if __name__ == "__main__" : 235 235 parser = PyKotaOptionParser(description=_("A tool to warn users and groups who have reached the limit of their printing quota."), 236 236 usage="warnpykota [options] [usernames|groupnames]") … … 243 243 default="*", 244 244 help=_("Acts on this printer only. You can specify several printer names by separating them with commas. The default value is '%default', which means all printers.")) 245 245 246 246 parser.add_example('', 247 247 _("This would notify all users who have reached the limit of their printing quota on any printer.")) … … 250 250 parser.add_example('--groups --printer "HP*,XER*" "dev*"', 251 251 _("This would notify all users of the groups whose name begins with 'dev' and for which the printing quota limit is reached on any printer whose name begins with 'HP' or 'XER'.")) 252 252 253 253 run(parser, WarnPyKota)