Skip to content

Commit

Permalink
Change default printer
Browse files Browse the repository at this point in the history
Allow default printer to be changed after initial deployment.
  • Loading branch information
scosist committed Aug 21, 2019
1 parent 52a655c commit 2271149
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions library/cups_lpadmin.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,8 @@ def __init__(self, module):

self.cups_current_options = {}
self.cups_expected_options = {}
self.cups_current_default = ""
self.cups_expected_default = ""
self.class_current_members = []
self.printer_current_options = {}

Expand Down Expand Up @@ -762,6 +764,45 @@ def _class_install_mandatory_options(self):
.format(self.name),
only_log_on_error=True)

def cups_item_get_current_default(self):
"""
Return current default printer.
"""
cmd = ['lpstat', '-d']
(rc, out, err) = self.process_info_command(cmd)

if rc != 0:
self.module.fail_json(
msg="Error occured while trying to discern printer '{0}' default.".format(self.name))

default = ""
# In the case of "no system default destination", return
if "no system default destination" in out:
return default

# Skip the first part as it's a description, ends with a ':'
(info, out) = out.split(':', 1)
default = str.strip(out)

self.cups_current_default = default

return default

def printer_check_default(self):
"""
It checks to see if printer is the default.
"""
expected_cups_default = self.name if self.default else False

self.cups_expected_default = expected_cups_default

cups_default = self.cups_item_get_current_default()

if expected_cups_default and expected_cups_default != cups_default:
return False

return True

def cups_item_uninstall_self(self):
"""
Uninstalls the printer or class defined in this class.
Expand Down Expand Up @@ -1040,6 +1081,9 @@ def printer_install(self):
if self.exists_self() and not self.printer_check_cups_options():
self.cups_item_uninstall_self()

if self.exists_self() and not self.printer_check_default():
self.cups_item_uninstall_self()

if not self.exists_self():
self._printer_install()

Expand Down Expand Up @@ -1125,6 +1169,10 @@ def start_process(self):
result['cups_current_options'] = self.cups_current_options
if self.cups_expected_options:
result['cups_expected_options'] = self.cups_expected_options
if self.cups_current_default:
result['cups_current_default'] = self.cups_current_default
if self.cups_expected_default:
result['cups_expected_default'] = self.cups_expected_default
if self.class_current_members:
result['class_current_members'] = self.class_current_members
if self.printer_current_options:
Expand Down

0 comments on commit 2271149

Please sign in to comment.