1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
|
#!/usr/bin/env python
# Copyright 2019 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
'''python %(prog)s [options]
Generate mapping from IPP attribute name to appropriate handler based on its
type as described in IPP registration files.'''
import argparse
import csv
import re
# Skip attributes that are already implemented in print preview plus job-priority (b/172208667).
NOOP_ATTRS = [
'copies',
'job-hold-until',
'job-copies',
'job-password',
'job-password-encryption',
'job-priority',
'media',
'media-col',
'multiple-document-handling',
'number-up',
'orientation-requested',
'page-ranges',
'presentation-direction-number-up',
'print-color-mode',
'print-scaling',
'printer-resolution',
'sheet-collate',
'sides',
]
# RFC 8011 (5.1.4) requires keywords to start with a letter. There's however at
# least one keyword that starts with a digit.
KEYWORD_PATTERN = re.compile(r'^[a-z1-9][a-z0-9\._-]*$')
HANDLER_HEADER = """// DO NOT MODIFY
// Generated by printing/backend/tools/code_generator.py
#include "printing/backend/ipp_handler_map.h"
#include <string_view>
#include "base/functional/bind.h"
#include "printing/backend/ipp_handlers.h"
namespace printing {
HandlerMap GenerateHandlers() {
HandlerMap result;
"""
HANDLER_FOOTER = """ return result;
}
} // namespace printing
"""
L10N_HEADER = """// DO NOT MODIFY
// Generated by printing/backend/tools/code_generator.py
#include "chrome/common/printing/ipp_l10n.h"
#include "base/no_destructor.h"
#include "components/strings/grit/components_strings.h"
const std::map<std::string_view, int>& CapabilityLocalizationMap() {
static const base::NoDestructor<std::map<std::string_view, int>> l10n_map({
"""
L10N_FOOTER = """ });
return *l10n_map;
}
"""
def get_handler(syntax, name):
if syntax.startswith('1setOf'):
handler = get_handler(syntax[6:].strip(), name)
if handler == 'EnumHandler':
# 3 is 'none' value for finishings.
# IPP enums always use positive numbers so we can use 0 in other cases.
default = 3 if name.endswith('finishings') else 0
return 'MultivalueEnumHandler, %d' % default
# TODO(crbug.com/964919): Add other multivalue handlers.
return ''
if syntax == 'collection':
# TODO(crbug.com/964919): Add collection handler.
return ''
if syntax.startswith('type1') or syntax.startswith('type2'):
# ignore prefix
return get_handler(syntax[5:].strip(), name)
if syntax.startswith('keyword'):
return 'KeywordHandler'
if syntax.startswith('enum'):
return 'EnumHandler'
if syntax == 'boolean':
return 'BooleanHandler'
if syntax.startswith('integer'):
# TODO(crbug.com/964919): Add integer handler.
return 'NumberHandler'
if syntax.startswith('name') or syntax.startswith('text'):
return 'TextHandler'
return ''
# Remove annotations like '(obsolete)', '(deprecated)' etc.
def remove_annotation(keyword):
parenthesis = keyword.find('(')
return keyword if parenthesis == -1 else keyword[:parenthesis].strip()
SPECIAL_CHARS = re.compile(r'[-/\.]')
def add_l10n(l10n_file, ipp_id, grit_id=None):
if not grit_id:
grit_id = ipp_id
l10n_file.write(' {"%s", IDS_PRINT_%s},\n' %
(ipp_id, SPECIAL_CHARS.sub('_', grit_id.upper())))
def main():
parser = argparse.ArgumentParser(usage=__doc__)
parser.add_argument(
'-a',
'--attributes-file',
dest='attributes_file',
help='path to ipp-registrations-2.csv input file',
metavar='FILE',
required=True)
parser.add_argument(
'-k',
'--keyword-values-file',
dest='keyword_values_file',
help='path to ipp-registrations-4.csv input file',
metavar='FILE',
required=True)
parser.add_argument(
'-e',
'--enum-values-file',
dest='enum_values_file',
help='path to ipp-registrations-6.csv input file',
metavar='FILE',
required=True)
parser.add_argument(
'-i',
'--ipp-handler-map',
dest='ipp_handler_map',
help='path to ipp_handler_map.cc output file',
metavar='FILE')
parser.add_argument(
'-l',
'--localization-map',
dest='localization_map',
help='path to ipp_l10n.cc output file',
metavar='FILE')
args = parser.parse_args()
if not (args.ipp_handler_map or args.localization_map):
parser.error('No output file selected')
handlers = []
supported_items = set()
with open(args.attributes_file, 'r') as attr_file:
attr_reader = csv.reader(attr_file)
for attr in attr_reader:
# Filter out by attribute group.
if attr[0] != 'Job Template':
continue
# Skip sub-attributes.
if attr[2] != '':
continue
attr_name = remove_annotation(attr[1])
# Skip duplicates
if attr_name in supported_items:
continue
if not KEYWORD_PATTERN.match(attr_name):
print('Warning: attribute name %s is invalid' % attr_name)
continue
syntax = attr[4]
handler = 'NoOpHandler'
if attr_name not in NOOP_ATTRS:
handler = get_handler(syntax.strip(), attr_name)
if handler == '':
continue
handlers.append((attr_name, handler))
if handler != 'NoOpHandler':
supported_items.add(attr_name)
if args.ipp_handler_map:
handler_file = open(args.ipp_handler_map, 'w')
handler_file.write(HANDLER_HEADER)
for (attr_name, handler) in handlers:
handler_file.write(' result.emplace("%s", base::BindRepeating(&%s));\n'
% (attr_name, handler))
handler_file.write(HANDLER_FOOTER)
handler_file.close()
if args.localization_map:
l10n_file = open(args.localization_map, 'w')
l10n_file.write(L10N_HEADER)
for (attr_name, handler) in handlers:
if handler != 'NoOpHandler':
if args.localization_map and not handler.startswith('Multivalue'):
add_l10n(l10n_file, attr_name)
# media-source and media-type are only handled for localization because
# they're inside of the media-col collection and we don't handle
# collections otherwise.
supported_items.add("media-source")
add_l10n(l10n_file, "media-source")
supported_items.add("media-type")
add_l10n(l10n_file, "media-type")
with open(args.keyword_values_file, 'r') as keyword_file:
keyword_reader = csv.reader(keyword_file)
for keyword_item in keyword_reader:
attr_name = keyword_item[0]
if attr_name in supported_items:
keyword_value = remove_annotation(keyword_item[1])
# TODO(crbug.com/964919): Also handle some plain English cases.
if KEYWORD_PATTERN.match(keyword_value):
l10n_key = '%s/%s' % (attr_name, keyword_value)
# Skip duplicates.
if l10n_key not in supported_items:
supported_items.add(l10n_key)
add_l10n(l10n_file, l10n_key)
with open(args.enum_values_file, 'r') as enum_file:
enum_reader = csv.reader(enum_file)
for enum_item in enum_reader:
attr_name = enum_item[0]
if attr_name in supported_items:
enum_value = enum_item[1]
try:
int(enum_value)
l10n_key = attr_name + '/' + enum_value
# Skip duplicates and finishings 'none' value.
if l10n_key not in supported_items and l10n_key != 'finishings/3':
supported_items.add(l10n_key)
add_l10n(l10n_file, l10n_key,
attr_name + '_' + remove_annotation(enum_item[2]))
except ValueError:
# TODO(crbug.com/964919): Handle some plain English cases.
pass
l10n_file.write(L10N_FOOTER)
l10n_file.close()
if __name__ == '__main__':
main()
|