acsm-calibre-plugin/calibre-plugin/prefs.py

116 lines
3.8 KiB
Python
Raw Normal View History

2021-09-19 20:20:56 +06:00
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
2023-02-24 19:11:15 +06:00
'''
Copyright (c) 2021-2023 Leseratte10
This file is part of the ACSM Input Plugin by Leseratte10
ACSM Input Plugin for Calibre / acsm-calibre-plugin
2021-09-19 20:20:56 +06:00
2023-02-24 19:11:15 +06:00
For more information, see:
https://github.com/Leseratte10/acsm-calibre-plugin
'''
2021-09-19 20:20:56 +06:00
# Standard Python modules.
import os
import traceback
from calibre.utils.config import JSONConfig, config_dir # type: ignore
2022-09-05 22:34:40 +06:00
class ACSMInput_Prefs():
2021-09-19 20:20:56 +06:00
def __init__(self):
2022-09-05 22:34:40 +06:00
JSON_PATH_OLD = os.path.join("plugins", "deacsm.json")
JSON_PATH = os.path.join("plugins", "ACSMInput", "ACSMInput.json")
if os.path.exists(os.path.join(config_dir, JSON_PATH_OLD)):
# If the file exists in the old location, use that.
JSON_PATH = JSON_PATH_OLD
2022-09-05 22:34:40 +06:00
2021-09-19 20:20:56 +06:00
self.deacsmprefs = JSONConfig(JSON_PATH)
self.deacsmprefs.defaults['configured'] = False
self.deacsmprefs.defaults['notify_fulfillment'] = True
self.deacsmprefs.defaults['detailed_logging'] = False
self.deacsmprefs.defaults['delete_acsm_after_fulfill'] = False
2022-10-17 17:53:30 +06:00
self.deacsmprefs.defaults['list_of_rented_books'] = []
if self.deacsmprefs['list_of_rented_books'] == []:
self.deacsmprefs['list_of_rented_books'] = []
self.__pluginsdir = os.path.join(config_dir,"plugins")
success = False
for f in ["DeACSM", "ACSMInput"]:
self.__maindir = os.path.join(self.__pluginsdir, f)
self.__accountdir = os.path.join(self.__maindir,"account")
if os.path.exists(self.__accountdir):
self.deacsmprefs.defaults['path_to_account_data'] = self.__accountdir
success = True
break
if not success:
# We did not find an account folder. See if we can create one ...
for f in ["DeACSM", "ACSMInput"]:
self.__maindir = os.path.join(self.__pluginsdir, f)
self.__accountdir = os.path.join(self.__maindir,"account")
if os.path.exists(self.__maindir):
os.mkdir(self.__accountdir)
self.deacsmprefs.defaults['path_to_account_data'] = self.__accountdir
success = True
break
if not success:
raise Exception("Why does the account folder not exist?")
2021-09-19 20:20:56 +06:00
2022-10-17 17:53:30 +06:00
def refresh(self):
return self.deacsmprefs.refresh()
def commit(self):
return self.deacsmprefs.commit()
2022-09-05 22:34:40 +06:00
2021-09-19 20:20:56 +06:00
def __getitem__(self,kind = None):
if kind is not None:
return self.deacsmprefs[kind]
return self.deacsmprefs
def set(self, kind, value):
self.deacsmprefs[kind] = value
def writeprefs(self,value = True):
self.deacsmprefs['configured'] = value
def addnamedvaluetoprefs(self, prefkind, keyname, keyvalue):
try:
if keyvalue not in self.deacsmprefs[prefkind].values():
# ensure that the keyname is unique
# by adding a number (starting with 2) to the name if it is not
namecount = 1
newname = keyname
while newname in self.deacsmprefs[prefkind]:
namecount += 1
newname = "{0:s}_{1:d}".format(keyname,namecount)
# add to the preferences
self.deacsmprefs[prefkind][newname] = keyvalue
return (True, newname)
except:
traceback.print_exc()
pass
return (False, keyname)
def addvaluetoprefs(self, prefkind, prefsvalue):
# ensure the keyvalue isn't already in the preferences
try:
if prefsvalue not in self.deacsmprefs[prefkind]:
self.deacsmprefs[prefkind].append(prefsvalue)
return True
except:
traceback.print_exc()
return False