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.
13 # Matches the include statement in the braille table files.
14 INCLUDE_RE
= re
.compile(r
"^\s*include\s+([^#\s]+)")
18 print >> sys
.stderr
, 'liblouis_list_tables: %s' % msg
22 def ToNativePath(pathname
):
23 return os
.path
.sep
.join(pathname
.split('/'))
26 def LoadTablesFile(filename
):
27 with
open(ToNativePath(filename
), 'r') as fh
:
31 def FindFile(filename
, directories
):
33 fullname
= '/'.join([d
, filename
])
34 if os
.path
.isfile(ToNativePath(fullname
)):
36 Error('File not found: %s' % filename
)
39 def GetIncludeFiles(filename
):
41 with
open(ToNativePath(filename
), 'r') as fh
:
42 for line
in fh
.xreadlines():
43 match
= INCLUDE_RE
.match(line
)
45 result
.append(match
.group(1))
49 def ProcessFile(output_set
, filename
, directories
):
50 fullname
= FindFile(filename
, directories
)
51 if fullname
in output_set
:
53 output_set
.add(fullname
)
54 for include_file
in GetIncludeFiles(fullname
):
55 ProcessFile(output_set
, include_file
, directories
)
59 "Entry point for gyp's pymod_do_main command."
60 parser
= optparse
.OptionParser()
61 # Give a clearer error message when this is used as a module.
62 parser
.prog
= 'liblouis_list_tables'
63 parser
.set_usage('usage: %prog [options] listfile')
64 parser
.add_option('-D', '--directory', dest
='directories',
65 action
='append', help='Where to search for table files')
66 (options
, args
) = parser
.parse_args(argv
)
69 parser
.error('Expecting exactly one argument')
70 if not options
.directories
:
71 parser
.error('At least one --directory option must be specified')
73 tables
= LoadTablesFile(args
[0])
76 ProcessFile(output_set
, table
['fileName'], options
.directories
)
77 return '\n'.join(output_set
)
81 print DoMain(argv
[1:])
84 if __name__
== '__main__':
86 sys
.exit(main(sys
.argv
))
87 except KeyboardInterrupt: