--- umc/python/printers/__init__.py (Revision 67378) +++ umc/python/printers/__init__.py (Revision 68926) @@ -31,34 +31,28 @@ # /usr/share/common-licenses/AGPL-3; if not, see # . -import pprint +import re import subprocess -import os -import univention.management.console as umc -import univention.management.console.modules as umcm -from univention.management.console.modules import UMC_CommandError +import lxml.html -import univention.config_registry +from univention.lib.i18n import Translation + import univention.admin.uldap -import re - +from univention.management.console.base import Base from univention.management.console.log import MODULE -from univention.management.console.protocol.definitions import * +from univention.management.console.config import ucr +from univention.management.console.modules import UMC_Error from univention.management.console.modules.decorators import simple_response, log, sanitize from univention.management.console.modules.sanitizers import PatternSanitizer, ChoicesSanitizer -_ = umc.Translation('univention-management-console-module-printers').translate +_ = Translation('univention-management-console-module-printers').translate -class Instance(umcm.Base): +class Instance(Base): def init(self): + self._hostname = ucr.get('hostname') - self.ucr = univention.config_registry.ConfigRegistry() - self.ucr.load() - - self._hostname = self.ucr.get('hostname') - @sanitize(pattern=PatternSanitizer(default='.*'), key=ChoicesSanitizer(choices=['printer', 'description', 'location'], required=True)) @simple_response def list_printers(self, key, pattern): @@ -105,43 +99,55 @@ """ lists all quota entries related to this printer. """ result = [] + status = None - if not os.path.exists('/usr/bin/pkusers'): - raise UMC_CommandError(_('The print quota settings are currently disabled. Please install the package univention-printquota to enable them.')) + try: + from pykota.tool import PyKotaTool + from pykota import reporter + from pykota.storages.pgstorage import PGError + except ImportError: + raise UMC_Error(_('The print quota settings are currently disabled. Please install the package univention-printquota to enable them.')) - (stdout, stderr, status) = self._shell_command(['/usr/bin/pkusers', '--list'], {'LANG':'C'}) - users = [] - expr = re.compile('^\s*(.*?)\s+\-\s\<') - if status == 0: - for line in stdout.split("\n"): - match = expr.match(line) - if match: - users.append(match.group(1)) + reportTool = PyKotaTool() + try: + reportTool.deferredInit() + printers = reportTool.storage.getMatchingPrinters(printer) + reportingtool = reporter.openReporter(reportTool, 'html', printers, '*', 0) + status = reportingtool.generateReport() + except PGError as exc: + MODULE.error('Cannot connect to postgres: %s' % (exc,)) + raise UMC_Error(_('The connection to the print quota postgres database failed. Please make sure the postgres service is running and reachable.')) + finally: + reportTool.regainPriv() - result = [] - for user in users: - if not os.path.exists('/usr/bin/repykota'): - raise UMC_CommandError(_('The print quota settings are currently disabled. Please install the package univention-printquota to enable them.')) + if status: + tree = lxml.html.fromstring(status) + table = tree.find_class('pykotatable') + for i in table: + for a in i.iterchildren(tag='tr'): + data = list() + for b in a.iterchildren(tag='td'): + data.append(b.text_content().strip()) + if data and len(data) >= 11: + user = data[0] + #limitby = data[1] + #overcharge = data[2] + used = data[3] + soft = data[4] + hard = data[5] + #balance = data[6] + #grace = data[7] + total = data[8] + #paid = data[9] + #warn = data[10] + result.append(dict( + user=user, + used=used, + soft=soft, + hard=hard, + total=total, + )) - (stdout, stderr, status) = self._shell_command(['/usr/bin/repykota', '-P', printer, user], {'LANG':'C'}) - if status == 0: - for line in stdout.split("\n"): - data = line[16:].split() # ignore possibly truncated user name - if len(data) >= 7: - ok = True - for n in (2, 3,4, len(data)-3): - if not data[n].isdigit(): - ok = False - if ok: - MODULE.info(" -> user='%s' used=%s soft=%s hard=%s total=%s" % (user, data[2], data[3], data[4], data[len(data)-3])) - entry = { - 'user': user, - 'used': data[2], - 'soft': data[3], - 'hard': data[4], - 'total': data[len(data)-3] - } - result.append(entry) return result @simple_response @@ -179,7 +185,6 @@ return self._cancel_jobs(printer, jobs) - @simple_response @log def set_quota(self, printer='', user='', soft=0, hard=0): @@ -324,7 +329,7 @@ cmd.append(user) (stdout, stderr, status) = self._shell_command(cmd, {'LANG':'C'}) - if status or len(stderr): + if status or stderr: return stderr return '' @@ -350,9 +355,7 @@ return result # Printer specified: return its quota value or False if not found. - if printer in result: - return result[printer] - return False + return result.get(printer, False) def _cancel_jobs(self, printer, jobs): """ internal function that cancels a list of jobs. @@ -375,4 +378,3 @@ outputs = proc.communicate() return (outputs[0], outputs[1], proc.returncode) -