4 # Auxiliary functions for the `docmaker' tool (library file).
6 # Copyright 2002, 2004, 2007, 2008, 2014 by
9 # This file is part of the FreeType project, and may only be used,
10 # modified, and distributed under the terms of the FreeType project
11 # license, LICENSE.TXT. By continuing to use, modify, or distribute
12 # this file you indicate that you have read the license and
13 # understand and accept it fully.
16 import string, sys, os, glob, itertools
19 # current output directory
24 # A function that generates a sorting key. We want lexicographical order
25 # (primary key) except that capital letters are sorted before lowercase
26 # ones (secondary key).
28 # The primary key is implemented by lowercasing the input. The secondary
29 # key is simply the original data appended, character by character. For
30 # example, the sort key for `FT_x' is `fFtT__xx', while the sort key for
31 # `ft_X' is `fftt__xX'. Since ASCII codes of uppercase letters are
32 # numerically smaller than the codes of lowercase letters, `fFtT__xx' gets
33 # sorted before `fftt__xX'.
36 return string.join( itertools.chain( *zip( s.lower(), s ) ) )
39 # Sort `input_list', placing the elements of `order_list' in front.
41 def sort_order_list( input_list, order_list ):
42 new_list = order_list[:]
44 if not id in order_list:
49 # Divert standard output to a given project documentation file. Use
50 # `output_dir' to determine the filename location if necessary and save the
51 # old stdout handle in a tuple that is returned by this function.
53 def open_output( filename ):
56 if output_dir and output_dir != "":
57 filename = output_dir + os.sep + filename
59 old_stdout = sys.stdout
60 new_file = open( filename, "w" )
63 return ( new_file, old_stdout )
66 # Close the output that was returned by `open_output'.
68 def close_output( output ):
70 sys.stdout = output[1]
73 # Check output directory.
79 if not os.path.isdir( output_dir ):
80 sys.stderr.write( "argument"
81 + " '" + output_dir + "' "
82 + "is not a valid directory" )
88 def file_exists( pathname ):
89 """Check that a given file exists."""
92 file = open( pathname, "r" )
96 sys.stderr.write( pathname + " couldn't be accessed\n" )
101 def make_file_list( args = None ):
102 """Build a list of input files from command-line arguments."""
104 # sys.stderr.write( repr( sys.argv[1 :] ) + '\n' )
109 for pathname in args:
110 if string.find( pathname, '*' ) >= 0:
111 newpath = glob.glob( pathname )
112 newpath.sort() # sort files -- this is important because
113 # of the order of files
117 file_list.extend( newpath )
119 if len( file_list ) == 0:
122 # now filter the file list to remove non-existing ones
123 file_list = filter( file_exists, file_list )