You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

134 lines
3.8 KiB

35 years ago
35 years ago
35 years ago
35 years ago
35 years ago
35 years ago
35 years ago
35 years ago
35 years ago
35 years ago
  1. """Cache lines from files.
  2. This is intended to read lines from modules imported -- hence if a filename
  3. is not found, it will look down the module search path for a file by
  4. that name.
  5. """
  6. import sys
  7. import os
  8. import tokenize
  9. __all__ = ["getline", "clearcache", "checkcache"]
  10. def getline(filename, lineno, module_globals=None):
  11. lines = getlines(filename, module_globals)
  12. if 1 <= lineno <= len(lines):
  13. return lines[lineno-1]
  14. else:
  15. return ''
  16. # The cache
  17. cache = {} # The cache
  18. def clearcache():
  19. """Clear the cache entirely."""
  20. global cache
  21. cache = {}
  22. def getlines(filename, module_globals=None):
  23. """Get the lines for a file from the cache.
  24. Update the cache if it doesn't contain an entry for this file already."""
  25. if filename in cache:
  26. return cache[filename][2]
  27. else:
  28. return updatecache(filename, module_globals)
  29. def checkcache(filename=None):
  30. """Discard cache entries that are out of date.
  31. (This is not checked upon each call!)"""
  32. if filename is None:
  33. filenames = list(cache.keys())
  34. else:
  35. if filename in cache:
  36. filenames = [filename]
  37. else:
  38. return
  39. for filename in filenames:
  40. size, mtime, lines, fullname = cache[filename]
  41. if mtime is None:
  42. continue # no-op for files loaded via a __loader__
  43. try:
  44. stat = os.stat(fullname)
  45. except os.error:
  46. del cache[filename]
  47. continue
  48. if size != stat.st_size or mtime != stat.st_mtime:
  49. del cache[filename]
  50. def updatecache(filename, module_globals=None):
  51. """Update a cache entry and return its list of lines.
  52. If something's wrong, print a message, discard the cache entry,
  53. and return an empty list."""
  54. if filename in cache:
  55. del cache[filename]
  56. if not filename or (filename.startswith('<') and filename.endswith('>')):
  57. return []
  58. fullname = filename
  59. try:
  60. stat = os.stat(fullname)
  61. except OSError:
  62. basename = filename
  63. # Try for a __loader__, if available
  64. if module_globals and '__loader__' in module_globals:
  65. name = module_globals.get('__name__')
  66. loader = module_globals['__loader__']
  67. get_source = getattr(loader, 'get_source', None)
  68. if name and get_source:
  69. try:
  70. data = get_source(name)
  71. except (ImportError, IOError):
  72. pass
  73. else:
  74. if data is None:
  75. # No luck, the PEP302 loader cannot find the source
  76. # for this module.
  77. return []
  78. cache[filename] = (
  79. len(data), None,
  80. [line+'\n' for line in data.splitlines()], fullname
  81. )
  82. return cache[filename][2]
  83. # Try looking through the module search path, which is only useful
  84. # when handling a relative filename.
  85. if os.path.isabs(filename):
  86. return []
  87. for dirname in sys.path:
  88. try:
  89. fullname = os.path.join(dirname, basename)
  90. except (TypeError, AttributeError):
  91. # Not sufficiently string-like to do anything useful with.
  92. continue
  93. try:
  94. stat = os.stat(fullname)
  95. break
  96. except os.error:
  97. pass
  98. else:
  99. return []
  100. try:
  101. with tokenize.open(fullname) as fp:
  102. lines = fp.readlines()
  103. except IOError:
  104. return []
  105. if lines and not lines[-1].endswith('\n'):
  106. lines[-1] += '\n'
  107. size, mtime = stat.st_size, stat.st_mtime
  108. cache[filename] = size, mtime, lines, fullname
  109. return lines