home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 February / maximum-cd-2011-02.iso / DiscContents / digsby_setup85.exe / lib / util / data_importer.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-11-24  |  5.5 KB  |  234 lines

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