[4bae1ef] | 1 | """ |
---|
| 2 | File extension registry. |
---|
| 3 | |
---|
| 4 | This provides routines for opening files based on extension, |
---|
| 5 | and registers the built-in file extensions. |
---|
| 6 | """ |
---|
[a1b8fee] | 7 | from __future__ import print_function |
---|
[4bae1ef] | 8 | |
---|
[270c882b] | 9 | from sas.sascalc.dataloader.loader_exceptions import NoKnownLoaderException |
---|
| 10 | |
---|
[4bae1ef] | 11 | |
---|
| 12 | class ExtensionRegistry(object): |
---|
| 13 | """ |
---|
| 14 | Associate a file loader with an extension. |
---|
| 15 | |
---|
| 16 | Note that there may be multiple loaders for the same extension. |
---|
| 17 | |
---|
[51f14603] | 18 | Example: :: |
---|
[4bae1ef] | 19 | |
---|
[51f14603] | 20 | registry = ExtensionRegistry() |
---|
[4bae1ef] | 21 | |
---|
[51f14603] | 22 | # Add an association by setting an element |
---|
| 23 | registry['.zip'] = unzip |
---|
[5d8f9b3] | 24 | |
---|
[51f14603] | 25 | # Multiple extensions for one loader |
---|
| 26 | registry['.tgz'] = untar |
---|
| 27 | registry['.tar.gz'] = untar |
---|
[4bae1ef] | 28 | |
---|
[3ece5dd] | 29 | # Generic extensions to use after trying more specific extensions; |
---|
[51f14603] | 30 | # these will be checked after the more specific extensions fail. |
---|
| 31 | registry['.gz'] = gunzip |
---|
[4bae1ef] | 32 | |
---|
[51f14603] | 33 | # Multiple loaders for one extension |
---|
| 34 | registry['.cx'] = cx1 |
---|
| 35 | registry['.cx'] = cx2 |
---|
| 36 | registry['.cx'] = cx3 |
---|
[4bae1ef] | 37 | |
---|
[51f14603] | 38 | # Show registered extensions |
---|
| 39 | print registry.extensions() |
---|
[5d8f9b3] | 40 | |
---|
[51f14603] | 41 | # Can also register a format name for explicit control from caller |
---|
| 42 | registry['cx3'] = cx3 |
---|
| 43 | print registry.formats() |
---|
[4bae1ef] | 44 | |
---|
[51f14603] | 45 | # Retrieve loaders for a file name |
---|
| 46 | registry.lookup('hello.cx') -> [cx3,cx2,cx1] |
---|
[4bae1ef] | 47 | |
---|
[51f14603] | 48 | # Run loader on a filename |
---|
| 49 | registry.load('hello.cx') -> |
---|
[4bae1ef] | 50 | try: |
---|
[51f14603] | 51 | return cx3('hello.cx') |
---|
[4bae1ef] | 52 | except: |
---|
[51f14603] | 53 | try: |
---|
| 54 | return cx2('hello.cx') |
---|
| 55 | except: |
---|
| 56 | return cx1('hello.cx') |
---|
[4bae1ef] | 57 | |
---|
[51f14603] | 58 | # Load in a specific format ignoring extension |
---|
| 59 | registry.load('hello.cx',format='cx3') -> |
---|
| 60 | return cx3('hello.cx') |
---|
[4bae1ef] | 61 | """ |
---|
| 62 | def __init__(self, **kw): |
---|
| 63 | self.loaders = {} |
---|
[270c882b] | 64 | |
---|
[4bae1ef] | 65 | def __setitem__(self, ext, loader): |
---|
| 66 | if ext not in self.loaders: |
---|
| 67 | self.loaders[ext] = [] |
---|
| 68 | self.loaders[ext].insert(0,loader) |
---|
[270c882b] | 69 | |
---|
[4bae1ef] | 70 | def __getitem__(self, ext): |
---|
| 71 | return self.loaders[ext] |
---|
[270c882b] | 72 | |
---|
[4bae1ef] | 73 | def __contains__(self, ext): |
---|
| 74 | return ext in self.loaders |
---|
[270c882b] | 75 | |
---|
[4bae1ef] | 76 | def formats(self): |
---|
| 77 | """ |
---|
| 78 | Return a sorted list of the registered formats. |
---|
| 79 | """ |
---|
| 80 | names = [a for a in self.loaders.keys() if not a.startswith('.')] |
---|
| 81 | names.sort() |
---|
| 82 | return names |
---|
[270c882b] | 83 | |
---|
[4bae1ef] | 84 | def extensions(self): |
---|
| 85 | """ |
---|
| 86 | Return a sorted list of registered extensions. |
---|
| 87 | """ |
---|
| 88 | exts = [a for a in self.loaders.keys() if a.startswith('.')] |
---|
| 89 | exts.sort() |
---|
| 90 | return exts |
---|
[270c882b] | 91 | |
---|
[4bae1ef] | 92 | def lookup(self, path): |
---|
| 93 | """ |
---|
| 94 | Return the loader associated with the file type of path. |
---|
[3ece5dd] | 95 | |
---|
[7f75a3f] | 96 | :param path: Data file path |
---|
| 97 | :raises ValueError: When no loaders are found for the file. |
---|
| 98 | :return: List of available readers for the file extension |
---|
[3ece5dd] | 99 | """ |
---|
[4bae1ef] | 100 | # Find matching extensions |
---|
| 101 | extlist = [ext for ext in self.extensions() if path.endswith(ext)] |
---|
| 102 | # Sort matching extensions by decreasing order of length |
---|
[dc8d1c2] | 103 | extlist.sort(key=len) |
---|
[4bae1ef] | 104 | # Combine loaders for matching extensions into one big list |
---|
| 105 | loaders = [] |
---|
| 106 | for L in [self.loaders[ext] for ext in extlist]: |
---|
| 107 | loaders.extend(L) |
---|
| 108 | # Remove duplicates if they exist |
---|
| 109 | if len(loaders) != len(set(loaders)): |
---|
| 110 | result = [] |
---|
| 111 | for L in loaders: |
---|
| 112 | if L not in result: result.append(L) |
---|
[8db20a9] | 113 | loaders = result |
---|
| 114 | #loaders = L |
---|
[4bae1ef] | 115 | # Raise an error if there are no matching extensions |
---|
| 116 | if len(loaders) == 0: |
---|
[270c882b] | 117 | raise ValueError("Unknown file type for "+path) |
---|
[4bae1ef] | 118 | return loaders |
---|
[270c882b] | 119 | |
---|
[4bae1ef] | 120 | def load(self, path, format=None): |
---|
| 121 | """ |
---|
| 122 | Call the loader for the file type of path. |
---|
| 123 | |
---|
[a26f67f] | 124 | :raises ValueError: if no loader is available. |
---|
| 125 | :raises KeyError: if format is not available. |
---|
| 126 | |
---|
[7f75a3f] | 127 | May raise a loader-defined exception if loader fails. |
---|
[4bae1ef] | 128 | """ |
---|
[7f75a3f] | 129 | loaders = [] |
---|
[4bae1ef] | 130 | if format is None: |
---|
[270c882b] | 131 | try: |
---|
| 132 | loaders = self.lookup(path) |
---|
| 133 | except ValueError as e: |
---|
| 134 | pass |
---|
[4bae1ef] | 135 | else: |
---|
[270c882b] | 136 | try: |
---|
| 137 | loaders = self.loaders[format] |
---|
| 138 | except KeyError as e: |
---|
| 139 | pass |
---|
[3ece5dd] | 140 | last_exc = None |
---|
[4bae1ef] | 141 | for fn in loaders: |
---|
| 142 | try: |
---|
| 143 | return fn(path) |
---|
[270c882b] | 144 | except Exception as e: |
---|
[3ece5dd] | 145 | last_exc = e |
---|
[270c882b] | 146 | pass # give other loaders a chance to succeed |
---|
[4bae1ef] | 147 | # If we get here it is because all loaders failed |
---|
[3ece5dd] | 148 | if last_exc is not None and len(loaders) != 0: |
---|
| 149 | # If file has associated loader(s) and they;ve failed |
---|
| 150 | raise last_exc |
---|
[270c882b] | 151 | raise NoKnownLoaderException(e.message) # raise generic exception |
---|