1# 2# utils.py 3# 4# Auxiliary functions for the `docmaker' tool (library file). 5# 6# Copyright 2002-2018 by 7# David Turner. 8# 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. 14 15 16import string, sys, os, glob, itertools 17 18 19# current output directory 20# 21output_dir = None 22 23 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). 27# 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'. 34# 35def index_key( s ): 36 return string.join( itertools.chain( *zip( s.lower(), s ) ) ) 37 38 39# Sort `input_list', placing the elements of `order_list' in front. 40# 41def sort_order_list( input_list, order_list ): 42 new_list = order_list[:] 43 for id in input_list: 44 if not id in order_list: 45 new_list.append( id ) 46 return new_list 47 48 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. 52# 53def open_output( filename ): 54 global output_dir 55 56 if output_dir and output_dir != "": 57 filename = output_dir + os.sep + filename 58 59 old_stdout = sys.stdout 60 new_file = open( filename, "w" ) 61 sys.stdout = new_file 62 63 return ( new_file, old_stdout ) 64 65 66# Close the output that was returned by `open_output'. 67# 68def close_output( output ): 69 output[0].close() 70 sys.stdout = output[1] 71 72 73# Check output directory. 74# 75def check_output(): 76 global output_dir 77 if output_dir: 78 if output_dir != "": 79 if not os.path.isdir( output_dir ): 80 sys.stderr.write( "argument" 81 + " '" + output_dir + "' " 82 + "is not a valid directory\n" ) 83 sys.exit( 2 ) 84 else: 85 output_dir = None 86 87 88def file_exists( pathname ): 89 """Check that a given file exists.""" 90 result = 1 91 try: 92 file = open( pathname, "r" ) 93 file.close() 94 except: 95 result = None 96 sys.stderr.write( pathname + " couldn't be accessed\n" ) 97 98 return result 99 100 101def make_file_list( args = None ): 102 """Build a list of input files from command-line arguments.""" 103 file_list = [] 104 # sys.stderr.write( repr( sys.argv[1 :] ) + '\n' ) 105 106 if not args: 107 args = sys.argv[1:] 108 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 114 else: 115 newpath = [pathname] 116 117 file_list.extend( newpath ) 118 119 if len( file_list ) == 0: 120 file_list = None 121 else: 122 # now filter the file list to remove non-existing ones 123 file_list = filter( file_exists, file_list ) 124 125 return file_list 126 127# eof 128