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
)
62 def GetTableFiles(tables_file
, directories
, extra_files
):
63 tables
= LoadTablesFile(tables_file
)
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
)
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
)
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
)
94 print DoMain(argv
[1:])
97 if __name__
== '__main__':
99 sys
.exit(main(sys
.argv
))
100 except KeyboardInterrupt: