2 # -*- coding: utf-8 -*-
4 # (c) Copyright 2009 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
24 __title__ = 'Model Query Utility'
26 __doc__ = "Query a printer model for static model information. Designed to be called from other processes."
33 from base import device, models, module
38 mod = module.Module(__mod__, __title__, __version__, __doc__, None,
39 (NON_INTERACTIVE_MODE,), quiet=True)
43 ("Specify model by device URI:", "-d<device_uri> or --device=<device_uri>", "option", False),
44 ("Specify normalized model name:", "-m<model_name> or --model=<model_name> (normalized models.dat format)", "option", False),
45 ("Specify raw model name:", "-r<model_name> or --raw=<model_name> (raw model name from MDL: field of device ID)", "option", False),
46 ("Specify key to query:", "-k<key> or --key=<key> (or, use -a/--all to return all keys)", "option", False),
47 ("Query all keys:", "-a or --all (default separator is a LF)", "option", False),
48 ("Specify the separator when multiple keys are queried:", "-s<sep> --sep=<sep> (character or 'tab', 'newline', 'cr', 'lf', 'crlf')(only valid when used with -a/--all)", "option", False),
49 ("Suppress trailing linefeed:", "-x", "option", False),],
50 see_also_list=['hp-info'])
52 opts, device_uri, printer_name, mode, ui_toolkit, lang = \
53 mod.parseStdOpts('m:k:as:d:r:x', ['model=', 'key=', 'sep=', 'all', 'device=', 'raw='],
54 handle_device_printer=False)
62 suppress_trailing_linefeed = False
65 if o in ('-m', '--model'):
68 elif o in ('-d', '--model'):
71 elif o in ('-k', '--key'):
75 elif o in ('-a', '--all'):
79 elif o in ('-r', '--raw'):
82 elif o in ('-s', '--sep'):
86 suppress_trailing_linefeed = True
88 if (device_uri and norm_model) or \
89 (device_uri and raw_model) or \
90 (norm_model and raw_model):
91 log.stderr("error: You may only specify one of -d, -m, or -r.")
94 if not device_uri and not norm_model and not raw_model:
95 log.stderr("error: You must specify one of -d, -m, or -r.")
100 back_end, is_hp, bus, norm_model, serial, dev_file, host, zc, port = \
101 device.parseDeviceURI(device_uri)
103 log.stderr("error: Invalid device URI: %s" % device_uri)
107 norm_model = models.normalizeModelName(raw_model).lower()
110 log.stderr("error: Invalid model name.")
114 if s in ('lf', 'newline'):
123 log.stderr("error: Separator must not be '='.")
126 data = device.queryModelByModel(norm_model)
129 log.stderr("error: Model name '%s' not found." % norm_model)
138 output = '%s=%s' % (k, data[k])
140 output = sep.join([output, '%s=%s' % (k, data[k])])
146 log.stderr("error: Key '%s' not found." % key)
149 output = '%s=%s' % (key, data[key])
152 log.stderr("error: Must specify key with -k/--key or specify -a/--all.")
155 if suppress_trailing_linefeed:
161 except KeyboardInterrupt: