home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyc (Python 2.6)
-
- __license__ = 'GPL v3'
- __copyright__ = '2008, Kovid Goyal kovid@kovidgoyal.net'
- __docformat__ = 'restructuredtext en'
- import locale
- import sys
- import os
- import re
- import __builtin__
-
- __builtin__.__dict__['_'] = lambda s: s
-
- __builtin__.__dict__['__'] = lambda s: s
- from calibre.constants import iswindows, preferred_encoding, plugins, isosx
- _run_once = False
- winutil = None
- winutilerror = None
- if not _run_once:
- _run_once = True
- if iswindows:
- (winutil, winutilerror) = plugins['winutil']
- if not winutil:
- raise RuntimeError('Failed to load the winutil plugin: %s' % winutilerror)
- winutil
- if len(sys.argv) > 1 and not isinstance(sys.argv[1], unicode):
- sys.argv[1:] = winutil.argv()[1 - len(sys.argv):]
-
-
- enc = preferred_encoding
- if isosx:
-
- try:
- [ x.decode('utf-8') for x in sys.argv[1:] ]
- enc = 'utf-8'
-
-
- for i in range(1, len(sys.argv)):
- if not isinstance(sys.argv[i], unicode):
- sys.argv[i] = sys.argv[i].decode(enc, 'replace')
- continue
-
- import calibre.utils.resources as resources
- resources
- from calibre.utils.localization import set_translators
- set_translators()
-
- try:
- locale.setlocale(locale.LC_ALL, '')
- except:
- dl = locale.getdefaultlocale()
-
- try:
- if dl:
- locale.setlocale(dl[0])
-
-
- _abspath = os.path.abspath
-
- def my_abspath(path, encoding = sys.getfilesystemencoding()):
- to_unicode = False
- if encoding is None:
- encoding = preferred_encoding
-
- if isinstance(path, unicode):
- path = path.encode(encoding)
- to_unicode = True
-
- res = _abspath(path)
- if to_unicode:
- res = res.decode(encoding)
-
- return res
-
- os.path.abspath = my_abspath
- _join = os.path.join
-
- def my_join(a, *p):
- encoding = sys.getfilesystemencoding()
- if not encoding:
- encoding = preferred_encoding
-
- p = [
- a] + list(p)
- _unicode = False
- for i in p:
- if isinstance(i, unicode):
- _unicode = True
- break
- continue
-
- p = [ _[1] if isinstance(i, unicode) else i for i in p ]
- res = _join(*p)
- if _unicode:
- res = res.decode(encoding)
-
- return res
-
- os.path.join = my_join
-
- def local_open(name, mode = 'r', bufsize = -1):
- if iswindows:
-
- class fwrapper(object):
-
- def __init__(self, name, fobject):
- object.__setattr__(self, 'fobject', fobject)
- object.__setattr__(self, 'name', name)
-
-
- def __getattribute__(self, attr):
- if attr in ('name', '__enter__', '__str__', '__unicode__', '__repr__'):
- return object.__getattribute__(self, attr)
- fobject = object.__getattribute__(self, 'fobject')
- return getattr(fobject, attr)
-
-
- def __setattr__(self, attr, val):
- fobject = object.__getattribute__(self, 'fobject')
- return setattr(fobject, attr, val)
-
-
- def __repr__(self):
- fobject = object.__getattribute__(self, 'fobject')
- name = object.__getattribute__(self, 'name')
- return re.sub('[\'"]<fdopen>[\'"]', repr(name), repr(fobject))
-
-
- def __str__(self):
- return repr(self)
-
-
- def __unicode__(self):
- return repr(self).decode('utf-8')
-
-
- def __enter__(self):
- fobject = object.__getattribute__(self, 'fobject')
- fobject.__enter__()
- return self
-
-
- m = mode[0]
- if len(mode) > 1:
- pass
- random = mode[1] == '+'
- binary = mode[-1] == 'b'
- if m == 'a':
- flags = os.O_APPEND | os.O_RDWR
- None |= flags if random else os.O_SEQUENTIAL
- elif m == 'r':
- if random:
- flags = os.O_RDWR | os.O_RANDOM
- else:
- flags = os.O_RDONLY | os.O_SEQUENTIAL
- elif m == 'w':
- if random:
- flags = os.O_RDWR | os.O_RANDOM
- else:
- flags = os.O_WRONLY | os.O_SEQUENTIAL
- flags |= os.O_TRUNC | os.O_CREAT
-
- if binary:
- flags |= os.O_BINARY
- else:
- flags |= os.O_TEXT
- flags |= os.O_NOINHERIT
- fd = os.open(name, flags)
- ans = os.fdopen(fd, mode, bufsize)
- ans = fwrapper(name, ans)
- else:
- import fcntl
-
- try:
- cloexec_flag = fcntl.FD_CLOEXEC
- except AttributeError:
- cloexec_flag = 1
-
- ans = open(name, mode, bufsize)
- old = fcntl.fcntl(ans, fcntl.F_GETFD)
- fcntl.fcntl(ans, fcntl.F_SETFD, old | cloexec_flag)
- return ans
-
- __builtin__.__dict__['lopen'] = local_open
-
-
- def test_lopen():
- TemporaryDirectory = TemporaryDirectory
- import calibre.ptempfile
- CurrentDir = CurrentDir
- import calibre
- n = u'fällen'
-
- try:
- tdir = _[1]
- CurrentDir(tdir).__enter__()
-
- try:
-
- try:
- f = _[2]
- f.write('one')
- finally:
- pass
-
- print 'O_CREAT tested'
-
- try:
- f = _[3]
- f.write('two')
- finally:
- pass
-
-
- try:
- f = _[4]
- if f.read() == 'two':
- print 'O_TRUNC tested'
- else:
- raise Exception('O_TRUNC failed')
-
- try:
- f = _[5]
- f.write('three')
- finally:
- pass
-
-
- try:
- f = _[6]
- if f.read() == 'twothree':
- print 'O_APPEND tested'
- else:
- raise Exception('O_APPEND failed')
-
- try:
- f = _[7]
- f.seek(3)
- f.write('xxxxx')
- f.seek(0)
- if f.read() == 'twoxxxxx':
- print 'O_RANDOM tested'
- else:
- raise Exception('O_RANDOM failed')
- finally:
- pass
-
- finally:
- pass
-
- return None
-
-
-
-
-