1
0
mirror of https://github.com/altlinux/gpupdate.git synced 2025-03-21 18:50:38 +03:00

Merge pull request #81 from altlinux/printer_applier

Enable Printers.xml parser and merger
This commit is contained in:
NIR 2020-06-25 13:24:18 +04:00 committed by GitHub
commit 004fc38962
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 38 additions and 17 deletions

View File

@ -18,6 +18,9 @@
import logging
import os
import json
import cups
from .applier_frontend import applier_frontend
from gpt.printers import json2printer
@ -32,18 +35,31 @@ def storage_get_printers(storage, sid):
printers = list()
for prnj in printer_objs:
prn_obj = json2printer(prnj)
printers.append(prn_obj)
printers.append(prnj)
return printers
def write_printer(prn):
def connect_printer(connection, prn):
'''
Dump printer cinfiguration to disk as CUPS config
'''
printer_config_path = os.path.join('/etc/cups', prn.name)
with open(printer_config_path, 'r') as f:
print(prn.cups_config(), file=f)
# PPD file location
printer_driver = 'generic'
pjson = json.loads(prn.printer)
printer_parts = pjson['printer']['path'].partition(' ')
# Printer queue name in CUPS
printer_name = printer_parts[2].replace('(', '').replace(')', '')
# Printer description in CUPS
printer_info = printer_name
printer_uri = printer_parts[0].replace('\\', '/')
printer_uri = 'smb:' + printer_uri
connection.addPrinter(
name=printer_name
, info=printer_info
, device=printer_uri
#filename=printer_driver
)
class cups_applier(applier_frontend):
def __init__(self, storage):
@ -57,11 +73,12 @@ class cups_applier(applier_frontend):
logging.warning(slogm('CUPS is not installed: no printer settings will be deployed'))
return
printers = storage_get_printers(self.storage, self.storage.get_info('machine_sid'))
self.cups_connection = cups.Connection()
self.printers = storage_get_printers(self.storage, self.storage.get_info('machine_sid'))
if printers:
for prn in printers:
write_printer(prn)
if self.printers:
for prn in self.printers:
connect_printer(self.cups_connection, prn)
class cups_applier_user(applier_frontend):
def __init__(self, storage, sid, username):
@ -84,9 +101,10 @@ class cups_applier_user(applier_frontend):
logging.warning(slogm('CUPS is not installed: no printer settings will be deployed'))
return
printers = storage_get_printers(self.storage, self.sid)
self.cups_connection = cups.Connection()
self.printers = storage_get_printers(self.storage, self.sid)
if printers:
for prn in printers:
write_printer(prn)
if self.printers:
for prn in self.printers:
connect_printer(self.cups_connection, prn)

View File

@ -87,6 +87,7 @@ class FileType(Enum):
ENVIRONMENTVARIABLES = 'environmentvariables.xml'
INIFILES = 'inifiles.xml'
SERVICES = 'services.xml'
PRINTERS = 'printers.xml'
def get_preftype(path_to_file):
fpath = Path(path_to_file)
@ -111,6 +112,7 @@ def pref_parsers():
parsers[FileType.ENVIRONMENTVARIABLES] = read_envvars
parsers[FileType.INIFILES] = read_inifiles
parsers[FileType.SERVICES] = read_services
parsers[FileType.PRINTERS] = read_printers
return parsers
@ -130,6 +132,7 @@ def pref_mergers():
mergers[FileType.ENVIRONMENTVARIABLES] = merge_envvars
mergers[FileType.INIFILES] = merge_inifiles
mergers[FileType.SERVICES] = merge_services
mergers[FileType.PRINTERS] = merge_printers
return mergers
@ -158,7 +161,7 @@ class gpt:
'shortcuts'
, 'drives'
, 'environmentvariables'
#, 'printers'
, 'printers'
, 'folders'
, 'files'
, 'inifiles'

View File

@ -43,7 +43,7 @@ def read_printers(printers_file):
def merge_printers(storage, sid, printer_objects, policy_name):
for device in printer_objects:
pass
storage.add_printer(sid, device, policy_name)
def json2printer(json_str):
'''

View File

@ -96,7 +96,7 @@ class printer_entry(object):
fields = dict()
fields['policy_name'] = self.policy_name
fields['name'] = self.name
fields['printer'] = self.printer
fields['printer'] = self.printer.to_json()
return fields