Replace 'tap' to 'spaces' to make gbs build succeed
[platform/upstream/hplip.git] / query.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 #
4 # (c) Copyright 2009 Hewlett-Packard Development Company, L.P.
5 #
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.
10 #
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.
15 #
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
19 #
20 # Author: Don Welch
21 #
22
23 __version__ = '0.2'
24 __title__ = 'Model Query Utility'
25 __mod__ = 'hp-query'
26 __doc__ = "Query a printer model for static model information. Designed to be called from other processes."
27
28 # Std Lib
29 import sys
30
31 # Local
32 from base.g import *
33 from base import device, models, module
34
35
36 try:
37
38     mod = module.Module(__mod__, __title__, __version__, __doc__, None,
39                         (NON_INTERACTIVE_MODE,),  quiet=True)
40
41     mod.setUsage(0,
42         extra_options=[
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'])
51
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)
55
56     norm_model = None
57     raw_model = None
58     device_uri = None
59     key = None
60     all_keys = False
61     sep = 'lf'
62     suppress_trailing_linefeed = False
63
64     for o, a in opts:
65         if o in ('-m', '--model'):
66             norm_model = a
67
68         elif o in ('-d', '--model'):
69             device_uri = a
70
71         elif o in ('-k', '--key'):
72             key = a
73             all_keys = False
74
75         elif o in ('-a', '--all'):
76             all_keys = True
77             key = None
78
79         elif o in ('-r', '--raw'):
80             raw_model = a
81
82         elif o in ('-s', '--sep'):
83             sep = a
84
85         elif o == '-x':
86             suppress_trailing_linefeed = True
87
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.")
92         sys.exit(1)
93
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.")
96         sys.exit(1)
97
98     if device_uri:
99         try:
100             back_end, is_hp, bus, norm_model, serial, dev_file, host, zc, port = \
101                 device.parseDeviceURI(device_uri)
102         except Error:
103             log.stderr("error: Invalid device URI: %s" % device_uri)
104             sys.exit(1)
105
106     elif raw_model:
107         norm_model = models.normalizeModelName(raw_model).lower()
108
109     if not norm_model:
110         log.stderr("error: Invalid model name.")
111         sys.exit(1)
112
113     s = sep.lower()
114     if s in ('lf', 'newline'):
115         sep = '\n'
116     elif s == 'cr':
117         sep = '\r'
118     elif s == 'crlf':
119         sep = '\r\n'
120     elif s == 'tab':
121         sep = '\t'
122     elif s == '=':
123         log.stderr("error: Separator must not be '='.")
124         sys.exit(1)
125
126     data = device.queryModelByModel(norm_model)
127
128     if not data:
129         log.stderr("error: Model name '%s' not found." % norm_model)
130         sys.exit(1)
131
132     output = ''
133     if all_keys:
134         kk = data.keys()
135         kk.sort()
136         for k in kk:
137             if not output:
138                 output = '%s=%s' % (k, data[k])
139             else:
140                 output = sep.join([output, '%s=%s' % (k, data[k])])
141
142     elif key:
143         try:
144             data[key]
145         except KeyError:
146             log.stderr("error: Key '%s' not found." % key)
147             sys.exit(1)
148         else:
149             output = '%s=%s' % (key, data[key])
150
151     else:
152         log.stderr("error: Must specify key with -k/--key or specify -a/--all.")
153         sys.exit(1)
154
155     if suppress_trailing_linefeed:
156         print output,
157     else:
158         print output
159
160
161 except KeyboardInterrupt:
162     pass
163
164
165