2 # -*- coding: utf-8 -*-
4 # (c) Copyright 2003-2008 Hewlett-Packard Development Company, L.P.
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 # Authors: Don Welch, Smith Kennedy
24 __title__ = 'Device URI Creation Utility'
25 __mod__ = 'hp-makeuri'
26 __doc__ = "Creates device URIs for local and network connected printers for use with CUPS."
36 from base.codes import *
37 from base import device, utils, module
40 USAGE = [ (__doc__, "", "name", True),
41 ("Usage: %s [OPTIONS] [SERIAL NO.|USB ID|IP|DEVNODE]" % __mod__, "", "summary", True),
42 ("[SERIAL NO.|USB ID|IP|DEVNODE]", "", "heading", False),
43 ("USB IDs (usb only):", """"xxx:yyy" where xxx is the USB bus ID and yyy is the USB device ID. The ':' and all leading zeroes must be present.""", 'option', False),
44 ("", """(Use the 'lsusb' command to obtain this information. See Note 1.)""", "option", False),
45 ("IPs (network only):", 'IPv4 address "a.b.c.d" or "hostname"', "option", False),
46 ("DEVNODE (parallel only):", '"/dev/parportX", X=0,1,2,...', "option", False),
47 ("SERIAL NO. (usb and parallel only):", '"serial no."', "option", True),
49 ("To specify the port on a multi-port JetDirect:", "-p<port> or --port=<port> (Valid values are 1\*, 2, and 3. \*default)", "option", False),
50 ("Show the CUPS URI only (quiet mode):", "-c or --cups", "option", False),
51 ("Show the SANE URI only (quiet mode):", "-s or --sane", "option", False),
52 ("Show the HP Fax URI only (quiet mode):", "-f or --fax", "option", False),
53 utils.USAGE_LOGGING1, utils.USAGE_LOGGING2, utils.USAGE_LOGGING3,
56 ("USB:", "$ hp-makeuri 001:002", "example", False),
57 ("Network:", "$ hp-makeuri 66.35.250.209", "example", False),
58 ("Parallel:", "$ hp-makeuri /dev/parport0", "example", False),
59 ("USB or parallel (using serial number):", "$ hp-makeuri US123456789", "example", False),
62 ("1. Example using 'lsusb' to obtain USB bus ID and USB device ID (example only, the values you obtain will differ) :", "", 'note', False),
63 (" $ lsusb", "", 'note', False),
64 (" Bus 003 Device 011: ID 03f0:c202 Hewlett-Packard", "", 'note', False),
65 (" $ hp-makeuri 003:011", "", 'note', False),
66 (" (Note: You may have to run 'lsusb' from /sbin or another location. Use '$ locate lsusb' to determine this.)", "", 'note', True),
69 ("hp-setup", "", "seealso", False),
73 mod = module.Module(__mod__, __title__, __version__, __doc__, USAGE,
74 (INTERACTIVE_MODE,), None, True, True)
76 opts, device_uri, printer_name, mode, ui_toolkit, lang = \
77 mod.parseStdOpts('p:csf', ['port', 'cups', 'sane', 'fax'],
78 handle_device_printer=False)
81 cups_quiet_mode = False
82 sane_quiet_mode = False
83 fax_quiet_mode = False
87 if o in ('-c', '--cups'):
88 cups_quiet_mode = True
90 elif o in ('-s', '--sane'):
91 sane_quiet_mode = True
93 elif o in ('-f', '--fax'):
96 elif o in ('-p', '--port'):
100 mod.usage(error_msg=["Invalid port number. Must be between 1 and 3 inclusive."])
103 log.set_level('debug')
106 quiet_mode = cups_quiet_mode or sane_quiet_mode or fax_quiet_mode
107 mod.quiet = quiet_mode
110 # log.set_level('warn')
112 #utils.log_title(__title__, __version__)
115 if len(mod.args) != 1:
116 mod.usage(error_msg=["You must specify one SERIAL NO., IP, USB ID or DEVNODE parameter."])
120 if 'localhost' in param.lower():
121 mod.usage(error_msg=['Invalid hostname'])
123 cups_uri, sane_uri, fax_uri = device.makeURI(param, jd_port)
126 log.error("Device not found")
133 print "CUPS URI: %s" % cups_uri
140 print "SANE URI: %s" % sane_uri
142 elif not sane_uri and sane_quiet_mode:
143 log.error("Device does not support scan.")
150 print "HP Fax URI: %s" % fax_uri
152 elif not fax_uri and fax_quiet_mode:
153 log.error("Device does not support fax.")
155 except KeyboardInterrupt:
156 log.error("User exit")