mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-08 10:44:09 -04:00
Usbms cli support
This commit is contained in:
parent
8b22f780b3
commit
4bdb536671
@ -125,6 +125,7 @@ class USBMS(Device):
|
|||||||
if os.path.exists(path):
|
if os.path.exists(path):
|
||||||
# Delete the ebook
|
# Delete the ebook
|
||||||
os.unlink(path)
|
os.unlink(path)
|
||||||
|
if self.SUPPORTS_SUB_DIRS:
|
||||||
try:
|
try:
|
||||||
os.removedirs(os.path.dirname(path))
|
os.removedirs(os.path.dirname(path))
|
||||||
except:
|
except:
|
||||||
@ -149,6 +150,17 @@ class USBMS(Device):
|
|||||||
src = open(path, 'rb')
|
src = open(path, 'rb')
|
||||||
shutil.copyfileobj(src, outfile, 10*1024*1024)
|
shutil.copyfileobj(src, outfile, 10*1024*1024)
|
||||||
|
|
||||||
|
def put_file(self, infile, path, replace_file=False, end_session=True):
|
||||||
|
path = self.munge_path(path)
|
||||||
|
if os.path.isdir(path):
|
||||||
|
path = os.path.join(path, infile.name)
|
||||||
|
if not replace_file and os.path.exists(path):
|
||||||
|
raise PathError('File already exists: ' + path)
|
||||||
|
dest = open(path, 'wb')
|
||||||
|
shutil.copyfileobj(infile, dest, 10*1024*1024)
|
||||||
|
dest.flush()
|
||||||
|
dest.close()
|
||||||
|
|
||||||
def munge_path(self, path):
|
def munge_path(self, path):
|
||||||
if path.startswith('/') and not (path.startswith(self._main_prefix) or \
|
if path.startswith('/') and not (path.startswith(self._main_prefix) or \
|
||||||
(self._card_prefix and path.startswith(self._card_prefix))):
|
(self._card_prefix and path.startswith(self._card_prefix))):
|
||||||
@ -157,6 +169,34 @@ class USBMS(Device):
|
|||||||
path = path.replace('card:', self._card_prefix[:-1])
|
path = path.replace('card:', self._card_prefix[:-1])
|
||||||
return path
|
return path
|
||||||
|
|
||||||
|
def list(self, path, recurse=False, end_session=True, munge=True):
|
||||||
|
if munge:
|
||||||
|
path = self.munge_path(path)
|
||||||
|
if os.path.isfile(path):
|
||||||
|
return [(os.path.dirname(path), [File(path)])]
|
||||||
|
entries = [File(os.path.join(path, f)) for f in os.listdir(path)]
|
||||||
|
dirs = [(path, entries)]
|
||||||
|
for _file in entries:
|
||||||
|
if recurse and _file.is_dir:
|
||||||
|
dirs[len(dirs):] = self.list(_file.path, recurse=True, munge=False)
|
||||||
|
return dirs
|
||||||
|
|
||||||
|
def mkdir(self, path, end_session=True):
|
||||||
|
if self.SUPPORTS_SUB_DIRS:
|
||||||
|
path = self.munge_path(path)
|
||||||
|
os.mkdir(path)
|
||||||
|
|
||||||
|
def rm(self, path, end_session=True):
|
||||||
|
path = self.munge_path(path)
|
||||||
|
self.delete_books([path])
|
||||||
|
|
||||||
|
def touch(self, path, end_session=True):
|
||||||
|
path = self.munge_path(path)
|
||||||
|
if not os.path.exists(path):
|
||||||
|
open(path, 'w').close()
|
||||||
|
if not os.path.isdir(path):
|
||||||
|
os.utime(path, None)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def extract_book_metadata_by_filename(cls, filename):
|
def extract_book_metadata_by_filename(cls, filename):
|
||||||
book_title = ''
|
book_title = ''
|
||||||
@ -183,5 +223,3 @@ class USBMS(Device):
|
|||||||
|
|
||||||
return book_title, book_author, book_mime
|
return book_title, book_author, book_mime
|
||||||
|
|
||||||
# ls, rm, cp, mkdir, touch, cat
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user