home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2009 June / maximum-cd-2009-06.iso / DiscContents / digsby_setup.exe / lib / util / data_importer.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-02-26  |  5.5 KB  |  236 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.5)
  3.  
  4. from __future__ import with_statement
  5. import sys
  6. import path
  7. import StringIO
  8. import zipfile
  9. import util
  10. from contextlib import closing
  11.  
  12. def zipopen(fpath, dirs = None, zip = None):
  13.     fpath = path.path(fpath)
  14.     if dirs is None:
  15.         dirs = fpath.splitall()
  16.         if dirs[0] == '\\\\':
  17.             uncstart = dirs[0]
  18.             hostname = dirs[1]
  19.             sharename = dirs[2]
  20.             rest = dirs[3:]
  21.             dirs = [
  22.                 uncstart + hostname + '\\' + sharename] + rest
  23.         
  24.     
  25.     if not dirs:
  26.         raise IOError('No such file or directory: %r' % fpath)
  27.     
  28.     if zip is None:
  29.         nextdir = dirs.pop(0)
  30.         if nextdir:
  31.             curpath = fpath / nextdir
  32.         else:
  33.             curpath = fpath
  34.         for ext in ('', '.zip', '.egg'):
  35.             possible = curpath + ext
  36.             if possible.exists():
  37.                 if possible.isdir():
  38.                     
  39.                     try:
  40.                         return zipopen(possible, dirs = dirs, zip = None)
  41.                     except IOError:
  42.                         pass
  43.                     except:
  44.                         None<EXCEPTION MATCH>IOError
  45.                     
  46.  
  47.                 None<EXCEPTION MATCH>IOError
  48.                 if not dirs:
  49.                     return open(possible, 'rb')
  50.                 
  51.                 
  52.                 try:
  53.                     zip = zipfile.ZipFile(possible)
  54.                 except zipfile.BadZipfile:
  55.                     raise IOError('Could not open %r because %r is not a valid zip file' % (possible.joinpath(*dirs), curpath))
  56.  
  57.                 return zipopen(possible, dirs = dirs, zip = zip)
  58.                 continue
  59.         else:
  60.             raise IOError('No such file or directory: %r' % possible.joinpath(*dirs))
  61.     else:
  62.         bytes = None
  63.         curpath = fpath
  64.         while dirs and bytes is None:
  65.             nextdir = dirs.pop(0)
  66.             curpath = curpath / nextdir
  67.             for ext in ('', '.zip', '.egg'):
  68.                 possible = curpath + ext
  69.                 relpath = fpath.relpathto(curpath).replace('\\', '/')
  70.                 
  71.                 try:
  72.                     bytes = zip.read(relpath)
  73.                 continue
  74.                 except KeyError:
  75.                     continue
  76.                     continue
  77.                 
  78.  
  79.             
  80.             continue
  81.             None<EXCEPTION MATCH>KeyError
  82.         if dirs and bytes:
  83.             
  84.             try:
  85.                 fobj = _[2]
  86.                 
  87.                 try:
  88.                     fobj = zipfile.ZipFile(fobj)
  89.                 except zipfile.BadZipfile:
  90.                     closing(StringIO.StringIO(bytes))
  91.                     closing(StringIO.StringIO(bytes))
  92.                     raise IOError('Could not open %r because %r was found inside a zip file but the embedded file is not the endpoint nor another zip file.' % (curpath.join(*dirs), curpath))
  93.                 except:
  94.                     closing(StringIO.StringIO(bytes))
  95.  
  96.                 return zipopen(fpath, dirs = dirs, zip = fobj)
  97.             finally:
  98.                 pass
  99.  
  100.         elif bytes:
  101.             fobj = StringIO.StringIO(bytes)
  102.             fobj.name = curpath
  103.             return fobj
  104.         else:
  105.             raise IOError('No such file or directory %r' % curpath)
  106.  
  107.  
  108. def ext_importer(*exts):
  109.     exts = list(exts)
  110.     
  111.     def wrapped1(f):
  112.         
  113.         def wrapped2(dotted, loadpath = (None, None)):
  114.             pth = find_dotted_resource(dotted, exts, loadpath = loadpath)
  115.             if pth is None:
  116.                 raise ImportError('%r was not found' % dotted)
  117.             
  118.             return f(pth)
  119.  
  120.         return wrapped2
  121.  
  122.     return wrapped1
  123.  
  124.  
  125. class YAMLModule(dict):
  126.     
  127.     def __init__(self, fname, d):
  128.         self.__file__ = fname
  129.         dict.__init__(self, d)
  130.  
  131.     
  132.     def __getattr__(self, a):
  133.         
  134.         try:
  135.             return dict.__getitem__(self, a)
  136.         except KeyError:
  137.             return dict.__getattribute__(self, a)
  138.  
  139.  
  140.  
  141.  
  142. def yaml_import(fname):
  143.     import syck as yaml
  144.     
  145.     try:
  146.         f = _[2]
  147.         modulefier = (util.dictrecurse,)((lambda x: YAMLModule(fname, x)))
  148.         raw_values = yaml.load(f)
  149.         if not isinstance(raw_values, dict):
  150.             val = dict(__content__ = raw_values)
  151.         else:
  152.             val = raw_values
  153.         mod = modulefier(val)
  154.         mod.__content__ = raw_values
  155.         return mod
  156.     finally:
  157.         pass
  158.  
  159.  
  160. yaml_import = ext_importer('.yaml')(yaml_import)
  161.  
  162. def file_import(fname, loadpath = None):
  163.     
  164.     try:
  165.         (dotted_path, ext) = fname.rsplit('.', 1)
  166.         ext = '.' + ext
  167.     except ValueError:
  168.         dotted_path = fname
  169.         ext = ''
  170.  
  171.     pth = find_dotted_resource(dotted_path, exts = [
  172.         ext], loadpath = None, do_no_ext = False)
  173.     if pth is None:
  174.         raise ImportError('No file named %r was found' % fname)
  175.     else:
  176.         return zipopen(pth)
  177.  
  178.  
  179. def find_dotted_resource(dotted, exts = [], loadpath = None, do_no_ext = True):
  180.     
  181.     try:
  182.         (pth, name) = dotted.rsplit('.', 1)
  183.     except ValueError:
  184.         pth = ''
  185.         name = dotted
  186.  
  187.     if not '/' in pth or '\\' in pth:
  188.         pth = pth.replace('.', '/')
  189.     
  190.     if loadpath is None:
  191.         loadpath = sys.path
  192.     
  193.     if do_no_ext:
  194.         exts = list(exts) + [
  195.             '']
  196.     else:
  197.         exts = list(exts)
  198.     for pathdir in map(path.path, loadpath):
  199.         for ext in exts:
  200.             if (pathdir / pth).isdir():
  201.                 to_check = pathdir / pth / (name + ext)
  202.                 to_check = to_check.expand()
  203.                 if to_check.exists():
  204.                     return to_check
  205.                 
  206.             
  207.             full = pathdir / pth / name + ext
  208.             if full.exists():
  209.                 
  210.                 try:
  211.                     
  212.                     try:
  213.                         f = _[2]
  214.                         return f.name
  215.                     finally:
  216.                         pass
  217.  
  218.                 except IOError:
  219.                     continue
  220.                 except:
  221.                     None<EXCEPTION MATCH>IOError
  222.                 
  223.  
  224.             None<EXCEPTION MATCH>IOError
  225.         
  226.     
  227.  
  228. if __name__ == '__main__':
  229.     from pprint import pprint
  230.     sys.path.append('c:\\workspace\\digsby\\')
  231.     sys.path.insert(0, 'c:\\')
  232.     netpath = path.path('\\\\mini\\mike\\test')
  233.     sys.path.insert(0, netpath)
  234.     print repr(yaml_import('test'))
  235.  
  236.