1 | # PyKota |
---|
2 | # |
---|
3 | # PyKota : Print Quotas for CUPS |
---|
4 | # |
---|
5 | # (c) 2003 Jerome Alet <alet@librelogiciel.com> |
---|
6 | # You're welcome to redistribute this software under the |
---|
7 | # terms of the GNU General Public Licence version 2.0 |
---|
8 | # or, at your option, any higher version. |
---|
9 | # |
---|
10 | # You can read the complete GNU GPL in the file COPYING |
---|
11 | # which should come along with this software, or visit |
---|
12 | # the Free Software Foundation's WEB site http://www.fsf.org |
---|
13 | # |
---|
14 | # $Id$ |
---|
15 | # |
---|
16 | # $Log$ |
---|
17 | # Revision 1.17 2003/02/10 08:41:36 jalet |
---|
18 | # edpykota's --reset command line option resets the limit date too. |
---|
19 | # |
---|
20 | # Revision 1.16 2003/02/08 22:39:46 jalet |
---|
21 | # --reset command line option added |
---|
22 | # |
---|
23 | # Revision 1.15 2003/02/08 22:12:09 jalet |
---|
24 | # Life time counter for users and groups added. |
---|
25 | # |
---|
26 | # Revision 1.14 2003/02/07 22:13:13 jalet |
---|
27 | # Perhaps edpykota is now able to add printers !!! Oh, stupid me ! |
---|
28 | # |
---|
29 | # Revision 1.13 2003/02/07 00:08:52 jalet |
---|
30 | # Typos |
---|
31 | # |
---|
32 | # Revision 1.12 2003/02/06 23:20:03 jalet |
---|
33 | # warnpykota doesn't need any user/group name argument, mimicing the |
---|
34 | # warnquota disk quota tool. |
---|
35 | # |
---|
36 | # Revision 1.11 2003/02/06 15:05:13 jalet |
---|
37 | # self was forgotten |
---|
38 | # |
---|
39 | # Revision 1.10 2003/02/06 15:03:11 jalet |
---|
40 | # added a method to set the limit date |
---|
41 | # |
---|
42 | # Revision 1.9 2003/02/06 14:52:35 jalet |
---|
43 | # Forgotten import |
---|
44 | # |
---|
45 | # Revision 1.8 2003/02/06 14:49:04 jalet |
---|
46 | # edpykota should be ok now |
---|
47 | # |
---|
48 | # Revision 1.7 2003/02/06 14:28:59 jalet |
---|
49 | # edpykota should be ok, minus some typos |
---|
50 | # |
---|
51 | # Revision 1.6 2003/02/06 09:19:02 jalet |
---|
52 | # More robust behavior (hopefully) when the user or printer is not managed |
---|
53 | # correctly by the Quota System : e.g. cupsFilter added in ppd file, but |
---|
54 | # printer and/or user not 'yet?' in storage. |
---|
55 | # |
---|
56 | # Revision 1.5 2003/02/05 23:26:22 jalet |
---|
57 | # Incorrect handling of grace delay |
---|
58 | # |
---|
59 | # Revision 1.4 2003/02/05 23:02:10 jalet |
---|
60 | # Typo |
---|
61 | # |
---|
62 | # Revision 1.3 2003/02/05 23:00:12 jalet |
---|
63 | # Forgotten import |
---|
64 | # Bad datetime conversion |
---|
65 | # |
---|
66 | # Revision 1.2 2003/02/05 22:28:38 jalet |
---|
67 | # More robust storage |
---|
68 | # |
---|
69 | # Revision 1.1 2003/02/05 21:28:17 jalet |
---|
70 | # Initial import into CVS |
---|
71 | # |
---|
72 | # |
---|
73 | # |
---|
74 | |
---|
75 | import fnmatch |
---|
76 | |
---|
77 | class SQLStorage : |
---|
78 | def getMatchingPrinters(self, printerpattern) : |
---|
79 | """Returns the list of all printer names which match a certain pattern.""" |
---|
80 | printerslist = [] |
---|
81 | # We 'could' do a SELECT printername FROM printers WHERE printername LIKE ... |
---|
82 | # but we don't because other storages semantics may be different, so every |
---|
83 | # storage should use fnmatch to match patterns and be storage agnostic |
---|
84 | result = self.doQuery("SELECT printername FROM printers;") |
---|
85 | result = self.doParseResult(result) |
---|
86 | if result is not None : |
---|
87 | for printer in result : |
---|
88 | if fnmatch.fnmatchcase(printer["printername"], printerpattern) : |
---|
89 | printerslist.append(printer["printername"]) |
---|
90 | return printerslist |
---|
91 | |
---|
92 | def addPrinter(self, printername) : |
---|
93 | """Adds a printer to the quota storage.""" |
---|
94 | self.doQuery("INSERT INTO printers (printername) VALUES (%s);" % self.doQuote(printername)) |
---|
95 | |
---|
96 | def getPrinterUsers(self, printername) : |
---|
97 | """Returns the list of usernames which uses a given printer.""" |
---|
98 | result = self.doQuery("SELECT DISTINCT username FROM users WHERE id IN (SELECT userid FROM userpquota WHERE printerid IN (SELECT printerid FROM printers WHERE printername=%s));" % self.doQuote(printername)) |
---|
99 | result = self.doParseResult(result) |
---|
100 | if result is None : |
---|
101 | return [] |
---|
102 | else : |
---|
103 | return [record["username"] for record in result] |
---|
104 | |
---|
105 | def getPrinterGroups(self, printername) : |
---|
106 | """Returns the list of groups which uses a given printer.""" |
---|
107 | result = self.doQuery("SELECT DISTINCT groupname FROM groups WHERE id IN (SELECT groupid FROM grouppquota WHERE printerid IN (SELECT printerid FROM printers WHERE printername=%s));" % self.doQuote(printername)) |
---|
108 | result = self.doParseResult(result) |
---|
109 | if result is None : |
---|
110 | return [] |
---|
111 | else : |
---|
112 | return [record["groupname"] for record in result] |
---|
113 | |
---|
114 | def getUserId(self, username) : |
---|
115 | """Returns a userid given a username.""" |
---|
116 | result = self.doQuery("SELECT id FROM users WHERE username=%s;" % self.doQuote(username)) |
---|
117 | try : |
---|
118 | return self.doParseResult(result)[0]["id"] |
---|
119 | except TypeError : # Not found |
---|
120 | return |
---|
121 | |
---|
122 | def getPrinterId(self, printername) : |
---|
123 | """Returns a printerid given a printername.""" |
---|
124 | result = self.doQuery("SELECT id FROM printers WHERE printername=%s;" % self.doQuote(printername)) |
---|
125 | try : |
---|
126 | return self.doParseResult(result)[0]["id"] |
---|
127 | except TypeError : # Not found |
---|
128 | return |
---|
129 | |
---|
130 | def getPrinterPageCounter(self, printername) : |
---|
131 | """Returns the last page counter value for a printer given its name.""" |
---|
132 | result = self.doQuery("SELECT pagecounter, lastusername FROM printers WHERE printername=%s;" % self.doQuote(printername)) |
---|
133 | try : |
---|
134 | return self.doParseResult(result)[0] |
---|
135 | except TypeError : # Not found |
---|
136 | return |
---|
137 | |
---|
138 | def updatePrinterPageCounter(self, printername, username, pagecount) : |
---|
139 | """Updates the last page counter information for a printer given its name, last username and pagecount.""" |
---|
140 | return self.doQuery("UPDATE printers SET pagecounter=%s, lastusername=%s WHERE printername=%s;" % (self.doQuote(pagecount), self.doQuote(username), self.doQuote(printername))) |
---|
141 | |
---|
142 | def addUserPQuota(self, username, printername) : |
---|
143 | """Initializes a user print quota on a printer, adds the printer and the user to the quota storage if needed.""" |
---|
144 | (userid, printerid) = self.getUPIds(username, printername) |
---|
145 | if printerid is None : |
---|
146 | self.addPrinter(printername) # should we still add it ? |
---|
147 | if userid is None : |
---|
148 | self.doQuery("INSERT INTO users (username) VALUES (%s);" % self.doQuote(username)) |
---|
149 | (userid, printerid) = self.getUPIds(username, printername) |
---|
150 | if (userid is not None) and (printerid is not None) : |
---|
151 | return self.doQuery("INSERT INTO userpquota (userid, printerid) VALUES (%s, %s);" % (self.doQuote(userid), self.doQuote(printerid))) |
---|
152 | |
---|
153 | def getUPIds(self, username, printername) : |
---|
154 | """Returns a tuple (userid, printerid) given a username and a printername.""" |
---|
155 | return (self.getUserId(username), self.getPrinterId(printername)) |
---|
156 | |
---|
157 | def getUserPQuota(self, username, printername) : |
---|
158 | """Returns the Print Quota information for a given (username, printername).""" |
---|
159 | (userid, printerid) = self.getUPIds(username, printername) |
---|
160 | if (userid is not None) and (printerid is not None) : |
---|
161 | result = self.doQuery("SELECT lifepagecounter, pagecounter, softlimit, hardlimit, datelimit FROM userpquota WHERE userid=%s AND printerid=%s;" % (self.doQuote(userid), self.doQuote(printerid))) |
---|
162 | try : |
---|
163 | return self.doParseResult(result)[0] |
---|
164 | except TypeError : # Not found |
---|
165 | pass |
---|
166 | |
---|
167 | def setUserPQuota(self, username, printername, softlimit, hardlimit) : |
---|
168 | """Sets soft and hard limits for a user quota on a specific printer given (username, printername).""" |
---|
169 | (userid, printerid) = self.getUPIds(username, printername) |
---|
170 | if (userid is not None) and (printerid is not None) : |
---|
171 | self.doQuery("UPDATE userpquota SET softlimit=%s, hardlimit=%s, datelimit=NULL WHERE userid=%s AND printerid=%s;" % (self.doQuote(softlimit), self.doQuote(hardlimit), self.doQuote(userid), self.doQuote(printerid))) |
---|
172 | |
---|
173 | def resetUserPQuota(self, username, printername) : |
---|
174 | """Resets the page counter to zero. Life time page counter is kept unchanged.""" |
---|
175 | (userid, printerid) = self.getUPIds(username, printername) |
---|
176 | if (userid is not None) and (printerid is not None) : |
---|
177 | self.doQuery("UPDATE userpquota SET pagecounter=0, datelimit=NULL WHERE userid=%s AND printerid=%s;" % (self.doQuote(userid), self.doQuote(printerid))) |
---|
178 | |
---|
179 | def setDateLimit(self, username, printername, datelimit) : |
---|
180 | """Sets the limit date for a soft limit to become an hard one given (username, printername).""" |
---|
181 | (userid, printerid) = self.getUPIds(username, printername) |
---|
182 | if (userid is not None) and (printerid is not None) : |
---|
183 | self.doQuery("UPDATE userpquota SET datelimit=%s::DATETIME WHERE userid=%s AND printerid=%s;" % (self.doQuote("%04i-%02i-%02i %02i:%02i:%02i" % (datelimit.year, datelimit.month, datelimit.day, datelimit.hour, datelimit.minute, datelimit.second)), self.doQuote(userid), self.doQuote(printerid))) |
---|
184 | |
---|
185 | def updateUserPQuota(self, username, printername, pagecount) : |
---|
186 | """Updates the used user Quota information given (username, printername) and a job size in pages.""" |
---|
187 | (userid, printerid) = self.getUPIds(username, printername) |
---|
188 | if (userid is not None) and (printerid is not None) : |
---|
189 | self.doQuery("UPDATE userpquota SET lifepagecounter=lifepagecounter+(%s), pagecounter=pagecounter+(%s) WHERE userid=%s AND printerid=%s;" % (self.doQuote(pagecount), self.doQuote(pagecount), self.doQuote(userid), self.doQuote(printerid))) |
---|
190 | |
---|