itags.py :  » Mobile » Python-for-PalmOS » Python-1.5.2+reduced-1.0 » Demo » ibrowse » 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 » Demo » ibrowse » itags.py
# Utility module for 'icache.py': interpret tag tables and indirect nodes.

# (This module is a bit chatty when confronted with the unexpected.)


import regexp
import string
import ifile


# Get the tag table of an open file, as a dictionary.
# Seeks around in the file; after reading, the position is undefined.
# Return an empty tag table if none is found.
#
def get_tags(f):
  #
  # First see if the last "node" is the end of tag table marker.
  #
  f.seek(0, 2) # Seek to EOF
  end = f.tell()
  buf = ifile.backup_node(f, end)
  if not labelmatch(buf, 0, 'end tag table\n'):
    return {} # No succes
  #
  # Next backup to the previous "node" -- the tag table itself.
  #
  ###print 'Getting prebuilt tag table...'
  end = f.tell() - len(buf)
  buf = ifile.backup_node(f, end)
  label = 'tag table:\n'
  if not labelmatch(buf, 0, label):
    print 'Weird: end tag table marker but no tag table?'
    print 'Node begins:', `buf[:50]`
    return {}
  #
  # Now read the whole tag table.
  #
  end = f.tell() - len(buf) # Do this first!
  buf = ifile.read_node(f, buf)
  #
  # First check for an indirection table.
  #
  indirlist = []
  if labelmatch(buf, len(label), '(indirect)\n'):
    indirbuf = ifile.backup_node(f, end)
    if not labelmatch(indirbuf, 0, 'indirect:\n'):
      print 'Weird: promised indirection table not found'
      print 'Node begins:', `indirbuf[:50]`
      # Carry on.  Things probably won't work though.
    else:
      indirbuf = ifile.read_node(f, indirbuf)
      indirlist = parse_indirlist(indirbuf)
  #
  # Now parse the tag table.
  #
  findtag = regexp.compile('^(.*[nN]ode:[ \t]*(.*))\177([0-9]+)$').match
  i = 0
  tags = {}
  while 1:
    match = findtag(buf, i)
    if not match:
      break
    (a,b), (a1,b1), (a2,b2), (a3,b3) = match
    i = b
    line = buf[a1:b1]
    node = string.lower(buf[a2:b2])
    offset = eval(buf[a3:b3]) # XXX What if it overflows?
    if tags.has_key(node):
      print 'Duplicate key in tag table:', `node`
    file, offset = map_offset(offset, indirlist)
    tags[node] = file, offset, line
  #
  return tags


# Return true if buf[i:] begins with a label, after lower case conversion.
# The label argument must be in lower case.
#
def labelmatch(buf, i, label):
  return string.lower(buf[i:i+len(label)]) == label


# Parse the indirection list.
# Return a list of (filename, offset) pairs ready for use.
#
def parse_indirlist(buf):
  list = []
  findindir = regexp.compile('^(.+):[ \t]*([0-9]+)$').match
  i = 0
  while 1:
    match = findindir(buf, i)
    if not match:
      break
    (a,b), (a1,b1), (a2,b2) = match
    file = buf[a1:b1]
    offset = eval(buf[a2:b2]) # XXX What if this gets overflow?
    list.append((file, offset))
    i = b
  return list


# Map an offset through the indirection list.
# Return (filename, new_offset).
# If the list is empty, return the given offset and an empty file name.
#
def map_offset(offset, indirlist):
  if not indirlist:
    return '', offset
  #
  # XXX This could be done more elegant.
  #
  filex, offx = indirlist[0]
  for i in range(len(indirlist)):
    file1, off1 = indirlist[i]
    if i+1 >= len(indirlist):
      file2, off2 = '', 0x7fffffff
    else:
      file2, off2 = indirlist[i+1]
    if off1 <= offset < off2:
      # Add offx+2 to compensate for extra header.
      # No idea whether this is always correct.
      return file1, offset-off1 + offx+2
  #
  # XXX Shouldn't get here.
  #
  print 'Oops, map_offset fell through'
  return '', offset # Not likely to get good results
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.