1 | # This program is public domain |
---|
2 | """ |
---|
3 | File extension registry. |
---|
4 | |
---|
5 | This provides routines for opening files based on extension, |
---|
6 | and registers the built-in file extensions. |
---|
7 | """ |
---|
8 | import logging |
---|
9 | import os.path |
---|
10 | |
---|
11 | class ExtensionRegistry(object): |
---|
12 | """ |
---|
13 | Associate a file loader with an extension. |
---|
14 | |
---|
15 | Note that there may be multiple loaders for the same extension. |
---|
16 | |
---|
17 | Example: |
---|
18 | |
---|
19 | registry = ExtensionRegistry() |
---|
20 | |
---|
21 | # Add an association by setting an element |
---|
22 | registry['.zip'] = unzip |
---|
23 | |
---|
24 | # Multiple extensions for one loader |
---|
25 | registry['.tgz'] = untar |
---|
26 | registry['.tar.gz'] = untar |
---|
27 | |
---|
28 | # Multiple loaders for one extension |
---|
29 | registry['.cx'] = cx1 |
---|
30 | registry['.cx'] = cx2 |
---|
31 | registry['.cx'] = cx3 |
---|
32 | |
---|
33 | # Can also register a format name for explicit control from caller |
---|
34 | registry['cx3'] = cx3 |
---|
35 | |
---|
36 | # Retrieve loaders for a file name |
---|
37 | registry.lookup('hello.cx') -> [cx3,cx2,cx1] |
---|
38 | |
---|
39 | # Run loader on a filename |
---|
40 | registry.load('hello.cx') -> |
---|
41 | try: |
---|
42 | return cx3('hello.cx') |
---|
43 | except: |
---|
44 | try: |
---|
45 | return cx2('hello.cx') |
---|
46 | except: |
---|
47 | return cx1('hello.cx') |
---|
48 | |
---|
49 | # Load in a specific format ignoring extension |
---|
50 | registry.load('hello.cx',format='cx3') -> |
---|
51 | return cx3('hello.cx') |
---|
52 | """ |
---|
53 | def __init__(self): |
---|
54 | self.loaders = {} |
---|
55 | def __setitem__(self, ext, loader): |
---|
56 | if ext not in self.loaders: |
---|
57 | self.loaders[ext] = [] |
---|
58 | self.loaders[ext].insert(0,loader) |
---|
59 | def __getitem__(self, ext): |
---|
60 | return self.loaders[ext] |
---|
61 | def __contains__(self, ext): |
---|
62 | return ext in self.loaders |
---|
63 | def formats(self, name=True, ext=False): |
---|
64 | """ |
---|
65 | Return a list of the registered formats. If name=True then |
---|
66 | named formats are returned. If ext=True then extensions |
---|
67 | are returned. |
---|
68 | """ |
---|
69 | names = [a for a in self.loaders.keys() if not a.startswith('.')] |
---|
70 | exts = [a for a in self.loaders.keys() if a.startswith('.')] |
---|
71 | names.sort() |
---|
72 | exts.sort() |
---|
73 | ret = [] |
---|
74 | if name: ret += names |
---|
75 | if ext: ret += exts |
---|
76 | return ret |
---|
77 | |
---|
78 | def lookup(self, path): |
---|
79 | """ |
---|
80 | Return the loader associated with the file type of path. |
---|
81 | """ |
---|
82 | file = os.path.basename(path) |
---|
83 | idx = file.find('.') |
---|
84 | ext = file[idx:] if idx >= 0 else '' |
---|
85 | try: |
---|
86 | return self.loaders[ext] |
---|
87 | except: |
---|
88 | #raise ValueError, "Unknown file type '%s'"%ext |
---|
89 | print "Unknown file type '%s'"%ext |
---|
90 | |
---|
91 | def loads(self, path, format=None): |
---|
92 | """ |
---|
93 | Call the loader for the file type of path. |
---|
94 | |
---|
95 | Raises ValueError if no loader is available. |
---|
96 | May raise a loader-defined exception if loader fails. |
---|
97 | """ |
---|
98 | if format is None: |
---|
99 | loaders = self.lookup(path) |
---|
100 | else: |
---|
101 | loaders = self.loaders[format] |
---|
102 | for fn in loaders: |
---|
103 | print fn |
---|
104 | try: |
---|
105 | value=fn.read(path) |
---|
106 | return value |
---|
107 | except ValueError,msg: |
---|
108 | #pass |
---|
109 | print str(msg) |
---|
110 | |
---|
111 | def load(self, path, format=None): |
---|
112 | """ |
---|
113 | Call the loader for the file type of path. |
---|
114 | |
---|
115 | Raises ValueError if no loader is available. |
---|
116 | May raise a loader-defined exception if loader fails. |
---|
117 | """ |
---|
118 | if format is None: |
---|
119 | loaders = self.lookup(path) |
---|
120 | else: |
---|
121 | loaders = self.loaders[format] |
---|
122 | if loaders!=None: |
---|
123 | for fn in loaders: |
---|
124 | try: |
---|
125 | return fn.read(path) |
---|
126 | #value=fn.read(path) |
---|
127 | #return value |
---|
128 | except ValueError,msg: |
---|
129 | #pass |
---|
130 | print str(msg) |
---|