linecach.py :  » Mobile » Python-for-PalmOS » Python-1.5.2+reduced-1.0 » Lib » dos-8x3 » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Mobile » Python for PalmOS 
Python for PalmOS » Python 1.5.2 reduced 1.0 » Lib » dos 8x3 » linecach.py
# Cache lines from files.
# This is intended to read lines from modules imported -- hence if a filename
# is not found, it will look down the module search path for a file by
# that name.

import sys
import os
from stat import *

def getline(filename, lineno):
  lines = getlines(filename)
  if 1 <= lineno <= len(lines):
    return lines[lineno-1]
  else:
    return ''


# The cache

cache = {} # The cache


# Clear the cache entirely

def clearcache():
  global cache
  cache = {}


# Get the lines for a file from the cache.
# Update the cache if it doesn't contain an entry for this file already.

def getlines(filename):
  if cache.has_key(filename):
    return cache[filename][2]
  else:
    return updatecache(filename)


# Discard cache entries that are out of date.
# (This is not checked upon each call!)

def checkcache():
  for filename in cache.keys():
    size, mtime, lines, fullname = cache[filename]
    try:
      stat = os.stat(fullname)
    except os.error:
      del cache[filename]
      continue
    if size <> stat[ST_SIZE] or mtime <> stat[ST_MTIME]:
      del cache[filename]


# Update a cache entry and return its list of lines.
# If something's wrong, print a message, discard the cache entry,
# and return an empty list.

def updatecache(filename):
  if cache.has_key(filename):
    del cache[filename]
  if not filename or filename[0] + filename[-1] == '<>':
    return []
  fullname = filename
  try:
    stat = os.stat(fullname)
  except os.error, msg:
    # Try looking through the module search path
    basename = os.path.split(filename)[1]
    for dirname in sys.path:
      fullname = os.path.join(dirname, basename)
      try:
        stat = os.stat(fullname)
        break
      except os.error:
        pass
    else:
      # No luck
##      print '*** Cannot stat', filename, ':', msg
      return []
  try:
    fp = open(fullname, 'r')
    lines = fp.readlines()
    fp.close()
  except IOError, msg:
##    print '*** Cannot open', fullname, ':', msg
    return []
  size, mtime = stat[ST_SIZE], stat[ST_MTIME]
  cache[filename] = size, mtime, lines, fullname
  return lines
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.