summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Étienne Deparis 2017-05-25 15:01:25 +0200
committerGravatar Étienne Deparis 2017-05-25 15:01:25 +0200
commitb1017fbf78e7cec8a7859158ae6567cbdd89d492 (patch)
treeb746d1ef8fbd9e83cb40f0db2f7e711661dc4d87
parent67d03e7daa8fee0a9dff70872d52f12b8da8827c (diff)
downloadeshelf-b1017fbf78e7cec8a7859158ae6567cbdd89d492.tar.gz
eshelf-b1017fbf78e7cec8a7859158ae6567cbdd89d492.tar.xz
eshelf-b1017fbf78e7cec8a7859158ae6567cbdd89d492.zip
Follow pep8 guidelines
-rw-r--r--eshelf.py189
-rw-r--r--plugins/backup.py20
2 files changed, 121 insertions, 88 deletions
diff --git a/eshelf.py b/eshelf.py
index 1fb825b..e41f41e 100644
--- a/eshelf.py
+++ b/eshelf.py
@@ -10,9 +10,8 @@ from configparser import ConfigParser
from xdg.BaseDirectory import xdg_config_home
-
def file_listing_helper(current_dir, padding=None):
- if padding == None:
+ if padding is None:
padding = ''
decorator = '├──'
@@ -55,9 +54,10 @@ def bookeen_identify():
return 'Bookeen Cybook Muse'
try:
- usbdetails = str(subprocess.check_output(['lsusb', '-d{0}'.format(prefix), '-v']))
+ usbdetails = str(subprocess.check_output(
+ ['lsusb', '-d{0}'.format(prefix), '-v']))
m = re.search('bcdDevice\s*([0-9.]+)\\\\n', usbdetails)
- if m != None and m.group(1) == '3.19':
+ if m is not None and m.group(1) == '3.19':
return 'Bookeen Cybook Orizon'
except subprocess.CalledProcessError:
@@ -68,68 +68,70 @@ def bookeen_identify():
return None
-
class Shelf:
def __init__(self, library_path):
- if library_path == None:
+ if library_path is None:
return
if not os.path.isdir(library_path):
- print('{0} does not exists, please provide us a valid path.'.format(library_path))
+ print('{0} does not exists, please provide us a valid path.'
+ .format(library_path))
sys.exit(1)
self.library_path = library_path
config_file = os.path.join(self.library_path, '.eshelf.conf')
if not os.path.isfile(config_file):
- print('{0} is not a valid library folder. Please do first `eshelf init`'.format(self.library_path))
+ print('{0} is not a valid library folder. Please do first'
+ ' `eshelf init`'.format(self.library_path))
sys.exit(1)
confparser = ConfigParser()
confparser.read(config_file)
self.config = confparser['DEFAULT']
- if not 'reader_mount_point' in self.config:
- print('{0} has been corrupted. Please redo `eshelf init`'.format(self.library_path))
+ if 'reader_mount_point' not in self.config:
+ print('{0} has been corrupted. Please redo'
+ ' `eshelf init`'.format(self.library_path))
sys.exit(1)
self.reader_mount_point = self.config['reader_mount_point']
- self.reader_db_path = os.path.join(self.reader_mount_point, 'system', 'library')
+ self.reader_db_path = os.path.join(
+ self.reader_mount_point, 'system', 'library')
self.reader_db = None
self.remote_physical_path = '/mnt/fat'
if 'remote_physical_path' in self.config:
self.remote_physical_path = self.config['remote_physical_path']
-
def get_config(self):
return self.config
-
def get_reader_db(self):
- if self.reader_db == None:
+ if self.reader_db is None:
try:
self.reader_db = sqlite3.connect(self.reader_db_path)
self.reader_db.row_factory = sqlite3.Row
self.reader_db.isolation_level = None
except sqlite3.OperationalError:
- print('Something wrong happen. Maybe you do not have mounted your ereader.')
+ print('Something wrong happen. Maybe you do not have'
+ ' mounted your ereader.')
sys.exit(1)
return self.reader_db
-
def get_cursor(self):
return self.get_reader_db().cursor()
-
def collections(self, opts=[]):
finaldata = []
cur = self.get_cursor()
- for row in cur.execute('select f_id_shelf, f_name, f_readonly from t_shelf order by f_name'):
+ for row in cur.execute('select f_id_shelf, f_name, f_readonly'
+ ' from t_shelf order by f_name'):
loc_data = dict(row)
if loc_data['f_readonly'] == 0:
- finaldata.append('[{0}] {1}'.format(loc_data['f_id_shelf'], loc_data['f_name']))
+ finaldata.append('[{0}] {1}'.format(
+ loc_data['f_id_shelf'], loc_data['f_name']))
if len(finaldata) == 0:
return
@@ -145,7 +147,6 @@ class Shelf:
curpointer += 1
print('{0} {1}'.format(decorator, line))
-
def books(self, opts=[]):
cur = self.get_cursor()
finaldata = []
@@ -159,22 +160,29 @@ class Shelf:
else:
if str(opts[0]).isdecimal():
- shelf_infos_req = 'select f_id_shelf, f_name from t_shelf where f_id_shelf = ?'
+ shelf_infos_req = 'select f_id_shelf, f_name ' \
+ 'from t_shelf where f_id_shelf = ?'
else:
- shelf_infos_req = 'select f_id_shelf, f_name from t_shelf where f_name = ? collate nocase'
+ shelf_infos_req = 'select f_id_shelf, f_name ' \
+ 'from t_shelf where f_name = ? collate nocase'
- shelf_infos = cur.execute(shelf_infos_req, (opts[0],)).fetchone()
- if shelf_infos == None:
+ shelf_infos = cur.execute(
+ shelf_infos_req, (opts[0],)).fetchone()
+ if shelf_infos is None:
print('No such shelf {0}'.format(opts[0]))
return
shelf_infos = dict(shelf_infos)
shelf_name = shelf_infos['f_name']
values = (shelf_infos['f_id_shelf'],)
- request = 'select b.f_id_item, b.f_title, b.f_internal_uri from t_item b, t_shelf_link tb where tb.f_item_id = b.f_id_item and tb.f_shelf_id = ? order by b.f_title'
+ request = 'select b.f_id_item, b.f_title, b.f_internal_uri ' \
+ 'from t_item b, t_shelf_link tb where ' \
+ 'tb.f_item_id = b.f_id_item and tb.f_shelf_id = ? ' \
+ 'order by b.f_title'
- if request == None:
- request = 'select f_id_item, f_title, f_internal_uri from t_item order by f_title'
+ if request is None:
+ request = 'select f_id_item, f_title, f_internal_uri ' \
+ 'from t_item order by f_title'
for row in cur.execute(request, values):
loc_data = dict(row)
@@ -184,7 +192,9 @@ class Shelf:
loc_data['f_internal_uri'])
local_file_path = re.sub(
- os.path.join(self.remote_physical_path, self.config['remote_library_path']) + '/',
+ os.path.join(
+ self.remote_physical_path,
+ self.config['remote_library_path']) + '/',
'',
loc_data['f_internal_uri'])
# Twice to be sure to coverage all cases
@@ -194,26 +204,27 @@ class Shelf:
local_file_path)
if orphans:
- if not os.path.isfile(os.path.join(self.library_path, local_file_path)):
+ if not os.path.isfile(
+ os.path.join(self.library_path, local_file_path)):
local_file = real_internal_path
else:
continue
else:
- local_file = '(not on your computer: {0})'.format(local_file_path)
- if os.path.isfile(os.path.join(self.library_path, local_file_path)):
+ local_file = '(not on your computer: {0})' \
+ .format(local_file_path)
+ if os.path.isfile(os.path.join(
+ self.library_path, local_file_path)):
local_file = ''
if os.path.isfile(real_internal_path):
- finaldata.append('[{0}] {1} {2}'.format(loc_data['f_id_item'], loc_data['f_title'], local_file))
-
+ finaldata.append('[{0}] {1} {2}'.format(
+ loc_data['f_id_item'], loc_data['f_title'], local_file))
-
- if shelf_name != None:
+ if shelf_name is not None:
print('Books in {0}:'.format(shelf_name))
else:
print('Books in your eReader library:')
-
if len(finaldata) == 0:
return
@@ -227,43 +238,50 @@ class Shelf:
curpointer += 1
print('{0} {1}'.format(decorator, line))
-
def prepare_add_remove(self, shelf_id_or_name, book_id_or_title):
cur = self.get_cursor()
# Ok now try to find corresponding shelf
if str(shelf_id_or_name).isdecimal():
- shelf_infos_req = 'select f_id_shelf from t_shelf where f_id_shelf = ?'
+ shelf_infos_req = 'select f_id_shelf from t_shelf ' \
+ 'where f_id_shelf = ?'
else:
- shelf_infos_req = 'select f_id_shelf from t_shelf where f_name = ? collate nocase'
+ shelf_infos_req = 'select f_id_shelf from t_shelf ' \
+ 'where f_name = ? collate nocase'
- shelf_infos = cur.execute(shelf_infos_req, (shelf_id_or_name,)).fetchone()
- if shelf_infos == None:
+ shelf_infos = cur.execute(
+ shelf_infos_req, (shelf_id_or_name,)).fetchone()
+ if shelf_infos is None:
print('No such shelf {0}'.format(shelf_id_or_name))
sys.exit(1)
shelf_infos = dict(shelf_infos)
# Now check if book exists
if str(book_id_or_title).isdecimal():
- book_infos_req = 'select f_id_item from t_item where f_id_item = ?'
+ book_infos_req = 'select f_id_item from t_item ' \
+ 'where f_id_item = ?'
else:
- book_infos_req = 'select f_id_item from t_item where f_title = ? collate nocase'
+ book_infos_req = 'select f_id_item from t_item ' \
+ 'where f_title = ? collate nocase'
- book_infos = cur.execute(book_infos_req, (book_id_or_title,)).fetchone()
- if book_infos == None:
+ book_infos = cur.execute(
+ book_infos_req, (book_id_or_title,)).fetchone()
+ if book_infos is None:
print('No such book {0}'.format(book_id_or_title))
sys.exit(1)
book_infos = dict(book_infos)
# Internal requirements
- b_uuid = dict(cur.execute('select f_db_uuid from t_uuiddb limit 1').fetchone())
-
- return shelf_infos['f_id_shelf'], book_infos['f_id_item'], b_uuid['f_db_uuid']
+ b_uuid = dict(
+ cur.execute('select f_db_uuid from t_uuiddb limit 1').fetchone())
+ return shelf_infos['f_id_shelf'], book_infos['f_id_item'], \
+ b_uuid['f_db_uuid']
def add(self, opts=[]):
if len(opts) < 2:
- print('Action must follow the form: add <id_or_title> <shelf_id_or_name>')
+ print('Action must follow the form: add <id_or_title>'
+ ' <shelf_id_or_name>')
sys.exit(1)
cur_shelf, cur_book, b_uuid = self.prepare_add_remove(opts[1], opts[0])
@@ -275,22 +293,22 @@ class Shelf:
print('Successfully added a book to a shelf.')
self.books([cur_shelf])
-
def remove(self, opts=[]):
if len(opts) < 2:
- print('Action must follow the form: remove <id_or_title> <shelf_id_or_name>')
+ print('Action must follow the form: remove <id_or_title>'
+ ' <shelf_id_or_name>')
sys.exit(1)
cur_shelf, cur_book, b_uuid = self.prepare_add_remove(opts[1], opts[0])
cur = self.get_cursor()
cur.execute(
- "delete from t_shelf_link where f_item_id = ? and f_shelf_id = ? and f_db_uuid = ?",
+ "delete from t_shelf_link where f_item_id = ?"
+ " and f_shelf_id = ? and f_db_uuid = ?",
(cur_book, cur_shelf, b_uuid))
print('Successfully removed a book to a shelf.')
self.books([cur_shelf])
-
def edit(self, opts=[]):
if len(opts) < 2:
print('Action must follow the form: edit <id> <new_title>')
@@ -305,7 +323,7 @@ class Shelf:
cur = self.get_cursor()
book_infos = cur.execute(book_infos_req, (opts[0],)).fetchone()
- if book_infos == None:
+ if book_infos is None:
print('No such book {0}'.format(opts[0]))
sys.exit(1)
book_infos = dict(book_infos)
@@ -316,49 +334,55 @@ class Shelf:
print(cur.rowcount)
print('Title successfully updated to {0}.'.format(opts[1]))
-
def remote(self, opts=[]):
if len(opts) == 1 and opts[1] == 'files':
print('Here is your reader content:')
- file_listing_helper(os.path.join(self.reader_mount_point, self.config['remote_library_path']))
+ file_listing_helper(
+ os.path.join(self.reader_mount_point,
+ self.config['remote_library_path']))
else:
self.device()
-
def files(self, opts=[]):
print('Here is your library content:')
file_listing_helper(self.library_path)
-
def device(self, opts=[]):
import xml.etree.ElementTree as ET
- adobe_info = os.path.join(self.reader_mount_point, '.adobe-digital-editions', 'device.xml')
+ adobe_info = os.path.join(self.reader_mount_point,
+ '.adobe-digital-editions',
+ 'device.xml')
adobe_sn = ''
adobe_name = ''
if os.path.isfile(adobe_info):
tree = ET.parse(adobe_info)
- device_info = tree.find('./adept:deviceClass', {'adept': 'http://ns.adobe.com/adept'})
- if device_info != None:
+ device_info = tree.find('./adept:deviceClass',
+ {'adept': 'http://ns.adobe.com/adept'})
+ if device_info is not None:
adobe_name = device_info.text
- adobe_serial = tree.find('./adept:deviceSerial', {'adept': 'http://ns.adobe.com/adept'})
- if adobe_serial != None:
+ adobe_serial = tree.find('./adept:deviceSerial',
+ {'adept': 'http://ns.adobe.com/adept'})
+ if adobe_serial is not None:
adobe_sn = adobe_serial.text
device_name = bookeen_identify()
- if device_name == None:
+ if device_name is None:
device_name = adobe_name
print(device_name)
cur = self.get_cursor()
- b_ver = dict(cur.execute('select f_desc from t_version order by f_versiondate desc limit 1').fetchone())
+ b_ver = dict(cur.execute('select f_desc from t_version order'
+ ' by f_versiondate desc limit 1').fetchone())
print('Internal software: {0}'.format(b_ver['f_desc']))
-
- print('Product ID: {0}'.format(read_file(os.path.join(self.reader_mount_point, 'system', 'product.dat'))))
- print('Device serial: {0}'.format(read_file(os.path.join(self.reader_mount_point, 'system', 'sn.txt'))))
+ p_id = read_file(os.path.join(
+ self.reader_mount_point, 'system', 'product.dat'))
+ print('Product ID: {0}'.format(p_id))
+ p_sn = read_file(os.path.join(
+ self.reader_mount_point, 'system', 'sn.txt'))
+ print('Device serial: {0}'.format(p_sn))
print('Adobe serial: {0}'.format(adobe_sn))
-
def init(self, opts=[]):
library_path = os.path.expanduser(opts[0])
if not os.path.isdir(library_path):
@@ -373,15 +397,16 @@ class Shelf:
reader_db_path = os.path.join(reader_mount_point, 'system', 'library')
if not os.path.isfile(reader_db_path):
- print('We cannot locate your eReader. Please check you have plugged it.')
+ print('We cannot locate your eReader. '
+ 'Please check you have plugged it.')
sys.exit(1)
confparser['DEFAULT']['reader_mount_point'] = reader_mount_point
- remote_physical_path = '/mnt/fat'
- print('Default internal path of your eReader will be /mnt/fat. You are free to change it now or just press return:')
+ print('Default internal path of your eReader will be /mnt/fat. '
+ 'You are free to change it now or just press return:')
int_path = input('?> ')
- if int_path != None and int_path != '':
+ if int_path is not None and int_path != '':
confparser['DEFAULT']['remote_physical_path'] = int_path
else:
confparser['DEFAULT']['remote_physical_path'] = '/mnt/fat'
@@ -397,15 +422,20 @@ if __name__ == "__main__":
plugins_files = []
plugins_path = os.path.join(xdg_config_home, 'eshelf', 'plugins')
if os.path.isdir(plugins_path):
- if not plugins_path in sys.path:
+ if plugins_path not in sys.path:
sys.path.append(plugins_path)
- plugins_files = [fname[:-3] for fname in os.listdir(plugins_path) if fname.endswith(".py")]
+ plugins_files = [fname[:-3] for fname in os.listdir(plugins_path)
+ if fname.endswith(".py")]
predefined_actions += plugins_files
parser = argparse.ArgumentParser(prog='eShelf')
- parser.add_argument('command', help='What to do', choices=predefined_actions)
- parser.add_argument('arguments', nargs='*', help='Supplementary options', default=[])
- parser.add_argument('--path', '-p', default=os.getcwd(), help='Path to your library.')
+ parser.add_argument('command', help='What to do',
+ choices=predefined_actions)
+ parser.add_argument('arguments', nargs='*',
+ help='Supplementary options',
+ default=[])
+ parser.add_argument('--path', '-p', default=os.getcwd(),
+ help='Path to your library.')
args = parser.parse_args()
if args.command == 'help':
@@ -420,7 +450,8 @@ if __name__ == "__main__":
if args.command in plugins_files:
loc_plugin = __import__(args.command)
- the_library = loc_plugin.register(library_path, my_library.get_config())
+ the_library = loc_plugin.register(
+ library_path, my_library.get_config())
if len(args.arguments) == 0:
loc_plugin.show_help()
exit(2)
diff --git a/plugins/backup.py b/plugins/backup.py
index 5c35e3c..2750dbe 100644
--- a/plugins/backup.py
+++ b/plugins/backup.py
@@ -5,6 +5,7 @@ import os
import shutil
from datetime import datetime
+
class Backup:
def __init__(self, library_path, shelf_config):
self.config = shelf_config
@@ -14,8 +15,6 @@ class Backup:
if 'internal_path' in self.config:
self.internal_path = self.config['internal_path']
-
-
def pull(self, opts=[]):
reader_db_path = os.path.join(self.reader_path, 'system', 'library')
restore_path = os.path.join(self.library_path, '.eshelf-archive')
@@ -23,12 +22,15 @@ class Backup:
if not os.path.isdir(restore_path):
os.mkdir(restore_path, mode=0o755)
- backupfile = shutil.copy(reader_db_path, os.path.join(restore_path, datetime.now().strftime('%Y%m%d%H%M%S')))
+ backupfile = shutil.copy(
+ reader_db_path,
+ os.path.join(
+ restore_path, datetime.now().strftime('%Y%m%d%H%M%S')))
if os.path.isfile(backupfile):
print('Library backup saved as {0}'.format(backupfile))
else:
- print('Library do not seem to have been saved. Please investigate.')
-
+ print('Library does not seem to have been saved. '
+ 'Please investigate.')
def push(self, opts=[]):
if len(opts) == 0:
@@ -44,18 +46,19 @@ class Backup:
backupfile = os.path.join(restore_path, opts[0])
if not os.path.isfile(backupfile):
- print('No backup file found for {0}. Please investigate'.format(backupfile))
+ print('No backup file found for {0}. '
+ 'Please investigate'.format(backupfile))
return
if not os.path.isfile(reader_db_path):
- print('Something wrong with the eReader library file. Nothing to do.')
+ print('Something wrong with the eReader library file. '
+ 'Nothing to do.')
return
os.remove(reader_db_path)
shutil.copy(backupfile, reader_db_path)
print('Backup has been restored.')
-
def list(self, opts=[]):
restore_path = os.path.join(self.library_path, '.eshelf-archive')
if not os.path.isdir(restore_path):
@@ -66,7 +69,6 @@ class Backup:
print(' - {0}'.format(backup))
-
def register(library_path, config):
return Backup(library_path, config)