home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_1741 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  7.1 KB  |  200 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import IPython.ipapi as IPython
  5. ip = IPython.ipapi.get()
  6. import shutil
  7. import os
  8. import shlex
  9. from IPython.external import mglob
  10. from IPython.external.path import path
  11. from IPython.ipapi import UsageError
  12.  
  13. def parse_args(args):
  14.     tup = args.split(None, 1)
  15.     if len(tup) == 1:
  16.         raise UsageError('Expected arguments for ' + tup[0])
  17.     len(tup) == 1
  18.     tup2 = shlex.split(tup[1])
  19.     flist = mglob.expand(tup2[0:-1])
  20.     trg = tup2[-1]
  21.     if not flist:
  22.         raise UsageError('No files found:' + str(tup2[0:-1]))
  23.     flist
  24.     return (flist, trg)
  25.  
  26.  
  27. def icp(ip, arg):
  28.     import distutils.dir_util as distutils
  29.     (fs, targetdir) = parse_args(arg)
  30.     if not os.path.isdir(targetdir) and len(fs) > 1:
  31.         distutils.dir_util.mkpath(targetdir, verbose = 1)
  32.     
  33.     for f in fs:
  34.         if os.path.isdir(f):
  35.             shutil.copytree(f, targetdir)
  36.             continue
  37.         shutil.copy2(f, targetdir)
  38.     
  39.     return fs
  40.  
  41. ip.defalias('icp', icp)
  42.  
  43. def imv(ip, arg):
  44.     (fs, target) = parse_args(arg)
  45.     if len(fs) > 1:
  46.         pass
  47.     
  48.     for f in fs:
  49.         shutil.move(f, target)
  50.     
  51.     return fs
  52.  
  53. ip.defalias('imv', imv)
  54.  
  55. def irm(ip, arg):
  56.     
  57.     try:
  58.         paths = mglob.expand(arg.split(None, 1)[1])
  59.     except IndexError:
  60.         raise UsageError('%irm paths...')
  61.  
  62.     import distutils.dir_util as distutils
  63.     for p in paths:
  64.         print 'rm', p
  65.         if os.path.isdir(p):
  66.             distutils.dir_util.remove_tree(p, verbose = 1)
  67.             continue
  68.         os.remove(p)
  69.     
  70.  
  71. ip.defalias('irm', irm)
  72.  
  73. def imkdir(ip, arg):
  74.     import distutils.dir_util as distutils
  75.     targetdir = arg.split(None, 1)[1]
  76.     distutils.dir_util.mkpath(targetdir, verbose = 1)
  77.  
  78. ip.defalias('imkdir', imkdir)
  79.  
  80. def igrep(ip, arg):
  81.     elems = shlex.split(arg)
  82.     dummy = elems[0]
  83.     pat = elems[1]
  84.     fs = mglob.expand(elems[2:])
  85.     res = []
  86.     for f in fs:
  87.         found = False
  88.         for l in open(f):
  89.             if pat.lower() in l.lower():
  90.                 if not found:
  91.                     print '[[', f, ']]'
  92.                     found = True
  93.                     res.append(f)
  94.                 
  95.                 print l.rstrip()
  96.                 continue
  97.         
  98.     
  99.     return res
  100.  
  101. ip.defalias('igrep', igrep)
  102.  
  103. def collect(ip, arg):
  104.     path = path
  105.     import IPython.external.path
  106.     basedir = path(ip.options.ipythondir + '/collect')
  107.     
  108.     try:
  109.         fs = mglob.expand(arg.split(None, 1)[1])
  110.     except IndexError:
  111.         os.startfile(basedir)
  112.         return None
  113.  
  114.     for f in fs:
  115.         f = path(f)
  116.         trg = basedir / f.splitdrive()[1].lstrip('/\\')
  117.         if f.isdir():
  118.             print 'mkdir', trg
  119.             trg.makedirs()
  120.             continue
  121.         
  122.         dname = trg.dirname()
  123.         if not dname.isdir():
  124.             dname.makedirs()
  125.         
  126.         print f, '=>', trg
  127.         shutil.copy2(f, trg)
  128.     
  129.  
  130. ip.defalias('collect', collect)
  131.  
  132. def inote(ip, arg):
  133.     import time
  134.     fname = ip.options.ipythondir + '/notes.txt'
  135.     
  136.     try:
  137.         entry = ' === ' + time.asctime() + ': ===\n' + arg.split(None, 1)[1] + '\n'
  138.         f = open(fname, 'a').write(entry)
  139.     except IndexError:
  140.         ip.IP.hooks.editor(fname)
  141.  
  142.  
  143. ip.defalias('inote', inote)
  144.  
  145. def pathobj_mangle(p):
  146.     return p.replace(' ', '__').replace('.', 'DOT')
  147.  
  148.  
  149. def pathobj_unmangle(s):
  150.     return s.replace('__', ' ').replace('DOT', '.')
  151.  
  152.  
  153. class PathObj(path):
  154.     
  155.     def __init__(self, p):
  156.         self.path = p
  157.  
  158.     
  159.     def __complete__(self):
  160.         if self.path != '.':
  161.             return self.ents
  162.         self.ents = [ pathobj_mangle(ent) for ent in os.listdir('.') ]
  163.         return self.ents
  164.  
  165.     
  166.     def __getattr__(self, name):
  167.         raise AttributeError, name
  168.  
  169.     
  170.     def __str__(self):
  171.         return self.path
  172.  
  173.     
  174.     def __repr__(self):
  175.         return '<PathObj to %s>' % self.path
  176.  
  177.     
  178.     def __call__(self):
  179.         print 'cd:', self.path
  180.         os.chdir(self.path)
  181.  
  182.  
  183.  
  184. def complete_pathobj(obj, prev_completions):
  185.     if hasattr(obj, '__complete__'):
  186.         res = obj.__complete__()
  187.         if res:
  188.             return res
  189.     
  190.     raise IPython.ipapi.TryNext
  191.  
  192. complete_pathobj = IPython.generics.complete_object.when_type(PathObj)(complete_pathobj)
  193.  
  194. def test_pathobj():
  195.     rootdir = PathObj('/')
  196.     startmenu = PathObj('d:/Documents and Settings/All Users/Start Menu/Programs')
  197.     cwd = PathObj('.')
  198.     ip.to_user_ns('rootdir startmenu cwd')
  199.  
  200.