1
# -*- coding: utf-8 -*-
2
from __future__ import with_statement
5
__copyright__ = '2009, John Schember <john@nachtimwald.com>'
6
__docformat__ = 'restructuredtext en'
10
from calibre.devices.errors import PathError
14
def __init__(self, path):
16
self.is_dir = os.path.isdir(path)
17
self.is_readonly = not os.access(path, os.W_OK)
18
self.ctime = stats.st_ctime
19
self.wtime = stats.st_mtime
20
self.size = stats.st_size
21
if path.endswith(os.sep):
24
self.name = os.path.basename(path)
29
def get_file(self, path, outfile, end_session=True):
30
path = self.munge_path(path)
31
with open(path, 'rb') as src:
32
shutil.copyfileobj(src, outfile, 10*1024*1024)
34
def put_file(self, infile, path, replace_file=False, end_session=True):
35
path = self.munge_path(path)
36
if os.path.isdir(path):
37
path = os.path.join(path, infile.name)
38
if not replace_file and os.path.exists(path):
39
raise PathError('File already exists: ' + path)
40
d = os.path.dirname(path)
41
if not os.path.exists(d):
43
dest = open(path, 'wb')
44
shutil.copyfileobj(infile, dest, 10*1024*1024)
48
def munge_path(self, path):
49
if path.startswith('/') and not (path.startswith(self._main_prefix) or \
50
(self._card_a_prefix and path.startswith(self._card_a_prefix)) or \
51
(self._card_b_prefix and path.startswith(self._card_b_prefix))):
52
path = self._main_prefix + path[1:]
53
elif path.startswith('carda:'):
54
path = path.replace('carda:', self._card_a_prefix[:-1])
55
elif path.startswith('cardb:'):
56
path = path.replace('cardb:', self._card_b_prefix[:-1])
59
def list(self, path, recurse=False, end_session=True, munge=True):
61
path = self.munge_path(path)
62
if os.path.isfile(path):
63
return [(os.path.dirname(path), [File(path)])]
64
entries = [File(os.path.join(path, f)) for f in os.listdir(path)]
65
dirs = [(path, entries)]
67
if recurse and _file.is_dir:
68
dirs[len(dirs):] = self.list(_file.path, recurse=True, munge=False)
71
def mkdir(self, path, end_session=True):
72
if self.SUPPORTS_SUB_DIRS:
73
path = self.munge_path(path)
76
def rm(self, path, end_session=True):
77
path = self.munge_path(path)
78
self.delete_books([path])
80
def touch(self, path, end_session=True):
81
path = self.munge_path(path)
82
if not os.path.exists(path):
83
open(path, 'w').close()
84
if not os.path.isdir(path):