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