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

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. globsyntax = '    This program allows specifying filenames with "mglob" mechanism.\n    Supported syntax in globs (wilcard matching patterns)::\n    \n     *.cpp ?ellowo*                \n         - obvious. Differs from normal glob in that dirs are not included.\n           Unix users might want to write this as: "*.cpp" "?ellowo*"\n     rec:/usr/share=*.txt,*.doc    \n         - get all *.txt and *.doc under /usr/share, \n           recursively\n     rec:/usr/share\n         - All files under /usr/share, recursively\n     rec:*.py\n         - All .py files under current working dir, recursively\n     foo                           \n         - File or dir foo\n     !*.bak readme*                   \n         - readme*, exclude files ending with .bak\n     !.svn/ !.hg/ !*_Data/ rec:.\n         - Skip .svn, .hg, foo_Data dirs (and their subdirs) in recurse.\n           Trailing / is the key, \\ does not work! Use !.*/ for all hidden.\n     dir:foo                       \n         - the directory foo if it exists (not files in foo)\n     dir:*                         \n         - all directories in current folder\n     foo.py bar.* !h* rec:*.py\n         - Obvious. !h* exclusion only applies for rec:*.py.\n           foo.py is *not* included twice.\n     @filelist.txt\n         - All files listed in \'filelist.txt\' file, on separate lines.\n     "cont:class \\wak:" rec:*.py\n         - Match files containing regexp. Applies to subsequent files.\n           note quotes because of whitespace.\n '
  5. __version__ = '0.2'
  6. import os
  7. import glob
  8. import fnmatch
  9. import sys
  10. import re
  11.  
  12. def expand(flist, exp_dirs = False):
  13.     if isinstance(flist, basestring):
  14.         import shlex
  15.         flist = shlex.split(flist)
  16.     
  17.     done_set = set()
  18.     denied_set = set()
  19.     cont_set = set()
  20.     cur_rejected_dirs = set()
  21.     
  22.     def recfind(p, pats = (None, [
  23.         '*'])):
  24.         denied_dirs = _[1]
  25.         for dp, dnames, fnames in os.walk(p):
  26.             dp_norm = dp.replace('\\', '/') + '/'
  27.             deny = False
  28.             for d in cur_rejected_dirs:
  29.                 if dp.startswith(d):
  30.                     deny = True
  31.                     break
  32.                     continue
  33.                 []
  34.             
  35.             if deny:
  36.                 continue
  37.             
  38.             bname = os.path.basename(dp)
  39.             for deny_pat in denied_dirs:
  40.                 if fnmatch.fnmatch(bname, deny_pat):
  41.                     deny = True
  42.                     cur_rejected_dirs.add(dp)
  43.                     break
  44.                     continue
  45.             
  46.             if deny:
  47.                 continue
  48.             
  49.             for f in fnames:
  50.                 matched = False
  51.                 for p in pats:
  52.                     if fnmatch.fnmatch(f, p):
  53.                         matched = True
  54.                         break
  55.                         continue
  56.                 
  57.                 if matched:
  58.                     yield os.path.join(dp, f)
  59.                     continue
  60.             
  61.         
  62.  
  63.     
  64.     def once_filter(seq):
  65.         for it in seq:
  66.             p = os.path.abspath(it)
  67.             if p in done_set:
  68.                 continue
  69.             
  70.             done_set.add(p)
  71.             deny = False
  72.             for deny_pat in denied_set:
  73.                 if fnmatch.fnmatch(os.path.basename(p), deny_pat):
  74.                     deny = True
  75.                     break
  76.                     continue
  77.             
  78.             if cont_set:
  79.                 
  80.                 try:
  81.                     cont = open(p).read()
  82.                 except IOError:
  83.                     continue
  84.  
  85.                 for pat in cont_set:
  86.                     if not re.search(pat, cont, re.IGNORECASE):
  87.                         deny = True
  88.                         break
  89.                         continue
  90.                 
  91.             
  92.             if not deny:
  93.                 yield it
  94.                 continue
  95.         
  96.  
  97.     res = []
  98.     for ent in flist:
  99.         ent = os.path.expanduser(os.path.expandvars(ent))
  100.         if ent.lower().startswith('rec:'):
  101.             fields = ent[4:].split('=')
  102.             if len(fields) == 2:
  103.                 (pth, patlist) = fields
  104.             elif len(fields) == 1:
  105.                 if os.path.isdir(fields[0]):
  106.                     pth = fields[0]
  107.                     patlist = '*'
  108.                 else:
  109.                     pth = '.'
  110.                     patlist = fields[0]
  111.             elif len(fields) == 0:
  112.                 (pth, pathlist) = ('.', '*')
  113.             
  114.             pats = patlist.split(',')
  115.             res.extend(once_filter(recfind(pth, pats)))
  116.             continue
  117.         if ent.startswith('@') and os.path.isfile(ent[1:]):
  118.             res.extend(once_filter(open(ent[1:]).read().splitlines()))
  119.             continue
  120.         if ent.startswith('!'):
  121.             denied_set.add(ent[1:])
  122.             continue
  123.         if ent.lower().startswith('dir:'):
  124.             res.extend(once_filter(filter(os.path.isdir, glob.glob(ent[4:]))))
  125.             continue
  126.         if ent.lower().startswith('cont:'):
  127.             cont_set.add(ent[5:])
  128.             continue
  129.         if os.path.isdir(ent) and exp_dirs:
  130.             res.extend(once_filter(filter(os.path.isfile, glob.glob(ent + os.sep + '*'))))
  131.             continue
  132.         if '*' in ent or '?' in ent:
  133.             res.extend(once_filter(filter(os.path.isfile, glob.glob(ent))))
  134.             continue
  135.         res.extend(once_filter([
  136.             ent]))
  137.     
  138.     return res
  139.  
  140.  
  141. def test():
  142.     pass
  143.  
  144.  
  145. def main():
  146.     if len(sys.argv) < 2:
  147.         print globsyntax
  148.         return None
  149.     print '\n'.join(expand(sys.argv[1:])),
  150.  
  151.  
  152. def mglob_f(self, arg):
  153.     SList = SList
  154.     import IPython.genutils
  155.     if arg.strip():
  156.         return SList(expand(arg))
  157.     print 'Please specify pattern!'
  158.     print globsyntax
  159.  
  160.  
  161. def init_ipython(ip):
  162.     mglob_f.__doc__ = globsyntax
  163.     ip.expose_magic('mglob', mglob_f)
  164.  
  165. if __name__ == '__main__':
  166.     main()
  167.  
  168.