source: trunk/poppler/freetype-2.1.10/src/tools/docmaker/utils.py @ 2

Last change on this file since 2 was 2, checked in by Eugene Romanenko, 15 years ago

First import

File size: 2.9 KB
RevLine 
[2]1import string, sys, os, glob
2
3# current output directory
4#
5output_dir = None
6
7
8# This function is used to sort the index.  It is a simple lexicographical
9# sort, except that it places capital letters before lowercase ones.
10#
11def index_sort( s1, s2 ):
12    if not s1:
13        return -1
14
15    if not s2:
16        return 1
17
18    l1 = len( s1 )
19    l2 = len( s2 )
20    m1 = string.lower( s1 )
21    m2 = string.lower( s2 )
22
23    for i in range( l1 ):
24        if i >= l2 or m1[i] > m2[i]:
25            return 1
26
27        if m1[i] < m2[i]:
28            return -1
29
30        if s1[i] < s2[i]:
31            return -1
32
33        if s1[i] > s2[i]:
34            return 1
35
36    if l2 > l1:
37        return -1
38
39    return 0
40
41# Sort input_list, placing the elements of order_list in front.
42#
43def sort_order_list( input_list, order_list ):
44    new_list = order_list[:]
45    for id in input_list:
46        if not id in order_list:
47            new_list.append( id )
48    return new_list
49
50
51
52# Open the standard output to a given project documentation file.  Use
53# "output_dir" to determine the filename location if necessary and save the
54# old stdout in a tuple that is returned by this function.
55#
56def open_output( filename ):
57    global output_dir
58
59    if output_dir and output_dir != "":
60        filename = output_dir + os.sep + filename
61
62    old_stdout = sys.stdout
63    new_file   = open( filename, "w" )
64    sys.stdout = new_file
65
66    return ( new_file, old_stdout )
67
68
69# Close the output that was returned by "close_output".
70#
71def close_output( output ):
72    output[0].close()
73    sys.stdout = output[1]
74
75
76# Check output directory.
77#
78def check_output( ):
79    global output_dir
80    if output_dir:
81        if output_dir != "":
82            if not os.path.isdir( output_dir ):
83                sys.stderr.write( "argument" + " '" + output_dir + "' " +
84                                  "is not a valid directory" )
85                sys.exit( 2 )
86        else:
87            output_dir = None
88
89def file_exists( pathname ):
90    """checks that a given file exists"""
91    result = 1
92    try:
93        file = open( pathname, "r" )
94        file.close()
95    except:
96        result = None
97        sys.stderr.write( pathname + " couldn't be accessed\n" )
98
99    return result
100
101
102def make_file_list( args = None ):
103    """builds a list of input files from command-line arguments"""
104
105    file_list = []
106    # sys.stderr.write( repr( sys.argv[1 :] ) + '\n' )
107
108    if not args:
109        args = sys.argv[1 :]
110
111    for pathname in args:
112        if string.find( pathname, '*' ) >= 0:
113            newpath = glob.glob( pathname )
114            newpath.sort()  # sort files -- this is important because
115                            # of the order of files
116        else:
117            newpath = [pathname]
118           
119        file_list.extend( newpath )
120
121    if len( file_list ) == 0:
122        file_list = None
123    else:
124        # now filter the file list to remove non-existing ones
125        file_list = filter( file_exists, file_list )
126   
127    return file_list
128
Note: See TracBrowser for help on using the repository browser.