1 | # PyKota |
---|
2 | # -*- coding: ISO-8859-15 -*- |
---|
3 | # |
---|
4 | # PyKota - Print Quotas for CUPS and LPRng |
---|
5 | # |
---|
6 | # (c) 2003-2004 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.13 2004/01/08 14:10:33 jalet |
---|
25 | # Copyright year changed. |
---|
26 | # |
---|
27 | # Revision 1.12 2003/10/07 09:07:29 jalet |
---|
28 | # Character encoding added to please latest version of Python |
---|
29 | # |
---|
30 | # Revision 1.11 2003/07/07 11:49:24 jalet |
---|
31 | # Lots of small fixes with the help of PyChecker |
---|
32 | # |
---|
33 | # Revision 1.10 2003/05/22 12:56:06 jalet |
---|
34 | # Uses SNMP version 1 instead of version 2c by default, which is probably |
---|
35 | # a safer bet. |
---|
36 | # |
---|
37 | # Revision 1.9 2003/05/21 10:07:09 jalet |
---|
38 | # Now works with net-snmp v5.0 and above. |
---|
39 | # (It already worked, but the sample configuration file didn't contain |
---|
40 | # appropriate values) |
---|
41 | # |
---|
42 | # Revision 1.8 2003/04/23 22:13:57 jalet |
---|
43 | # Preliminary support for LPRng added BUT STILL UNTESTED. |
---|
44 | # |
---|
45 | # Revision 1.7 2003/03/29 13:45:27 jalet |
---|
46 | # GPL paragraphs were incorrectly (from memory) copied into the sources. |
---|
47 | # Two README files were added. |
---|
48 | # Upgrade script for PostgreSQL pre 1.01 schema was added. |
---|
49 | # |
---|
50 | # Revision 1.6 2003/02/10 11:47:39 jalet |
---|
51 | # Moved some code down into the requesters |
---|
52 | # |
---|
53 | # Revision 1.5 2003/02/10 00:42:17 jalet |
---|
54 | # External requester should be ok (untested) |
---|
55 | # New syntax for configuration file wrt requesters |
---|
56 | # |
---|
57 | # Revision 1.4 2003/02/09 13:05:43 jalet |
---|
58 | # Internationalization continues... |
---|
59 | # |
---|
60 | # Revision 1.3 2003/02/07 13:12:41 jalet |
---|
61 | # Bad old comment |
---|
62 | # |
---|
63 | # Revision 1.2 2003/02/05 23:00:12 jalet |
---|
64 | # Forgotten import |
---|
65 | # Bad datetime conversion |
---|
66 | # |
---|
67 | # Revision 1.1 2003/02/05 21:28:17 jalet |
---|
68 | # Initial import into CVS |
---|
69 | # |
---|
70 | # |
---|
71 | # |
---|
72 | |
---|
73 | import os |
---|
74 | from pykota.requester import PyKotaRequesterError |
---|
75 | |
---|
76 | class Requester : |
---|
77 | """A class to send queries to printers via SNMP.""" |
---|
78 | def __init__(self, printername, arguments) : |
---|
79 | """Sets instance vars depending on the current printer.""" |
---|
80 | self.printername = printername |
---|
81 | args = [x.strip() for x in arguments.split(',')] |
---|
82 | self.community = args[0] |
---|
83 | self.oid = args[1] |
---|
84 | |
---|
85 | def getPrinterPageCounter(self, hostname) : |
---|
86 | """Returns the page counter from the hostname printer via SNMP. |
---|
87 | |
---|
88 | Currently uses the snmpget external command. TODO : do it internally |
---|
89 | """ |
---|
90 | if hostname is None : |
---|
91 | raise PyKotaRequesterError, _("Unknown printer address in SNMP(%s, %s) for printer %s") % (self.community, self.oid, self.printername) |
---|
92 | answer = os.popen("snmpget -v1 -c %s -Ov %s %s" % (self.community, hostname, self.oid)) |
---|
93 | try : |
---|
94 | pagecounter = int(answer.readline().split()[-1].strip()) |
---|
95 | except IndexError : |
---|
96 | raise PyKotaRequesterError, _("Unable to query printer %s via SNMP(%s, %s)") % (hostname, self.community, self.oid) |
---|
97 | answer.close() |
---|
98 | return pagecounter |
---|
99 | |
---|