home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 January / maximum-cd-2011-01.iso / DiscContents / calibre-0.7.26.msi / file_1441 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-10-31  |  6.7 KB  |  257 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. __license__ = 'GPL v3'
  5. __copyright__ = '2008, Kovid Goyal kovid@kovidgoyal.net'
  6. __docformat__ = 'restructuredtext en'
  7. import locale
  8. import sys
  9. import os
  10. import re
  11. import __builtin__
  12.  
  13. __builtin__.__dict__['_'] = lambda s: s
  14.  
  15. __builtin__.__dict__['__'] = lambda s: s
  16. from calibre.constants import iswindows, preferred_encoding, plugins, isosx
  17. _run_once = False
  18. winutil = None
  19. winutilerror = None
  20. if not _run_once:
  21.     _run_once = True
  22.     if iswindows:
  23.         (winutil, winutilerror) = plugins['winutil']
  24.         if not winutil:
  25.             raise RuntimeError('Failed to load the winutil plugin: %s' % winutilerror)
  26.         winutil
  27.         if len(sys.argv) > 1 and not isinstance(sys.argv[1], unicode):
  28.             sys.argv[1:] = winutil.argv()[1 - len(sys.argv):]
  29.         
  30.     
  31.     enc = preferred_encoding
  32.     if isosx:
  33.         
  34.         try:
  35.             [ x.decode('utf-8') for x in sys.argv[1:] ]
  36.             enc = 'utf-8'
  37.  
  38.     
  39.     for i in range(1, len(sys.argv)):
  40.         if not isinstance(sys.argv[i], unicode):
  41.             sys.argv[i] = sys.argv[i].decode(enc, 'replace')
  42.             continue
  43.     
  44.     import calibre.utils.resources as resources
  45.     resources
  46.     from calibre.utils.localization import set_translators
  47.     set_translators()
  48.     
  49.     try:
  50.         locale.setlocale(locale.LC_ALL, '')
  51.     except:
  52.         dl = locale.getdefaultlocale()
  53.         
  54.         try:
  55.             if dl:
  56.                 locale.setlocale(dl[0])
  57.  
  58.  
  59.     _abspath = os.path.abspath
  60.     
  61.     def my_abspath(path, encoding = sys.getfilesystemencoding()):
  62.         to_unicode = False
  63.         if encoding is None:
  64.             encoding = preferred_encoding
  65.         
  66.         if isinstance(path, unicode):
  67.             path = path.encode(encoding)
  68.             to_unicode = True
  69.         
  70.         res = _abspath(path)
  71.         if to_unicode:
  72.             res = res.decode(encoding)
  73.         
  74.         return res
  75.  
  76.     os.path.abspath = my_abspath
  77.     _join = os.path.join
  78.     
  79.     def my_join(a, *p):
  80.         encoding = sys.getfilesystemencoding()
  81.         if not encoding:
  82.             encoding = preferred_encoding
  83.         
  84.         p = [
  85.             a] + list(p)
  86.         _unicode = False
  87.         for i in p:
  88.             if isinstance(i, unicode):
  89.                 _unicode = True
  90.                 break
  91.                 continue
  92.         
  93.         p = [ _[1] if isinstance(i, unicode) else i for i in p ]
  94.         res = _join(*p)
  95.         if _unicode:
  96.             res = res.decode(encoding)
  97.         
  98.         return res
  99.  
  100.     os.path.join = my_join
  101.     
  102.     def local_open(name, mode = 'r', bufsize = -1):
  103.         if iswindows:
  104.             
  105.             class fwrapper(object):
  106.                 
  107.                 def __init__(self, name, fobject):
  108.                     object.__setattr__(self, 'fobject', fobject)
  109.                     object.__setattr__(self, 'name', name)
  110.  
  111.                 
  112.                 def __getattribute__(self, attr):
  113.                     if attr in ('name', '__enter__', '__str__', '__unicode__', '__repr__'):
  114.                         return object.__getattribute__(self, attr)
  115.                     fobject = object.__getattribute__(self, 'fobject')
  116.                     return getattr(fobject, attr)
  117.  
  118.                 
  119.                 def __setattr__(self, attr, val):
  120.                     fobject = object.__getattribute__(self, 'fobject')
  121.                     return setattr(fobject, attr, val)
  122.  
  123.                 
  124.                 def __repr__(self):
  125.                     fobject = object.__getattribute__(self, 'fobject')
  126.                     name = object.__getattribute__(self, 'name')
  127.                     return re.sub('[\'"]<fdopen>[\'"]', repr(name), repr(fobject))
  128.  
  129.                 
  130.                 def __str__(self):
  131.                     return repr(self)
  132.  
  133.                 
  134.                 def __unicode__(self):
  135.                     return repr(self).decode('utf-8')
  136.  
  137.                 
  138.                 def __enter__(self):
  139.                     fobject = object.__getattribute__(self, 'fobject')
  140.                     fobject.__enter__()
  141.                     return self
  142.  
  143.  
  144.             m = mode[0]
  145.             if len(mode) > 1:
  146.                 pass
  147.             random = mode[1] == '+'
  148.             binary = mode[-1] == 'b'
  149.             if m == 'a':
  150.                 flags = os.O_APPEND | os.O_RDWR
  151.                 None |= flags if random else os.O_SEQUENTIAL
  152.             elif m == 'r':
  153.                 if random:
  154.                     flags = os.O_RDWR | os.O_RANDOM
  155.                 else:
  156.                     flags = os.O_RDONLY | os.O_SEQUENTIAL
  157.             elif m == 'w':
  158.                 if random:
  159.                     flags = os.O_RDWR | os.O_RANDOM
  160.                 else:
  161.                     flags = os.O_WRONLY | os.O_SEQUENTIAL
  162.                 flags |= os.O_TRUNC | os.O_CREAT
  163.             
  164.             if binary:
  165.                 flags |= os.O_BINARY
  166.             else:
  167.                 flags |= os.O_TEXT
  168.             flags |= os.O_NOINHERIT
  169.             fd = os.open(name, flags)
  170.             ans = os.fdopen(fd, mode, bufsize)
  171.             ans = fwrapper(name, ans)
  172.         else:
  173.             import fcntl
  174.             
  175.             try:
  176.                 cloexec_flag = fcntl.FD_CLOEXEC
  177.             except AttributeError:
  178.                 cloexec_flag = 1
  179.  
  180.             ans = open(name, mode, bufsize)
  181.             old = fcntl.fcntl(ans, fcntl.F_GETFD)
  182.             fcntl.fcntl(ans, fcntl.F_SETFD, old | cloexec_flag)
  183.         return ans
  184.  
  185.     __builtin__.__dict__['lopen'] = local_open
  186.  
  187.  
  188. def test_lopen():
  189.     TemporaryDirectory = TemporaryDirectory
  190.     import calibre.ptempfile
  191.     CurrentDir = CurrentDir
  192.     import calibre
  193.     n = u'f├ñllen'
  194.     
  195.     try:
  196.         tdir = _[1]
  197.         CurrentDir(tdir).__enter__()
  198.         
  199.         try:
  200.             
  201.             try:
  202.                 f = _[2]
  203.                 f.write('one')
  204.             finally:
  205.                 pass
  206.  
  207.             print 'O_CREAT tested'
  208.             
  209.             try:
  210.                 f = _[3]
  211.                 f.write('two')
  212.             finally:
  213.                 pass
  214.  
  215.             
  216.             try:
  217.                 f = _[4]
  218.                 if f.read() == 'two':
  219.                     print 'O_TRUNC tested'
  220.                 else:
  221.                     raise Exception('O_TRUNC failed')
  222.                 
  223.                 try:
  224.                     f = _[5]
  225.                     f.write('three')
  226.                 finally:
  227.                     pass
  228.  
  229.                 
  230.                 try:
  231.                     f = _[6]
  232.                     if f.read() == 'twothree':
  233.                         print 'O_APPEND tested'
  234.                     else:
  235.                         raise Exception('O_APPEND failed')
  236.                     
  237.                     try:
  238.                         f = _[7]
  239.                         f.seek(3)
  240.                         f.write('xxxxx')
  241.                         f.seek(0)
  242.                         if f.read() == 'twoxxxxx':
  243.                             print 'O_RANDOM tested'
  244.                         else:
  245.                             raise Exception('O_RANDOM failed')
  246.                     finally:
  247.                         pass
  248.  
  249.                 finally:
  250.                     pass
  251.  
  252.                 return None
  253.  
  254.  
  255.  
  256.  
  257.