PageRenderTime 23ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/rtt-1.1.0b/rt-thread_os/components/external/freetype/src/tools/docmaker/utils.py

https://bitbucket.org/darcyg/shenzhou-iv-stm32f107-rt-thread
Python | 132 lines | 102 code | 17 blank | 13 comment | 13 complexity | 710f0bf63c961669ce5e0d04bf04fb55 MD5 | raw file
Possible License(s): GPL-2.0
  1. # Utils (c) 2002, 2004, 2007, 2008 David Turner <david@freetype.org>
  2. #
  3. import string, sys, os, glob
  4. # current output directory
  5. #
  6. output_dir = None
  7. # This function is used to sort the index. It is a simple lexicographical
  8. # sort, except that it places capital letters before lowercase ones.
  9. #
  10. def index_sort( s1, s2 ):
  11. if not s1:
  12. return -1
  13. if not s2:
  14. return 1
  15. l1 = len( s1 )
  16. l2 = len( s2 )
  17. m1 = string.lower( s1 )
  18. m2 = string.lower( s2 )
  19. for i in range( l1 ):
  20. if i >= l2 or m1[i] > m2[i]:
  21. return 1
  22. if m1[i] < m2[i]:
  23. return -1
  24. if s1[i] < s2[i]:
  25. return -1
  26. if s1[i] > s2[i]:
  27. return 1
  28. if l2 > l1:
  29. return -1
  30. return 0
  31. # Sort input_list, placing the elements of order_list in front.
  32. #
  33. def sort_order_list( input_list, order_list ):
  34. new_list = order_list[:]
  35. for id in input_list:
  36. if not id in order_list:
  37. new_list.append( id )
  38. return new_list
  39. # Open the standard output to a given project documentation file. Use
  40. # "output_dir" to determine the filename location if necessary and save the
  41. # old stdout in a tuple that is returned by this function.
  42. #
  43. def open_output( filename ):
  44. global output_dir
  45. if output_dir and output_dir != "":
  46. filename = output_dir + os.sep + filename
  47. old_stdout = sys.stdout
  48. new_file = open( filename, "w" )
  49. sys.stdout = new_file
  50. return ( new_file, old_stdout )
  51. # Close the output that was returned by "close_output".
  52. #
  53. def close_output( output ):
  54. output[0].close()
  55. sys.stdout = output[1]
  56. # Check output directory.
  57. #
  58. def check_output():
  59. global output_dir
  60. if output_dir:
  61. if output_dir != "":
  62. if not os.path.isdir( output_dir ):
  63. sys.stderr.write( "argument" + " '" + output_dir + "' " + \
  64. "is not a valid directory" )
  65. sys.exit( 2 )
  66. else:
  67. output_dir = None
  68. def file_exists( pathname ):
  69. """checks that a given file exists"""
  70. result = 1
  71. try:
  72. file = open( pathname, "r" )
  73. file.close()
  74. except:
  75. result = None
  76. sys.stderr.write( pathname + " couldn't be accessed\n" )
  77. return result
  78. def make_file_list( args = None ):
  79. """builds a list of input files from command-line arguments"""
  80. file_list = []
  81. # sys.stderr.write( repr( sys.argv[1 :] ) + '\n' )
  82. if not args:
  83. args = sys.argv[1 :]
  84. for pathname in args:
  85. if string.find( pathname, '*' ) >= 0:
  86. newpath = glob.glob( pathname )
  87. newpath.sort() # sort files -- this is important because
  88. # of the order of files
  89. else:
  90. newpath = [pathname]
  91. file_list.extend( newpath )
  92. if len( file_list ) == 0:
  93. file_list = None
  94. else:
  95. # now filter the file list to remove non-existing ones
  96. file_list = filter( file_exists, file_list )
  97. return file_list
  98. # eof