misc/libfreetype/src/tools/docmaker/utils.py
author koda
Mon, 25 Apr 2011 01:46:54 +0200
changeset 5172 88f2e05288ba
permissions -rw-r--r--
aaand let's add freetype as well while we are at it other smaller changes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5172
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
     1
#  Utils (c) 2002, 2004, 2007, 2008  David Turner <david@freetype.org>
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
     2
#
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
     3
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
     4
import string, sys, os, glob
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
     5
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
     6
# current output directory
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
     7
#
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
     8
output_dir = None
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
     9
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    10
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    11
# This function is used to sort the index.  It is a simple lexicographical
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    12
# sort, except that it places capital letters before lowercase ones.
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    13
#
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    14
def  index_sort( s1, s2 ):
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    15
    if not s1:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    16
        return -1
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    17
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    18
    if not s2:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    19
        return 1
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    20
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    21
    l1 = len( s1 )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    22
    l2 = len( s2 )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    23
    m1 = string.lower( s1 )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    24
    m2 = string.lower( s2 )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    25
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    26
    for i in range( l1 ):
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    27
        if i >= l2 or m1[i] > m2[i]:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    28
            return 1
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    29
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    30
        if m1[i] < m2[i]:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    31
            return -1
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    32
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    33
        if s1[i] < s2[i]:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    34
            return -1
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    35
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    36
        if s1[i] > s2[i]:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    37
            return 1
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    38
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    39
    if l2 > l1:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    40
        return -1
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    41
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    42
    return 0
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    43
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    44
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    45
# Sort input_list, placing the elements of order_list in front.
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    46
#
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    47
def  sort_order_list( input_list, order_list ):
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    48
    new_list = order_list[:]
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    49
    for id in input_list:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    50
        if not id in order_list:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    51
            new_list.append( id )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    52
    return new_list
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    53
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    54
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    55
# Open the standard output to a given project documentation file.  Use
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    56
# "output_dir" to determine the filename location if necessary and save the
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    57
# old stdout in a tuple that is returned by this function.
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    58
#
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    59
def  open_output( filename ):
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    60
    global output_dir
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    61
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    62
    if output_dir and output_dir != "":
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    63
        filename = output_dir + os.sep + filename
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    64
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    65
    old_stdout = sys.stdout
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    66
    new_file   = open( filename, "w" )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    67
    sys.stdout = new_file
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    68
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    69
    return ( new_file, old_stdout )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    70
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    71
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    72
# Close the output that was returned by "close_output".
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    73
#
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    74
def  close_output( output ):
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    75
    output[0].close()
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    76
    sys.stdout = output[1]
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    77
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    78
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    79
# Check output directory.
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    80
#
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    81
def  check_output():
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    82
    global output_dir
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    83
    if output_dir:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    84
        if output_dir != "":
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    85
            if not os.path.isdir( output_dir ):
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    86
                sys.stderr.write( "argument" + " '" + output_dir + "' " + \
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    87
                                  "is not a valid directory" )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    88
                sys.exit( 2 )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    89
        else:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    90
            output_dir = None
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    91
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    92
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    93
def  file_exists( pathname ):
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    94
    """checks that a given file exists"""
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    95
    result = 1
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    96
    try:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    97
        file = open( pathname, "r" )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    98
        file.close()
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    99
    except:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   100
        result = None
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   101
        sys.stderr.write( pathname + " couldn't be accessed\n" )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   102
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   103
    return result
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   104
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   105
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   106
def  make_file_list( args = None ):
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   107
    """builds a list of input files from command-line arguments"""
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   108
    file_list = []
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   109
    # sys.stderr.write( repr( sys.argv[1 :] ) + '\n' )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   110
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   111
    if not args:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   112
        args = sys.argv[1 :]
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   113
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   114
    for pathname in args:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   115
        if string.find( pathname, '*' ) >= 0:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   116
            newpath = glob.glob( pathname )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   117
            newpath.sort()  # sort files -- this is important because
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   118
                            # of the order of files
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   119
        else:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   120
            newpath = [pathname]
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   121
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   122
        file_list.extend( newpath )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   123
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   124
    if len( file_list ) == 0:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   125
        file_list = None
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   126
    else:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   127
        # now filter the file list to remove non-existing ones
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   128
        file_list = filter( file_exists, file_list )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   129
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   130
    return file_list
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   131
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   132
# eof