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 raise ValueError('Error parsing braille table file %s: %s' %
32 (filename
, e
.message
))
35 def FindFile(filename
, directories
):
37 fullname
= '/'.join([d
, filename
])
38 if os
.path
.isfile(ToNativePath(fullname
)):
40 Error('File not found: %s' % filename
)
43 def GetIncludeFiles(filename
):
45 with
open(ToNativePath(filename
), 'r') as fh
:
46 for line
in fh
.xreadlines():
47 match
= INCLUDE_RE
.match(line
)
49 result
.append(match
.group(1))
53 def ProcessFile(output_set
, filename
, directories
):
54 fullname
= FindFile(filename
, directories
)
55 if fullname
in output_set
:
57 output_set
.add(fullname
)
58 for include_file
in GetIncludeFiles(fullname
):
59 ProcessFile(output_set
, include_file
, directories
)
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
)
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])
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
)
90 print DoMain(argv
[1:])
93 if __name__
== '__main__':
95 sys
.exit(main(sys
.argv
))
96 except KeyboardInterrupt: