Roll src/third_party/WebKit 3c1b001:57aef96 (svn 201977:201978)
[chromium-blink-merge.git] / third_party / liblouis / liblouis_list_tables.py
blob62834003fed0d89e2dc3915468d0c88a1b269527
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 GetTableFiles(tables_file, directories, extra_files):
63 tables = LoadTablesFile(tables_file)
64 output_set = set()
65 for table in tables:
66 for name in table['fileNames'].split(','):
67 ProcessFile(output_set, name, directories)
68 for name in extra_files:
69 ProcessFile(output_set, name, directories)
70 return output_set
73 def DoMain(argv):
74 "Entry point for gyp's pymod_do_main command."
75 parser = optparse.OptionParser()
76 # Give a clearer error message when this is used as a module.
77 parser.prog = 'liblouis_list_tables'
78 parser.set_usage('usage: %prog [options] listfile')
79 parser.add_option('-D', '--directory', dest='directories',
80 action='append', help='Where to search for table files')
81 parser.add_option('-e', '--extra_file', dest='extra_files', action='append',
82 default=[], help='Extra liblouis table file to process')
83 (options, args) = parser.parse_args(argv)
85 if len(args) != 1:
86 parser.error('Expecting exactly one argument')
87 if not options.directories:
88 parser.error('At least one --directory option must be specified')
89 files = GetTableFiles(args[0], options.directories, options.extra_files)
90 return '\n'.join(files)
93 def main(argv):
94 print DoMain(argv[1:])
97 if __name__ == '__main__':
98 try:
99 sys.exit(main(sys.argv))
100 except KeyboardInterrupt:
101 Error('interrupted')