pep8 style fixes - round 1

This commit is contained in:
Rob Weber 2019-11-25 15:56:59 -06:00
parent 35e05acaf2
commit db18c6a7b4
8 changed files with 54 additions and 29 deletions

View File

@ -4,6 +4,7 @@ import xbmcgui
import resources.lib.utils as utils import resources.lib.utils as utils
from resources.lib.backup import XbmcBackup from resources.lib.backup import XbmcBackup
def get_params(): def get_params():
param = {} param = {}
try: try:
@ -19,6 +20,7 @@ def get_params():
return param return param
# the program mode # the program mode
mode = -1 mode = -1
params = get_params() params = get_params()
@ -30,6 +32,7 @@ if("mode" in params):
elif(params['mode'] == 'restore'): elif(params['mode'] == 'restore'):
mode = 1 mode = 1
# if mode wasn't passed in as arg, get from user # if mode wasn't passed in as arg, get from user
if(mode == -1): if(mode == -1):
# by default, Backup,Restore,Open Settings # by default, Backup,Restore,Open Settings

View File

@ -1,3 +1,4 @@
# launcher for various helpful functions found in the settings.xml area
import sys import sys
import xbmc import xbmc
import xbmcgui import xbmcgui
@ -6,7 +7,6 @@ import resources.lib.utils as utils
from resources.lib.authorizers import DropboxAuthorizer from resources.lib.authorizers import DropboxAuthorizer
from resources.lib.advanced_editor import AdvancedBackupEditor from resources.lib.advanced_editor import AdvancedBackupEditor
# launcher for various helpful functions found in the settings.xml area
def authorize_cloud(cloudProvider): def authorize_cloud(cloudProvider):
# drobpox # drobpox
@ -18,15 +18,17 @@ def authorize_cloud(cloudProvider):
else: else:
xbmcgui.Dialog().ok(utils.getString(30010), utils.getString(30107) + ' ' + utils.getString(30027)) xbmcgui.Dialog().ok(utils.getString(30010), utils.getString(30107) + ' ' + utils.getString(30027))
def remove_auth(): def remove_auth():
# triggered from settings.xml - asks if user wants to delete OAuth token information # triggered from settings.xml - asks if user wants to delete OAuth token information
shouldDelete = xbmcgui.Dialog().yesno(utils.getString(30093), utils.getString(30094), utils.getString(30095), autoclose=7000) shouldDelete = xbmcgui.Dialog().yesno(utils.getString(30093), utils.getString(30094), utils.getString(30095), autoclose=7000)
if(shouldDelete): if(shouldDelete):
# delete any of the known token file types # delete any of the known token file types
xbmcvfs.delete(xbmc.translatePath(utils.data_dir() + "tokens.txt")) # dropbox xbmcvfs.delete(xbmc.translatePath(utils.data_dir() + "tokens.txt")) # dropbox
xbmcvfs.delete(xbmc.translatePath(utils.data_dir() + "google_drive.dat")) # google drive xbmcvfs.delete(xbmc.translatePath(utils.data_dir() + "google_drive.dat")) # google drive
def get_params(): def get_params():
param = {} param = {}
try: try:
@ -34,7 +36,7 @@ def get_params():
args = i args = i
if('=' in args): if('=' in args):
if(args.startswith('?')): if(args.startswith('?')):
args = args[1:] # legacy in case of url params args = args[1:] # legacy in case of url params
splitString = args.split('=') splitString = args.split('=')
param[splitString[0]] = splitString[1] param[splitString[0]] = splitString[1]
except: except:
@ -42,6 +44,7 @@ def get_params():
return param return param
params = get_params() params = get_params()
if(params['action'] == 'authorize_cloud'): if(params['action'] == 'authorize_cloud'):

View File

@ -1,11 +1,12 @@
from . import utils as utils from . import utils as utils
class ZipExtractor: class ZipExtractor:
def extract(self, zipFile, outLoc, progressBar): def extract(self, zipFile, outLoc, progressBar):
utils.log("extracting zip archive") utils.log("extracting zip archive")
result = True # result is true unless we fail result = True # result is true unless we fail
# update the progress bar # update the progress bar
progressBar.updateProgress(0, utils.getString(30100)) progressBar.updateProgress(0, utils.getString(30100))

View File

@ -1,6 +1,7 @@
import xbmcgui import xbmcgui
from . import utils as utils from . import utils as utils
class BackupProgressBar: class BackupProgressBar:
NONE = 2 NONE = 2
DIALOG = 0 DIALOG = 0
@ -31,7 +32,7 @@ class BackupProgressBar:
# update the progress bar # update the progress bar
if(self.mode != self.NONE): if(self.mode != self.NONE):
if(message != None): if(message is not None):
# need different calls for dialog and background bars # need different calls for dialog and background bars
if(self.mode == self.DIALOG): if(self.mode == self.DIALOG):
self.progressBar.update(percent, message) self.progressBar.update(percent, message)

View File

@ -1,7 +1,8 @@
from future.moves.urllib.request import urlopen
# this is duplicated in snipppets of code from all over the web, credit to no one # this is duplicated in snipppets of code from all over the web, credit to no one
# in particular - to all those that have gone before me! # in particular - to all those that have gone before me!
from future.moves.urllib.request import urlopen
def shorten(aUrl): def shorten(aUrl):
tinyurl = 'http://tinyurl.com/api-create.php?url=' tinyurl = 'http://tinyurl.com/api-create.php?url='
req = urlopen(tinyurl + aUrl) req = urlopen(tinyurl + aUrl)

View File

@ -5,30 +5,39 @@ import xbmcaddon
__addon_id__= 'script.xbmcbackup' __addon_id__= 'script.xbmcbackup'
__Addon = xbmcaddon.Addon(__addon_id__) __Addon = xbmcaddon.Addon(__addon_id__)
def data_dir(): def data_dir():
return __Addon.getAddonInfo('profile') return __Addon.getAddonInfo('profile')
def addon_dir(): def addon_dir():
return __Addon.getAddonInfo('path') return __Addon.getAddonInfo('path')
def openSettings(): def openSettings():
__Addon.openSettings() __Addon.openSettings()
def log(message, loglevel=xbmc.LOGDEBUG): def log(message, loglevel=xbmc.LOGDEBUG):
xbmc.log(__addon_id__ + "-" + __Addon.getAddonInfo('version') + ": " + message, level=loglevel) xbmc.log(__addon_id__ + "-" + __Addon.getAddonInfo('version') + ": " + message, level=loglevel)
def showNotification(message): def showNotification(message):
xbmcgui.Dialog().notification(getString(30010), message, time=4000, icon=xbmc.translatePath(__Addon.getAddonInfo('path') + "/resources/images/icon.png")) xbmcgui.Dialog().notification(getString(30010), message, time=4000, icon=xbmc.translatePath(__Addon.getAddonInfo('path') + "/resources/images/icon.png"))
def getSetting(name): def getSetting(name):
return __Addon.getSetting(name) return __Addon.getSetting(name)
def setSetting(name, value): def setSetting(name, value):
__Addon.setSetting(name, value) __Addon.setSetting(name, value)
def getString(string_id): def getString(string_id):
return __Addon.getLocalizedString(string_id) return __Addon.getLocalizedString(string_id)
def getRegionalTimestamp(date_time, dateformat=['dateshort']): def getRegionalTimestamp(date_time, dateformat=['dateshort']):
result = '' result = ''

View File

@ -10,6 +10,7 @@ from . import utils as utils
from .dropbox.files import WriteMode, CommitInfo, UploadSessionCursor from .dropbox.files import WriteMode, CommitInfo, UploadSessionCursor
from . authorizers import DropboxAuthorizer from . authorizers import DropboxAuthorizer
class Vfs: class Vfs:
root_path = None root_path = None
@ -54,6 +55,7 @@ class Vfs:
def cleanup(self): def cleanup(self):
return True return True
class XBMCFileSystem(Vfs): class XBMCFileSystem(Vfs):
def listdir(self, directory): def listdir(self, directory):
@ -77,6 +79,7 @@ class XBMCFileSystem(Vfs):
def exists(self, aFile): def exists(self, aFile):
return xbmcvfs.exists(aFile) return xbmcvfs.exists(aFile)
class ZipFileSystem(Vfs): class ZipFileSystem(Vfs):
zip = None zip = None
@ -115,8 +118,9 @@ class ZipFileSystem(Vfs):
def listFiles(self): def listFiles(self):
return self.zip.infolist() return self.zip.infolist()
class DropboxFileSystem(Vfs): class DropboxFileSystem(Vfs):
MAX_CHUNK = 50 * 1000 * 1000 # dropbox uses 150, reduced to 50 for small mem systems MAX_CHUNK = 50 * 1000 * 1000 # dropbox uses 150, reduced to 50 for small mem systems
client = None client = None
APP_KEY = '' APP_KEY = ''
APP_SECRET = '' APP_SECRET = ''
@ -136,7 +140,7 @@ class DropboxFileSystem(Vfs):
def listdir(self, directory): def listdir(self, directory):
directory = self._fix_slashes(directory) directory = self._fix_slashes(directory)
if(self.client != None and self.exists(directory)): if(self.client is not None and self.exists(directory)):
files = [] files = []
dirs = [] dirs = []
metadata = self.client.files_list_folder(directory) metadata = self.client.files_list_folder(directory)
@ -153,7 +157,7 @@ class DropboxFileSystem(Vfs):
def mkdir(self, directory): def mkdir(self, directory):
directory = self._fix_slashes(directory) directory = self._fix_slashes(directory)
if(self.client != None): if(self.client is not None):
# sort of odd but always return true, folder create is implicit with file upload # sort of odd but always return true, folder create is implicit with file upload
return True return True
else: else:
@ -161,7 +165,7 @@ class DropboxFileSystem(Vfs):
def rmdir(self, directory): def rmdir(self, directory):
directory = self._fix_slashes(directory) directory = self._fix_slashes(directory)
if(self.client != None and self.exists(directory)): if(self.client is not None and self.exists(directory)):
# dropbox is stupid and will refuse to do this sometimes, need to delete recursively # dropbox is stupid and will refuse to do this sometimes, need to delete recursively
dirs, files = self.listdir(directory) dirs, files = self.listdir(directory)
@ -178,7 +182,7 @@ class DropboxFileSystem(Vfs):
def rmfile(self, aFile): def rmfile(self, aFile):
aFile = self._fix_slashes(aFile) aFile = self._fix_slashes(aFile)
if(self.client != None and self.exists(aFile)): if(self.client is not None and self.exists(aFile)):
self.client.files_delete(aFile) self.client.files_delete(aFile)
return True return True
else: else:
@ -187,13 +191,13 @@ class DropboxFileSystem(Vfs):
def exists(self, aFile): def exists(self, aFile):
aFile = self._fix_slashes(aFile) aFile = self._fix_slashes(aFile)
if(self.client != None): if(self.client is not None):
# can't list root metadata # can't list root metadata
if(aFile == ''): if(aFile == ''):
return True return True
try: try:
meta_data = self.client.files_get_metadata(aFile) self.client.files_get_metadata(aFile)
# if we make it here the file does exist # if we make it here the file does exist
return True return True
except: except:
@ -204,7 +208,7 @@ class DropboxFileSystem(Vfs):
def put(self, source, dest, retry=True): def put(self, source, dest, retry=True):
dest = self._fix_slashes(dest) dest = self._fix_slashes(dest)
if(self.client != None): if(self.client is not None):
# open the file and get its size # open the file and get its size
f = open(source, 'rb') f = open(source, 'rb')
f_size = os.path.getsize(source) f_size = os.path.getsize(source)
@ -212,7 +216,7 @@ class DropboxFileSystem(Vfs):
try: try:
if(f_size < self.MAX_CHUNK): if(f_size < self.MAX_CHUNK):
# use the regular upload # use the regular upload
response = self.client.files_upload(f.read(), dest, mode=WriteMode('overwrite')) self.client.files_upload(f.read(), dest, mode=WriteMode('overwrite'))
else: else:
# start the upload session # start the upload session
upload_session = self.client.files_upload_session_start(f.read(self.MAX_CHUNK)) upload_session = self.client.files_upload_session_start(f.read(self.MAX_CHUNK))
@ -228,7 +232,7 @@ class DropboxFileSystem(Vfs):
self.client.files_upload_session_append_v2(f.read(self.MAX_CHUNK), upload_cursor) self.client.files_upload_session_append_v2(f.read(self.MAX_CHUNK), upload_cursor)
upload_cursor.offset = f.tell() upload_cursor.offset = f.tell()
# if no errors we're good! # if no errors we're good!
return True return True
except Exception as anError: except Exception as anError:
utils.log(str(anError)) utils.log(str(anError))
@ -243,9 +247,9 @@ class DropboxFileSystem(Vfs):
return False return False
def get_file(self, source, dest): def get_file(self, source, dest):
if(self.client != None): if(self.client is not None):
# write the file locally # write the file locally
f = self.client.files_download_to_file(dest, source) self.client.files_download_to_file(dest, source)
return True return True
else: else:
return False return False

View File

@ -9,6 +9,7 @@ from resources.lib.backup import XbmcBackup
UPGRADE_INT = 2 # to keep track of any upgrade notifications UPGRADE_INT = 2 # to keep track of any upgrade notifications
class BackupScheduler: class BackupScheduler:
monitor = None monitor = None
enabled = "false" enabled = "false"
@ -17,7 +18,7 @@ class BackupScheduler:
restore_point = None restore_point = None
def __init__(self): def __init__(self):
self.monitor = UpdateMonitor(update_method = self.settingsChanged) self.monitor = UpdateMonitor(update_method=self.settingsChanged)
self.enabled = utils.getSetting("enable_scheduler") self.enabled = utils.getSetting("enable_scheduler")
self.next_run_path = xbmc.translatePath(utils.data_dir()) + 'next_run.txt' self.next_run_path = xbmc.translatePath(utils.data_dir()) + 'next_run.txt'
@ -42,12 +43,12 @@ class BackupScheduler:
if(0 < nr <= time.time() and utils.getSetting('schedule_miss') == 'true'): if(0 < nr <= time.time() and utils.getSetting('schedule_miss') == 'true'):
utils.log("scheduled backup was missed, doing it now...") utils.log("scheduled backup was missed, doing it now...")
progress_mode = int(utils.getSetting('progress_mode')) progress_mode = int(utils.getSetting('progress_mode'))
if(progress_mode == 0): if(progress_mode == 0):
progress_mode = 1 # Kodi just started, don't block it with a foreground progress bar progress_mode = 1 # Kodi just started, don't block it with a foreground progress bar
self.doScheduledBackup(progress_mode) self.doScheduledBackup(progress_mode)
self.setup() self.setup()
def setup(self): def setup(self):
@ -104,7 +105,7 @@ class BackupScheduler:
if(backup.remoteConfigured()): if(backup.remoteConfigured()):
if(int(utils.getSetting('progress_mode')) in [0,1]): if(int(utils.getSetting('progress_mode')) in [0, 1]):
backup.backup(True) backup.backup(True)
else: else:
backup.backup(False) backup.backup(False)
@ -136,7 +137,7 @@ class BackupScheduler:
fh.close() fh.close()
# only show when not in silent mode # only show when not in silent mode
if(progress_mode != 2): if(progress_mode != 2):
utils.showNotification(utils.getString(30081) + " " + utils.getRegionalTimestamp(datetime.fromtimestamp(self.next_run), ['dateshort', 'time'])) utils.showNotification(utils.getString(30081) + " " + utils.getRegionalTimestamp(datetime.fromtimestamp(self.next_run), ['dateshort', 'time']))
def settingsChanged(self): def settingsChanged(self):
@ -184,6 +185,7 @@ class BackupScheduler:
return shouldContinue return shouldContinue
class UpdateMonitor(xbmc.Monitor): class UpdateMonitor(xbmc.Monitor):
update_method = None update_method = None
@ -194,4 +196,5 @@ class UpdateMonitor(xbmc.Monitor):
def onSettingsChanged(self): def onSettingsChanged(self):
self.update_method() self.update_method()
BackupScheduler().start() BackupScheduler().start()