root / pykota / trunk / pykota / storage.py @ 1252

Revision 1252, 19.1 kB (checked in by jalet, 20 years ago)

This time printer groups caching works.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1# PyKota
2# -*- coding: ISO-8859-15 -*-
3#
4# PyKota : Print Quotas for CUPS and LPRng
5#
6# (c) 2003 Jerome Alet <alet@librelogiciel.com>
7# This program is free software; you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation; either version 2 of the License, or
10# (at your option) any later version.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with this program; if not, write to the Free Software
19# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
20#
21# $Id$
22#
23# $Log$
24# Revision 1.32  2004/01/06 16:02:57  jalet
25# This time printer groups caching works.
26#
27# Revision 1.31  2004/01/06 15:51:24  jalet
28# Fixed caching of printer groups
29#
30# Revision 1.30  2004/01/06 14:24:59  jalet
31# Printer groups should be cached now, if caching is enabled.
32#
33# Revision 1.29  2003/12/27 16:49:25  uid67467
34# Should be ok now.
35#
36# Revision 1.28  2003/11/25 23:46:40  jalet
37# Don't try to verify if module name is valid, Python does this better than us.
38#
39# Revision 1.27  2003/11/23 19:01:36  jalet
40# Job price added to history
41#
42# Revision 1.26  2003/11/21 14:28:45  jalet
43# More complete job history.
44#
45# Revision 1.25  2003/10/08 21:12:27  jalet
46# Do not cache anymore entries which don't exist.
47#
48# Revision 1.24  2003/10/07 22:06:05  jalet
49# Preliminary code to disable job history
50#
51# Revision 1.23  2003/10/07 09:07:28  jalet
52# Character encoding added to please latest version of Python
53#
54# Revision 1.22  2003/10/06 13:12:27  jalet
55# More work on caching
56#
57# Revision 1.21  2003/10/03 09:02:20  jalet
58# Logs cache store actions too
59#
60# Revision 1.20  2003/10/02 20:23:18  jalet
61# Storage caching mechanism added.
62#
63# Revision 1.19  2003/07/16 21:53:07  jalet
64# Really big modifications wrt new configuration file's location and content.
65#
66# Revision 1.18  2003/07/07 08:33:18  jalet
67# Bug fix due to a typo in LDAP code
68#
69# Revision 1.17  2003/07/05 07:46:50  jalet
70# The previous bug fix was incomplete.
71#
72# Revision 1.16  2003/06/25 19:52:31  jalet
73# Should be ready for testing :-)
74#
75# Revision 1.15  2003/06/25 14:10:58  jalet
76# Exception raising for now.
77#
78# Revision 1.14  2003/06/25 14:10:01  jalet
79# Hey, it may work (edpykota --reset excepted) !
80#
81# Revision 1.13  2003/06/10 16:37:54  jalet
82# Deletion of the second user which is not needed anymore.
83# Added a debug configuration field in /etc/pykota.conf
84# All queries can now be sent to the logger in debug mode, this will
85# greatly help improve performance when time for this will come.
86#
87# Revision 1.12  2003/04/23 22:13:57  jalet
88# Preliminary support for LPRng added BUT STILL UNTESTED.
89#
90# Revision 1.11  2003/04/10 21:47:20  jalet
91# Job history added. Upgrade script neutralized for now !
92#
93# Revision 1.10  2003/03/29 13:45:27  jalet
94# GPL paragraphs were incorrectly (from memory) copied into the sources.
95# Two README files were added.
96# Upgrade script for PostgreSQL pre 1.01 schema was added.
97#
98# Revision 1.9  2003/02/17 22:55:01  jalet
99# More options can now be set per printer or globally :
100#
101#       admin
102#       adminmail
103#       gracedelay
104#       requester
105#
106# the printer option has priority when both are defined.
107#
108# Revision 1.8  2003/02/17 22:05:50  jalet
109# Storage backend now supports admin and user passwords (untested)
110#
111# Revision 1.7  2003/02/10 12:07:31  jalet
112# Now repykota should output the recorded total page number for each printer too.
113#
114# Revision 1.6  2003/02/09 13:05:43  jalet
115# Internationalization continues...
116#
117# Revision 1.5  2003/02/08 22:39:46  jalet
118# --reset command line option added
119#
120# Revision 1.4  2003/02/08 09:59:59  jalet
121# Added preliminary base class for all storages
122#
123# Revision 1.3  2003/02/05 22:10:29  jalet
124# Typos
125#
126# Revision 1.2  2003/02/05 22:02:22  jalet
127# __import__ statement didn't work as expected
128#
129# Revision 1.1  2003/02/05 21:28:17  jalet
130# Initial import into CVS
131#
132#
133#
134
135class PyKotaStorageError(Exception):
136    """An exception for Quota Storage related stuff."""
137    def __init__(self, message = ""):
138        self.message = message
139        Exception.__init__(self, message)
140    def __repr__(self):
141        return self.message
142    __str__ = __repr__
143       
144class StorageObject :
145    """Object present in the Quota Storage."""
146    def __init__(self, parent) :
147        "Initialize minimal data."""
148        self.parent = parent
149        self.ident = None
150        self.Exists = 0
151       
152class StorageUser(StorageObject) :       
153    """User class."""
154    def __init__(self, parent, name) :
155        StorageObject.__init__(self, parent)
156        self.Name = name
157        self.LimitBy = None
158        self.AccountBalance = None
159        self.LifeTimePaid = None
160        self.Email = None
161       
162    def consumeAccountBalance(self, amount) :     
163        """Consumes an amount of money from the user's account balance."""
164        newbalance = float(self.AccountBalance or 0.0) - amount
165        self.parent.writeUserAccountBalance(self, newbalance)
166        self.AccountBalance = newbalance
167       
168    def setAccountBalance(self, balance, lifetimepaid) :   
169        """Sets the user's account balance in case he pays more money."""
170        self.parent.writeUserAccountBalance(self, balance, lifetimepaid)
171        self.AccountBalance = balance
172        self.LifeTimePaid = lifetimepaid
173       
174    def setLimitBy(self, limitby) :   
175        """Sets the user's limiting factor."""
176        try :
177            limitby = limitby.lower()
178        except AttributeError :   
179            limitby = "quota"
180        if limitby in ["quota", "balance"] :
181            self.parent.writeUserLimitBy(self, limitby)
182            self.LimitBy = limitby
183       
184    def delete(self) :   
185        """Deletes an user from the Quota Storage."""
186        self.parent.beginTransaction()
187        try :
188            self.parent.deleteUser(self)
189        except PyKotaStorageError, msg :   
190            self.parent.rollbackTransaction()
191            raise PyKotaStorageError, msg
192        else :   
193            self.parent.commitTransaction()
194       
195class StorageGroup(StorageObject) :       
196    """User class."""
197    def __init__(self, parent, name) :
198        StorageObject.__init__(self, parent)
199        self.Name = name
200        self.LimitBy = None
201        self.AccountBalance = None
202        self.LifeTimePaid = None
203       
204    def setLimitBy(self, limitby) :   
205        """Sets the user's limiting factor."""
206        try :
207            limitby = limitby.lower()
208        except AttributeError :   
209            limitby = "quota"
210        if limitby in ["quota", "balance"] :
211            self.parent.writeGroupLimitBy(self, limitby)
212            self.LimitBy = limitby
213       
214    def delete(self) :   
215        """Deletes a group from the Quota Storage."""
216        self.parent.beginTransaction()
217        try :
218            self.parent.deleteGroup(self)
219        except PyKotaStorageError, msg :   
220            self.parent.rollbackTransaction()
221            raise PyKotaStorageError, msg
222        else :   
223            self.parent.commitTransaction()
224       
225class StoragePrinter(StorageObject) :
226    """Printer class."""
227    def __init__(self, parent, name) :
228        StorageObject.__init__(self, parent)
229        self.Name = name
230        self.PricePerPage = None
231        self.PricePerJob = None
232        self.LastJob = None
233       
234    def addJobToHistory(self, jobid, user, pagecounter, action, jobsize=None, jobprice=None, filename=None, title=None, copies=None, options=None) :
235        """Adds a job to the printer's history."""
236        self.parent.writeJobNew(self, user, jobid, pagecounter, action, jobsize, jobprice, filename, title, copies, options)
237        # TODO : update LastJob object ? Probably not needed.
238       
239    def setPrices(self, priceperpage = None, priceperjob = None) :   
240        """Sets the printer's prices."""
241        if priceperpage is None :
242            priceperpage = self.PricePerPage
243        else :   
244            self.PricePerPage = float(priceperpage)
245        if priceperjob is None :   
246            priceperjob = self.PricePerJob
247        else :   
248            self.PricePerJob = float(priceperjob)
249        self.parent.writePrinterPrices(self)
250       
251class StorageUserPQuota(StorageObject) :
252    """User Print Quota class."""
253    def __init__(self, parent, user, printer) :
254        StorageObject.__init__(self, parent)
255        self.User = user
256        self.Printer = printer
257        self.PageCounter = None
258        self.LifePageCounter = None
259        self.SoftLimit = None
260        self.HardLimit = None
261        self.DateLimit = None
262        self.ParentPrintersUserPQuota = (user.Exists and printer.Exists and parent.getParentPrintersUserPQuota(self)) or []
263       
264    def setDateLimit(self, datelimit) :   
265        """Sets the date limit for this quota."""
266        date = "%04i-%02i-%02i %02i:%02i:%02i" % (datelimit.year, datelimit.month, datelimit.day, datelimit.hour, datelimit.minute, datelimit.second)
267        self.parent.writeUserPQuotaDateLimit(self, date)
268        self.DateLimit = date
269       
270    def setLimits(self, softlimit, hardlimit) :   
271        """Sets the soft and hard limit for this quota."""
272        self.parent.writeUserPQuotaLimits(self, softlimit, hardlimit)
273        self.SoftLimit = softlimit
274        self.HardLimit = hardlimit
275       
276    def reset(self) :   
277        """Resets page counter to 0."""
278        self.parent.writeUserPQuotaPagesCounters(self, 0, int(self.LifePageCounter or 0))
279        self.PageCounter = 0
280       
281    def increasePagesUsage(self, nbpages) :
282        """Increase the value of used pages and money."""
283        jobprice = (float(self.Printer.PricePerPage or 0.0) * nbpages) + float(self.Printer.PricePerJob or 0.0)
284        self.parent.beginTransaction()
285        try :
286            if nbpages :
287                self.User.consumeAccountBalance(jobprice)
288                for upq in [ self ] + self.ParentPrintersUserPQuota :
289                    newpagecounter = int(upq.PageCounter or 0) + nbpages
290                    newlifepagecounter = int(upq.LifePageCounter or 0) + nbpages
291                    self.parent.writeUserPQuotaPagesCounters(upq, newpagecounter, newlifepagecounter)
292                    upq.PageCounter = newpagecounter
293                    upq.LifePageCounter = newlifepagecounter
294        except PyKotaStorageError, msg :   
295            self.parent.rollbackTransaction()
296            raise PyKotaStorageError, msg
297        else :   
298            self.parent.commitTransaction()
299       
300class StorageGroupPQuota(StorageObject) :
301    """Group Print Quota class."""
302    def __init__(self, parent, group, printer) :
303        StorageObject.__init__(self, parent)
304        self.Group = group
305        self.Printer = printer
306        self.PageCounter = None
307        self.LifePageCounter = None
308        self.SoftLimit = None
309        self.HardLimit = None
310        self.DateLimit = None
311       
312    def setDateLimit(self, datelimit) :   
313        """Sets the date limit for this quota."""
314        date = "%04i-%02i-%02i %02i:%02i:%02i" % (datelimit.year, datelimit.month, datelimit.day, datelimit.hour, datelimit.minute, datelimit.second)
315        self.parent.writeGroupPQuotaDateLimit(self, date)
316        self.DateLimit = date
317       
318    def setLimits(self, softlimit, hardlimit) :   
319        """Sets the soft and hard limit for this quota."""
320        self.parent.writeGroupPQuotaLimits(self, softlimit, hardlimit)
321        self.SoftLimit = softlimit
322        self.HardLimit = hardlimit
323       
324class StorageLastJob(StorageObject) :
325    """Printer's Last Job class."""
326    def __init__(self, parent, printer) :
327        StorageObject.__init__(self, parent)
328        self.Printer = printer
329        self.JobId = None
330        self.User = None
331        self.PrinterPageCounter = None
332        self.JobSize = None
333        self.JobAction = None
334        self.JobDate = None
335        self.JobPrice = None
336        self.JobFileName = None
337        self.JobTitle = None
338        self.JobCopies = None
339        self.JobOptions = None
340       
341    def setSize(self, jobsize) :
342        """Sets the last job's size."""
343        jobprice = (float(self.Printer.PricePerPage or 0.0) * jobsize) + float(self.Printer.PricePerJob or 0.0)
344        self.parent.writeLastJobSize(self, jobsize, jobprice)
345        self.JobSize = jobsize
346        self.JobPrice = jobprice
347   
348class BaseStorage :
349    def __init__(self, pykotatool) :
350        """Opens the LDAP connection."""
351        # raise PyKotaStorageError, "Sorry, the LDAP backend for PyKota is not yet implemented !"
352        self.closed = 1
353        self.tool = pykotatool
354        self.usecache = pykotatool.config.getCaching()
355        self.disablehistory = pykotatool.config.getDisableHistory()
356        if self.usecache :
357            self.tool.logdebug("Caching enabled.")
358            self.caches = { "USERS" : {}, "GROUPS" : {}, "PRINTERS" : {}, "USERPQUOTAS" : {}, "GROUPPQUOTAS" : {}, "JOBS" : {}, "LASTJOBS" : {} }
359       
360    def close(self) :   
361        """Must be overriden in children classes."""
362        raise RuntimeError, "BaseStorage.close() must be overriden !"
363       
364    def __del__(self) :       
365        """Ensures that the database connection is closed."""
366        self.close()
367       
368    def getFromCache(self, cachetype, key) :
369        """Tries to extract something from the cache."""
370        if self.usecache :
371            entry = self.caches[cachetype].get(key)
372            if entry is not None :
373                self.tool.logdebug("Cache hit (%s->%s)" % (cachetype, key))
374            else :   
375                self.tool.logdebug("Cache miss (%s->%s)" % (cachetype, key))
376            return entry   
377           
378    def cacheEntry(self, cachetype, key, value) :       
379        """Puts an entry in the cache."""
380        if self.usecache and getattr(value, "Exists", 0) :
381            self.caches[cachetype][key] = value
382            self.tool.logdebug("Cache store (%s->%s)" % (cachetype, key))
383           
384    def getUser(self, username) :       
385        """Returns the user from cache."""
386        user = self.getFromCache("USERS", username)
387        if user is None :
388            user = self.getUserFromBackend(username)
389            self.cacheEntry("USERS", username, user)
390        return user   
391       
392    def getGroup(self, groupname) :       
393        """Returns the group from cache."""
394        group = self.getFromCache("GROUPS", groupname)
395        if group is None :
396            group = self.getGroupFromBackend(groupname)
397            self.cacheEntry("GROUPS", groupname, group)
398        return group   
399       
400    def getPrinter(self, printername) :       
401        """Returns the printer from cache."""
402        printer = self.getFromCache("PRINTERS", printername)
403        if printer is None :
404            printer = self.getPrinterFromBackend(printername)
405            self.cacheEntry("PRINTERS", printername, printer)
406        return printer   
407       
408    def getUserPQuota(self, user, printer) :       
409        """Returns the user quota information from cache."""
410        useratprinter = "%s@%s" % (user.Name, printer.Name)
411        upquota = self.getFromCache("USERPQUOTAS", useratprinter)
412        if upquota is None :
413            upquota = self.getUserPQuotaFromBackend(user, printer)
414            self.cacheEntry("USERPQUOTAS", useratprinter, upquota)
415        return upquota   
416       
417    def getGroupPQuota(self, group, printer) :       
418        """Returns the group quota information from cache."""
419        groupatprinter = "%s@%s" % (group.Name, printer.Name)
420        gpquota = self.getFromCache("GROUPPQUOTAS", groupatprinter)
421        if gpquota is None :
422            gpquota = self.getGroupPQuotaFromBackend(group, printer)
423            self.cacheEntry("GROUPPQUOTAS", groupatprinter, gpquota)
424        return gpquota   
425       
426    def getPrinterLastJob(self, printer) :       
427        """Extracts last job information for a given printer from cache."""
428        lastjob = self.getFromCache("LASTJOBS", printer.Name)
429        if lastjob is None :
430            lastjob = self.getPrinterLastJobFromBackend(printer)
431            self.cacheEntry("LASTJOBS", printer.Name, lastjob)
432        return lastjob   
433       
434    def getParentPrinters(self, printer) :   
435        """Extracts parent printers information for a given printer from cache."""
436        if self.usecache :
437            if not hasattr(printer, "Parents") :
438                self.tool.logdebug("Cache miss (%s->Parents)" % printer.Name)
439                printer.Parents = self.getParentPrintersFromBackend(printer)
440                self.tool.logdebug("Cache store (%s->Parents)" % printer.Name)
441            else :
442                self.tool.logdebug("Cache hit (%s->Parents)" % printer.Name)
443        else :       
444            printer.Parents = self.getParentPrintersFromBackend(printer)
445        return printer.Parents
446       
447    def getGroupMembers(self, group) :       
448        """Returns the group's members list from in-group cache."""
449        if self.usecache :
450            if not hasattr(group, "Members") :
451                self.tool.logdebug("Cache miss (%s->Members)" % group.Name)
452                group.Members = self.getGroupMembersFromBackend(group)
453                self.tool.logdebug("Cache store (%s->Members)" % group.Name)
454            else :
455                self.tool.logdebug("Cache hit (%s->Members)" % group.Name)
456        else :       
457            group.Members = self.getGroupMembersFromBackend(group)
458        return group.Members   
459       
460    def getUserGroups(self, user) :       
461        """Returns the user's groups list from in-user cache."""
462        if self.usecache :
463            if not hasattr(user, "Groups") :
464                self.tool.logdebug("Cache miss (%s->Groups)" % user.Name)
465                user.Groups = self.getUserGroupsFromBackend(user)
466                self.tool.logdebug("Cache store (%s->Groups)" % user.Name)
467            else :
468                self.tool.logdebug("Cache hit (%s->Groups)" % user.Name)
469        else :       
470            user.Groups = self.getUserGroupsFromBackend(user)
471        return user.Groups   
472       
473    def getParentPrintersUserPQuota(self, userpquota) :     
474        """Returns all user print quota on the printer and its parents."""
475        upquotas = [ ]
476        for printer in self.getParentPrinters(userpquota.Printer) :
477            upquotas.append(self.getUserPQuota(userpquota.User, printer))
478        return upquotas       
479       
480def openConnection(pykotatool) :
481    """Returns a connection handle to the appropriate Quota Storage Database."""
482    backendinfo = pykotatool.config.getStorageBackend()
483    backend = backendinfo["storagebackend"]
484    try :
485        exec "from pykota.storages import %s as storagebackend" % backend.lower()
486    except ImportError :
487        raise PyKotaStorageError, _("Unsupported quota storage backend %s") % backend
488    else :   
489        host = backendinfo["storageserver"]
490        database = backendinfo["storagename"]
491        admin = backendinfo["storageadmin"] or backendinfo["storageuser"]
492        adminpw = backendinfo["storageadminpw"] or backendinfo["storageuserpw"]
493        return storagebackend.Storage(pykotatool, host, database, admin, adminpw)
Note: See TracBrowser for help on using the browser.