我找到了这个问题的答案:
我使用一个函数来查找每个登录的用户并使用每个登录用户的所有 yaml 文件(userconfig.yaml 被永久保存并在登录期间获取更新)。
# using self made module system.current_users()
users = system.current_users()
printers_with_users = {}
for username in users:
try:
printer_yaml = config_data.retrieve_for(username).get(u"printers", [])
except:
print("Aborting: Can't find userconfig.yaml!\n")
exit(1)
for device, dev_list in printer_yaml.iteritems():
# parsing datas...
config_classes = dev_list.get("config_classes", None)
descr = get_unicode(dev_list, u"descr")
printer_id = get_unicode(dev_list, u"id")
is_local = dev_list.get("is_local", None)
listen_network = dev_list.get("listen_network", None)
location = get_unicode(dev_list, u"location")
ppd_file = get_unicode(dev_list, u"ppd_file")
ppd_uri = get_unicode(dev_list, u"ppd_uri")
printer_uri = get_unicode(dev_list, u"printer_uri")
# here is the cool part ;)
printers_with_users.setdefault(printer_id, []).append(username)
#configuring printers...
# configure access rules in printers.conf
for printers, accepted_users in printers_with_users.iteritems():
users_string = ",".join(accepted_users)
try:
subproc.call('lpadmin', ['-p', printers, '-u', 'allow:%s' % users_string])
except:
print ( " - unable to set user access rules\n" )
print (config_error) % printers
continue
我也在为班级做同样的事情。
我仍然可以在每次登录期间清理整个配置文件,并且还可以确保每个用户都可以使用自己的打印机。
希望这对遇到相同问题的每个人都有帮助(即使它只是整个打印机管理器的一小部分;))。