Clean up extension confirmation prompts and make them consistent between Views and...
[chromium-blink-merge.git] / third_party / liblouis / liblouis_list_tables.py
blob9cd3fb8b9ac8355fd387d19a823eceee27ab9309
1 #!/usr/bin/env python
2 # Copyright 2013 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
6 import os
7 import re
8 import sys
10 import json
11 import optparse
13 # Matches the include statement in the braille table files.
14 INCLUDE_RE = re.compile(r"^\s*include\s+([^#\s]+)")
17 def Error(msg):
18 print >> sys.stderr, 'liblouis_list_tables: %s' % msg
19 sys.exit(1)
22 def ToNativePath(pathname):
23 return os.path.sep.join(pathname.split('/'))
26 def LoadTablesFile(filename):
27 with open(ToNativePath(filename), 'r') as fh:
28 try:
29 return json.load(fh)
30 except ValueError, e:
31 raise ValueError('Error parsing braille table file %s: %s' %
32 (filename, e.message))
35 def FindFile(filename, directories):
36 for d in directories:
37 fullname = '/'.join([d, filename])
38 if os.path.isfile(ToNativePath(fullname)):
39 return fullname
40 Error('File not found: %s' % filename)
43 def GetIncludeFiles(filename):
44 result = []
45 with open(ToNativePath(filename), 'r') as fh:
46 for line in fh.xreadlines():
47 match = INCLUDE_RE.match(line)
48 if match:
49 result.append(match.group(1))
50 return result
53 def ProcessFile(output_set, filename, directories):
54 fullname = FindFile(filename, directories)
55 if fullname in output_set:
56 return
57 output_set.add(fullname)
58 for include_file in GetIncludeFiles(fullname):
59 ProcessFile(output_set, include_file, directories)
62 def DoMain(argv):
63 "Entry point for gyp's pymod_do_main command."
64 parser = optparse.OptionParser()
65 # Give a clearer error message when this is used as a module.
66 parser.prog = 'liblouis_list_tables'
67 parser.set_usage('usage: %prog [options] listfile')
68 parser.add_option('-D', '--directory', dest='directories',
69 action='append', help='Where to search for table files')
70 parser.add_option('-e', '--extra_file', dest='extra_files', action='append',
71 default=[], help='Extra liblouis table file to process')
72 (options, args) = parser.parse_args(argv)
74 if len(args) != 1:
75 parser.error('Expecting exactly one argument')
76 if not options.directories:
77 parser.error('At least one --directory option must be specified')
79 tables = LoadTablesFile(args[0])
80 output_set = set()
81 for table in tables:
82 for name in table['fileNames'].split(','):
83 ProcessFile(output_set, name, options.directories)
84 for name in options.extra_files:
85 ProcessFile(output_set, name, options.directories)
86 return '\n'.join(output_set)
89 def main(argv):
90 print DoMain(argv[1:])
93 if __name__ == '__main__':
94 try:
95 sys.exit(main(sys.argv))
96 except KeyboardInterrupt:
97 Error('interrupted')