mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
...
This commit is contained in:
parent
8538c08b03
commit
b5801dc63d
@ -9,10 +9,12 @@ __docformat__ = 'restructuredtext en'
|
|||||||
|
|
||||||
__all__ = ['dukpy', 'Context', 'undefined', 'JSError', 'to_python']
|
__all__ = ['dukpy', 'Context', 'undefined', 'JSError', 'to_python']
|
||||||
|
|
||||||
import errno, os, sys, numbers, hashlib, json, tempfile, stat
|
import errno, os, sys, numbers, hashlib, json
|
||||||
from functools import partial
|
from functools import partial
|
||||||
|
|
||||||
from calibre.constants import plugins
|
from calibre.constants import plugins, iswindows
|
||||||
|
from calibre.utils.filenames import atomic_rename
|
||||||
|
|
||||||
dukpy, err = plugins['dukpy']
|
dukpy, err = plugins['dukpy']
|
||||||
if err:
|
if err:
|
||||||
raise RuntimeError('Failed to load dukpy with error: %s' % err)
|
raise RuntimeError('Failed to load dukpy with error: %s' % err)
|
||||||
@ -120,10 +122,11 @@ def readfile(path, enc='utf-8'):
|
|||||||
return [None, errno.errorcode[e.errno], 'Failed to read from file: %s with error: %s' % (path, e.message or e)]
|
return [None, errno.errorcode[e.errno], 'Failed to read from file: %s with error: %s' % (path, e.message or e)]
|
||||||
|
|
||||||
def atomic_write(name, raw):
|
def atomic_write(name, raw):
|
||||||
with tempfile.NamedTemporaryFile(dir=os.getcwdu(), delete=False) as f:
|
bdir, bname = os.path.dirname(os.path.abspath(name)), os.path.basename(name)
|
||||||
|
tname = ('_' if iswindows else '.') + bname
|
||||||
|
with open(os.path.join(bdir, tname), 'wb') as f:
|
||||||
f.write(raw)
|
f.write(raw)
|
||||||
os.fchmod(f.fileno(), stat.S_IREAD|stat.S_IWRITE|stat.S_IRGRP|stat.S_IROTH)
|
atomic_rename(tname, name)
|
||||||
os.rename(f.name, name)
|
|
||||||
|
|
||||||
def writefile(path, data, enc='utf-8'):
|
def writefile(path, data, enc='utf-8'):
|
||||||
if enc == undefined:
|
if enc == undefined:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user