File: PathVFS.py

package info (click to toggle)
jython 2.5.1-2
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 41,624 kB
  • ctags: 101,579
  • sloc: python: 351,444; java: 204,338; xml: 1,316; sh: 330; ansic: 126; perl: 114; makefile: 94
file content (90 lines) | stat: -rw-r--r-- 2,493 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# Copyright 2000 Samuele Pedroni
import sys

from java import io
from java.util import zip


class JarVFS:
    def __init__(self,fname):
        self.zipfile = zip.ZipFile(fname)

    def open(self,id):
        ent = self.zipfile.getEntry(id)
        if ent:
##            print "path-jar-open: %s!%s" % (self.zipfile.name,id) # ?? dbg
            return self.zipfile.getInputStream(ent)
        else:
            return None

    def __repr__(self):
        return "<jar-vfs '%s'>" % self.zipfile.name
        
class DirVFS:
    def __init__(self,dir):
        if dir == '':
            self.pfx = None
        else:
            self.pfx = dir

    def open(self,id):
        f = io.File(self.pfx, id.replace('/',io.File.separator))
        if f.file:
##            print "path-open:",f # ?? dbg
            return io.BufferedInputStream(io.FileInputStream(f))
        return None

    def __repr__(self):
        return "<dir-vfs '%s'>" % self.pfx

class PathVFS:

    def add_vfs(self,fname):
        if fname == '':
            if not self.once.has_key(''):
                self.once['']=1
                self.vfs.append(DirVFS(''));
            return        
        file=io.File(fname);
        canon = file.canonicalPath
        if not self.once.has_key(canon):
            self.once[canon]=1
            try:
                if file.directory:
                    self.vfs.append(DirVFS(fname));
                else:
                    if file.exists and (fname.endswith('.jar') or fname.endswith('.zip')):
                        self.vfs.append(JarVFS(fname))
            except:
                pass
        

    def __init__(self,registry):
        self.once = {}
        self.vfs = []
        paths = registry.getProperty("python.packages.paths","java.class.path")
        paths = paths.split(',')
        # opt
        if "sun.boot.class.path" in paths: # ??pending strip boot class paths of other jvms?
            paths.remove("sun.boot.class.path")

        #paths.append("python.packages.fakepath")

        for p in paths:
            e = registry.getProperty(p)
            if e != None:
                path = e.split(io.File.pathSeparator)
                for name in path:
                    self.add_vfs(name)

        for name in sys.path:
            self.add_vfs(name)
            
        del self.once

    def open(self,id):
        for v in self.vfs:
            stream = v.open(id)
            if stream:
                return stream
        return None