Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

allow remote cups, user defined file prefix

  • Loading branch information...
commit af37647a168c234d7b11d40c70b1d992fd627c95 1 parent 99f781a
Timothy J Fontaine authored
Showing with 66 additions and 9 deletions.
  1. +32 −1 README
  2. +34 −8 airprint-generate.py
33 README
View
@@ -1 +1,32 @@
-A script to generate Avahi service files for cups printers
+airprint-generate.py
+
+This script will generate avahi .service files for shared CUPS printers.
+
+This script will connect to a CUPS server and for each printer configured and
+marked as shared will generate a .service file for avahi that is compatible
+with Apple's AirPrint announcements. Any printer that can be configured to work
+with CUPS can be used. Printers should not be configured in CUPS as raw, unless
+the printer can natively print PDF. That is to say, CUPS needs to already be
+configured with a PDF filter. Debian based distributions ship CUPS pre-configured
+this way.
+
+DNSSD has a limit of 255 Chars for a given txt-record, because of this the list
+of accepted pdl's will be truncated to fit. If you're curious to see which ones
+are trimmed out of the list run with the script with the verbose flag (--verbose)
+
+If python-lxml is installed, .service files will be generated in a human
+readble format, I wasn't able to get minidom's version to work acceptably.
+
+Usage: airprint-generate.py [options]
+
+Options:
+ -h, --help show this help message and exit
+ -H HOSTNAME, --host=HOSTNAME
+ Hostname of CUPS server (optional)
+ -P PORT, --port=PORT Port number of CUPS server
+ -u USER, --user=USER Username to authenticate with against CUPS
+ -d DIRECTORY, --directory=DIRECTORY
+ Directory to create service files
+ -v, --verbose Print debugging information to STDERR
+ -p PREFIX, --prefix=PREFIX
+ Prefix all files with this string
42 airprint-generate.py
View
@@ -57,6 +57,7 @@
DOCUMENT_TYPES = {
+ # These content-types will be at the front of the list
'application/pdf': True,
'application/postscript': True,
'application/vnd.cups-raster': True,
@@ -69,6 +70,7 @@
'text/plain': True,
'text/html': True,
+ # These content-types will never be reported
'image/x-xwindowdump': False,
'image/x-xpixmap': False,
'image/x-xbitmap': False,
@@ -83,19 +85,27 @@
'application/x-csource': False,
'application/x-cshell': False,
}
+
class AirPrintGenerate(object):
- def __init__(self, host=None, user=None, port=None, verbose=False, directory=None):
+ def __init__(self, host=None, user=None, port=None, verbose=False, directory=None, prefix='AirPrint-'):
self.host = host
self.user = user
self.port = port
self.verbose = verbose
self.directory = directory
+ self.prefix = prefix
if self.user:
cups.setUser(self.user)
def generate(self):
- conn = cups.Connection()
+ if not self.host:
+ conn = cups.Connection()
+ else:
+ if not self.port:
+ self.port = 631
+ conn = cups.Connection(self.host, self.port)
+
printers = conn.getPrinters()
for p, v in printers.items():
@@ -167,7 +177,7 @@ def generate(self):
dt= minidom.getDOMImplementation('').createDocumentType('service-group', None, 'avahi-service.dtd')
doc.insertBefore(dt, doc.documentElement)
- fname = 'airprint-%s.service' % p
+ fname = '%s%s.service' % (self.prefix, p)
if self.directory:
fname = os.path.join(self.directory, fname)
@@ -186,14 +196,29 @@ def generate(self):
if __name__ == '__main__':
parser = optparse.OptionParser()
- parser.add_option('-H', '--host', action="store", type="string", dest='hostname', help='Hostname of CUPS server', metavar='HOSTNAME')
- parser.add_option('-P', '--port', action="store", type="int", dest='port', help='Port number of CUPS server', metavar='PORT')
- parser.add_option('-u', '--user', action="store", type="string", dest='username', help='Username to authenticate with against CUPS', metavar='USER')
- parser.add_option('-d', '--directory', action="store", type="string", dest='directory', help='Directory to create service files', metavar='DIRECTORY')
- parser.add_option('-v', '--verbose', action="store_true", dest="verbose", help="Print debugging information to STDERR")
+ parser.add_option('-H', '--host', action="store", type="string",
+ dest='hostname', help='Hostname of CUPS server (optional)', metavar='HOSTNAME')
+ parser.add_option('-P', '--port', action="store", type="int",
+ dest='port', help='Port number of CUPS server', metavar='PORT')
+ parser.add_option('-u', '--user', action="store", type="string",
+ dest='username', help='Username to authenticate with against CUPS',
+ metavar='USER')
+ parser.add_option('-d', '--directory', action="store", type="string",
+ dest='directory', help='Directory to create service files',
+ metavar='DIRECTORY')
+ parser.add_option('-v', '--verbose', action="store_true", dest="verbose",
+ help="Print debugging information to STDERR")
+ parser.add_option('-p', '--prefix', action="store", type="string",
+ dest='prefix', help='Prefix all files with this string', metavar='PREFIX',
+ default='AirPrint-')
(options, args) = parser.parse_args()
+ # TODO XXX FIXME -- if cups login required, need to add
+ # air=username,password
+ from getpass import getpass
+ cups.setPasswordCB(getpass)
+
if options.directory:
if not os.path.exists(options.directory):
os.mkdir(options.directory)
@@ -204,6 +229,7 @@ def generate(self):
port=options.port,
verbose=options.verbose,
directory=options.directory,
+ prefix=options.prefix
)
apg.generate()
Please sign in to comment.
Something went wrong with that request. Please try again.