Compare commits
126 Commits
krypton_cu
...
matrix-1.6
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b7587c6170 | ||
|
|
79cddb422c | ||
|
|
8415ec12ba | ||
|
|
a284451640 | ||
|
|
dc8d334352 | ||
|
|
c17a185639 | ||
|
|
aff124af1f | ||
|
|
350d81caf4 | ||
|
|
46d7d22523 | ||
|
|
11c644cb15 | ||
|
|
941b593751 | ||
|
|
e622a0455f | ||
|
|
9c1ecc254f | ||
|
|
44fdf7a20a | ||
|
|
ec214c074f | ||
|
|
02d852a7e9 | ||
|
|
d7e4946d9a | ||
|
|
6fdaa4f253 | ||
|
|
5f1f9fef38 | ||
|
|
4098cd18cb | ||
|
|
8119a09449 | ||
|
|
a0ccd85d9e | ||
|
|
71c8d9ae54 | ||
|
|
b470412b4f | ||
|
|
7a5886cd26 | ||
|
|
f20887b6e7 | ||
|
|
a0eb28a5f6 | ||
|
|
a198c9448a | ||
|
|
fa3a30eb55 | ||
|
|
201d04afeb | ||
|
|
2dabb23c2d | ||
|
|
2f19ec2b75 | ||
|
|
db215873cf | ||
|
|
bd963719d4 | ||
|
|
18b7f338c7 | ||
|
|
92a9245bdc | ||
|
|
568c3758a4 | ||
|
|
af999f7d04 | ||
|
|
1264ab86b2 | ||
|
|
90c458d4fc | ||
|
|
0e6f5acfb5 | ||
|
|
c9dd381037 | ||
|
|
55b2ac83d4 | ||
|
|
0d14dd17c6 | ||
|
|
9fa354b467 | ||
|
|
006485b19e | ||
|
|
3c2f512ecf | ||
|
|
190b4fd86f | ||
|
|
9ecf706d63 | ||
|
|
05c53b7ed8 | ||
|
|
8bc73f2832 | ||
|
|
edc4a7b20f | ||
|
|
90b4aeeebe | ||
|
|
7ce9123e1f | ||
|
|
8bfef6692f | ||
|
|
e63560f0c4 | ||
|
|
51f2ef3973 | ||
|
|
04bac77690 | ||
|
|
b1f6d36d73 | ||
|
|
5d398836ba | ||
|
|
23a14d67c4 | ||
|
|
0eebe1c5cc | ||
|
|
4d55385179 | ||
|
|
ac68001aa1 | ||
|
|
233dff0e15 | ||
|
|
6f69f80742 | ||
|
|
47fcb119f3 | ||
|
|
f9f49e3fe6 | ||
|
|
7c23c17e33 | ||
|
|
710bcd08f8 | ||
|
|
5e2d099448 | ||
|
|
8d66fa6a9f | ||
|
|
5ee610a586 | ||
|
|
8c4465f552 | ||
|
|
3849a902ea | ||
|
|
4492ab593e | ||
|
|
16e13c7d80 | ||
|
|
0cc0684263 | ||
|
|
dd5b99c978 | ||
|
|
6c99667afa | ||
|
|
6514b3db02 | ||
|
|
88341d9e1f | ||
|
|
95649c2b3f | ||
|
|
3e9de429dd | ||
|
|
db18c6a7b4 | ||
|
|
35e05acaf2 | ||
|
|
92ec8bf25c | ||
|
|
0c79aef4e7 | ||
|
|
fea7dca500 | ||
|
|
f7665c8ddd | ||
|
|
bbbfc3dd84 | ||
|
|
0b03914175 | ||
|
|
51553f7720 | ||
|
|
294683fb43 | ||
|
|
b74c1af704 | ||
|
|
edd4002d3f | ||
|
|
3aa912ca4c | ||
|
|
5c3e1712f6 | ||
|
|
82bdc955b5 | ||
|
|
4f1e5060e9 | ||
|
|
7d895a6028 | ||
|
|
7ede17fbbd | ||
|
|
d32620ea18 | ||
|
|
def99767e8 | ||
|
|
c7a9a8512d | ||
|
|
8d07310980 | ||
|
|
048d016e0e | ||
|
|
c50c5245fc | ||
|
|
e91037208b | ||
|
|
c0b0fa82cb | ||
|
|
6ac1d3559b | ||
|
|
d93589ecad | ||
|
|
aa94060cfe | ||
|
|
b9e0424ea5 | ||
|
|
495ecb1048 | ||
|
|
a1c0c0bbfe | ||
|
|
9f570233d9 | ||
|
|
b38aff2a8e | ||
|
|
456ebe9374 | ||
|
|
30f8b93629 | ||
|
|
94f872fb81 | ||
|
|
8f8402ae8a | ||
|
|
db93e40f59 | ||
|
|
72c77fb33a | ||
|
|
1f0e262c5b | ||
|
|
b75487bb2a |
7
.gitattributes
vendored
Normal file
@@ -0,0 +1,7 @@
|
||||
.github/ export-ignore
|
||||
.settings/ export-ignore
|
||||
.gitattributes export-ignore
|
||||
.gitignore export-ignore
|
||||
.travis.yml export-ignore
|
||||
README.md export-ignore
|
||||
changelog.md export-ignore
|
||||
2
.settings/org.eclipse.core.resources.prefs
Normal file
@@ -0,0 +1,2 @@
|
||||
eclipse.preferences.version=1
|
||||
encoding//resources/lib/croniter.py=utf-8
|
||||
11
.travis.yml
@@ -3,11 +3,18 @@ language: python
|
||||
python: 3.7
|
||||
|
||||
install:
|
||||
- pip install kodi-addon-checker
|
||||
- pip install flake8 flake8_polyfill kodi-addon-checker git+https://github.com/romanvm/kodi-addon-submitter.git
|
||||
|
||||
before_script:
|
||||
- git config core.quotepath false
|
||||
|
||||
# command to run our tests
|
||||
script:
|
||||
- kodi-addon-checker --branch=krypton --allow-folder-id-mismatch
|
||||
- flake8 ./ --statistics --show-source --builtins=sys --ignore=E501,E722 --exclude=croniter.py # check python structure against flake8 tests, ignore long lines
|
||||
- kodi-addon-checker --branch=matrix --allow-folder-id-mismatch
|
||||
|
||||
deploy:
|
||||
- provider: script
|
||||
script: submit-addon -r repo-scripts -b matrix --push-branch script.xbmcbackup
|
||||
on:
|
||||
tags: true
|
||||
@@ -1,5 +1,5 @@
|
||||
# Backup Addon
|
||||
 [](https://travis-ci.org/robweber/xbmcbackup) [](https://github.com/robweber/xbmcbackup/blob/master/LICENSE.txt)
|
||||
  [](https://travis-ci.com/robweber/xbmcbackup) [](https://github.com/robweber/xbmcbackup/blob/master/LICENSE.txt) [](https://www.python.org/dev/peps/pep-0008/)
|
||||
|
||||
## About
|
||||
|
||||
|
||||
34
addon.xml
@@ -1,20 +1,16 @@
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
|
||||
<addon id="script.xbmcbackup"
|
||||
name="Backup" version="1.5.2" provider-name="robweber">
|
||||
name="Backup" version="1.6.6" provider-name="robweber">
|
||||
<requires>
|
||||
<!-- jarvis -->
|
||||
<import addon="xbmc.python" version="2.25.0"/>
|
||||
<import addon="script.module.httplib2" version="0.8.0" />
|
||||
<import addon="script.module.oauth2client" version="4.1.2" />
|
||||
<import addon="script.module.uritemplate" version="0.6" />
|
||||
<import addon="script.module.yaml" version="3.11"/>
|
||||
<import addon="script.module.googleapi" version="1.6.4" />
|
||||
<import addon="script.module.dropbox" version="8.4.2"/>
|
||||
<import addon="xbmc.python" version="3.0.0"/>
|
||||
<import addon="script.module.dateutil" version="2.8.0" />
|
||||
<import addon="script.module.future" version="0.18.2+matrix.1" />
|
||||
<import addon="script.module.dropbox" version="9.4.0" />
|
||||
</requires>
|
||||
<extension point="xbmc.python.script" library="default.py">
|
||||
<provides>executable</provides>
|
||||
</extension>
|
||||
<extension point="xbmc.service" library="scheduler.py" start="startup" />
|
||||
<extension point="xbmc.service" library="service.py" />
|
||||
<extension point="xbmc.addon.metadata">
|
||||
<summary lang="ar_SA">إنسخ إحتياطياً قاعده بيانات إكس بى إم سى وملفات اﻹعدادات فى حاله وقوع إنهيار مع إمكانيه اﻹسترجاع</summary>
|
||||
<summary lang="be_BY">Backup and restore your Kodi database and configuration files in the event of a crash or file corruption.</summary>
|
||||
@@ -82,23 +78,19 @@
|
||||
<description lang="sk_SK">Už ste niekedy poškodili konfiguráciu Kodi a priali si mať zálohu? Teraz môžete - na jeden klik. Môžete exportovať Vašu databázu, playlist, náhľady, doplnky a konfigurácie na ktorýkoľvek zdroj zapisovateľný Kodi. Zálohy môžu byť púšťané na požiadanie alebo plánovačom. </description>
|
||||
<description lang="sv_SE">Har du någonsin tappat bort din Kodi konfiguration och önskat att du hade en backup? Nu kan du enkelt med ett klick. Du kan exportera din databas, spellista, minityrer, tillägg och andra konfigurationsdetaljer till valfri källa som är skrivbar för Kodi. Backupper kan köras på begäran eller via scheman.</description>
|
||||
<description lang="zh_CN">你是否经常折腾你的 Kodi,因而希望能够有个备份?你可以把资料库、播放列表、缩略图、插件和其他配置细节导出到 Kodi 可以写入的任意位置。备份可以按需运行或通过计划任务执行。</description>
|
||||
<language></language>
|
||||
<platform>all</platform>
|
||||
<license>The MIT License</license>
|
||||
<license>MIT</license>
|
||||
<forum>https://forum.kodi.tv/showthread.php?tid=129499</forum>
|
||||
<source>https://github.com/robweber/xbmcbackup</source>
|
||||
<email></email>
|
||||
<assets>
|
||||
<icon>resources/images/icon.png</icon>
|
||||
<screenshot>resources/images/screenshot1.png</screenshot>
|
||||
<screenshot>resources/images/screenshot2.png</screenshot>
|
||||
<screenshot>resources/images/screenshot3.png</screenshot>
|
||||
<screenshot>resources/images/screenshot4.png</screenshot>
|
||||
<screenshot>resources/images/screenshot1.jpg</screenshot>
|
||||
<screenshot>resources/images/screenshot2.jpg</screenshot>
|
||||
<screenshot>resources/images/screenshot3.jpg</screenshot>
|
||||
<screenshot>resources/images/screenshot4.jpg</screenshot>
|
||||
</assets>
|
||||
<news>Version 1.5.2
|
||||
- Added script.module.dropbox import as a dependency for Dropbox filesystem
|
||||
- Fixed issue getting xbmcbackup.val file from non-zipped remote directories. Was being copied as though it was a local file so it was failing.
|
||||
- Use linux path separator (/) all the time, Kodi will interpret this correctly on windows. Was causing issues with remote file systems since os.path.sep
|
||||
<news>Version 1.6.6
|
||||
- fixed issue with backup rotations not working properly
|
||||
</news>
|
||||
</extension>
|
||||
</addon>
|
||||
|
||||
96
changelog.md
@@ -4,11 +4,105 @@ All notable changes to this project will be documented in this file.
|
||||
|
||||
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
|
||||
|
||||
## [Unreleased](https://github.com/robweber/xbmcbackup/compare/krypton-1.5.2...HEAD)
|
||||
## [Version 1.6.6](https://github.com/robweber/xbmcbackup/compare/matrix-1.6.5...robweber:matrix-1.6.6)
|
||||
|
||||
### Fixed
|
||||
|
||||
- error when typing the remote path, ```listBackups()``` function was not working if final slash not included in typed directory path name.
|
||||
- added ```force=True``` flag to the ```rmdir()``` function. Fixes issue with directories being removed when not empty
|
||||
|
||||
## [Version 1.6.5](https://github.com/robweber/xbmcbackup/compare/matrix-1.6.4...robweber:matrix-1.6.5) - 2021-03-06
|
||||
|
||||
### Added
|
||||
|
||||
- added Expert setting to change location of zip file temp location as it's being built or extracted
|
||||
|
||||
### Changed
|
||||
|
||||
- updated ```settings.xml``` to match new [Kodi settings syntax](https://kodi.wiki/view/Add-on_settings_conversion), including visibility levels
|
||||
|
||||
### Fixed
|
||||
|
||||
- when restoring from a zip file the command to delete the extracted directory was incorrect
|
||||
- ```Dialog().yesno()``` no longer takes line1 arg, changed to message
|
||||
|
||||
## [Version 1.6.4](https://github.com/robweber/xbmcbackup/compare/matrix-1.6.3...robweber:matrix-1.6.4) - 2020-12-23
|
||||
|
||||
### Added
|
||||
|
||||
- merged duplicate copy code into ```_copyFile``` method
|
||||
- added method to backup/restore Kodi settings via the GetSettings/SetSettingValue JSON methods in the validation file
|
||||
- added setting to always restore settings or prompt at the time of backup
|
||||
|
||||
### Changed
|
||||
|
||||
- updated script.module.future version to current
|
||||
- swapped xbmc.translatePath for xbmcvfs.translatePath, deprecated
|
||||
|
||||
### Fixed
|
||||
|
||||
- fixed calls to ```xbmcgui.Dialog().ok()```, method definition changed to only allow one message arg with Kodi 19
|
||||
- fixed import of dropbox Oauth package in authorizer flow
|
||||
|
||||
### Removed
|
||||
|
||||
- removed old xml GuiSettings parsing for settings restore
|
||||
|
||||
## [Version 1.6.3](https://github.com/robweber/xbmcbackup/compare/matrix-1.6.2...robweber:matrix-1.6.3) - 2020-06-15
|
||||
|
||||
### Changed
|
||||
|
||||
- fixed validatePath error (issue #166) thanks (thanks @AnonTester)
|
||||
|
||||
## [Version 1.6.2](https://github.com/robweber/xbmcbackup/compare/matrix-1.6.1...robweber:matrix-1.6.2) - 2019-04-09
|
||||
|
||||
### Changed
|
||||
|
||||
- changed PNG screenshots to JPG (per [#165](https://github.com/robweber/xbmcbackup/issues/165))
|
||||
|
||||
## [Version 1.6.1](https://github.com/robweber/xbmcbackup/compare/matrix-1.6.0...robweber:matrix-1.6.1) - 2019-12-30
|
||||
|
||||
### Added
|
||||
|
||||
- added method to get size of a file from the VFS
|
||||
- added total transfer size information to progress bar with appropriate precision (KB, MB, etc)
|
||||
- show file size of zip files in the restore selection dialog
|
||||
- added getSettingInt and getSettingBool to utils.py class
|
||||
- added verbose logging setting and tied it to logging related to file paths added/written, this will significantly reduce the debug log size (thanks CastagnaIT)
|
||||
- localize advanced editor strings instead of hard coding English
|
||||
|
||||
### Changed
|
||||
|
||||
- display every file transfered in progress bar, not just directory
|
||||
- base progress bar percent on transfer size, not total files
|
||||
- changed getSettings where needed to getSettingBool and getSettingInt
|
||||
- use service.py to start scheduler, moving scheduler to resources/lib/scheduler.py Kodi doesn't cache files in the root directory
|
||||
- fixed issues with rotating backups where trailing slash was missing (thanks @AnonTester)
|
||||
- read/write files using contextlib
|
||||
|
||||
## [Version 1.6.0](https://github.com/robweber/xbmcbackup/compare/krypton-1.5.2...robweber:matrix-1.6.0) - 2019-11-26
|
||||
|
||||
### Added
|
||||
|
||||
- added new badges for Kodi Version, TravisCI and license information from shields.io
|
||||
- dependency on script.module.dateutil for relativedelta.py class
|
||||
|
||||
### Changed
|
||||
|
||||
- addon.xml updated to use Leia specific syntax and library imports
|
||||
- removed specific encode() calls per Python2/3 compatibility
|
||||
- call isdigit() method on the string directly instead of str.isdigit() (results in unicode error)
|
||||
- added flake8 testing to travis-ci
|
||||
- updated code to make python3 compatible
|
||||
- updated code for pep9 styling
|
||||
- use setArt() to set ListItem icons as the icon= constructor is deprecated
|
||||
- Dropbox dependency is now 9.4.0
|
||||
|
||||
### Removed
|
||||
|
||||
- removed need for urlparse library
|
||||
- Removed GoogleDrive support - issues with python 3 compatibility
|
||||
- removed relativedelta.py, use the dateutil module for this
|
||||
|
||||
## [Version 1.5.2](https://github.com/robweber/xbmcbackup/compare/krypton-1.5.1...robweber:krypton-1.5.2) - 2019-09-30
|
||||
|
||||
|
||||
22
default.py
@@ -1,20 +1,25 @@
|
||||
import sys, urlparse
|
||||
import xbmc, xbmcgui
|
||||
import xbmc
|
||||
import xbmcgui
|
||||
import resources.lib.utils as utils
|
||||
from resources.lib.backup import XbmcBackup
|
||||
|
||||
|
||||
def get_params():
|
||||
param = {}
|
||||
|
||||
if(len(sys.argv) > 1):
|
||||
try:
|
||||
for i in sys.argv:
|
||||
args = i
|
||||
if('=' in args):
|
||||
if(args.startswith('?')):
|
||||
args = args[1:]
|
||||
param.update(dict(urlparse.parse_qsl(args)))
|
||||
args = args[1:] # legacy in case of url params
|
||||
splitString = args.split('=')
|
||||
param[splitString[0]] = splitString[1]
|
||||
except:
|
||||
pass
|
||||
|
||||
return param
|
||||
|
||||
|
||||
# the program mode
|
||||
mode = -1
|
||||
params = get_params()
|
||||
@@ -26,13 +31,14 @@ if("mode" in params):
|
||||
elif(params['mode'] == 'restore'):
|
||||
mode = 1
|
||||
|
||||
|
||||
# if mode wasn't passed in as arg, get from user
|
||||
if(mode == -1):
|
||||
# by default, Backup,Restore,Open Settings
|
||||
options = [utils.getString(30016), utils.getString(30017), utils.getString(30099)]
|
||||
|
||||
# find out if we're using the advanced editor
|
||||
if(int(utils.getSetting('backup_selection_type')) == 1):
|
||||
if(utils.getSettingInt('backup_selection_type') == 1):
|
||||
options.append(utils.getString(30125))
|
||||
|
||||
# figure out if this is a backup or a restore from the user
|
||||
@@ -46,7 +52,7 @@ if(mode != -1):
|
||||
if(mode == 2):
|
||||
# open the settings dialog
|
||||
utils.openSettings()
|
||||
elif(mode == 3 and int(utils.getSetting('backup_selection_type')) == 1):
|
||||
elif(mode == 3 and utils.getSettingInt('backup_selection_type') == 1):
|
||||
# open the advanced editor
|
||||
xbmc.executebuiltin('RunScript(special://home/addons/script.xbmcbackup/launcher.py, action=advanced_editor)')
|
||||
elif(backup.remoteConfigured()):
|
||||
|
||||
32
launcher.py
@@ -1,33 +1,22 @@
|
||||
# launcher for various helpful functions found in the settings.xml area
|
||||
import sys
|
||||
import urlparse
|
||||
import xbmc
|
||||
import xbmcgui
|
||||
import xbmcvfs
|
||||
import resources.lib.utils as utils
|
||||
from resources.lib.authorizers import DropboxAuthorizer,GoogleDriveAuthorizer
|
||||
from resources.lib.authorizers import DropboxAuthorizer
|
||||
from resources.lib.advanced_editor import AdvancedBackupEditor
|
||||
|
||||
|
||||
#launcher for various helpful functions found in the settings.xml area
|
||||
|
||||
def authorize_cloud(cloudProvider):
|
||||
# drobpox
|
||||
if(cloudProvider == 'dropbox'):
|
||||
authorizer = DropboxAuthorizer()
|
||||
|
||||
if(authorizer.authorize()):
|
||||
xbmcgui.Dialog().ok(utils.getString(30010),utils.getString(30027) + ' ' + utils.getString(30106))
|
||||
xbmcgui.Dialog().ok(utils.getString(30010), '%s %s' % (utils.getString(30027), utils.getString(30106)))
|
||||
else:
|
||||
xbmcgui.Dialog().ok(utils.getString(30010),utils.getString(30107) + ' ' + utils.getString(30027))
|
||||
xbmcgui.Dialog().ok(utils.getString(30010), '%s %s' % (utils.getString(30107), utils.getString(30027)))
|
||||
|
||||
#google drive
|
||||
elif(cloudProvider == 'google_drive'):
|
||||
authorizer = GoogleDriveAuthorizer()
|
||||
|
||||
if(authorizer.authorize()):
|
||||
xbmcgui.Dialog().ok("Backup",utils.getString(30098) + ' ' + utils.getString(30106))
|
||||
else:
|
||||
xbmcgui.Dialog().ok("Backup",utils.getString(30107) + ' ' + utils.getString(30098))
|
||||
|
||||
def remove_auth():
|
||||
# triggered from settings.xml - asks if user wants to delete OAuth token information
|
||||
@@ -35,21 +24,26 @@ def remove_auth():
|
||||
|
||||
if(shouldDelete):
|
||||
# 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() + "google_drive.dat")) #google drive
|
||||
xbmcvfs.delete(xbmcvfs.translatePath(utils.data_dir() + "tokens.txt")) # dropbox
|
||||
xbmcvfs.delete(xbmcvfs.translatePath(utils.data_dir() + "google_drive.dat")) # google drive
|
||||
|
||||
|
||||
def get_params():
|
||||
param = {}
|
||||
try:
|
||||
for i in sys.argv:
|
||||
args = i
|
||||
if('=' in args):
|
||||
if(args.startswith('?')):
|
||||
args = args[1:]
|
||||
param.update(dict(urlparse.parse_qsl(args)))
|
||||
args = args[1:] # legacy in case of url params
|
||||
splitString = args.split('=')
|
||||
param[splitString[0]] = splitString[1]
|
||||
except:
|
||||
pass
|
||||
|
||||
return param
|
||||
|
||||
|
||||
params = get_params()
|
||||
|
||||
if(params['action'] == 'authorize_cloud'):
|
||||
|
||||
2
resources/.gitignore
vendored
@@ -1,2 +0,0 @@
|
||||
|
||||
*.pyo
|
||||
|
Before Width: | Height: | Size: 125 KiB |
BIN
resources/images/screenshot1.jpg
Normal file
|
After Width: | Height: | Size: 100 KiB |
|
Before Width: | Height: | Size: 125 KiB |
|
Before Width: | Height: | Size: 129 KiB |
BIN
resources/images/screenshot2.jpg
Normal file
|
After Width: | Height: | Size: 96 KiB |
|
Before Width: | Height: | Size: 129 KiB |
|
Before Width: | Height: | Size: 270 KiB |
BIN
resources/images/screenshot3.jpg
Normal file
|
After Width: | Height: | Size: 135 KiB |
|
Before Width: | Height: | Size: 270 KiB |
|
Before Width: | Height: | Size: 150 KiB |
BIN
resources/images/screenshot4.jpg
Normal file
|
After Width: | Height: | Size: 95 KiB |
|
Before Width: | Height: | Size: 150 KiB |
BIN
resources/images/screenshot5.jpg
Normal file
|
After Width: | Height: | Size: 96 KiB |
|
Before Width: | Height: | Size: 102 KiB |
BIN
resources/images/screenshot6.jpg
Normal file
|
After Width: | Height: | Size: 103 KiB |
|
Before Width: | Height: | Size: 76 KiB |
@@ -173,8 +173,8 @@ msgid "Would you like to continue?"
|
||||
msgstr "Would you like to continue?"
|
||||
|
||||
msgctxt "#30045"
|
||||
msgid "Error: Remote path doesn't exist"
|
||||
msgstr "Error: Remote path doesn't exist"
|
||||
msgid "Error: Remote or zip file path doesn't exist"
|
||||
msgstr "Error: Remote or zip file path doesn't exist"
|
||||
|
||||
msgctxt "#30046"
|
||||
msgid "Starting"
|
||||
@@ -556,3 +556,71 @@ msgstr ""
|
||||
msgctxt "#30141"
|
||||
msgid "This will erase any current Advanced Editor settings"
|
||||
msgstr ""
|
||||
|
||||
msgctxt "#30142"
|
||||
msgid "Enable Verbose Logging"
|
||||
msgstr ""
|
||||
|
||||
msgctxt "#30143"
|
||||
msgid "Exclude a specific folder from this backup set"
|
||||
msgstr ""
|
||||
|
||||
msgctxt "#30144"
|
||||
msgid "Include a specific folder to this backup set"
|
||||
msgstr ""
|
||||
|
||||
msgctxt "#30145"
|
||||
msgid "Type"
|
||||
msgstr ""
|
||||
|
||||
msgctxt "#30146"
|
||||
msgid "Include Sub Folders"
|
||||
msgstr ""
|
||||
|
||||
msgctxt "#30147"
|
||||
msgid "Toggle Sub Folders"
|
||||
msgstr ""
|
||||
|
||||
msgctxt "#30148"
|
||||
msgid "Ask before restoring Kodi UI settings"
|
||||
msgstr ""
|
||||
|
||||
msgctxt "#30149"
|
||||
msgid "Restore Kodi UI Settings"
|
||||
msgstr ""
|
||||
|
||||
msgctxt "#30150"
|
||||
msgid "Restore saved Kodi system settings from backup?"
|
||||
msgstr ""
|
||||
|
||||
msgctxt "#30151"
|
||||
msgid "Enable Verbose Logging"
|
||||
msgstr ""
|
||||
|
||||
msgctxt "#30152"
|
||||
msgid "Set Zip File Location"
|
||||
msgstr ""
|
||||
|
||||
msgctxt "#30153"
|
||||
msgid "Full path to where the zip file will be staged during backup or restore - must be local to this device"
|
||||
msgstr ""
|
||||
|
||||
msgctxt "#30154"
|
||||
msgid "Always prompt if Kodi settings should be restored - no by default"
|
||||
msgstr ""
|
||||
|
||||
msgctxt "#30155"
|
||||
msgid "Adds additional information to the log file"
|
||||
msgstr ""
|
||||
|
||||
msgctxt "#30156"
|
||||
msgid "Must save key/secret first, then return to settings to authorize"
|
||||
msgstr ""
|
||||
|
||||
msgctxt "#30157"
|
||||
msgid "Simple uses pre-defined folder locations, use Advanced Editor to define custom paths"
|
||||
msgstr ""
|
||||
|
||||
msgctxt "#30158"
|
||||
msgid "Run backup on daily, weekly, monthly, or custom schedule"
|
||||
msgstr ""
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
import json
|
||||
import xbmcvfs
|
||||
import xbmc
|
||||
import xbmcgui
|
||||
import xbmcvfs
|
||||
import os.path
|
||||
from . import utils as utils
|
||||
|
||||
|
||||
class BackupSetManager:
|
||||
jsonFile = xbmc.translatePath(utils.data_dir() + "custom_paths.json")
|
||||
jsonFile = xbmcvfs.translatePath(utils.data_dir() + "custom_paths.json")
|
||||
paths = None
|
||||
|
||||
def __init__(self):
|
||||
@@ -44,7 +45,7 @@ class BackupSetManager:
|
||||
return keys
|
||||
|
||||
def getSet(self, index):
|
||||
keys = self.getSets();
|
||||
keys = self.getSets()
|
||||
|
||||
# return the set at this index
|
||||
return {'name': keys[index], 'set': self.paths[keys[index]]}
|
||||
@@ -72,6 +73,7 @@ class BackupSetManager:
|
||||
# write a blank file
|
||||
self._writeFile()
|
||||
|
||||
|
||||
class AdvancedBackupEditor:
|
||||
dialog = None
|
||||
|
||||
@@ -89,10 +91,10 @@ class AdvancedBackupEditor:
|
||||
|
||||
name = self.dialog.input(utils.getString(30110), defaultt='Backup Set')
|
||||
|
||||
if(name != None):
|
||||
if(name is not None):
|
||||
|
||||
# give a choice to start in home or enter a root path
|
||||
enterHome = self.dialog.yesno(utils.getString(30111),line1=utils.getString(30112) + " - " + utils.getString(30114),line2=utils.getString(30113) + " - " + utils.getString(30115),nolabel=utils.getString(30112),yeslabel=utils.getString(30113))
|
||||
enterHome = self.dialog.yesno(utils.getString(30111), message=utils.getString(30112) + " - " + utils.getString(30114) + "\n" + utils.getString(30113) + " - " + utils.getString(30115), nolabel=utils.getString(30112), yeslabel=utils.getString(30113))
|
||||
|
||||
rootFolder = 'special://home'
|
||||
if(enterHome):
|
||||
@@ -103,7 +105,7 @@ class AdvancedBackupEditor:
|
||||
rootFolder = rootFolder + '/'
|
||||
|
||||
# check that this path even exists
|
||||
if(not xbmcvfs.exists(xbmc.translatePath(rootFolder))):
|
||||
if(not xbmcvfs.exists(xbmcvfs.translatePath(rootFolder))):
|
||||
self.dialog.ok(utils.getString(30117), utils.getString(30118), rootFolder)
|
||||
return None
|
||||
else:
|
||||
@@ -117,15 +119,15 @@ class AdvancedBackupEditor:
|
||||
def editSet(self, name, backupSet):
|
||||
optionSelected = ''
|
||||
rootPath = backupSet['root']
|
||||
utils.log(rootPath)
|
||||
|
||||
while(optionSelected != -1):
|
||||
options = [xbmcgui.ListItem(utils.getString(30120),"Exclude a specific folder from this backup set"),xbmcgui.ListItem(utils.getString(30135),"Include a specific folder to this backup set"),xbmcgui.ListItem(rootPath,utils.getString(30121))]
|
||||
options = [xbmcgui.ListItem(utils.getString(30120), utils.getString(30143)), xbmcgui.ListItem(utils.getString(30135), utils.getString(30144)), xbmcgui.ListItem(rootPath, utils.getString(30121))]
|
||||
|
||||
for aDir in backupSet['dirs']:
|
||||
if(aDir['type'] == 'exclude'):
|
||||
options.append(xbmcgui.ListItem(self._cleanPath(rootPath,aDir['path']),"%s: %s" % ("Type",utils.getString(30129))))
|
||||
options.append(xbmcgui.ListItem(self._cleanPath(rootPath, aDir['path']), "%s: %s" % (utils.getString(30145), utils.getString(30129))))
|
||||
elif(aDir['type'] == 'include'):
|
||||
options.append(xbmcgui.ListItem(self._cleanPath(rootPath,aDir['path']),"%s: %s | %s: %s" % ("Type",utils.getString(30134),"Include Sub Folders",str(aDir['recurse']))))
|
||||
options.append(xbmcgui.ListItem(self._cleanPath(rootPath, aDir['path']), "%s: %s | %s: %s" % (utils.getString(30145), utils.getString(30134), utils.getString(30146), str(aDir['recurse']))))
|
||||
|
||||
optionSelected = self.dialog.select(utils.getString(30122) + ' ' + name, options, useDetails=True)
|
||||
|
||||
@@ -154,12 +156,12 @@ class AdvancedBackupEditor:
|
||||
|
||||
cOptions = ['Delete']
|
||||
if(backupSet['dirs'][optionSelected - 3]['type'] == 'include'):
|
||||
cOptions.append('Toggle Sub Folders')
|
||||
cOptions.append(utils.getString(30147))
|
||||
|
||||
contextOption = self.dialog.contextmenu(cOptions)
|
||||
|
||||
if(contextOption == 0):
|
||||
if(self.dialog.yesno(heading=utils.getString(30123),line1=utils.getString(30128))):
|
||||
if(self.dialog.yesno(heading=utils.getString(30123), message=utils.getString(30128))):
|
||||
# remove folder
|
||||
del backupSet['dirs'][optionSelected - 3]
|
||||
elif(contextOption == 1 and backupSet['dirs'][optionSelected - 3]['type'] == 'include'):
|
||||
@@ -168,7 +170,6 @@ class AdvancedBackupEditor:
|
||||
|
||||
return backupSet
|
||||
|
||||
|
||||
def showMainScreen(self):
|
||||
exitCondition = ""
|
||||
customPaths = BackupSetManager()
|
||||
@@ -178,11 +179,16 @@ class AdvancedBackupEditor:
|
||||
|
||||
while(exitCondition != -1):
|
||||
# load the custom paths
|
||||
options = [xbmcgui.ListItem(utils.getString(30126),'',utils.addon_dir() + '/resources/images/plus-icon.png')]
|
||||
listItem = xbmcgui.ListItem(utils.getString(30126), '')
|
||||
listItem.setArt({'icon': os.path.join(utils.addon_dir(), 'resources', 'images', 'plus-icon.png')})
|
||||
options = [listItem]
|
||||
|
||||
for index in range(0, len(customPaths.getSets())):
|
||||
aSet = customPaths.getSet(index)
|
||||
options.append(xbmcgui.ListItem(aSet['name'],utils.getString(30121) + ': ' + aSet['set']['root'],utils.addon_dir() + '/resources/images/folder-icon.png'))
|
||||
|
||||
listItem = xbmcgui.ListItem(aSet['name'], utils.getString(30121) + ': ' + aSet['set']['root'])
|
||||
listItem.setArt({'icon': os.path.join(utils.addon_dir(), 'resources', 'images', 'folder-icon.png')})
|
||||
options.append(listItem)
|
||||
|
||||
# show the gui
|
||||
exitCondition = self.dialog.select(utils.getString(30125), options, useDetails=True)
|
||||
@@ -211,7 +217,7 @@ class AdvancedBackupEditor:
|
||||
customPaths.updateSet(aSet['name'], updatedSet)
|
||||
|
||||
elif(menuOption == 1):
|
||||
if(self.dialog.yesno(heading=utils.getString(30127),line1=utils.getString(30128))):
|
||||
if(self.dialog.yesno(heading=utils.getString(30127), message=utils.getString(30128))):
|
||||
# delete this path - subtract one because of "add" item
|
||||
customPaths.deleteSet(exitCondition - 1)
|
||||
|
||||
@@ -220,10 +226,7 @@ class AdvancedBackupEditor:
|
||||
shouldContinue = self.dialog.yesno(utils.getString(30139), utils.getString(30140), utils.getString(30141))
|
||||
|
||||
if(shouldContinue):
|
||||
source = xbmc.translatePath(utils.addon_dir() + "/resources/data/default_files.json")
|
||||
dest = xbmc.translatePath(utils.data_dir() + "/custom_paths.json")
|
||||
source = xbmcvfs.translatePath(os.path.join(utils.addon_dir(), 'resources', 'data', 'default_files.json'))
|
||||
dest = xbmcvfs.translatePath(os.path.join(utils.data_dir(), 'custom_paths.json'))
|
||||
|
||||
xbmcvfs.copy(source, dest)
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
import xbmc
|
||||
import xbmcgui
|
||||
import xbmcvfs
|
||||
import resources.lib.tinyurl as tinyurl
|
||||
@@ -6,15 +5,11 @@ import resources.lib.utils as utils
|
||||
|
||||
# don't die on import error yet, these might not even get used
|
||||
try:
|
||||
import dropbox
|
||||
from dropbox import dropbox
|
||||
from dropbox import oauth
|
||||
except ImportError:
|
||||
pass
|
||||
|
||||
try:
|
||||
from resources.lib.pydrive.auth import GoogleAuth
|
||||
from resources.lib.pydrive.drive import GoogleDrive
|
||||
except ImportError:
|
||||
pass
|
||||
|
||||
class DropboxAuthorizer:
|
||||
APP_KEY = ""
|
||||
@@ -29,7 +24,7 @@ class DropboxAuthorizer:
|
||||
|
||||
if(self.APP_KEY == '' and self.APP_SECRET == ''):
|
||||
# we can't go any farther, need these for sure
|
||||
xbmcgui.Dialog().ok(utils.getString(30010),utils.getString(30027) + ' ' + utils.getString(30058),utils.getString(30059))
|
||||
xbmcgui.Dialog().ok(utils.getString(30010), '%s %s\n%s' % (utils.getString(30027), utils.getString(30058), utils.getString(30059)))
|
||||
|
||||
result = False
|
||||
|
||||
@@ -51,13 +46,13 @@ class DropboxAuthorizer:
|
||||
self._deleteToken()
|
||||
|
||||
# copied flow from http://dropbox-sdk-python.readthedocs.io/en/latest/moduledoc.html#dropbox.oauth.DropboxOAuth2FlowNoRedirect
|
||||
flow = dropbox.oauth.DropboxOAuth2FlowNoRedirect(self.APP_KEY,self.APP_SECRET)
|
||||
flow = oauth.DropboxOAuth2FlowNoRedirect(self.APP_KEY, self.APP_SECRET)
|
||||
|
||||
url = flow.start()
|
||||
|
||||
# print url in log
|
||||
utils.log("Authorize URL: " + url)
|
||||
xbmcgui.Dialog().ok(utils.getString(30010),utils.getString(30056),utils.getString(30057),tinyurl.shorten(url))
|
||||
xbmcgui.Dialog().ok(utils.getString(30010), '%s\n%s\n%s' % (utils.getString(30056), utils.getString(30057), str(tinyurl.shorten(url), 'utf-8')))
|
||||
|
||||
# get the auth code
|
||||
code = xbmcgui.Dialog().input(utils.getString(30027) + ' ' + utils.getString(30103))
|
||||
@@ -71,7 +66,7 @@ class DropboxAuthorizer:
|
||||
utils.log("Error: %s" % (e,))
|
||||
result = False
|
||||
|
||||
return result;
|
||||
return result
|
||||
|
||||
# return the DropboxClient, or None if can't be created
|
||||
def getClient(self):
|
||||
@@ -94,14 +89,14 @@ class DropboxAuthorizer:
|
||||
|
||||
def _setToken(self, token):
|
||||
# write the token files
|
||||
token_file = open(xbmc.translatePath(utils.data_dir() + "tokens.txt"),'w')
|
||||
token_file = open(xbmcvfs.translatePath(utils.data_dir() + "tokens.txt"), 'w')
|
||||
token_file.write(token)
|
||||
token_file.close()
|
||||
|
||||
def _getToken(self):
|
||||
# get token, if it exists
|
||||
if(xbmcvfs.exists(xbmc.translatePath(utils.data_dir() + "tokens.txt"))):
|
||||
token_file = open(xbmc.translatePath(utils.data_dir() + "tokens.txt"))
|
||||
if(xbmcvfs.exists(xbmcvfs.translatePath(utils.data_dir() + "tokens.txt"))):
|
||||
token_file = open(xbmcvfs.translatePath(utils.data_dir() + "tokens.txt"))
|
||||
token = token_file.read()
|
||||
token_file.close()
|
||||
|
||||
@@ -110,64 +105,5 @@ class DropboxAuthorizer:
|
||||
return ""
|
||||
|
||||
def _deleteToken(self):
|
||||
if(xbmcvfs.exists(xbmc.translatePath(utils.data_dir() + "tokens.txt"))):
|
||||
xbmcvfs.delete(xbmc.translatePath(utils.data_dir() + "tokens.txt"))
|
||||
|
||||
class GoogleDriveAuthorizer:
|
||||
CLIENT_ID = ''
|
||||
CLIENT_SECRET = ''
|
||||
|
||||
def __init__(self):
|
||||
self.CLIENT_ID = utils.getSetting('google_drive_id')
|
||||
self.CLIENT_SECRET = utils.getSetting('google_drive_secret')
|
||||
|
||||
def setup(self):
|
||||
result = True
|
||||
|
||||
if(self.CLIENT_ID == '' and self.CLIENT_SECRET == ''):
|
||||
#we can't go any farther, need these for sure
|
||||
xbmcgui.Dialog().ok(utils.getString(30010),utils.getString(30098) + ' ' + utils.getString(30058),utils.getString(30108))
|
||||
result = False
|
||||
|
||||
return result
|
||||
|
||||
def isAuthorized(self):
|
||||
return xbmcvfs.exists(xbmc.translatePath(utils.data_dir() + "google_drive.dat"))
|
||||
|
||||
def authorize(self):
|
||||
result = True
|
||||
|
||||
if(not self.setup()):
|
||||
return False
|
||||
|
||||
#create authorization helper and load default settings
|
||||
gauth = GoogleAuth(xbmc.validatePath(xbmc.translatePath(utils.addon_dir() + '/resources/lib/pydrive/settings.yaml')))
|
||||
gauth.LoadClientConfigSettings()
|
||||
|
||||
settings = {"client_id":self.CLIENT_ID,'client_secret':self.CLIENT_SECRET}
|
||||
|
||||
drive_url = gauth.GetAuthUrl(settings)
|
||||
|
||||
utils.log("Google Drive Authorize URL: " + drive_url)
|
||||
|
||||
xbmcgui.Dialog().ok(utils.getString(30010),utils.getString(30056),utils.getString(30102),tinyurl.shorten(drive_url))
|
||||
code = xbmcgui.Dialog().input(utils.getString(30098) + ' ' + utils.getString(30103))
|
||||
|
||||
gauth.Auth(code)
|
||||
gauth.SaveCredentialsFile(xbmc.validatePath(xbmc.translatePath(utils.data_dir() + 'google_drive.dat')))
|
||||
|
||||
return result
|
||||
|
||||
def getClient(self):
|
||||
#create authorization helper and load default settings
|
||||
gauth = GoogleAuth(xbmc.validatePath(xbmc.translatePath(utils.addon_dir() + '/resources/lib/pydrive/settings.yaml')))
|
||||
gauth.LoadClientConfigSettings()
|
||||
|
||||
gauth.LoadCredentialsFile(xbmc.validatePath(xbmc.translatePath(utils.data_dir() + 'google_drive.dat')))
|
||||
|
||||
result = GoogleDrive(gauth)
|
||||
|
||||
return result
|
||||
|
||||
|
||||
|
||||
if(xbmcvfs.exists(xbmcvfs.translatePath(utils.data_dir() + "tokens.txt"))):
|
||||
xbmcvfs.delete(xbmcvfs.translatePath(utils.data_dir() + "tokens.txt"))
|
||||
|
||||
@@ -1,15 +1,18 @@
|
||||
from __future__ import unicode_literals
|
||||
import time
|
||||
import json
|
||||
import xbmc
|
||||
import xbmcgui
|
||||
import xbmcvfs
|
||||
import time
|
||||
import json
|
||||
import os.path
|
||||
from . import utils as utils
|
||||
from datetime import datetime
|
||||
from .vfs import XBMCFileSystem,DropboxFileSystem,ZipFileSystem,GoogleDriveFilesystem
|
||||
from . vfs import XBMCFileSystem, DropboxFileSystem, ZipFileSystem
|
||||
from . progressbar import BackupProgressBar
|
||||
from resources.lib.guisettings import GuiSettingsManager
|
||||
from resources.lib.extractor import ZipExtractor
|
||||
|
||||
|
||||
def folderSort(aKey):
|
||||
result = aKey[0]
|
||||
|
||||
@@ -24,6 +27,8 @@ class XbmcBackup:
|
||||
Backup = 0
|
||||
Restore = 1
|
||||
|
||||
ZIP_TEMP_PATH = None
|
||||
|
||||
# list of dirs for the "simple" file selection
|
||||
simple_directory_list = ['addons', 'addon_data', 'database', 'game_saves', 'playlists', 'profiles', 'thumbnails', 'config']
|
||||
|
||||
@@ -37,37 +42,34 @@ class XbmcBackup:
|
||||
|
||||
# for the progress bar
|
||||
progressBar = None
|
||||
filesLeft = 0
|
||||
filesTotal = 1
|
||||
transferSize = 0
|
||||
transferLeft = 0
|
||||
|
||||
restore_point = None
|
||||
skip_advanced = False # if we should check for the existance of advancedsettings in the restore
|
||||
|
||||
def __init__(self):
|
||||
self.xbmc_vfs = XBMCFileSystem(xbmc.translatePath('special://home'))
|
||||
self.xbmc_vfs = XBMCFileSystem(xbmcvfs.translatePath('special://home'))
|
||||
self.ZIP_TEMP_PATH = xbmcvfs.translatePath(utils.getSetting('zip_temp_path'))
|
||||
|
||||
self.configureRemote()
|
||||
utils.log(utils.getString(30046))
|
||||
|
||||
def configureRemote(self):
|
||||
if(utils.getSetting('remote_selection') == '1'):
|
||||
self.remote_base_path = utils.getSetting('remote_path_2');
|
||||
self.remote_vfs = XBMCFileSystem(utils.getSetting('remote_path_2'))
|
||||
utils.setSetting("remote_path", "")
|
||||
elif(utils.getSetting('remote_selection') == '0'):
|
||||
self.remote_base_path = utils.getSetting('remote_path');
|
||||
self.remote_vfs = XBMCFileSystem(utils.getSetting("remote_path"))
|
||||
elif(utils.getSetting('remote_selection') == '2'):
|
||||
self.remote_base_path = "/"
|
||||
self.remote_vfs = DropboxFileSystem("/")
|
||||
elif(utils.getSetting('remote_selection') == '3'):
|
||||
self.remote_base_path = '/Kodi Backup/'
|
||||
self.remote_vfs = GoogleDriveFilesystem('/Kodi Backup/')
|
||||
|
||||
self.remote_base_path = self.remote_vfs.root_path
|
||||
|
||||
def remoteConfigured(self):
|
||||
result = True
|
||||
|
||||
if(self.remote_base_path == ""):
|
||||
if(self.remote_base_path == "" or not xbmcvfs.exists(self.ZIP_TEMP_PATH)):
|
||||
result = False
|
||||
|
||||
return result
|
||||
@@ -89,16 +91,15 @@ class XbmcBackup:
|
||||
|
||||
for aFile in files:
|
||||
file_ext = aFile.split('.')[-1]
|
||||
folderName = utils.encode(aFile.split('.')[0])
|
||||
folderName = aFile.split('.')[0]
|
||||
|
||||
if(file_ext == 'zip' and len(folderName) == 12 and str.isdigit(folderName)):
|
||||
if(file_ext == 'zip' and len(folderName) == 12 and folderName.isdigit()):
|
||||
|
||||
#format the name according to regional settings
|
||||
folderName = self._dateFormat(folderName)
|
||||
# format the name according to regional settings and display the file size
|
||||
folderName = "%s - %s" % (self._dateFormat(folderName), utils.diskString(self.remote_vfs.fileSize(self.remote_base_path + aFile)))
|
||||
|
||||
result.append((aFile, folderName))
|
||||
|
||||
|
||||
result.sort(key=folderSort, reverse=reverse)
|
||||
|
||||
return result
|
||||
@@ -126,14 +127,14 @@ class XbmcBackup:
|
||||
utils.log('File Selection Type: ' + str(utils.getSetting('backup_selection_type')))
|
||||
allFiles = []
|
||||
|
||||
if(int(utils.getSetting('backup_selection_type')) == 0):
|
||||
if(utils.getSettingInt('backup_selection_type') == 0):
|
||||
# read in a list of the directories to backup
|
||||
selectedDirs = self._readBackupConfig(utils.addon_dir() + "/resources/data/default_files.json")
|
||||
|
||||
# simple mode - get file listings for all enabled directories
|
||||
for aDir in self.simple_directory_list:
|
||||
# if this dir enabled
|
||||
if(utils.getSetting('backup_' + aDir) == 'true'):
|
||||
if(utils.getSettingBool('backup_' + aDir)):
|
||||
# get a file listing and append it to the allfiles array
|
||||
allFiles.append(self._addBackupDir(aDir, selectedDirs[aDir]['root'], selectedDirs[aDir]['dirs']))
|
||||
else:
|
||||
@@ -156,7 +157,7 @@ class XbmcBackup:
|
||||
|
||||
if(not writeCheck):
|
||||
# we may not be able to write to this destination for some reason
|
||||
shouldContinue = xbmcgui.Dialog().yesno(utils.getString(30089),utils.getString(30090), utils.getString(30044),autoclose=25000)
|
||||
shouldContinue = xbmcgui.Dialog().yesno(utils.getString(30089), "%s\n%s" % (utils.getString(30090), utils.getString(30044)), autoclose=25000)
|
||||
|
||||
if(not shouldContinue):
|
||||
return
|
||||
@@ -164,9 +165,9 @@ class XbmcBackup:
|
||||
orig_base_path = self.remote_vfs.root_path
|
||||
|
||||
# backup all the files
|
||||
self.filesLeft = self.filesTotal
|
||||
self.transferLeft = self.transferSize
|
||||
for fileGroup in allFiles:
|
||||
self.xbmc_vfs.set_root(xbmc.translatePath(fileGroup['source']))
|
||||
self.xbmc_vfs.set_root(xbmcvfs.translatePath(fileGroup['source']))
|
||||
self.remote_vfs.set_root(fileGroup['dest'] + fileGroup['name'])
|
||||
filesCopied = self._copyFiles(fileGroup['files'], self.xbmc_vfs, self.remote_vfs)
|
||||
|
||||
@@ -178,28 +179,30 @@ class XbmcBackup:
|
||||
self.xbmc_vfs.set_root("special://home/")
|
||||
self.remote_vfs.set_root(orig_base_path)
|
||||
|
||||
if(utils.getSetting("compress_backups") == 'true'):
|
||||
if(utils.getSettingBool("compress_backups")):
|
||||
fileManager = FileManager(self.xbmc_vfs)
|
||||
|
||||
# send the zip file to the real remote vfs
|
||||
zip_name = self.remote_vfs.root_path[:-1] + ".zip"
|
||||
zip_name = os.path.join(self.ZIP_TEMP_PATH, self.remote_vfs.root_path[:-1] + ".zip")
|
||||
self.remote_vfs.cleanup()
|
||||
self.xbmc_vfs.rename(xbmc.translatePath("special://temp/xbmc_backup_temp.zip"), xbmc.translatePath("special://temp/" + zip_name))
|
||||
fileManager.addFile(xbmc.translatePath("special://temp/" + zip_name))
|
||||
|
||||
#set root to data dir home
|
||||
self.xbmc_vfs.set_root(xbmc.translatePath("special://temp/"))
|
||||
self.xbmc_vfs.rename(os.path.join(self.ZIP_TEMP_PATH, "xbmc_backup_temp.zip"), zip_name)
|
||||
fileManager.addFile(zip_name)
|
||||
|
||||
# set root to data dir home and reset remote
|
||||
self.xbmc_vfs.set_root(self.ZIP_TEMP_PATH)
|
||||
self.remote_vfs = self.saved_remote_vfs
|
||||
self.progressBar.updateProgress(98, utils.getString(30088))
|
||||
|
||||
# update the amount to transfer
|
||||
self.transferSize = fileManager.fileSize()
|
||||
self.transferLeft = self.transferSize
|
||||
fileCopied = self._copyFiles(fileManager.getFiles(), self.xbmc_vfs, self.remote_vfs)
|
||||
|
||||
if(not fileCopied):
|
||||
# zip archive copy filed, inform the user
|
||||
shouldContinue = xbmcgui.Dialog().ok(utils.getString(30089),utils.getString(30090), utils.getString(30091))
|
||||
shouldContinue = xbmcgui.Dialog().ok(utils.getString(30089), '%s\n%s' % (utils.getString(30090), utils.getString(30091)))
|
||||
|
||||
# delete the temp zip file
|
||||
self.xbmc_vfs.rmfile(xbmc.translatePath("special://temp/" + zip_name))
|
||||
self.xbmc_vfs.rmfile(zip_name)
|
||||
|
||||
# remove old backups
|
||||
self._rotateBackups()
|
||||
@@ -219,25 +222,28 @@ class XbmcBackup:
|
||||
utils.log("copying zip file: " + self.restore_point)
|
||||
|
||||
# set root to data dir home
|
||||
self.xbmc_vfs.set_root(xbmc.translatePath("special://temp/"))
|
||||
|
||||
if(not self.xbmc_vfs.exists(xbmc.translatePath("special://temp/" + self.restore_point))):
|
||||
self.xbmc_vfs.set_root(self.ZIP_TEMP_PATH)
|
||||
restore_path = os.path.join(self.ZIP_TEMP_PATH, self.restore_point)
|
||||
if(not self.xbmc_vfs.exists(restore_path)):
|
||||
# copy just this file from the remote vfs
|
||||
self.transferSize = self.remote_vfs.fileSize(self.remote_base_path + self.restore_point)
|
||||
zipFile = []
|
||||
zipFile.append(self.remote_base_path + self.restore_point)
|
||||
zipFile.append({'file': self.remote_base_path + self.restore_point, 'size': self.transferSize})
|
||||
|
||||
# set transfer size
|
||||
self.transferLeft = self.transferSize
|
||||
self._copyFiles(zipFile, self.remote_vfs, self.xbmc_vfs)
|
||||
else:
|
||||
utils.log("zip file exists already")
|
||||
|
||||
# extract the zip file
|
||||
zip_vfs = ZipFileSystem(xbmc.translatePath("special://temp/"+ self.restore_point),'r')
|
||||
zip_vfs = ZipFileSystem(restore_path, 'r')
|
||||
extractor = ZipExtractor()
|
||||
|
||||
if(not extractor.extract(zip_vfs, xbmc.translatePath("special://temp/"), self.progressBar)):
|
||||
if(not extractor.extract(zip_vfs, self.ZIP_TEMP_PATH, self.progressBar)):
|
||||
# we had a problem extracting the archive, delete everything
|
||||
zip_vfs.cleanup()
|
||||
self.xbmc_vfs.rmfile(xbmc.translatePath("special://temp/" + self.restore_point))
|
||||
self.xbmc_vfs.rmfile(restore_path)
|
||||
|
||||
xbmcgui.Dialog().ok(utils.getString(30010), utils.getString(30101))
|
||||
return
|
||||
@@ -246,16 +252,16 @@ class XbmcBackup:
|
||||
|
||||
self.progressBar.updateProgress(0, utils.getString(30049) + "......")
|
||||
# set the new remote vfs and fix xbmc path
|
||||
self.remote_vfs = XBMCFileSystem(xbmc.translatePath("special://temp/" + self.restore_point.split(".")[0] + "/"))
|
||||
self.xbmc_vfs.set_root(xbmc.translatePath("special://home/"))
|
||||
self.remote_vfs = XBMCFileSystem(os.path.join(self.ZIP_TEMP_PATH, self.restore_point.split(".")[0]))
|
||||
self.xbmc_vfs.set_root(xbmcvfs.translatePath("special://home/"))
|
||||
|
||||
# for restores remote path must exist
|
||||
if(not self.remote_vfs.exists(self.remote_vfs.root_path)):
|
||||
xbmcgui.Dialog().ok(utils.getString(30010),utils.getString(30045),self.remote_vfs.root_path)
|
||||
xbmcgui.Dialog().ok(utils.getString(30010), '%s\n%s' % (utils.getString(30045), self.remote_vfs.root_path))
|
||||
return
|
||||
|
||||
valFile = self._checkValidationFile(self.remote_vfs.root_path)
|
||||
if(valFile == None):
|
||||
if(valFile is None):
|
||||
# don't continue
|
||||
return
|
||||
|
||||
@@ -266,7 +272,7 @@ class XbmcBackup:
|
||||
# check for the existance of an advancedsettings file
|
||||
if(self.remote_vfs.exists(self.remote_vfs.root_path + "config/advancedsettings.xml") and not self.skip_advanced):
|
||||
# let the user know there is an advanced settings file present
|
||||
restartXbmc = xbmcgui.Dialog().yesno(utils.getString(30038),utils.getString(30039),utils.getString(30040), utils.getString(30041))
|
||||
restartXbmc = xbmcgui.Dialog().yesno(utils.getString(30038), "%s\n%s" % (utils.getString(30039), utils.getString(30040)), utils.getString(30041))
|
||||
|
||||
if(restartXbmc):
|
||||
# add only this file to the file list
|
||||
@@ -280,50 +286,61 @@ class XbmcBackup:
|
||||
xbmcgui.Dialog().ok(utils.getString(30077), utils.getString(30078))
|
||||
return
|
||||
|
||||
# check if settings should be restored from this backup
|
||||
restoreSettings = not utils.getSettingBool('always_prompt_restore_settings')
|
||||
if(not restoreSettings and 'system_settings' in valFile):
|
||||
# prompt the user to restore settings yes/no
|
||||
restoreSettings = xbmcgui.Dialog().yesno(utils.getString(30149), utils.getString(30150))
|
||||
|
||||
# use a multiselect dialog to select sets to restore
|
||||
restoreSets = [n['name'] for n in valFile['directories']]
|
||||
|
||||
# if passed in list, skip selection
|
||||
if(selectedSets == None):
|
||||
if(selectedSets is None):
|
||||
selectedSets = xbmcgui.Dialog().multiselect(utils.getString(30131), restoreSets)
|
||||
else:
|
||||
selectedSets = [restoreSets.index(n) for n in selectedSets if n in restoreSets] # if set name not found just skip it
|
||||
|
||||
if(selectedSets != None):
|
||||
if(selectedSets is not None):
|
||||
|
||||
# go through each of the directories in the backup and write them to the correct location
|
||||
for index in selectedSets:
|
||||
|
||||
# add this directory
|
||||
aDir = valFile['directories'][index]
|
||||
|
||||
self.xbmc_vfs.set_root(xbmc.translatePath(aDir['path']))
|
||||
self.xbmc_vfs.set_root(xbmcvfs.translatePath(aDir['path']))
|
||||
if(self.remote_vfs.exists(self.remote_vfs.root_path + aDir['name'] + '/')):
|
||||
# walk the directory
|
||||
fileManager.walkTree(self.remote_vfs.root_path + aDir['name'] + '/')
|
||||
self.filesTotal = self.filesTotal + fileManager.size()
|
||||
self.transferSize = self.transferSize + fileManager.fileSize()
|
||||
|
||||
allFiles.append({"source": self.remote_vfs.root_path + aDir['name'], "dest": self.xbmc_vfs.root_path, "files": fileManager.getFiles()})
|
||||
else:
|
||||
utils.log("error path not found: " + self.remote_vfs.root_path + aDir['name'])
|
||||
xbmcgui.Dialog().ok(utils.getString(30010),utils.getString(30045),self.remote_vfs.root_path + aDir['name'])
|
||||
xbmcgui.Dialog().ok(utils.getString(30010), '%s\n%s' % (utils.getString(30045), self.remote_vfs.root_path + aDir['name']))
|
||||
|
||||
# restore all the files
|
||||
self.filesLeft = self.filesTotal
|
||||
self.transferLeft = self.transferSize
|
||||
for fileGroup in allFiles:
|
||||
self.remote_vfs.set_root(fileGroup['source'])
|
||||
self.xbmc_vfs.set_root(fileGroup['dest'])
|
||||
self._copyFiles(fileGroup['files'], self.remote_vfs, self.xbmc_vfs)
|
||||
|
||||
# update the Kodi settings - if we can
|
||||
if('system_settings' in valFile and restoreSettings):
|
||||
self.progressBar.updateProgress(98, "Restoring Kodi settings")
|
||||
gui_settings = GuiSettingsManager()
|
||||
gui_settings.restore(valFile['system_settings'])
|
||||
|
||||
self.progressBar.updateProgress(99, "Clean up operations .....")
|
||||
|
||||
if(self.restore_point.split('.')[-1] == 'zip'):
|
||||
# delete the zip file and the extracted directory
|
||||
self.xbmc_vfs.rmfile(xbmc.translatePath("special://temp/" + self.restore_point))
|
||||
self.xbmc_vfs.rmdir(self.remote_vfs.root_path)
|
||||
|
||||
|
||||
#update the guisettings information (or what we can from it)
|
||||
gui_settings = GuiSettingsManager()
|
||||
gui_settings.run()
|
||||
self.xbmc_vfs.rmfile(os.path.join(self.ZIP_TEMP_PATH, self.restore_point))
|
||||
xbmc.sleep(1000)
|
||||
self.xbmc_vfs.rmdir(self.remote_vfs.clean_path(os.path.join(self.ZIP_TEMP_PATH, self.restore_point.split(".")[0])))
|
||||
xbmc.sleep(1000)
|
||||
|
||||
# call update addons to refresh everything
|
||||
xbmc.executebuiltin('UpdateLocalAddons')
|
||||
@@ -336,21 +353,22 @@ class XbmcBackup:
|
||||
# append backup folder name
|
||||
progressBarTitle = utils.getString(30010) + " - "
|
||||
if(mode == self.Backup and self.remote_vfs.root_path != ''):
|
||||
if(utils.getSetting("compress_backups") == 'true'):
|
||||
if(utils.getSettingBool("compress_backups")):
|
||||
# delete old temp file
|
||||
if(self.xbmc_vfs.exists(xbmc.translatePath('special://temp/xbmc_backup_temp.zip'))):
|
||||
if(not self.xbmc_vfs.rmfile(xbmc.translatePath('special://temp/xbmc_backup_temp.zip'))):
|
||||
zip_path = os.path.join(self.ZIP_TEMP_PATH, 'xbmc_backup_temp.zip')
|
||||
if(self.xbmc_vfs.exists(zip_path)):
|
||||
if(not self.xbmc_vfs.rmfile(zip_path)):
|
||||
# we had some kind of error deleting the old file
|
||||
xbmcgui.Dialog().ok(utils.getString(30010),utils.getString(30096),utils.getString(30097))
|
||||
xbmcgui.Dialog().ok(utils.getString(30010), '%s\n%s' % (utils.getString(30096), utils.getString(30097)))
|
||||
return False
|
||||
|
||||
# save the remote file system and use the zip vfs
|
||||
self.saved_remote_vfs = self.remote_vfs
|
||||
self.remote_vfs = ZipFileSystem(xbmc.translatePath("special://temp/xbmc_backup_temp.zip"),"w")
|
||||
self.remote_vfs = ZipFileSystem(zip_path, "w")
|
||||
|
||||
self.remote_vfs.set_root(self.remote_vfs.root_path + time.strftime("%Y%m%d%H%M") + "/")
|
||||
progressBarTitle = progressBarTitle + utils.getString(30023) + ": " + utils.getString(30016)
|
||||
elif(mode == self.Restore and self.restore_point != None and self.remote_vfs.root_path != ''):
|
||||
elif(mode == self.Restore and self.restore_point is not None and self.remote_vfs.root_path != ''):
|
||||
if(self.restore_point.split('.')[-1] != 'zip'):
|
||||
self.remote_vfs.set_root(self.remote_vfs.root_path + self.restore_point + "/")
|
||||
progressBarTitle = progressBarTitle + utils.getString(30023) + ": " + utils.getString(30017)
|
||||
@@ -361,7 +379,7 @@ class XbmcBackup:
|
||||
|
||||
utils.log(utils.getString(30047) + ": " + self.xbmc_vfs.root_path)
|
||||
utils.log(utils.getString(30048) + ": " + self.remote_vfs.root_path)
|
||||
|
||||
utils.log(utils.getString(30152) + ": " + utils.getSetting('zip_temp_path'))
|
||||
|
||||
# setup the progress bar
|
||||
self.progressBar = BackupProgressBar(progressOverride)
|
||||
@@ -383,7 +401,7 @@ class XbmcBackup:
|
||||
result = True
|
||||
|
||||
utils.log("Source: " + source.root_path)
|
||||
utils.log("Desintation: " + dest.root_path)
|
||||
utils.log("Destination: " + dest.root_path)
|
||||
|
||||
# make sure the dest folder exists - can cause write errors if the full path doesn't exist
|
||||
if(not dest.exists(dest.root_path)):
|
||||
@@ -391,26 +409,34 @@ class XbmcBackup:
|
||||
|
||||
for aFile in fileList:
|
||||
if(not self.progressBar.checkCancel()):
|
||||
utils.log('Writing file: ' + aFile,xbmc.LOGDEBUG)
|
||||
if(aFile.startswith("-")):
|
||||
self._updateProgress(aFile[len(source.root_path) + 1:])
|
||||
dest.mkdir(dest.root_path + aFile[len(source.root_path) + 1:])
|
||||
else:
|
||||
self._updateProgress()
|
||||
if(utils.getSettingBool('verbose_logging')):
|
||||
utils.log('Writing file: ' + aFile['file'])
|
||||
|
||||
wroteFile = True
|
||||
destFile = dest.root_path + aFile[len(source.root_path):]
|
||||
if(isinstance(source,DropboxFileSystem) or isinstance(source,GoogleDriveFilesystem)):
|
||||
#if copying from cloud storage we need the file handle, use get_file
|
||||
wroteFile = source.get_file(aFile,destFile)
|
||||
if(aFile['file'].startswith("-")):
|
||||
self._updateProgress('%s remaining, writing %s' % (utils.diskString(self.transferLeft), os.path.basename(aFile['file'][len(source.root_path):]) + "/"))
|
||||
dest.mkdir(dest.root_path + aFile['file'][len(source.root_path) + 1:])
|
||||
else:
|
||||
#copy using normal method
|
||||
wroteFile = dest.put(aFile,destFile)
|
||||
self._updateProgress('%s remaining, writing %s' % (utils.diskString(self.transferLeft), os.path.basename(aFile['file'][len(source.root_path):])))
|
||||
self.transferLeft = self.transferLeft - aFile['size']
|
||||
|
||||
# copy the file
|
||||
wroteFile = self._copyFile(source, dest, aFile['file'], dest.root_path + aFile['file'][len(source.root_path):])
|
||||
|
||||
# if result is still true but this file failed
|
||||
if(not wroteFile and result):
|
||||
result = False
|
||||
|
||||
return result
|
||||
|
||||
def _copyFile(self, source, dest, sourceFile, destFile):
|
||||
result = True
|
||||
|
||||
if(isinstance(source, DropboxFileSystem)):
|
||||
# if copying from cloud storage we need the file handle, use get_file
|
||||
result = source.get_file(sourceFile, destFile)
|
||||
else:
|
||||
# copy using normal method
|
||||
result = dest.put(sourceFile, destFile)
|
||||
|
||||
return result
|
||||
|
||||
@@ -418,14 +444,15 @@ class XbmcBackup:
|
||||
utils.log('Backup set: ' + folder_name)
|
||||
fileManager = FileManager(self.xbmc_vfs)
|
||||
|
||||
self.xbmc_vfs.set_root(xbmc.translatePath(root_path))
|
||||
self.xbmc_vfs.set_root(xbmcvfs.translatePath(root_path))
|
||||
for aDir in dirList:
|
||||
fileManager.addDir(aDir)
|
||||
|
||||
# walk all the root trees
|
||||
fileManager.walk()
|
||||
#update total files
|
||||
self.filesTotal = self.filesTotal + fileManager.size()
|
||||
|
||||
# update total size
|
||||
self.transferSize = self.transferSize + fileManager.fileSize()
|
||||
|
||||
return {"name": folder_name, "source": root_path, "dest": self.remote_vfs.root_path, "files": fileManager.getFiles()}
|
||||
|
||||
@@ -439,11 +466,10 @@ class XbmcBackup:
|
||||
return result
|
||||
|
||||
def _updateProgress(self, message=None):
|
||||
self.filesLeft = self.filesLeft - 1
|
||||
self.progressBar.updateProgress(int((float(self.filesTotal - self.filesLeft)/float(self.filesTotal)) * 100),message)
|
||||
self.progressBar.updateProgress(int((float(self.transferSize - self.transferLeft) / float(self.transferSize)) * 100), message)
|
||||
|
||||
def _rotateBackups(self):
|
||||
total_backups = int(utils.getSetting('backup_rotation'))
|
||||
total_backups = utils.getSettingInt('backup_rotation')
|
||||
|
||||
if(total_backups > 0):
|
||||
# get a list of valid backup folders
|
||||
@@ -452,7 +478,6 @@ class XbmcBackup:
|
||||
if(len(dirs) > total_backups):
|
||||
# remove backups to equal total wanted
|
||||
remove_num = 0
|
||||
self.filesTotal = self.filesTotal + remove_num + 1
|
||||
|
||||
# update the progress bar if it is available
|
||||
while(remove_num < (len(dirs) - total_backups) and not self.progressBar.checkCancel()):
|
||||
@@ -461,37 +486,42 @@ class XbmcBackup:
|
||||
|
||||
if(dirs[remove_num][0].split('.')[-1] == 'zip'):
|
||||
# this is a file, remove it that way
|
||||
self.remote_vfs.rmfile(self.remote_base_path + dirs[remove_num][0])
|
||||
self.remote_vfs.rmfile(self.remote_vfs.clean_path(self.remote_base_path) + dirs[remove_num][0])
|
||||
else:
|
||||
self.remote_vfs.rmdir(self.remote_base_path + dirs[remove_num][0] + "/")
|
||||
self.remote_vfs.rmdir(self.remote_vfs.clean_path(self.remote_base_path) + dirs[remove_num][0] + "/")
|
||||
|
||||
remove_num = remove_num + 1
|
||||
|
||||
def _createValidationFile(self, dirList):
|
||||
valInfo = {"name":"XBMC Backup Validation File","xbmc_version":xbmc.getInfoLabel('System.BuildVersion'),"type":0}
|
||||
valInfo = {"name": "XBMC Backup Validation File", "xbmc_version": xbmc.getInfoLabel('System.BuildVersion'), "type": 0, "system_settings": []}
|
||||
valDirs = []
|
||||
|
||||
# save list of file sets
|
||||
for aDir in dirList:
|
||||
valDirs.append({"name": aDir['name'], "path": aDir['source']})
|
||||
valInfo['directories'] = valDirs
|
||||
|
||||
vFile = xbmcvfs.File(xbmc.translatePath(utils.data_dir() + "xbmcbackup.val"),'w')
|
||||
# dump all current Kodi settings
|
||||
gui_settings = GuiSettingsManager()
|
||||
valInfo['system_settings'] = gui_settings.backup()
|
||||
|
||||
vFile = xbmcvfs.File(xbmcvfs.translatePath(utils.data_dir() + "xbmcbackup.val"), 'w')
|
||||
vFile.write(json.dumps(valInfo))
|
||||
vFile.write("")
|
||||
vFile.close()
|
||||
|
||||
success = self.remote_vfs.put(xbmc.translatePath(utils.data_dir() + "xbmcbackup.val"),self.remote_vfs.root_path + "xbmcbackup.val")
|
||||
success = self._copyFile(self.xbmc_vfs, self.remote_vfs, xbmcvfs.translatePath(utils.data_dir() + "xbmcbackup.val"), self.remote_vfs.root_path + "xbmcbackup.val")
|
||||
|
||||
# remove the validation file
|
||||
xbmcvfs.delete(xbmc.translatePath(utils.data_dir() + "xbmcbackup.val"))
|
||||
xbmcvfs.delete(xbmcvfs.translatePath(utils.data_dir() + "xbmcbackup.val"))
|
||||
|
||||
if(success):
|
||||
# android requires a .nomedia file to not index the directory as media
|
||||
if(not xbmcvfs.exists(xbmc.translatePath(utils.data_dir() + ".nomedia"))):
|
||||
nmFile = xbmcvfs.File(xbmc.translatePath(utils.data_dir() + ".nomedia"),'w')
|
||||
if(not xbmcvfs.exists(xbmcvfs.translatePath(utils.data_dir() + ".nomedia"))):
|
||||
nmFile = xbmcvfs.File(xbmcvfs.translatePath(utils.data_dir() + ".nomedia"), 'w')
|
||||
nmFile.close()
|
||||
|
||||
success = self.remote_vfs.put(xbmc.translatePath(utils.data_dir() + ".nomedia"),self.remote_vfs.root_path + ".nomedia")
|
||||
success = self._copyFile(self.xbmc_vfs, self.remote_vfs, xbmcvfs.translatePath(utils.data_dir() + ".nomedia"), self.remote_vfs.root_path + ".nomedia")
|
||||
|
||||
return success
|
||||
|
||||
@@ -499,23 +529,19 @@ class XbmcBackup:
|
||||
result = None
|
||||
|
||||
# copy the file and open it
|
||||
if(isinstance(self.remote_vfs,DropboxFileSystem) or isinstance(self.remote_vfs,GoogleDriveFilesystem)):
|
||||
self.remote_vfs.get_file(path + "xbmcbackup.val", xbmc.translatePath(utils.data_dir() + "xbmcbackup_restore.val"))
|
||||
else:
|
||||
self.xbmc_vfs.put(path + "xbmcbackup.val",xbmc.translatePath(utils.data_dir() + "xbmcbackup_restore.val"))
|
||||
self._copyFile(self.remote_vfs, self.xbmc_vfs, path + "xbmcbackup.val", xbmcvfs.translatePath(utils.data_dir() + "xbmcbackup_restore.val"))
|
||||
|
||||
vFile = xbmcvfs.File(xbmc.translatePath(utils.data_dir() + "xbmcbackup_restore.val"),'r')
|
||||
with xbmcvfs.File(xbmcvfs.translatePath(utils.data_dir() + "xbmcbackup_restore.val"), 'r') as vFile:
|
||||
jsonString = vFile.read()
|
||||
vFile.close()
|
||||
|
||||
# delete after checking
|
||||
xbmcvfs.delete(xbmc.translatePath(utils.data_dir() + "xbmcbackup_restore.val"))
|
||||
xbmcvfs.delete(xbmcvfs.translatePath(utils.data_dir() + "xbmcbackup_restore.val"))
|
||||
|
||||
try:
|
||||
result = json.loads(jsonString)
|
||||
|
||||
if(xbmc.getInfoLabel('System.BuildVersion') != result['xbmc_version']):
|
||||
shouldContinue = xbmcgui.Dialog().yesno(utils.getString(30085),utils.getString(30086),utils.getString(30044))
|
||||
shouldContinue = xbmcgui.Dialog().yesno(utils.getString(30085), "%s\n%s" % (utils.getString(30086), utils.getString(30044)))
|
||||
|
||||
if(not shouldContinue):
|
||||
result = None
|
||||
@@ -527,21 +553,21 @@ class XbmcBackup:
|
||||
return result
|
||||
|
||||
def _createResumeBackupFile(self):
|
||||
rFile = xbmcvfs.File(xbmc.translatePath(utils.data_dir() + "resume.txt"),'w')
|
||||
rFile.write(self.restore_point)
|
||||
rFile.close()
|
||||
with xbmcvfs.File(xbmcvfs.translatePath(utils.data_dir() + "resume.txt"), 'w') as f:
|
||||
f.write(self.restore_point)
|
||||
|
||||
def _readBackupConfig(self, aFile):
|
||||
jFile = xbmcvfs.File(xbmc.translatePath(aFile),'r')
|
||||
jsonString = jFile.read()
|
||||
jFile.close()
|
||||
with xbmcvfs.File(xbmcvfs.translatePath(aFile), 'r') as f:
|
||||
jsonString = f.read()
|
||||
return json.loads(jsonString)
|
||||
|
||||
|
||||
class FileManager:
|
||||
not_dir = ['.zip', '.xsp', '.rar']
|
||||
exclude_dir = []
|
||||
root_dirs = []
|
||||
pathSep = '/'
|
||||
totalSize = 0
|
||||
|
||||
def __init__(self, vfs):
|
||||
self.vfs = vfs
|
||||
@@ -552,11 +578,13 @@ class FileManager:
|
||||
def walk(self):
|
||||
|
||||
for aDir in self.root_dirs:
|
||||
self.addFile('-' + xbmc.translatePath(aDir['path']))
|
||||
self.walkTree(xbmc.translatePath(aDir['path']),aDir['recurse'])
|
||||
self.addFile('-' + xbmcvfs.translatePath(aDir['path']))
|
||||
self.walkTree(xbmcvfs.translatePath(aDir['path']), aDir['recurse'])
|
||||
|
||||
def walkTree(self, directory, recurse=True):
|
||||
if(utils.getSettingBool('verbose_logging')):
|
||||
utils.log('walking ' + directory + ', recurse: ' + str(recurse))
|
||||
|
||||
if(directory[-1:] == '/' or directory[-1:] == '\\'):
|
||||
directory = directory[:-1]
|
||||
|
||||
@@ -566,7 +594,7 @@ class FileManager:
|
||||
if(recurse):
|
||||
# create all the subdirs first
|
||||
for aDir in dirs:
|
||||
dirPath = xbmc.validatePath(xbmc.translatePath(directory + self.pathSep + aDir))
|
||||
dirPath = xbmcvfs.validatePath(xbmcvfs.translatePath(directory + self.pathSep + aDir))
|
||||
file_ext = aDir.split('.')[-1]
|
||||
|
||||
# check if directory is excluded
|
||||
@@ -586,31 +614,27 @@ class FileManager:
|
||||
|
||||
# copy all the files
|
||||
for aFile in files:
|
||||
filePath = xbmc.translatePath(directory + self.pathSep + aFile)
|
||||
filePath = xbmcvfs.translatePath(directory + self.pathSep + aFile)
|
||||
self.addFile(filePath)
|
||||
|
||||
def addDir(self, dirMeta):
|
||||
if(dirMeta['type'] == 'include'):
|
||||
self.root_dirs.append({'path': dirMeta['path'], 'recurse': dirMeta['recurse']})
|
||||
else:
|
||||
self.excludeFile(xbmc.translatePath(dirMeta['path']))
|
||||
self.excludeFile(xbmcvfs.translatePath(dirMeta['path']))
|
||||
|
||||
def addFile(self, filename):
|
||||
try:
|
||||
filename = filename.decode('UTF-8')
|
||||
except UnicodeDecodeError:
|
||||
filename = filename.decode('ISO-8859-2')
|
||||
|
||||
# write the full remote path name of this file
|
||||
utils.log("Add File: " + filename,xbmc.LOGDEBUG)
|
||||
self.fileArray.append(filename)
|
||||
if(utils.getSettingBool('verbose_logging')):
|
||||
utils.log("Add File: " + filename)
|
||||
|
||||
# get the file size
|
||||
fSize = self.vfs.fileSize(filename)
|
||||
self.totalSize = self.totalSize + fSize
|
||||
|
||||
self.fileArray.append({'file': filename, 'size': fSize})
|
||||
|
||||
def excludeFile(self, filename):
|
||||
try:
|
||||
filename = filename.decode('UTF-8')
|
||||
except UnicodeDecodeError:
|
||||
filename = filename.decode('ISO-8859-2')
|
||||
|
||||
# remove trailing slash
|
||||
if(filename[-1] == '/' or filename[-1] == '\\'):
|
||||
filename = filename[:-1]
|
||||
@@ -624,8 +648,12 @@ class FileManager:
|
||||
self.fileArray = []
|
||||
self.root_dirs = []
|
||||
self.exclude_dir = []
|
||||
self.totalSize = 0
|
||||
|
||||
return result
|
||||
|
||||
def size(self):
|
||||
def totalFiles(self):
|
||||
return len(self.fileArray)
|
||||
|
||||
def fileSize(self):
|
||||
return self.totalSize
|
||||
|
||||
@@ -3,8 +3,8 @@
|
||||
|
||||
import re
|
||||
from time import time, mktime
|
||||
from datetime import datetime, date
|
||||
from .relativedelta import relativedelta
|
||||
from datetime import datetime
|
||||
from dateutil.relativedelta import relativedelta
|
||||
|
||||
search_re = re.compile(r'^([^-]+)-([^-/]+)(/(.*))?$')
|
||||
only_int_re = re.compile(r'^\d+$')
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
from . import utils as utils
|
||||
|
||||
|
||||
class ZipExtractor:
|
||||
|
||||
def extract(self, zipFile, outLoc, progressBar):
|
||||
@@ -23,9 +24,8 @@ class ZipExtractor:
|
||||
# extract the file
|
||||
zipFile.extract(aFile, outLoc)
|
||||
|
||||
except Exception as e:
|
||||
except Exception:
|
||||
utils.log("Error extracting file")
|
||||
result = False
|
||||
|
||||
return result
|
||||
|
||||
|
||||
@@ -1,73 +1,47 @@
|
||||
import json
|
||||
import xbmc,xbmcvfs
|
||||
import xbmc
|
||||
from . import utils as utils
|
||||
from xml.dom import minidom
|
||||
from xml.parsers.expat import ExpatError
|
||||
|
||||
|
||||
class GuiSettingsManager:
|
||||
doc = None
|
||||
filename = 'kodi_settings.json'
|
||||
systemSettings = None
|
||||
|
||||
def __init__(self):
|
||||
#first make a copy of the file
|
||||
xbmcvfs.copy(xbmc.translatePath('special://home/userdata/guisettings.xml'), xbmc.translatePath("special://home/userdata/guisettings.xml.restored"))
|
||||
# get all of the current Kodi settings
|
||||
json_response = json.loads(xbmc.executeJSONRPC('{"jsonrpc":"2.0", "id":1, "method":"Settings.GetSettings","params":{"level":"expert"}}'))
|
||||
|
||||
#read in the copy
|
||||
self._readFile(xbmc.translatePath('special://home/userdata/guisettings.xml.restored'))
|
||||
self.systemSettings = json_response['result']['settings']
|
||||
|
||||
def run(self):
|
||||
#get a list of all the settings we can manipulate via json
|
||||
json_response = json.loads(xbmc.executeJSONRPC('{"jsonrpc":"2.0", "id":1, "method":"Settings.GetSettings","params":{"level":"advanced"}}'))
|
||||
def backup(self):
|
||||
utils.log('Backing up Kodi settings')
|
||||
|
||||
settings = json_response['result']['settings']
|
||||
currentSettings = {}
|
||||
# return all current settings
|
||||
return self.systemSettings
|
||||
|
||||
for aSetting in settings:
|
||||
if('value' in aSetting):
|
||||
currentSettings[aSetting['id']] = aSetting['value']
|
||||
def restore(self, restoreSettings):
|
||||
utils.log('Restoring Kodi settings')
|
||||
|
||||
#parse the existing xml file and get all the settings we need to restore
|
||||
restoreSettings = self.__parseNodes(self.doc.getElementsByTagName('setting'))
|
||||
updateJson = {"jsonrpc": "2.0", "id": 1, "method": "Settings.SetSettingValue", "params": {"setting": "", "value": ""}}
|
||||
|
||||
#get a list where the restore setting value != the current value
|
||||
updateSettings = {k: v for k, v in list(restoreSettings.items()) if (k in currentSettings and currentSettings[k] != v)}
|
||||
# create a setting=value dict of the current settings
|
||||
settingsDict = {}
|
||||
for aSetting in self.systemSettings:
|
||||
# ignore action types, no value
|
||||
if(aSetting['type'] != 'action'):
|
||||
settingsDict[aSetting['id']] = aSetting['value']
|
||||
|
||||
#go through all the found settings and update them
|
||||
jsonObj = {"jsonrpc":"2.0","id":1,"method":"Settings.SetSettingValue","params":{"setting":"","value":""}}
|
||||
for anId, aValue in list(updateSettings.items()):
|
||||
utils.log("updating: " + anId + ", value: " + str(aValue))
|
||||
restoreCount = 0
|
||||
for aSetting in restoreSettings:
|
||||
# only update a setting if its different than the current (action types have no value)
|
||||
if(aSetting['type'] != 'action' and settingsDict[aSetting['id']] != aSetting['value']):
|
||||
if(utils.getSettingBool('verbose_logging')):
|
||||
utils.log('%s different than current: %s' % (aSetting['id'], str(aSetting['value'])))
|
||||
|
||||
jsonObj['params']['setting'] = anId
|
||||
jsonObj['params']['value'] = aValue
|
||||
|
||||
xbmc.executeJSONRPC(json.dumps(jsonObj))
|
||||
|
||||
def __parseNodes(self,nodeList):
|
||||
result = {}
|
||||
|
||||
for node in nodeList:
|
||||
nodeValue = ''
|
||||
if(node.firstChild != None):
|
||||
nodeValue = node.firstChild.nodeValue
|
||||
|
||||
#check for numbers and booleans
|
||||
if(nodeValue.isdigit()):
|
||||
nodeValue = int(nodeValue)
|
||||
elif(nodeValue == 'true'):
|
||||
nodeValue = True
|
||||
elif(nodeValue == 'false'):
|
||||
nodeValue = False
|
||||
|
||||
result[node.getAttribute('id')] = nodeValue
|
||||
|
||||
return result
|
||||
|
||||
def _readFile(self,fileLoc):
|
||||
|
||||
if(xbmcvfs.exists(fileLoc)):
|
||||
try:
|
||||
self.doc = minidom.parse(fileLoc)
|
||||
except ExpatError:
|
||||
utils.log("Can't read " + fileLoc)
|
||||
updateJson['params']['setting'] = aSetting['id']
|
||||
updateJson['params']['value'] = aSetting['value']
|
||||
|
||||
xbmc.executeJSONRPC(json.dumps(updateJson))
|
||||
restoreCount = restoreCount + 1
|
||||
|
||||
utils.log('Update %d settings' % restoreCount)
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import xbmcgui
|
||||
from . import utils as utils
|
||||
|
||||
|
||||
class BackupProgressBar:
|
||||
NONE = 2
|
||||
DIALOG = 0
|
||||
@@ -14,9 +15,9 @@ class BackupProgressBar:
|
||||
self.override = progressOverride
|
||||
|
||||
# check if we should use the progress bar
|
||||
if(int(utils.getSetting('progress_mode')) != 2):
|
||||
if(utils.getSettingInt('progress_mode') != 2):
|
||||
# check if background or normal
|
||||
if(int(utils.getSetting('progress_mode')) == 0 and not self.override):
|
||||
if(utils.getSettingInt('progress_mode') == 0 and not self.override):
|
||||
self.mode = self.DIALOG
|
||||
self.progressBar = xbmcgui.DialogProgress()
|
||||
else:
|
||||
@@ -31,7 +32,7 @@ class BackupProgressBar:
|
||||
|
||||
# update the progress bar
|
||||
if(self.mode != self.NONE):
|
||||
if(message != None):
|
||||
if(message is not None):
|
||||
# need different calls for dialog and background bars
|
||||
if(self.mode == self.DIALOG):
|
||||
self.progressBar.update(percent, message)
|
||||
|
||||
@@ -1,185 +0,0 @@
|
||||
Copyright 2013 Google Inc. All Rights Reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
@@ -1,174 +0,0 @@
|
||||
class ApiAttribute(object):
|
||||
"""A data descriptor that sets and returns values."""
|
||||
|
||||
def __init__(self, name):
|
||||
"""Create an instance of ApiAttribute.
|
||||
|
||||
:param name: name of this attribute.
|
||||
:type name: str.
|
||||
"""
|
||||
self.name = name
|
||||
|
||||
def __get__(self, obj, type=None):
|
||||
"""Accesses value of this attribute."""
|
||||
return obj.attr.get(self.name)
|
||||
|
||||
def __set__(self, obj, value):
|
||||
"""Write value of this attribute."""
|
||||
obj.attr[self.name] = value
|
||||
if obj.dirty.get(self.name) is not None:
|
||||
obj.dirty[self.name] = True
|
||||
|
||||
def __del__(self, obj=None):
|
||||
"""Delete value of this attribute."""
|
||||
if(obj != None):
|
||||
del obj.attr[self.name]
|
||||
if obj.dirty.get(self.name) is not None:
|
||||
del obj.dirty[self.name]
|
||||
|
||||
|
||||
class ApiAttributeMixin(object):
|
||||
"""Mixin to initialize required global variables to use ApiAttribute."""
|
||||
|
||||
def __init__(self):
|
||||
self.attr = {}
|
||||
self.dirty = {}
|
||||
|
||||
|
||||
class ApiResource(dict):
|
||||
"""Super class of all api resources.
|
||||
|
||||
Inherits and behaves as a python dictionary to handle api resources.
|
||||
Save clean copy of metadata in self.metadata as a dictionary.
|
||||
Provides changed metadata elements to efficiently update api resources.
|
||||
"""
|
||||
auth = ApiAttribute('auth')
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
"""Create an instance of ApiResource."""
|
||||
self.update(*args, **kwargs)
|
||||
|
||||
def __getitem__(self, key):
|
||||
"""Overwritten method of dictionary.
|
||||
|
||||
:param key: key of the query.
|
||||
:type key: str.
|
||||
:returns: value of the query.
|
||||
"""
|
||||
return dict.__getitem__(self, key)
|
||||
|
||||
def __setitem__(self, key, val):
|
||||
"""Overwritten method of dictionary.
|
||||
|
||||
:param key: key of the query.
|
||||
:type key: str.
|
||||
:param val: value of the query.
|
||||
"""
|
||||
dict.__setitem__(self, key, val)
|
||||
|
||||
def __repr__(self):
|
||||
"""Overwritten method of dictionary."""
|
||||
dictrepr = dict.__repr__(self)
|
||||
return '%s(%s)' % (type(self).__name__, dictrepr)
|
||||
|
||||
def update(self, *args, **kwargs):
|
||||
"""Overwritten method of dictionary."""
|
||||
for k, v in dict(*args, **kwargs).iteritems():
|
||||
self[k] = v
|
||||
|
||||
def UpdateMetadata(self, metadata=None):
|
||||
"""Update metadata and mark all of them to be clean."""
|
||||
if metadata:
|
||||
self.update(metadata)
|
||||
self.metadata = dict(self)
|
||||
|
||||
def GetChanges(self):
|
||||
"""Returns changed metadata elements to update api resources efficiently.
|
||||
|
||||
:returns: dict -- changed metadata elements.
|
||||
"""
|
||||
dirty = {}
|
||||
for key in self:
|
||||
if self.metadata.get(key) is None:
|
||||
dirty[key] = self[key]
|
||||
elif self.metadata[key] != self[key]:
|
||||
dirty[key] = self[key]
|
||||
return dirty
|
||||
|
||||
|
||||
class ApiResourceList(ApiAttributeMixin, ApiResource):
|
||||
"""Abstract class of all api list resources.
|
||||
|
||||
Inherits ApiResource and builds iterator to list any API resource.
|
||||
"""
|
||||
metadata = ApiAttribute('metadata')
|
||||
|
||||
def __init__(self, auth=None, metadata=None):
|
||||
"""Create an instance of ApiResourceList.
|
||||
|
||||
:param auth: authorized GoogleAuth instance.
|
||||
:type auth: GoogleAuth.
|
||||
:param metadata: parameter to send to list command.
|
||||
:type metadata: dict.
|
||||
"""
|
||||
ApiAttributeMixin.__init__(self)
|
||||
ApiResource.__init__(self)
|
||||
self.auth = auth
|
||||
self.UpdateMetadata()
|
||||
if metadata:
|
||||
self.update(metadata)
|
||||
|
||||
def __iter__(self):
|
||||
"""Returns iterator object.
|
||||
|
||||
:returns: ApiResourceList -- self
|
||||
"""
|
||||
return self
|
||||
|
||||
def next(self):
|
||||
"""Make API call to list resources and return them.
|
||||
|
||||
Auto updates 'pageToken' everytime it makes API call and
|
||||
raises StopIteration when it reached the end of iteration.
|
||||
|
||||
:returns: list -- list of API resources.
|
||||
:raises: StopIteration
|
||||
"""
|
||||
if 'pageToken' in self and self['pageToken'] is None:
|
||||
raise StopIteration
|
||||
result = self._GetList()
|
||||
self['pageToken'] = self.metadata.get('nextPageToken')
|
||||
return result
|
||||
|
||||
def GetList(self):
|
||||
"""Get list of API resources.
|
||||
|
||||
If 'maxResults' is not specified, it will automatically iterate through
|
||||
every resources available. Otherwise, it will make API call once and
|
||||
update 'pageToken'.
|
||||
|
||||
:returns: list -- list of API resources.
|
||||
"""
|
||||
if self.get('maxResults') is None:
|
||||
self['maxResults'] = 1000
|
||||
result = []
|
||||
for x in self:
|
||||
result.extend(x)
|
||||
del self['maxResults']
|
||||
return result
|
||||
else:
|
||||
return self.next()
|
||||
|
||||
def _GetList(self):
|
||||
"""Helper function which actually makes API call.
|
||||
|
||||
Should be overwritten.
|
||||
|
||||
:raises: NotImplementedError
|
||||
"""
|
||||
raise NotImplementedError
|
||||
|
||||
def Reset(self):
|
||||
"""Resets current iteration"""
|
||||
if 'pageToken' in self:
|
||||
del self['pageToken']
|
||||
@@ -1,415 +0,0 @@
|
||||
import socket
|
||||
import webbrowser
|
||||
import httplib2
|
||||
import oauth2client.clientsecrets as clientsecrets
|
||||
|
||||
from googleapiclient.discovery import build
|
||||
from functools import wraps
|
||||
from oauth2client.client import FlowExchangeError
|
||||
from oauth2client.client import AccessTokenRefreshError
|
||||
from oauth2client.client import OAuth2WebServerFlow
|
||||
from oauth2client.client import OOB_CALLBACK_URN
|
||||
from oauth2client.file import Storage
|
||||
from oauth2client.tools import ClientRedirectHandler
|
||||
from oauth2client.tools import ClientRedirectServer
|
||||
from oauth2client._helpers import scopes_to_string
|
||||
from .apiattr import ApiAttribute
|
||||
from .apiattr import ApiAttributeMixin
|
||||
from .settings import LoadSettingsFile
|
||||
from .settings import ValidateSettings
|
||||
from .settings import SettingsError
|
||||
from .settings import InvalidConfigError
|
||||
|
||||
|
||||
class AuthError(Exception):
|
||||
"""Base error for authentication/authorization errors."""
|
||||
|
||||
|
||||
class InvalidCredentialsError(IOError):
|
||||
"""Error trying to read credentials file."""
|
||||
|
||||
|
||||
class AuthenticationRejected(AuthError):
|
||||
"""User rejected authentication."""
|
||||
|
||||
|
||||
class AuthenticationError(AuthError):
|
||||
"""General authentication error."""
|
||||
|
||||
|
||||
class RefreshError(AuthError):
|
||||
"""Access token refresh error."""
|
||||
|
||||
def LoadAuth(decoratee):
|
||||
"""Decorator to check if the auth is valid and loads auth if not."""
|
||||
@wraps(decoratee)
|
||||
def _decorated(self, *args, **kwargs):
|
||||
if self.auth is None: # Initialize auth if needed.
|
||||
self.auth = GoogleAuth()
|
||||
if self.auth.access_token_expired:
|
||||
self.auth.LocalWebserverAuth()
|
||||
if self.auth.service is None: # Check if drive api is built.
|
||||
self.auth.Authorize()
|
||||
return decoratee(self, *args, **kwargs)
|
||||
return _decorated
|
||||
|
||||
def CheckAuth(decoratee):
|
||||
"""Decorator to check if it requires OAuth2 flow request."""
|
||||
@wraps(decoratee)
|
||||
def _decorated(self, *args, **kwargs):
|
||||
dirty = False
|
||||
code = None
|
||||
save_credentials = self.settings.get('save_credentials')
|
||||
if self.credentials is None and save_credentials:
|
||||
self.LoadCredentials()
|
||||
if self.flow is None:
|
||||
self.GetFlow()
|
||||
if self.credentials is None:
|
||||
code = decoratee(self, *args, **kwargs)
|
||||
dirty = True
|
||||
else:
|
||||
if self.access_token_expired:
|
||||
if self.credentials.refresh_token is not None:
|
||||
self.Refresh()
|
||||
else:
|
||||
code = decoratee(self, *args, **kwargs)
|
||||
dirty = True
|
||||
if code is not None:
|
||||
self.Auth(code)
|
||||
if dirty and save_credentials:
|
||||
self.SaveCredentials()
|
||||
return _decorated
|
||||
|
||||
|
||||
class GoogleAuth(ApiAttributeMixin, object):
|
||||
"""Wrapper class for oauth2client library in google-api-python-client.
|
||||
|
||||
Loads all settings and credentials from one 'settings.yaml' file
|
||||
and performs common OAuth2.0 related functionality such as authentication
|
||||
and authorization.
|
||||
"""
|
||||
DEFAULT_SETTINGS = {
|
||||
'client_config_backend': 'file',
|
||||
'client_config_file': 'client_secrets.json',
|
||||
'save_credentials': False,
|
||||
'oauth_scope': ['https://www.googleapis.com/auth/drive']
|
||||
}
|
||||
CLIENT_CONFIGS_LIST = ['client_id', 'client_secret', 'auth_uri',
|
||||
'token_uri', 'revoke_uri', 'redirect_uri']
|
||||
settings = ApiAttribute('settings')
|
||||
client_config = ApiAttribute('client_config')
|
||||
flow = ApiAttribute('flow')
|
||||
credentials = ApiAttribute('credentials')
|
||||
http = ApiAttribute('http')
|
||||
service = ApiAttribute('service')
|
||||
|
||||
def __init__(self, settings_file='settings.yaml'):
|
||||
"""Create an instance of GoogleAuth.
|
||||
|
||||
This constructor just sets the path of settings file.
|
||||
It does not actually read the file.
|
||||
|
||||
:param settings_file: path of settings file. 'settings.yaml' by default.
|
||||
:type settings_file: str.
|
||||
"""
|
||||
ApiAttributeMixin.__init__(self)
|
||||
self.client_config = {}
|
||||
try:
|
||||
self.settings = LoadSettingsFile(settings_file)
|
||||
except SettingsError:
|
||||
self.settings = self.DEFAULT_SETTINGS
|
||||
else:
|
||||
if self.settings is None:
|
||||
self.settings = self.DEFAULT_SETTINGS
|
||||
else:
|
||||
ValidateSettings(self.settings)
|
||||
|
||||
@property
|
||||
def access_token_expired(self):
|
||||
"""Checks if access token doesn't exist or is expired.
|
||||
|
||||
:returns: bool -- True if access token doesn't exist or is expired.
|
||||
"""
|
||||
if self.credentials is None:
|
||||
return True
|
||||
return self.credentials.access_token_expired
|
||||
|
||||
@CheckAuth
|
||||
def LocalWebserverAuth(self, host_name='localhost',
|
||||
port_numbers=[8080, 8090]):
|
||||
"""Authenticate and authorize from user by creating local webserver and
|
||||
retrieving authentication code.
|
||||
|
||||
This function is not for webserver application. It creates local webserver
|
||||
for user from standalone application.
|
||||
|
||||
:param host_name: host name of the local webserver.
|
||||
:type host_name: str.
|
||||
:param port_numbers: list of port numbers to be tried to used.
|
||||
:type port_numbers: list.
|
||||
:returns: str -- code returned from local webserver
|
||||
:raises: AuthenticationRejected, AuthenticationError
|
||||
"""
|
||||
success = False
|
||||
port_number = 0
|
||||
for port in port_numbers:
|
||||
port_number = port
|
||||
try:
|
||||
httpd = ClientRedirectServer((host_name, port), ClientRedirectHandler)
|
||||
except socket.error as e:
|
||||
pass
|
||||
else:
|
||||
success = True
|
||||
break
|
||||
if success:
|
||||
oauth_callback = 'http://%s:%s/' % (host_name, port_number)
|
||||
else:
|
||||
raise AuthenticationError()
|
||||
self.flow.redirect_uri = oauth_callback
|
||||
authorize_url = self.GetAuthUrl()
|
||||
webbrowser.open(authorize_url, new=1, autoraise=True)
|
||||
httpd.handle_request()
|
||||
if 'error' in httpd.query_params:
|
||||
raise AuthenticationRejected('User rejected authentication')
|
||||
if 'code' in httpd.query_params:
|
||||
return httpd.query_params['code']
|
||||
else:
|
||||
raise AuthenticationError('No code found in redirect')
|
||||
|
||||
@CheckAuth
|
||||
def CommandLineAuth(self):
|
||||
"""Authenticate and authorize from user by printing authentication url
|
||||
retrieving authentication code from command-line.
|
||||
|
||||
:returns: str -- code returned from commandline.
|
||||
"""
|
||||
self.flow.redirect_uri = OOB_CALLBACK_URN
|
||||
authorize_url = self.GetAuthUrl()
|
||||
return raw_input('Enter verification code: ').strip()
|
||||
|
||||
def LoadCredentials(self, backend=None):
|
||||
"""Loads credentials or create empty credentials if it doesn't exist.
|
||||
|
||||
:param backend: target backend to save credential to.
|
||||
:type backend: str.
|
||||
:raises: InvalidConfigError
|
||||
"""
|
||||
if backend is None:
|
||||
backend = self.settings.get('save_credentials_backend')
|
||||
if backend is None:
|
||||
raise InvalidConfigError('Please specify credential backend')
|
||||
if backend == 'file':
|
||||
self.LoadCredentialsFile()
|
||||
else:
|
||||
raise InvalidConfigError('Unknown save_credentials_backend')
|
||||
|
||||
def LoadCredentialsFile(self, credentials_file=None):
|
||||
"""Loads credentials or create empty credentials if it doesn't exist.
|
||||
|
||||
Loads credentials file from path in settings if not specified.
|
||||
|
||||
:param credentials_file: path of credentials file to read.
|
||||
:type credentials_file: str.
|
||||
:raises: InvalidConfigError, InvalidCredentialsError
|
||||
"""
|
||||
if credentials_file is None:
|
||||
credentials_file = self.settings.get('save_credentials_file')
|
||||
if credentials_file is None:
|
||||
raise InvalidConfigError('Please specify credentials file to read')
|
||||
try:
|
||||
storage = Storage(credentials_file)
|
||||
self.credentials = storage.get()
|
||||
except IOError:
|
||||
raise InvalidCredentialsError('Credentials file cannot be symbolic link')
|
||||
|
||||
def SaveCredentials(self, backend=None):
|
||||
"""Saves credentials according to specified backend.
|
||||
|
||||
If you have any specific credentials backend in mind, don't use this
|
||||
function and use the corresponding function you want.
|
||||
|
||||
:param backend: backend to save credentials.
|
||||
:type backend: str.
|
||||
:raises: InvalidConfigError
|
||||
"""
|
||||
if backend is None:
|
||||
backend = self.settings.get('save_credentials_backend')
|
||||
if backend is None:
|
||||
raise InvalidConfigError('Please specify credential backend')
|
||||
if backend == 'file':
|
||||
self.SaveCredentialsFile()
|
||||
else:
|
||||
raise InvalidConfigError('Unknown save_credentials_backend')
|
||||
|
||||
def SaveCredentialsFile(self, credentials_file=None):
|
||||
"""Saves credentials to the file in JSON format.
|
||||
|
||||
:param credentials_file: destination to save file to.
|
||||
:type credentials_file: str.
|
||||
:raises: InvalidConfigError, InvalidCredentialsError
|
||||
"""
|
||||
if self.credentials is None:
|
||||
raise InvalidCredentialsError('No credentials to save')
|
||||
if credentials_file is None:
|
||||
credentials_file = self.settings.get('save_credentials_file')
|
||||
if credentials_file is None:
|
||||
raise InvalidConfigError('Please specify credentials file to read')
|
||||
try:
|
||||
storage = Storage(credentials_file)
|
||||
storage.put(self.credentials)
|
||||
self.credentials.set_store(storage)
|
||||
except CredentialsFileSymbolicLinkError:
|
||||
raise InvalidCredentialsError('Credentials file cannot be symbolic link')
|
||||
|
||||
def LoadClientConfig(self, backend=None):
|
||||
"""Loads client configuration according to specified backend.
|
||||
|
||||
If you have any specific backend to load client configuration from in mind,
|
||||
don't use this function and use the corresponding function you want.
|
||||
|
||||
:param backend: backend to load client configuration from.
|
||||
:type backend: str.
|
||||
:raises: InvalidConfigError
|
||||
"""
|
||||
if backend is None:
|
||||
backend = self.settings.get('client_config_backend')
|
||||
if backend is None:
|
||||
raise InvalidConfigError('Please specify client config backend')
|
||||
if backend == 'file':
|
||||
self.LoadClientConfigFile()
|
||||
elif backend == 'settings':
|
||||
self.LoadClientConfigSettings()
|
||||
else:
|
||||
raise InvalidConfigError('Unknown client_config_backend')
|
||||
|
||||
def LoadClientConfigFile(self, client_config_file=None):
|
||||
"""Loads client configuration file downloaded from APIs console.
|
||||
|
||||
Loads client config file from path in settings if not specified.
|
||||
|
||||
:param client_config_file: path of client config file to read.
|
||||
:type client_config_file: str.
|
||||
:raises: InvalidConfigError
|
||||
"""
|
||||
if client_config_file is None:
|
||||
client_config_file = self.settings['client_config_file']
|
||||
try:
|
||||
client_type, client_info = clientsecrets.loadfile(client_config_file)
|
||||
except clientsecrets.InvalidClientSecretsError as error:
|
||||
raise InvalidConfigError('Invalid client secrets file %s' % error)
|
||||
if not client_type in (clientsecrets.TYPE_WEB,
|
||||
clientsecrets.TYPE_INSTALLED):
|
||||
raise InvalidConfigError('Unknown client_type of client config file')
|
||||
try:
|
||||
config_index = ['client_id', 'client_secret', 'auth_uri', 'token_uri']
|
||||
for config in config_index:
|
||||
self.client_config[config] = client_info[config]
|
||||
self.client_config['revoke_uri'] = client_info.get('revoke_uri')
|
||||
self.client_config['redirect_uri'] = client_info['redirect_uris'][0]
|
||||
except KeyError:
|
||||
raise InvalidConfigError('Insufficient client config in file')
|
||||
|
||||
def LoadClientConfigSettings(self):
|
||||
"""Loads client configuration from settings file.
|
||||
|
||||
:raises: InvalidConfigError
|
||||
"""
|
||||
|
||||
for config in self.CLIENT_CONFIGS_LIST:
|
||||
try:
|
||||
self.client_config[config] = self.settings['client_config'][config]
|
||||
|
||||
except KeyError:
|
||||
raise InvalidConfigError('Insufficient client config in settings')
|
||||
|
||||
def GetFlow(self):
|
||||
"""Gets Flow object from client configuration.
|
||||
|
||||
:raises: InvalidConfigError
|
||||
"""
|
||||
if not all(config in self.client_config \
|
||||
for config in self.CLIENT_CONFIGS_LIST):
|
||||
self.LoadClientConfig()
|
||||
constructor_kwargs = {
|
||||
'redirect_uri': self.client_config['redirect_uri'],
|
||||
'auth_uri': self.client_config['auth_uri'],
|
||||
'token_uri': self.client_config['token_uri'],
|
||||
}
|
||||
if self.client_config['revoke_uri'] is not None:
|
||||
constructor_kwargs['revoke_uri'] = self.client_config['revoke_uri']
|
||||
self.flow = OAuth2WebServerFlow(
|
||||
self.client_config['client_id'],
|
||||
self.client_config['client_secret'],
|
||||
scopes_to_string(self.settings['oauth_scope']),
|
||||
**constructor_kwargs)
|
||||
if self.settings.get('get_refresh_token'):
|
||||
self.flow.params.update({'access_type': 'offline'})
|
||||
|
||||
def Refresh(self):
|
||||
"""Refreshes the access_token.
|
||||
|
||||
:raises: RefreshError
|
||||
"""
|
||||
if self.credentials is None:
|
||||
raise RefreshError('No credential to refresh.')
|
||||
if self.credentials.refresh_token is None:
|
||||
raise RefreshError('No refresh_token found.'
|
||||
'Please set access_type of OAuth to offline.')
|
||||
if self.http is None:
|
||||
self.http = httplib2.Http()
|
||||
try:
|
||||
self.credentials.refresh(self.http)
|
||||
except AccessTokenRefreshError as error:
|
||||
raise RefreshError('Access token refresh failed: %s' % error)
|
||||
|
||||
def GetAuthUrl(self, keys = None):
|
||||
"""Creates authentication url where user visits to grant access.
|
||||
|
||||
:returns: str -- Authentication url.
|
||||
"""
|
||||
|
||||
if(keys != None):
|
||||
#update some of the settings in the client_config dict
|
||||
self.client_config['client_id'] = keys['client_id']
|
||||
self.client_config['client_secret'] = keys['client_secret']
|
||||
|
||||
if self.flow is None:
|
||||
self.GetFlow()
|
||||
|
||||
return self.flow.step1_get_authorize_url()
|
||||
|
||||
def Auth(self, code):
|
||||
"""Authenticate, authorize, and build service.
|
||||
|
||||
:param code: Code for authentication.
|
||||
:type code: str.
|
||||
:raises: AuthenticationError
|
||||
"""
|
||||
self.Authenticate(code)
|
||||
self.Authorize()
|
||||
|
||||
def Authenticate(self, code):
|
||||
"""Authenticates given authentication code back from user.
|
||||
|
||||
:param code: Code for authentication.
|
||||
:type code: str.
|
||||
:raises: AuthenticationError
|
||||
"""
|
||||
if self.flow is None:
|
||||
self.GetFlow()
|
||||
try:
|
||||
self.credentials = self.flow.step2_exchange(code)
|
||||
except FlowExchangeError as e:
|
||||
raise AuthenticationError('OAuth2 code exchange failed: %s' % e)
|
||||
|
||||
def Authorize(self):
|
||||
"""Authorizes and builds service.
|
||||
|
||||
:raises: AuthenticationError
|
||||
"""
|
||||
if self.http is None:
|
||||
self.http = httplib2.Http()
|
||||
if self.access_token_expired:
|
||||
raise AuthenticationError('No valid credentials provided to authorize')
|
||||
self.http = self.credentials.authorize(self.http)
|
||||
self.service = build('drive', 'v2', http=self.http)
|
||||
@@ -1,38 +0,0 @@
|
||||
from .apiattr import ApiAttributeMixin
|
||||
from .files import GoogleDriveFile
|
||||
from .files import GoogleDriveFileList
|
||||
|
||||
|
||||
class GoogleDrive(ApiAttributeMixin, object):
|
||||
"""Main Google Drive class."""
|
||||
|
||||
def __init__(self, auth=None):
|
||||
"""Create an instance of GoogleDrive.
|
||||
|
||||
:param auth: authorized GoogleAuth instance.
|
||||
:type auth: pydrive.auth.GoogleAuth.
|
||||
"""
|
||||
ApiAttributeMixin.__init__(self)
|
||||
self.auth = auth
|
||||
|
||||
def CreateFile(self, metadata=None):
|
||||
"""Create an instance of GoogleDriveFile with auth of this instance.
|
||||
|
||||
This method would not upload a file to GoogleDrive.
|
||||
|
||||
:param metadata: file resource to initialize GoogleDriveFile with.
|
||||
:type metadata: dict.
|
||||
:returns: pydrive.files.GoogleDriveFile -- initialized with auth of this instance.
|
||||
"""
|
||||
return GoogleDriveFile(auth=self.auth, metadata=metadata)
|
||||
|
||||
def ListFile(self, param=None):
|
||||
"""Create an instance of GoogleDriveFileList with auth of this instance.
|
||||
|
||||
This method will not fetch from Files.List().
|
||||
|
||||
:param param: parameter to be sent to Files.List().
|
||||
:type param: dict.
|
||||
:returns: pydrive.files.GoogleDriveFileList -- initialized with auth of this instance.
|
||||
"""
|
||||
return GoogleDriveFileList(auth=self.auth, param=param)
|
||||
@@ -1,322 +0,0 @@
|
||||
import io
|
||||
import mimetypes
|
||||
|
||||
from googleapiclient import errors
|
||||
from googleapiclient.http import MediaIoBaseUpload
|
||||
from functools import wraps
|
||||
|
||||
from .apiattr import ApiAttribute
|
||||
from .apiattr import ApiAttributeMixin
|
||||
from .apiattr import ApiResource
|
||||
from .apiattr import ApiResourceList
|
||||
from .auth import LoadAuth
|
||||
|
||||
|
||||
class FileNotUploadedError(RuntimeError):
|
||||
"""Error trying to access metadata of file that is not uploaded."""
|
||||
|
||||
|
||||
class ApiRequestError(IOError):
|
||||
"""Error while making any API requests."""
|
||||
|
||||
|
||||
class FileNotDownloadableError(RuntimeError):
|
||||
"""Error trying to download file that is not downloadable."""
|
||||
|
||||
|
||||
def LoadMetadata(decoratee):
|
||||
"""Decorator to check if the file has metadata and fetches it if not.
|
||||
|
||||
:raises: ApiRequestError, FileNotUploadedError
|
||||
"""
|
||||
@wraps(decoratee)
|
||||
def _decorated(self, *args, **kwargs):
|
||||
if not self.uploaded:
|
||||
self.FetchMetadata()
|
||||
return decoratee(self, *args, **kwargs)
|
||||
return _decorated
|
||||
|
||||
|
||||
class GoogleDriveFileList(ApiResourceList):
|
||||
"""Google Drive FileList instance.
|
||||
|
||||
Equivalent to Files.list() in Drive APIs.
|
||||
"""
|
||||
|
||||
def __init__(self, auth=None, param=None):
|
||||
"""Create an instance of GoogleDriveFileList."""
|
||||
super(GoogleDriveFileList, self).__init__(auth=auth, metadata=param)
|
||||
|
||||
@LoadAuth
|
||||
def _GetList(self):
|
||||
"""Overwritten method which actually makes API call to list files.
|
||||
|
||||
:returns: list -- list of pydrive.files.GoogleDriveFile.
|
||||
"""
|
||||
self.metadata = self.auth.service.files().list(**dict(self)).execute()
|
||||
result = []
|
||||
for file_metadata in self.metadata['items']:
|
||||
tmp_file = GoogleDriveFile(
|
||||
auth=self.auth,
|
||||
metadata=file_metadata,
|
||||
uploaded=True)
|
||||
result.append(tmp_file)
|
||||
return result
|
||||
|
||||
|
||||
class GoogleDriveFile(ApiAttributeMixin, ApiResource):
|
||||
"""Google Drive File instance.
|
||||
|
||||
Inherits ApiResource which inherits dict.
|
||||
Can access and modify metadata like dictionary.
|
||||
"""
|
||||
content = ApiAttribute('content')
|
||||
uploaded = ApiAttribute('uploaded')
|
||||
metadata = ApiAttribute('metadata')
|
||||
|
||||
def __init__(self, auth=None, metadata=None, uploaded=False):
|
||||
"""Create an instance of GoogleDriveFile.
|
||||
|
||||
:param auth: authorized GoogleAuth instance.
|
||||
:type auth: pydrive.auth.GoogleAuth
|
||||
:param metadata: file resource to initialize GoogleDirveFile with.
|
||||
:type metadata: dict.
|
||||
:param uploaded: True if this file is confirmed to be uploaded.
|
||||
:type uploaded: bool.
|
||||
"""
|
||||
ApiAttributeMixin.__init__(self)
|
||||
ApiResource.__init__(self)
|
||||
self.metadata = {}
|
||||
self.dirty = {'content': False}
|
||||
self.auth = auth
|
||||
self.uploaded = uploaded
|
||||
if uploaded:
|
||||
self.UpdateMetadata(metadata)
|
||||
elif metadata:
|
||||
self.update(metadata)
|
||||
|
||||
def __getitem__(self, key):
|
||||
"""Overwrites manner of accessing Files resource.
|
||||
|
||||
If this file instance is not uploaded and id is specified,
|
||||
it will try to look for metadata with Files.get().
|
||||
|
||||
:param key: key of dictionary query.
|
||||
:type key: str.
|
||||
:returns: value of Files resource
|
||||
:raises: KeyError, FileNotUploadedError
|
||||
"""
|
||||
try:
|
||||
return dict.__getitem__(self, key)
|
||||
except KeyError as e:
|
||||
if self.uploaded:
|
||||
raise KeyError(e)
|
||||
if self.get('id'):
|
||||
self.FetchMetadata()
|
||||
return dict.__getitem__(self, key)
|
||||
else:
|
||||
raise FileNotUploadedError()
|
||||
|
||||
def SetContentString(self, content):
|
||||
"""Set content of this file to be a string.
|
||||
|
||||
Creates io.BytesIO instance of utf-8 encoded string.
|
||||
Sets mimeType to be 'text/plain' if not specified.
|
||||
|
||||
:param content: content of the file in string.
|
||||
:type content: str.
|
||||
"""
|
||||
self.content = io.BytesIO(content.encode('utf-8'))
|
||||
if self.get('mimeType') is None:
|
||||
self['mimeType'] = 'text/plain'
|
||||
|
||||
def SetContentFile(self, filename):
|
||||
"""Set content of this file from a file.
|
||||
|
||||
Opens the file specified by this method.
|
||||
Will be read, uploaded, and closed by Upload() method.
|
||||
Sets metadata 'title' and 'mimeType' automatically if not specified.
|
||||
|
||||
:param filename: name of the file to be uploaded.
|
||||
:type filename: str.
|
||||
"""
|
||||
self.content = open(filename, 'rb')
|
||||
|
||||
if self.get('title') is None:
|
||||
self['title'] = filename
|
||||
if self.get('mimeType') is None:
|
||||
self['mimeType'] = mimetypes.guess_type(filename)[0]
|
||||
|
||||
def GetContentString(self):
|
||||
"""Get content of this file as a string.
|
||||
|
||||
:returns: str -- utf-8 decoded content of the file
|
||||
:raises: ApiRequestError, FileNotUploadedError, FileNotDownloadableError
|
||||
"""
|
||||
if self.content is None or type(self.content) is not io.BytesIO:
|
||||
self.FetchContent()
|
||||
return self.content.getvalue().decode('utf-8')
|
||||
|
||||
def GetContentFile(self, filename, mimetype=None):
|
||||
"""Save content of this file as a local file.
|
||||
|
||||
:param filename: name of the file to write to.
|
||||
:type filename: str.
|
||||
:raises: ApiRequestError, FileNotUploadedError, FileNotDownloadableError
|
||||
"""
|
||||
if self.content is None or type(self.content) is not io.BytesIO:
|
||||
self.FetchContent(mimetype)
|
||||
f = open(filename, 'wb')
|
||||
f.write(self.content.getvalue())
|
||||
f.close()
|
||||
|
||||
@LoadAuth
|
||||
def FetchMetadata(self):
|
||||
"""Download file's metadata from id using Files.get().
|
||||
|
||||
:raises: ApiRequestError, FileNotUploadedError
|
||||
"""
|
||||
file_id = self.metadata.get('id') or self.get('id')
|
||||
if file_id:
|
||||
try:
|
||||
metadata = self.auth.service.files().get(fileId=file_id).execute()
|
||||
except errors.HttpError as error:
|
||||
raise ApiRequestError(error)
|
||||
else:
|
||||
self.uploaded = True
|
||||
self.UpdateMetadata(metadata)
|
||||
else:
|
||||
raise FileNotUploadedError()
|
||||
|
||||
@LoadMetadata
|
||||
def FetchContent(self, mimetype=None):
|
||||
"""Download file's content from download_url.
|
||||
|
||||
:raises: ApiRequestError, FileNotUploadedError, FileNotDownloadableError
|
||||
"""
|
||||
download_url = self.metadata.get('downloadUrl')
|
||||
if download_url:
|
||||
self.content = io.BytesIO(self._DownloadFromUrl(download_url))
|
||||
self.dirty['content'] = False
|
||||
return
|
||||
|
||||
export_links = self.metadata.get('exportLinks')
|
||||
if export_links and export_links.get(mimetype):
|
||||
self.content = io.BytesIO(
|
||||
self._DownloadFromUrl(export_links.get(mimetype)))
|
||||
self.dirty['content'] = False
|
||||
return
|
||||
|
||||
raise FileNotDownloadableError(
|
||||
'No downloadLink/exportLinks for mimetype found in metadata')
|
||||
|
||||
def Upload(self, param=None):
|
||||
"""Upload/update file by choosing the most efficient method.
|
||||
|
||||
:param param: additional parameter to upload file.
|
||||
:type param: dict.
|
||||
:raises: ApiRequestError
|
||||
"""
|
||||
if self.uploaded or self.get('id') is not None:
|
||||
if self.dirty['content']:
|
||||
self._FilesUpdate(param=param)
|
||||
else:
|
||||
self._FilesPatch(param=param)
|
||||
else:
|
||||
self._FilesInsert(param=param)
|
||||
|
||||
def Delete(self):
|
||||
if self.get('id') is not None:
|
||||
self.auth.service.files().delete(fileId=self.get('id')).execute()
|
||||
|
||||
@LoadAuth
|
||||
def _FilesInsert(self, param=None):
|
||||
"""Upload a new file using Files.insert().
|
||||
|
||||
:param param: additional parameter to upload file.
|
||||
:type param: dict.
|
||||
:raises: ApiRequestError
|
||||
"""
|
||||
if param is None:
|
||||
param = {}
|
||||
param['body'] = self.GetChanges()
|
||||
try:
|
||||
if self.dirty['content']:
|
||||
param['media_body'] = self._BuildMediaBody()
|
||||
metadata = self.auth.service.files().insert(**param).execute()
|
||||
except errors.HttpError as error:
|
||||
raise ApiRequestError(error)
|
||||
else:
|
||||
self.uploaded = True
|
||||
self.dirty['content'] = False
|
||||
self.UpdateMetadata(metadata)
|
||||
|
||||
@LoadAuth
|
||||
@LoadMetadata
|
||||
def _FilesUpdate(self, param=None):
|
||||
"""Update metadata and/or content using Files.Update().
|
||||
|
||||
:param param: additional parameter to upload file.
|
||||
:type param: dict.
|
||||
:raises: ApiRequestError, FileNotUploadedError
|
||||
"""
|
||||
if param is None:
|
||||
param = {}
|
||||
param['body'] = self.GetChanges()
|
||||
param['fileId'] = self.metadata.get('id')
|
||||
try:
|
||||
if self.dirty['content']:
|
||||
param['media_body'] = self._BuildMediaBody()
|
||||
metadata = self.auth.service.files().update(**param).execute()
|
||||
except errors.HttpError as error:
|
||||
raise ApiRequestError(error)
|
||||
else:
|
||||
self.uploaded = True
|
||||
self.dirty['content'] = False
|
||||
self.UpdateMetadata(metadata)
|
||||
|
||||
@LoadAuth
|
||||
@LoadMetadata
|
||||
def _FilesPatch(self, param=None):
|
||||
"""Update metadata using Files.Patch().
|
||||
|
||||
:param param: additional parameter to upload file.
|
||||
:type param: dict.
|
||||
:raises: ApiRequestError, FileNotUploadedError
|
||||
"""
|
||||
if param is None:
|
||||
param = {}
|
||||
param['body'] = self.GetChanges()
|
||||
param['fileId'] = self.metadata.get('id')
|
||||
try:
|
||||
metadata = self.auth.service.files().patch(**param).execute()
|
||||
except errors.HttpError as error:
|
||||
raise ApiRequestError(error)
|
||||
else:
|
||||
self.UpdateMetadata(metadata)
|
||||
|
||||
def _BuildMediaBody(self):
|
||||
"""Build MediaIoBaseUpload to get prepared to upload content of the file.
|
||||
|
||||
Sets mimeType as 'application/octet-stream' if not specified.
|
||||
|
||||
:returns: MediaIoBaseUpload -- instance that will be used to upload content.
|
||||
"""
|
||||
if self.get('mimeType') is None:
|
||||
self['mimeType'] = 'application/octet-stream'
|
||||
|
||||
return MediaIoBaseUpload(self.content, self['mimeType'])
|
||||
|
||||
@LoadAuth
|
||||
def _DownloadFromUrl(self, url):
|
||||
"""Download file from url using provided credential.
|
||||
|
||||
:param url: link of the file to download.
|
||||
:type url: str.
|
||||
:returns: str -- content of downloaded file in string.
|
||||
:raises: ApiRequestError
|
||||
"""
|
||||
resp, content = self.auth.service._http.request(url)
|
||||
if resp.status != 200:
|
||||
raise ApiRequestError('Cannot download file: %s' % resp)
|
||||
return content
|
||||
@@ -1,192 +0,0 @@
|
||||
from yaml import load
|
||||
from yaml import YAMLError
|
||||
try:
|
||||
from yaml import CLoader as Loader
|
||||
except ImportError:
|
||||
from yaml import Loader
|
||||
|
||||
SETTINGS_FILE = 'settings.yaml'
|
||||
SETTINGS_STRUCT = {
|
||||
'client_config_backend': {
|
||||
'type': str,
|
||||
'required': True,
|
||||
'default': 'file',
|
||||
'dependency': [
|
||||
{
|
||||
'value': 'file',
|
||||
'attribute': ['client_config_file']
|
||||
},
|
||||
{
|
||||
'value': 'settings',
|
||||
'attribute': ['client_config']
|
||||
}
|
||||
]
|
||||
},
|
||||
'save_credentials': {
|
||||
'type': bool,
|
||||
'required': True,
|
||||
'default': False,
|
||||
'dependency': [
|
||||
{
|
||||
'value': True,
|
||||
'attribute': ['save_credentials_backend']
|
||||
}
|
||||
]
|
||||
},
|
||||
'get_refresh_token': {
|
||||
'type': bool,
|
||||
'required': False,
|
||||
'default': False
|
||||
},
|
||||
'client_config_file': {
|
||||
'type': str,
|
||||
'required': False,
|
||||
'default': 'client_secrets.json'
|
||||
},
|
||||
'save_credentials_backend': {
|
||||
'type': str,
|
||||
'required': False,
|
||||
'dependency': [
|
||||
{
|
||||
'value': 'file',
|
||||
'attribute': ['save_credentials_file']
|
||||
}
|
||||
]
|
||||
},
|
||||
'client_config': {
|
||||
'type': dict,
|
||||
'required': False,
|
||||
'struct': {
|
||||
'client_id': {
|
||||
'type': str,
|
||||
'required': True,
|
||||
'default':'blank'
|
||||
},
|
||||
'client_secret': {
|
||||
'type': str,
|
||||
'required': True,
|
||||
'default':'blank'
|
||||
},
|
||||
'auth_uri': {
|
||||
'type': str,
|
||||
'required': True,
|
||||
'default': 'https://accounts.google.com/o/oauth2/auth'
|
||||
},
|
||||
'token_uri': {
|
||||
'type': str,
|
||||
'required': True,
|
||||
'default': 'https://accounts.google.com/o/oauth2/token'
|
||||
},
|
||||
'redirect_uri': {
|
||||
'type': str,
|
||||
'required': True,
|
||||
'default': 'urn:ietf:wg:oauth:2.0:oob'
|
||||
},
|
||||
'revoke_uri': {
|
||||
'type': str,
|
||||
'required': True,
|
||||
'default': None
|
||||
}
|
||||
}
|
||||
},
|
||||
'oauth_scope': {
|
||||
'type': list,
|
||||
'required': True,
|
||||
'struct': str,
|
||||
'default': ['https://www.googleapis.com/auth/drive']
|
||||
},
|
||||
'save_credentials_file': {
|
||||
'type': str,
|
||||
'required': False,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class SettingsError(IOError):
|
||||
"""Error while loading/saving settings"""
|
||||
|
||||
|
||||
class InvalidConfigError(IOError):
|
||||
"""Error trying to read client configuration."""
|
||||
|
||||
|
||||
def LoadSettingsFile(filename=SETTINGS_FILE):
|
||||
"""Loads settings file in yaml format given file name.
|
||||
|
||||
:param filename: path for settings file. 'settings.yaml' by default.
|
||||
:type filename: str.
|
||||
:raises: SettingsError
|
||||
"""
|
||||
try:
|
||||
stream = file(filename, 'r')
|
||||
data = load(stream, Loader=Loader)
|
||||
except (YAMLError, IOError) as e:
|
||||
raise SettingsError(e)
|
||||
return data
|
||||
|
||||
|
||||
def ValidateSettings(data):
|
||||
"""Validates if current settings is valid.
|
||||
|
||||
:param data: dictionary containing all settings.
|
||||
:type data: dict.
|
||||
:raises: InvalidConfigError
|
||||
"""
|
||||
_ValidateSettingsStruct(data, SETTINGS_STRUCT)
|
||||
|
||||
|
||||
def _ValidateSettingsStruct(data, struct):
|
||||
"""Validates if provided data fits provided structure.
|
||||
|
||||
:param data: dictionary containing settings.
|
||||
:type data: dict.
|
||||
:param struct: dictionary containing structure information of settings.
|
||||
:type struct: dict.
|
||||
:raises: InvalidConfigError
|
||||
"""
|
||||
# Validate required elements of the setting.
|
||||
for key in struct:
|
||||
if struct[key]['required']:
|
||||
_ValidateSettingsElement(data, struct, key)
|
||||
|
||||
|
||||
def _ValidateSettingsElement(data, struct, key):
|
||||
"""Validates if provided element of settings data fits provided structure.
|
||||
|
||||
:param data: dictionary containing settings.
|
||||
:type data: dict.
|
||||
:param struct: dictionary containing structure information of settings.
|
||||
:type struct: dict.
|
||||
:param key: key of the settings element to validate.
|
||||
:type key: str.
|
||||
:raises: InvalidConfigError
|
||||
"""
|
||||
# Check if data exists. If not, check if default value exists.
|
||||
value = data.get(key)
|
||||
data_type = struct[key]['type']
|
||||
if value is None:
|
||||
try:
|
||||
default = struct[key]['default']
|
||||
except KeyError:
|
||||
raise InvalidConfigError('Missing required setting %s' % key)
|
||||
else:
|
||||
data[key] = default
|
||||
# If data exists, Check type of the data
|
||||
elif type(value) is not data_type:
|
||||
raise InvalidConfigError('Setting %s should be type %s' % (key, data_type))
|
||||
# If type of this data is dict, check if structure of the data is valid.
|
||||
if data_type is dict:
|
||||
_ValidateSettingsStruct(data[key], struct[key]['struct'])
|
||||
# If type of this data is list, check if all values in the list is valid.
|
||||
elif data_type is list:
|
||||
for element in data[key]:
|
||||
if type(element) is not struct[key]['struct']:
|
||||
raise InvalidConfigError('Setting %s should be list of %s' %
|
||||
(key, struct[key]['struct']))
|
||||
# Check dependency of this attribute.
|
||||
dependencies = struct[key].get('dependency')
|
||||
if dependencies:
|
||||
for dependency in dependencies:
|
||||
if value == dependency['value']:
|
||||
for reqkey in dependency['attribute']:
|
||||
_ValidateSettingsElement(data, struct, reqkey)
|
||||
@@ -1,7 +0,0 @@
|
||||
client_config_backend: 'settings'
|
||||
client_config:
|
||||
client_id: "blank"
|
||||
client_secret: "blank"
|
||||
get_refresh_token: True
|
||||
oauth_scope:
|
||||
- "https://www.googleapis.com/auth/drive.file"
|
||||
@@ -1,430 +0,0 @@
|
||||
"""
|
||||
Copyright (c) 2003-2010 Gustavo Niemeyer <gustavo@niemeyer.net>
|
||||
|
||||
This module offers extensions to the standard python 2.3+
|
||||
datetime module.
|
||||
"""
|
||||
__author__ = "Gustavo Niemeyer <gustavo@niemeyer.net>"
|
||||
__license__ = "PSF License"
|
||||
|
||||
import datetime
|
||||
import calendar
|
||||
|
||||
__all__ = ["relativedelta", "MO", "TU", "WE", "TH", "FR", "SA", "SU"]
|
||||
|
||||
class weekday(object):
|
||||
__slots__ = ["weekday", "n"]
|
||||
|
||||
def __init__(self, weekday, n=None):
|
||||
self.weekday = weekday
|
||||
self.n = n
|
||||
|
||||
def __call__(self, n):
|
||||
if n == self.n:
|
||||
return self
|
||||
else:
|
||||
return self.__class__(self.weekday, n)
|
||||
|
||||
def __eq__(self, other):
|
||||
try:
|
||||
if self.weekday != other.weekday or self.n != other.n:
|
||||
return False
|
||||
except AttributeError:
|
||||
return False
|
||||
return True
|
||||
|
||||
def __repr__(self):
|
||||
s = ("MO", "TU", "WE", "TH", "FR", "SA", "SU")[self.weekday]
|
||||
if not self.n:
|
||||
return s
|
||||
else:
|
||||
return "%s(%+d)" % (s, self.n)
|
||||
|
||||
MO, TU, WE, TH, FR, SA, SU = weekdays = tuple([weekday(x) for x in range(7)])
|
||||
|
||||
class relativedelta:
|
||||
"""
|
||||
The relativedelta type is based on the specification of the excelent
|
||||
work done by M.-A. Lemburg in his mx.DateTime extension. However,
|
||||
notice that this type does *NOT* implement the same algorithm as
|
||||
his work. Do *NOT* expect it to behave like mx.DateTime's counterpart.
|
||||
|
||||
There's two different ways to build a relativedelta instance. The
|
||||
first one is passing it two date/datetime classes:
|
||||
|
||||
relativedelta(datetime1, datetime2)
|
||||
|
||||
And the other way is to use the following keyword arguments:
|
||||
|
||||
year, month, day, hour, minute, second, microsecond:
|
||||
Absolute information.
|
||||
|
||||
years, months, weeks, days, hours, minutes, seconds, microseconds:
|
||||
Relative information, may be negative.
|
||||
|
||||
weekday:
|
||||
One of the weekday instances (MO, TU, etc). These instances may
|
||||
receive a parameter N, specifying the Nth weekday, which could
|
||||
be positive or negative (like MO(+1) or MO(-2). Not specifying
|
||||
it is the same as specifying +1. You can also use an integer,
|
||||
where 0=MO.
|
||||
|
||||
leapdays:
|
||||
Will add given days to the date found, if year is a leap
|
||||
year, and the date found is post 28 of february.
|
||||
|
||||
yearday, nlyearday:
|
||||
Set the yearday or the non-leap year day (jump leap days).
|
||||
These are converted to day/month/leapdays information.
|
||||
|
||||
Here is the behavior of operations with relativedelta:
|
||||
|
||||
1) Calculate the absolute year, using the 'year' argument, or the
|
||||
original datetime year, if the argument is not present.
|
||||
|
||||
2) Add the relative 'years' argument to the absolute year.
|
||||
|
||||
3) Do steps 1 and 2 for month/months.
|
||||
|
||||
4) Calculate the absolute day, using the 'day' argument, or the
|
||||
original datetime day, if the argument is not present. Then,
|
||||
subtract from the day until it fits in the year and month
|
||||
found after their operations.
|
||||
|
||||
5) Add the relative 'days' argument to the absolute day. Notice
|
||||
that the 'weeks' argument is multiplied by 7 and added to
|
||||
'days'.
|
||||
|
||||
6) Do steps 1 and 2 for hour/hours, minute/minutes, second/seconds,
|
||||
microsecond/microseconds.
|
||||
|
||||
7) If the 'weekday' argument is present, calculate the weekday,
|
||||
with the given (wday, nth) tuple. wday is the index of the
|
||||
weekday (0-6, 0=Mon), and nth is the number of weeks to add
|
||||
forward or backward, depending on its signal. Notice that if
|
||||
the calculated date is already Monday, for example, using
|
||||
(0, 1) or (0, -1) won't change the day.
|
||||
"""
|
||||
|
||||
def __init__(self, dt1=None, dt2=None,
|
||||
years=0, months=0, days=0, leapdays=0, weeks=0,
|
||||
hours=0, minutes=0, seconds=0, microseconds=0,
|
||||
year=None, month=None, day=None, weekday=None,
|
||||
yearday=None, nlyearday=None,
|
||||
hour=None, minute=None, second=None, microsecond=None):
|
||||
if dt1 and dt2:
|
||||
if not isinstance(dt1, datetime.date) or \
|
||||
not isinstance(dt2, datetime.date):
|
||||
raise TypeError("relativedelta only diffs datetime/date")
|
||||
if type(dt1) is not type(dt2):
|
||||
if not isinstance(dt1, datetime.datetime):
|
||||
dt1 = datetime.datetime.fromordinal(dt1.toordinal())
|
||||
elif not isinstance(dt2, datetime.datetime):
|
||||
dt2 = datetime.datetime.fromordinal(dt2.toordinal())
|
||||
self.years = 0
|
||||
self.months = 0
|
||||
self.days = 0
|
||||
self.leapdays = 0
|
||||
self.hours = 0
|
||||
self.minutes = 0
|
||||
self.seconds = 0
|
||||
self.microseconds = 0
|
||||
self.year = None
|
||||
self.month = None
|
||||
self.day = None
|
||||
self.weekday = None
|
||||
self.hour = None
|
||||
self.minute = None
|
||||
self.second = None
|
||||
self.microsecond = None
|
||||
self._has_time = 0
|
||||
|
||||
months = (dt1.year*12+dt1.month)-(dt2.year*12+dt2.month)
|
||||
self._set_months(months)
|
||||
dtm = self.__radd__(dt2)
|
||||
if dt1 < dt2:
|
||||
while dt1 > dtm:
|
||||
months += 1
|
||||
self._set_months(months)
|
||||
dtm = self.__radd__(dt2)
|
||||
else:
|
||||
while dt1 < dtm:
|
||||
months -= 1
|
||||
self._set_months(months)
|
||||
dtm = self.__radd__(dt2)
|
||||
delta = dt1 - dtm
|
||||
self.seconds = delta.seconds+delta.days*86400
|
||||
self.microseconds = delta.microseconds
|
||||
else:
|
||||
self.years = years
|
||||
self.months = months
|
||||
self.days = days+weeks*7
|
||||
self.leapdays = leapdays
|
||||
self.hours = hours
|
||||
self.minutes = minutes
|
||||
self.seconds = seconds
|
||||
self.microseconds = microseconds
|
||||
self.year = year
|
||||
self.month = month
|
||||
self.day = day
|
||||
self.hour = hour
|
||||
self.minute = minute
|
||||
self.second = second
|
||||
self.microsecond = microsecond
|
||||
|
||||
if type(weekday) is int:
|
||||
self.weekday = weekdays[weekday]
|
||||
else:
|
||||
self.weekday = weekday
|
||||
|
||||
yday = 0
|
||||
if nlyearday:
|
||||
yday = nlyearday
|
||||
elif yearday:
|
||||
yday = yearday
|
||||
if yearday > 59:
|
||||
self.leapdays = -1
|
||||
if yday:
|
||||
ydayidx = [31,59,90,120,151,181,212,243,273,304,334,366]
|
||||
for idx, ydays in enumerate(ydayidx):
|
||||
if yday <= ydays:
|
||||
self.month = idx+1
|
||||
if idx == 0:
|
||||
self.day = yday
|
||||
else:
|
||||
self.day = yday-ydayidx[idx-1]
|
||||
break
|
||||
else:
|
||||
raise ValueError("invalid year day (%d)" % yday)
|
||||
|
||||
self._fix()
|
||||
|
||||
def _fix(self):
|
||||
if abs(self.microseconds) > 999999:
|
||||
s = self.microseconds//abs(self.microseconds)
|
||||
div, mod = divmod(self.microseconds*s, 1000000)
|
||||
self.microseconds = mod*s
|
||||
self.seconds += div*s
|
||||
if abs(self.seconds) > 59:
|
||||
s = self.seconds//abs(self.seconds)
|
||||
div, mod = divmod(self.seconds*s, 60)
|
||||
self.seconds = mod*s
|
||||
self.minutes += div*s
|
||||
if abs(self.minutes) > 59:
|
||||
s = self.minutes//abs(self.minutes)
|
||||
div, mod = divmod(self.minutes*s, 60)
|
||||
self.minutes = mod*s
|
||||
self.hours += div*s
|
||||
if abs(self.hours) > 23:
|
||||
s = self.hours//abs(self.hours)
|
||||
div, mod = divmod(self.hours*s, 24)
|
||||
self.hours = mod*s
|
||||
self.days += div*s
|
||||
if abs(self.months) > 11:
|
||||
s = self.months//abs(self.months)
|
||||
div, mod = divmod(self.months*s, 12)
|
||||
self.months = mod*s
|
||||
self.years += div*s
|
||||
if (self.hours or self.minutes or self.seconds or self.microseconds or
|
||||
self.hour is not None or self.minute is not None or
|
||||
self.second is not None or self.microsecond is not None):
|
||||
self._has_time = 1
|
||||
else:
|
||||
self._has_time = 0
|
||||
|
||||
def _set_months(self, months):
|
||||
self.months = months
|
||||
if abs(self.months) > 11:
|
||||
s = self.months//abs(self.months)
|
||||
div, mod = divmod(self.months*s, 12)
|
||||
self.months = mod*s
|
||||
self.years = div*s
|
||||
else:
|
||||
self.years = 0
|
||||
|
||||
def __radd__(self, other):
|
||||
if not isinstance(other, datetime.date):
|
||||
raise TypeError("unsupported type for add operation")
|
||||
elif self._has_time and not isinstance(other, datetime.datetime):
|
||||
other = datetime.datetime.fromordinal(other.toordinal())
|
||||
year = (self.year or other.year)+self.years
|
||||
month = self.month or other.month
|
||||
if self.months:
|
||||
assert 1 <= abs(self.months) <= 12
|
||||
month += self.months
|
||||
if month > 12:
|
||||
year += 1
|
||||
month -= 12
|
||||
elif month < 1:
|
||||
year -= 1
|
||||
month += 12
|
||||
day = min(calendar.monthrange(year, month)[1],
|
||||
self.day or other.day)
|
||||
repl = {"year": year, "month": month, "day": day}
|
||||
for attr in ["hour", "minute", "second", "microsecond"]:
|
||||
value = getattr(self, attr)
|
||||
if value is not None:
|
||||
repl[attr] = value
|
||||
days = self.days
|
||||
if self.leapdays and month > 2 and calendar.isleap(year):
|
||||
days += self.leapdays
|
||||
ret = (other.replace(**repl)
|
||||
+ datetime.timedelta(days=days,
|
||||
hours=self.hours,
|
||||
minutes=self.minutes,
|
||||
seconds=self.seconds,
|
||||
microseconds=self.microseconds))
|
||||
if self.weekday:
|
||||
weekday, nth = self.weekday.weekday, self.weekday.n or 1
|
||||
jumpdays = (abs(nth)-1)*7
|
||||
if nth > 0:
|
||||
jumpdays += (7-ret.weekday()+weekday)%7
|
||||
else:
|
||||
jumpdays += (ret.weekday()-weekday)%7
|
||||
jumpdays *= -1
|
||||
ret += datetime.timedelta(days=jumpdays)
|
||||
return ret
|
||||
|
||||
def __rsub__(self, other):
|
||||
return self.__neg__().__radd__(other)
|
||||
|
||||
def __add__(self, other):
|
||||
if not isinstance(other, relativedelta):
|
||||
raise TypeError("unsupported type for add operation")
|
||||
return relativedelta(years=other.years+self.years,
|
||||
months=other.months+self.months,
|
||||
days=other.days+self.days,
|
||||
hours=other.hours+self.hours,
|
||||
minutes=other.minutes+self.minutes,
|
||||
seconds=other.seconds+self.seconds,
|
||||
microseconds=other.microseconds+self.microseconds,
|
||||
leapdays=other.leapdays or self.leapdays,
|
||||
year=other.year or self.year,
|
||||
month=other.month or self.month,
|
||||
day=other.day or self.day,
|
||||
weekday=other.weekday or self.weekday,
|
||||
hour=other.hour or self.hour,
|
||||
minute=other.minute or self.minute,
|
||||
second=other.second or self.second,
|
||||
microsecond=other.second or self.microsecond)
|
||||
|
||||
def __sub__(self, other):
|
||||
if not isinstance(other, relativedelta):
|
||||
raise TypeError("unsupported type for sub operation")
|
||||
return relativedelta(years=other.years-self.years,
|
||||
months=other.months-self.months,
|
||||
days=other.days-self.days,
|
||||
hours=other.hours-self.hours,
|
||||
minutes=other.minutes-self.minutes,
|
||||
seconds=other.seconds-self.seconds,
|
||||
microseconds=other.microseconds-self.microseconds,
|
||||
leapdays=other.leapdays or self.leapdays,
|
||||
year=other.year or self.year,
|
||||
month=other.month or self.month,
|
||||
day=other.day or self.day,
|
||||
weekday=other.weekday or self.weekday,
|
||||
hour=other.hour or self.hour,
|
||||
minute=other.minute or self.minute,
|
||||
second=other.second or self.second,
|
||||
microsecond=other.second or self.microsecond)
|
||||
|
||||
def __neg__(self):
|
||||
return relativedelta(years=-self.years,
|
||||
months=-self.months,
|
||||
days=-self.days,
|
||||
hours=-self.hours,
|
||||
minutes=-self.minutes,
|
||||
seconds=-self.seconds,
|
||||
microseconds=-self.microseconds,
|
||||
leapdays=self.leapdays,
|
||||
year=self.year,
|
||||
month=self.month,
|
||||
day=self.day,
|
||||
weekday=self.weekday,
|
||||
hour=self.hour,
|
||||
minute=self.minute,
|
||||
second=self.second,
|
||||
microsecond=self.microsecond)
|
||||
|
||||
def __nonzero__(self):
|
||||
return not (not self.years and
|
||||
not self.months and
|
||||
not self.days and
|
||||
not self.hours and
|
||||
not self.minutes and
|
||||
not self.seconds and
|
||||
not self.microseconds and
|
||||
not self.leapdays and
|
||||
self.year is None and
|
||||
self.month is None and
|
||||
self.day is None and
|
||||
self.weekday is None and
|
||||
self.hour is None and
|
||||
self.minute is None and
|
||||
self.second is None and
|
||||
self.microsecond is None)
|
||||
|
||||
def __mul__(self, other):
|
||||
f = float(other)
|
||||
return relativedelta(years=self.years*f,
|
||||
months=self.months*f,
|
||||
days=self.days*f,
|
||||
hours=self.hours*f,
|
||||
minutes=self.minutes*f,
|
||||
seconds=self.seconds*f,
|
||||
microseconds=self.microseconds*f,
|
||||
leapdays=self.leapdays,
|
||||
year=self.year,
|
||||
month=self.month,
|
||||
day=self.day,
|
||||
weekday=self.weekday,
|
||||
hour=self.hour,
|
||||
minute=self.minute,
|
||||
second=self.second,
|
||||
microsecond=self.microsecond)
|
||||
|
||||
def __eq__(self, other):
|
||||
if not isinstance(other, relativedelta):
|
||||
return False
|
||||
if self.weekday or other.weekday:
|
||||
if not self.weekday or not other.weekday:
|
||||
return False
|
||||
if self.weekday.weekday != other.weekday.weekday:
|
||||
return False
|
||||
n1, n2 = self.weekday.n, other.weekday.n
|
||||
if n1 != n2 and not ((not n1 or n1 == 1) and (not n2 or n2 == 1)):
|
||||
return False
|
||||
return (self.years == other.years and
|
||||
self.months == other.months and
|
||||
self.days == other.days and
|
||||
self.hours == other.hours and
|
||||
self.minutes == other.minutes and
|
||||
self.seconds == other.seconds and
|
||||
self.leapdays == other.leapdays and
|
||||
self.year == other.year and
|
||||
self.month == other.month and
|
||||
self.day == other.day and
|
||||
self.hour == other.hour and
|
||||
self.minute == other.minute and
|
||||
self.second == other.second and
|
||||
self.microsecond == other.microsecond)
|
||||
|
||||
def __ne__(self, other):
|
||||
return not self.__eq__(other)
|
||||
|
||||
def __div__(self, other):
|
||||
return self.__mul__(1/float(other))
|
||||
|
||||
def __repr__(self):
|
||||
l = []
|
||||
for attr in ["years", "months", "days", "leapdays",
|
||||
"hours", "minutes", "seconds", "microseconds"]:
|
||||
value = getattr(self, attr)
|
||||
if value:
|
||||
l.append("%s=%+d" % (attr, value))
|
||||
for attr in ["year", "month", "day", "weekday",
|
||||
"hour", "minute", "second", "microsecond"]:
|
||||
value = getattr(self, attr)
|
||||
if value is not None:
|
||||
l.append("%s=%s" % (attr, value))
|
||||
return "%s(%s)" % (self.__class__.__name__, ", ".join(l))
|
||||
194
resources/lib/scheduler.py
Normal file
@@ -0,0 +1,194 @@
|
||||
import time
|
||||
from datetime import datetime
|
||||
import xbmc
|
||||
import xbmcvfs
|
||||
import xbmcgui
|
||||
from . import utils as utils
|
||||
from resources.lib.croniter import croniter
|
||||
from resources.lib.backup import XbmcBackup
|
||||
|
||||
UPGRADE_INT = 2 # to keep track of any upgrade notifications
|
||||
|
||||
|
||||
class BackupScheduler:
|
||||
monitor = None
|
||||
enabled = False
|
||||
next_run = 0
|
||||
next_run_path = None
|
||||
restore_point = None
|
||||
|
||||
def __init__(self):
|
||||
self.monitor = UpdateMonitor(update_method=self.settingsChanged)
|
||||
self.enabled = utils.getSettingBool("enable_scheduler")
|
||||
self.next_run_path = xbmcvfs.translatePath(utils.data_dir()) + 'next_run.txt'
|
||||
|
||||
if(self.enabled):
|
||||
|
||||
# sleep for 2 minutes so Kodi can start and time can update correctly
|
||||
xbmc.Monitor().waitForAbort(120)
|
||||
|
||||
nr = 0
|
||||
if(xbmcvfs.exists(self.next_run_path)):
|
||||
|
||||
with xbmcvfs.File(self.next_run_path) as fh:
|
||||
try:
|
||||
# check if we saved a run time from the last run
|
||||
nr = float(fh.read())
|
||||
except ValueError:
|
||||
nr = 0
|
||||
|
||||
# if we missed and the user wants to play catch-up
|
||||
if(0 < nr <= time.time() and utils.getSettingBool('schedule_miss')):
|
||||
utils.log("scheduled backup was missed, doing it now...")
|
||||
progress_mode = utils.getSettingInt('progress_mode')
|
||||
|
||||
if(progress_mode == 0):
|
||||
progress_mode = 1 # Kodi just started, don't block it with a foreground progress bar
|
||||
|
||||
self.doScheduledBackup(progress_mode)
|
||||
|
||||
self.setup()
|
||||
|
||||
def setup(self):
|
||||
# scheduler was turned on, find next run time
|
||||
utils.log("scheduler enabled, finding next run time")
|
||||
self.findNextRun(time.time())
|
||||
|
||||
def start(self):
|
||||
|
||||
# display upgrade messages if they exist
|
||||
if(utils.getSettingInt('upgrade_notes') < UPGRADE_INT):
|
||||
xbmcgui.Dialog().ok(utils.getString(30010), utils.getString(30132))
|
||||
utils.setSetting('upgrade_notes', str(UPGRADE_INT))
|
||||
|
||||
# check if a backup should be resumed
|
||||
resumeRestore = self._resumeCheck()
|
||||
|
||||
if(resumeRestore):
|
||||
restore = XbmcBackup()
|
||||
restore.selectRestore(self.restore_point)
|
||||
# skip the advanced settings check
|
||||
restore.skipAdvanced()
|
||||
restore.restore()
|
||||
|
||||
while(not self.monitor.abortRequested()):
|
||||
|
||||
if(self.enabled):
|
||||
# scheduler is still on
|
||||
now = time.time()
|
||||
|
||||
if(self.next_run <= now):
|
||||
progress_mode = utils.getSettingInt('progress_mode')
|
||||
self.doScheduledBackup(progress_mode)
|
||||
|
||||
# check if we should shut the computer down
|
||||
if(utils.getSettingBool("cron_shutdown")):
|
||||
# wait 10 seconds to make sure all backup processes and files are completed
|
||||
time.sleep(10)
|
||||
xbmc.executebuiltin('ShutDown()')
|
||||
else:
|
||||
# find the next run time like normal
|
||||
self.findNextRun(now)
|
||||
|
||||
xbmc.sleep(500)
|
||||
|
||||
# delete monitor to free up memory
|
||||
del self.monitor
|
||||
|
||||
def doScheduledBackup(self, progress_mode):
|
||||
if(progress_mode != 2):
|
||||
utils.showNotification(utils.getString(30053))
|
||||
|
||||
backup = XbmcBackup()
|
||||
|
||||
if(backup.remoteConfigured()):
|
||||
|
||||
if(utils.getSettingInt('progress_mode') in [0, 1]):
|
||||
backup.backup(True)
|
||||
else:
|
||||
backup.backup(False)
|
||||
|
||||
# check if this is a "one-off"
|
||||
if(utils.getSettingInt("schedule_interval") == 0):
|
||||
# disable the scheduler after this run
|
||||
self.enabled = False
|
||||
utils.setSetting('enable_scheduler', 'false')
|
||||
else:
|
||||
utils.showNotification(utils.getString(30045))
|
||||
|
||||
def findNextRun(self, now):
|
||||
progress_mode = utils.getSettingInt('progress_mode')
|
||||
|
||||
# find the cron expression and get the next run time
|
||||
cron_exp = self.parseSchedule()
|
||||
|
||||
cron_ob = croniter(cron_exp, datetime.fromtimestamp(now))
|
||||
new_run_time = cron_ob.get_next(float)
|
||||
|
||||
if(new_run_time != self.next_run):
|
||||
self.next_run = new_run_time
|
||||
utils.log("scheduler will run again on " + utils.getRegionalTimestamp(datetime.fromtimestamp(self.next_run), ['dateshort', 'time']))
|
||||
|
||||
# write the next time to a file
|
||||
with xbmcvfs.File(self.next_run_path, 'w') as fh:
|
||||
fh.write(str(self.next_run))
|
||||
|
||||
# only show when not in silent mode
|
||||
if(progress_mode != 2):
|
||||
utils.showNotification(utils.getString(30081) + " " + utils.getRegionalTimestamp(datetime.fromtimestamp(self.next_run), ['dateshort', 'time']))
|
||||
|
||||
def settingsChanged(self):
|
||||
current_enabled = utils.getSettingBool("enable_scheduler")
|
||||
|
||||
if(current_enabled and not self.enabled):
|
||||
# scheduler was just turned on
|
||||
self.enabled = current_enabled
|
||||
self.setup()
|
||||
elif (not current_enabled and self.enabled):
|
||||
# schedule was turn off
|
||||
self.enabled = current_enabled
|
||||
|
||||
if(self.enabled):
|
||||
# always recheck the next run time after an update
|
||||
self.findNextRun(time.time())
|
||||
|
||||
def parseSchedule(self):
|
||||
schedule_type = utils.getSettingInt("schedule_interval")
|
||||
cron_exp = utils.getSetting("cron_schedule")
|
||||
|
||||
hour_of_day = utils.getSetting("schedule_time")
|
||||
hour_of_day = int(hour_of_day[0:2])
|
||||
if(schedule_type == 0 or schedule_type == 1):
|
||||
# every day
|
||||
cron_exp = "0 " + str(hour_of_day) + " * * *"
|
||||
elif(schedule_type == 2):
|
||||
# once a week
|
||||
day_of_week = utils.getSetting("day_of_week")
|
||||
cron_exp = "0 " + str(hour_of_day) + " * * " + day_of_week
|
||||
elif(schedule_type == 3):
|
||||
# first day of month
|
||||
cron_exp = "0 " + str(hour_of_day) + " 1 * *"
|
||||
|
||||
return cron_exp
|
||||
|
||||
def _resumeCheck(self):
|
||||
shouldContinue = False
|
||||
if(xbmcvfs.exists(xbmcvfs.translatePath(utils.data_dir() + "resume.txt"))):
|
||||
rFile = xbmcvfs.File(xbmcvfs.translatePath(utils.data_dir() + "resume.txt"), 'r')
|
||||
self.restore_point = rFile.read()
|
||||
rFile.close()
|
||||
xbmcvfs.delete(xbmcvfs.translatePath(utils.data_dir() + "resume.txt"))
|
||||
shouldContinue = xbmcgui.Dialog().yesno(utils.getString(30042), utils.getString(30043), utils.getString(30044))
|
||||
|
||||
return shouldContinue
|
||||
|
||||
|
||||
class UpdateMonitor(xbmc.Monitor):
|
||||
update_method = None
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
xbmc.Monitor.__init__(self)
|
||||
self.update_method = kwargs['update_method']
|
||||
|
||||
def onSettingsChanged(self):
|
||||
self.update_method()
|
||||
@@ -1,11 +1,12 @@
|
||||
import urllib2
|
||||
|
||||
# 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!
|
||||
from future.moves.urllib.request import urlopen
|
||||
|
||||
|
||||
def shorten(aUrl):
|
||||
tinyurl = 'http://tinyurl.com/api-create.php?url='
|
||||
req = urllib2.urlopen(tinyurl + aUrl)
|
||||
req = urlopen(tinyurl + aUrl)
|
||||
data = req.read()
|
||||
|
||||
# should be a tiny url
|
||||
return str(data)
|
||||
return data
|
||||
|
||||
@@ -1,34 +1,52 @@
|
||||
import xbmc
|
||||
import xbmcgui
|
||||
import xbmcaddon
|
||||
import xbmcvfs
|
||||
|
||||
__addon_id__ = 'script.xbmcbackup'
|
||||
__Addon = xbmcaddon.Addon(__addon_id__)
|
||||
|
||||
|
||||
def data_dir():
|
||||
return __Addon.getAddonInfo('profile')
|
||||
|
||||
|
||||
def addon_dir():
|
||||
return __Addon.getAddonInfo('path')
|
||||
|
||||
|
||||
def openSettings():
|
||||
__Addon.openSettings()
|
||||
|
||||
|
||||
def log(message, loglevel=xbmc.LOGDEBUG):
|
||||
xbmc.log(encode(__addon_id__ + "-" + __Addon.getAddonInfo('version') + ": " + message),level=loglevel)
|
||||
xbmc.log(__addon_id__ + "-" + __Addon.getAddonInfo('version') + ": " + message, level=loglevel)
|
||||
|
||||
|
||||
def showNotification(message):
|
||||
xbmcgui.Dialog().notification(encode(getString(30010)),encode(message),time=4000,icon=xbmc.translatePath(__Addon.getAddonInfo('path') + "/resources/images/icon.png"))
|
||||
xbmcgui.Dialog().notification(getString(30010), message, time=4000, icon=xbmcvfs.translatePath(__Addon.getAddonInfo('path') + "/resources/images/icon.png"))
|
||||
|
||||
|
||||
def getSetting(name):
|
||||
return __Addon.getSetting(name)
|
||||
|
||||
|
||||
def getSettingBool(name):
|
||||
return bool(__Addon.getSettingBool(name))
|
||||
|
||||
|
||||
def getSettingInt(name):
|
||||
return __Addon.getSettingInt(name)
|
||||
|
||||
|
||||
def setSetting(name, value):
|
||||
__Addon.setSetting(name, value)
|
||||
|
||||
|
||||
def getString(string_id):
|
||||
return __Addon.getLocalizedString(string_id)
|
||||
|
||||
|
||||
def getRegionalTimestamp(date_time, dateformat=['dateshort']):
|
||||
result = ''
|
||||
|
||||
@@ -37,12 +55,15 @@ def getRegionalTimestamp(date_time,dateformat=['dateshort']):
|
||||
|
||||
return result.strip()
|
||||
|
||||
def encode(string):
|
||||
result = ''
|
||||
|
||||
try:
|
||||
result = string.encode('UTF-8','replace')
|
||||
except UnicodeDecodeError:
|
||||
result = 'Unicode Error'
|
||||
def diskString(fSize):
|
||||
# convert a size in kilobytes to the best possible match and return as a string
|
||||
fSize = float(fSize)
|
||||
i = 0
|
||||
sizeNames = ['KB', 'MB', 'GB', 'TB']
|
||||
|
||||
return result
|
||||
while(fSize > 1024):
|
||||
fSize = fSize / 1024
|
||||
i = i + 1
|
||||
|
||||
return "%0.2f%s" % (fSize, sizeNames[i])
|
||||
|
||||
@@ -1,13 +1,14 @@
|
||||
import xbmc
|
||||
import xbmcvfs
|
||||
import xbmcgui
|
||||
from __future__ import unicode_literals
|
||||
import zipfile
|
||||
import os.path
|
||||
import sys
|
||||
import dropbox
|
||||
import xbmcvfs
|
||||
import xbmcgui
|
||||
from dropbox import dropbox
|
||||
from . import utils as utils
|
||||
from dropbox.files import WriteMode, CommitInfo, UploadSessionCursor
|
||||
from .authorizers import DropboxAuthorizer,GoogleDriveAuthorizer
|
||||
from . authorizers import DropboxAuthorizer
|
||||
|
||||
|
||||
class Vfs:
|
||||
root_path = None
|
||||
@@ -15,16 +16,19 @@ class Vfs:
|
||||
def __init__(self, rootString):
|
||||
self.set_root(rootString)
|
||||
|
||||
def set_root(self,rootString):
|
||||
old_root = self.root_path
|
||||
self.root_path = rootString
|
||||
|
||||
def clean_path(self, path):
|
||||
# fix slashes
|
||||
self.root_path = self.root_path.replace("\\","/")
|
||||
path = path.replace("\\", "/")
|
||||
|
||||
# check if trailing slash is included
|
||||
if(self.root_path[-1:] != "/"):
|
||||
self.root_path = self.root_path + "/"
|
||||
if(path[-1:] != '/'):
|
||||
path = path + '/'
|
||||
|
||||
return path
|
||||
|
||||
def set_root(self, rootString):
|
||||
old_root = self.root_path
|
||||
self.root_path = self.clean_path(rootString)
|
||||
|
||||
# return the old root
|
||||
return old_root
|
||||
@@ -53,19 +57,23 @@ class Vfs:
|
||||
def cleanup(self):
|
||||
return True
|
||||
|
||||
def fileSize(self, filename):
|
||||
return 0 # result should be in KB
|
||||
|
||||
|
||||
class XBMCFileSystem(Vfs):
|
||||
|
||||
def listdir(self, directory):
|
||||
return xbmcvfs.listdir(directory)
|
||||
|
||||
def mkdir(self, directory):
|
||||
return xbmcvfs.mkdir(xbmc.translatePath(directory))
|
||||
return xbmcvfs.mkdir(xbmcvfs.translatePath(directory))
|
||||
|
||||
def put(self, source, dest):
|
||||
return xbmcvfs.copy(xbmc.translatePath(source),xbmc.translatePath(dest))
|
||||
return xbmcvfs.copy(xbmcvfs.translatePath(source), xbmcvfs.translatePath(dest))
|
||||
|
||||
def rmdir(self, directory):
|
||||
return xbmcvfs.rmdir(directory,True)
|
||||
return xbmcvfs.rmdir(directory, force=True) # use force=True to make sure it works recursively
|
||||
|
||||
def rmfile(self, aFile):
|
||||
return xbmcvfs.delete(aFile)
|
||||
@@ -76,6 +84,13 @@ class XBMCFileSystem(Vfs):
|
||||
def exists(self, aFile):
|
||||
return xbmcvfs.exists(aFile)
|
||||
|
||||
def fileSize(self, filename):
|
||||
with xbmcvfs.File(filename) as f:
|
||||
result = f.size() / 1024 # bytes to kilobytes
|
||||
|
||||
return result
|
||||
|
||||
|
||||
class ZipFileSystem(Vfs):
|
||||
zip = None
|
||||
|
||||
@@ -92,9 +107,9 @@ class ZipFileSystem(Vfs):
|
||||
|
||||
def put(self, source, dest):
|
||||
|
||||
aFile = xbmcvfs.File(xbmc.translatePath(source),'r')
|
||||
aFile = xbmcvfs.File(xbmcvfs.translatePath(source), 'r')
|
||||
|
||||
self.zip.writestr(utils.encode(dest),aFile.read())
|
||||
self.zip.writestr(dest, aFile.readBytes())
|
||||
|
||||
return True
|
||||
|
||||
@@ -114,6 +129,7 @@ class ZipFileSystem(Vfs):
|
||||
def listFiles(self):
|
||||
return self.zip.infolist()
|
||||
|
||||
|
||||
class DropboxFileSystem(Vfs):
|
||||
MAX_CHUNK = 50 * 1000 * 1000 # dropbox uses 150, reduced to 50 for small mem systems
|
||||
client = None
|
||||
@@ -135,25 +151,24 @@ class DropboxFileSystem(Vfs):
|
||||
def listdir(self, 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 = []
|
||||
dirs = []
|
||||
metadata = self.client.files_list_folder(directory)
|
||||
|
||||
for aFile in metadata.entries:
|
||||
if(isinstance(aFile, dropbox.files.FolderMetadata)):
|
||||
dirs.append(utils.encode(aFile.name))
|
||||
dirs.append(aFile.name)
|
||||
else:
|
||||
files.append(utils.encode(aFile.name))
|
||||
files.append(aFile.name)
|
||||
|
||||
return [dirs, files]
|
||||
else:
|
||||
return [[], []]
|
||||
|
||||
|
||||
def mkdir(self, 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
|
||||
return True
|
||||
else:
|
||||
@@ -161,7 +176,7 @@ class DropboxFileSystem(Vfs):
|
||||
|
||||
def rmdir(self, 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
|
||||
dirs, files = self.listdir(directory)
|
||||
|
||||
@@ -178,7 +193,7 @@ class DropboxFileSystem(Vfs):
|
||||
def rmfile(self, 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)
|
||||
return True
|
||||
else:
|
||||
@@ -187,13 +202,13 @@ class DropboxFileSystem(Vfs):
|
||||
def exists(self, aFile):
|
||||
aFile = self._fix_slashes(aFile)
|
||||
|
||||
if(self.client != None):
|
||||
if(self.client is not None):
|
||||
# can't list root metadata
|
||||
if(aFile == ''):
|
||||
return True
|
||||
|
||||
try:
|
||||
meta_data = self.client.files_get_metadata(aFile)
|
||||
self.client.files_get_metadata(aFile)
|
||||
# if we make it here the file does exist
|
||||
return True
|
||||
except:
|
||||
@@ -204,7 +219,7 @@ class DropboxFileSystem(Vfs):
|
||||
def put(self, source, dest, retry=True):
|
||||
dest = self._fix_slashes(dest)
|
||||
|
||||
if(self.client != None):
|
||||
if(self.client is not None):
|
||||
# open the file and get its size
|
||||
f = open(source, 'rb')
|
||||
f_size = os.path.getsize(source)
|
||||
@@ -212,7 +227,7 @@ class DropboxFileSystem(Vfs):
|
||||
try:
|
||||
if(f_size < self.MAX_CHUNK):
|
||||
# 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:
|
||||
# start the upload session
|
||||
upload_session = self.client.files_upload_session_start(f.read(self.MAX_CHUNK))
|
||||
@@ -242,10 +257,20 @@ class DropboxFileSystem(Vfs):
|
||||
else:
|
||||
return False
|
||||
|
||||
def fileSize(self, filename):
|
||||
result = 0
|
||||
aFile = self._fix_slashes(filename)
|
||||
|
||||
if(self.client is not None):
|
||||
metadata = self.client.files_get_metadata(aFile)
|
||||
result = metadata.size / 1024 # bytes to KB
|
||||
|
||||
return result
|
||||
|
||||
def get_file(self, source, dest):
|
||||
if(self.client != None):
|
||||
if(self.client is not None):
|
||||
# write the file locally
|
||||
f = self.client.files_download_to_file(dest,source)
|
||||
self.client.files_download_to_file(dest, source)
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
@@ -262,191 +287,3 @@ class DropboxFileSystem(Vfs):
|
||||
result = result[:-1]
|
||||
|
||||
return result
|
||||
|
||||
|
||||
class GoogleDriveFilesystem(Vfs):
|
||||
drive = None
|
||||
history = {}
|
||||
FOLDER_TYPE = 'application/vnd.google-apps.folder'
|
||||
|
||||
def __init__(self,rootString):
|
||||
self.set_root(rootString)
|
||||
|
||||
authorizer = GoogleDriveAuthorizer()
|
||||
|
||||
if(authorizer.isAuthorized()):
|
||||
self.drive = authorizer.getClient()
|
||||
else:
|
||||
#tell the user to go back and run the authorizer
|
||||
xbmcgui.Dialog().ok(utils.getString(30010),utils.getString(30105))
|
||||
sys.exit()
|
||||
|
||||
#make sure we have the folder we need
|
||||
xbmc_folder = self._getGoogleFile(self.root_path)
|
||||
if(xbmc_folder == None):
|
||||
self.mkdir(self.root_path)
|
||||
|
||||
def listdir(self,directory):
|
||||
files = []
|
||||
dirs = []
|
||||
|
||||
if(not directory.startswith('/')):
|
||||
directory = '/' + directory
|
||||
|
||||
#get the id of this folder
|
||||
parentFolder = self._getGoogleFile(directory)
|
||||
|
||||
#need to do this after
|
||||
if(not directory.endswith('/')):
|
||||
directory = directory + '/'
|
||||
|
||||
if(parentFolder != None):
|
||||
|
||||
fileList = self.drive.ListFile({'q':"'" + parentFolder['id'] + "' in parents and trashed = false"}).GetList()
|
||||
|
||||
for aFile in fileList:
|
||||
if(aFile['mimeType'] == self.FOLDER_TYPE):
|
||||
dirs.append(utils.encode(aFile['title']))
|
||||
else:
|
||||
files.append(utils.encode(aFile['title']))
|
||||
|
||||
|
||||
return [dirs,files]
|
||||
|
||||
def mkdir(self,directory):
|
||||
result = True
|
||||
|
||||
if(not directory.startswith('/')):
|
||||
directory = '/' + directory
|
||||
|
||||
if(directory.endswith('/')):
|
||||
directory = directory[:-1]
|
||||
|
||||
#split the string by the directory separator
|
||||
pathList = os.path.split(directory)
|
||||
|
||||
if(pathList[0] == '/'):
|
||||
|
||||
#we're at the root, just make the folder
|
||||
newFolder = self.drive.CreateFile({'title': pathList[1], 'parent':'root','mimeType':self.FOLDER_TYPE})
|
||||
newFolder.Upload()
|
||||
else:
|
||||
#get the id of the parent folder
|
||||
parentFolder = self._getGoogleFile(pathList[0])
|
||||
|
||||
if(parentFolder != None):
|
||||
newFolder = self.drive.CreateFile({'title': pathList[1],"parents":[{'kind':'drive#fileLink','id':parentFolder['id']}],'mimeType':self.FOLDER_TYPE})
|
||||
newFolder.Upload()
|
||||
else:
|
||||
result = False
|
||||
|
||||
return result
|
||||
|
||||
def put(self,source,dest):
|
||||
result = True
|
||||
|
||||
#make the name separate from the path
|
||||
if(not dest.startswith('/')):
|
||||
dest = '/' + dest
|
||||
|
||||
pathList = os.path.split(dest)
|
||||
|
||||
#get the parent location
|
||||
parentFolder = self._getGoogleFile(pathList[0])
|
||||
|
||||
if(parentFolder != None):
|
||||
#create a new file in this folder
|
||||
newFile = self.drive.CreateFile({"title":pathList[1],"parents":[{'kind':'drive#fileLink','id':parentFolder['id']}]})
|
||||
newFile.SetContentFile(source)
|
||||
newFile.Upload()
|
||||
else:
|
||||
result = False
|
||||
|
||||
return result
|
||||
|
||||
def get_file(self,source, dest):
|
||||
result = True
|
||||
|
||||
#get the id of this file
|
||||
file = self._getGoogleFile(source)
|
||||
|
||||
if(file != None):
|
||||
file.GetContentFile(dest)
|
||||
else:
|
||||
result = False
|
||||
|
||||
return result
|
||||
|
||||
def rmdir(self,directory):
|
||||
result = True
|
||||
|
||||
#check that the folder exists
|
||||
folder = self._getGoogleFile(directory)
|
||||
|
||||
if(folder != None):
|
||||
#delete the folder
|
||||
folder.Delete()
|
||||
else:
|
||||
result = False
|
||||
|
||||
return result
|
||||
|
||||
def rmfile(self,aFile):
|
||||
#really just the same as the remove directory function
|
||||
return self.rmdir(aFile)
|
||||
|
||||
def exists(self,aFile):
|
||||
#attempt to get this file
|
||||
foundFile = self._getGoogleFile(aFile)
|
||||
|
||||
if(foundFile != None):
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
def rename(self,aFile,newName):
|
||||
return True
|
||||
|
||||
def _getGoogleFile(self,file):
|
||||
result = None
|
||||
|
||||
#file must start with / and not end with one (even directory)
|
||||
if(not file.startswith('/')):
|
||||
file = '/' + file
|
||||
|
||||
if(file.endswith('/')):
|
||||
file = file[:-1]
|
||||
|
||||
if(file in self.history):
|
||||
|
||||
result = self.history[file]
|
||||
else:
|
||||
pathList = os.path.split(file)
|
||||
|
||||
#end of recurision, we got the root
|
||||
if(pathList[0] == '/'):
|
||||
#get the id of this file (if it exists)
|
||||
file_list = self.drive.ListFile({'q':"title='" + pathList[1] + "' and 'root' in parents and trashed=false"}).GetList()
|
||||
|
||||
if(len(file_list) > 0):
|
||||
result = file_list[0]
|
||||
self.history[pathList[1]] = result
|
||||
else:
|
||||
#recurse down the tree
|
||||
current_file = pathList[1]
|
||||
|
||||
parentId = self._getGoogleFile(pathList[0])
|
||||
|
||||
if(parentId != None):
|
||||
self.history[pathList[0]] = parentId
|
||||
|
||||
#attempt to get the id of this file, with this parent
|
||||
file_list = file_list = self.drive.ListFile({'q':"title='" + current_file + "' and '" + parentId['id'] + "' in parents and trashed=false"}).GetList()
|
||||
|
||||
if(len(file_list) > 0):
|
||||
result = file_list[0]
|
||||
self.history[file] = result
|
||||
|
||||
|
||||
return result
|
||||
|
||||
|
||||
@@ -1,43 +1,392 @@
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
|
||||
<settings>
|
||||
<category id="general" label="30011">
|
||||
<setting id="compress_backups" type="bool" label="30087" default="false" />
|
||||
<setting id="backup_rotation" type="number" label="30026" default="0" />
|
||||
<setting id="progress_mode" type="enum" label="30022" lvalues="30082|30083|30084" default="0" />
|
||||
<setting id="upgrade_notes" type="number" label="upgrade_notes" visible="false" default="1" />
|
||||
<?xml version="1.0"?>
|
||||
<settings version="1">
|
||||
<section id="service.xbmcbackup">
|
||||
<category id="general" label="30011" help="">
|
||||
<group id="1" label="">
|
||||
<!-- compress backups -->
|
||||
<setting id="compress_backups" type="boolean" label="30087" help="">
|
||||
<level>0</level>
|
||||
<default>false</default>
|
||||
<control type="toggle" />
|
||||
</setting>
|
||||
<!-- zip folder staging path -->
|
||||
<setting id="zip_temp_path" type="string" label="30152" help="30153">
|
||||
<level>3</level>
|
||||
<default>special://temp</default>
|
||||
<constraints>
|
||||
<allowempty>true</allowempty>
|
||||
</constraints>
|
||||
<dependencies>
|
||||
<dependency type="visible" setting="compress_backups">true</dependency>
|
||||
</dependencies>
|
||||
<control type="edit" format="string">
|
||||
<heading>30152</heading>
|
||||
</control>
|
||||
</setting>
|
||||
<!-- backup rotation -->
|
||||
<setting id="backup_rotation" type="integer" label="30026" help="">
|
||||
<level>0</level>
|
||||
<default>0</default>
|
||||
<control type="edit" format="integer">
|
||||
<heading>30026</heading>
|
||||
</control>
|
||||
</setting>
|
||||
<!-- prompt to restore settings -->
|
||||
<setting id="always_prompt_restore_settings" type="boolean" label="30148" help="30154">
|
||||
<level>2</level>
|
||||
<default>false</default>
|
||||
<control type="toggle" />
|
||||
</setting>
|
||||
<!-- progress mode -->
|
||||
<setting id="progress_mode" type="integer" label="30022" help="">
|
||||
<level>1</level>
|
||||
<default>0</default>
|
||||
<constraints>
|
||||
<options>
|
||||
<option label="30082">0</option>
|
||||
<option label="30083">1</option>
|
||||
<option label="30084">2</option>
|
||||
</options>
|
||||
</constraints>
|
||||
<control type="spinner" format="string" />
|
||||
</setting>
|
||||
</group>
|
||||
<group id="2" label="">
|
||||
<!-- verbose logging -->
|
||||
<setting id="verbose_logging" type="boolean" label="30151" help="30155">
|
||||
<level>3</level>
|
||||
<default>false</default>
|
||||
<control type="toggle" />
|
||||
</setting>
|
||||
<!-- upgrade notes not visible to users -->
|
||||
<setting id="upgrade_notes" type="integer" label="upgrade_notes" help="">
|
||||
<level>4</level>
|
||||
<default>1</default>
|
||||
<visible>false</visible>
|
||||
<control type="edit" format="integer">
|
||||
<heading>upgrade_notes</heading>
|
||||
</control>
|
||||
</setting>
|
||||
</group>
|
||||
</category>
|
||||
<category id="backup_path" label="30048">
|
||||
<setting id="remote_selection" type="enum" lvalues="30018|30019|30027|30098" default="0" label="30025"/>
|
||||
<setting id="remote_path_2" type="text" label="30024" default="" visible="eq(-1,1)" />
|
||||
<setting id="remote_path" type="folder" label="30020" visible="eq(-2,0)" />
|
||||
<setting id="dropbox_key" type="text" label="30028" visible="eq(-3,2)" default="" />
|
||||
<setting id="dropbox_secret" type="text" label="30029" visible="eq(-4,2)" default="" />
|
||||
<setting id="google_drive_id" type="text" label="Client ID" visible="eq(-5,3)" default="" />
|
||||
<setting id="google_drive_secret" type="text" label="Client Secret" visible="eq(-6,3)" default="" />
|
||||
<setting id="auth_dropbox_button" type="action" label="30104" action="RunScript(special://home/addons/script.xbmcbackup/launcher.py,action=authorize_cloud,provider=dropbox)" visible="eq(-7,2)"/>
|
||||
<setting id="auth_google_button" type="action" label="30104" action="RunScript(special://home/addons/script.xbmcbackup/launcher.py,action=authorize_cloud,provider=google_drive)" visible="eq(-8,3)"/>
|
||||
<setting id="remove_auth_button" type="action" label="30093" action="RunScript(special://home/addons/script.xbmcbackup/launcher.py,action=remove_auth)" visible="gt(-9,1)"/>
|
||||
<category id="backup_path" label="30048" help="">
|
||||
<group id="1" label="">
|
||||
<!-- backup repo type -->
|
||||
<setting id="remote_selection" type="integer" label="30025" help="">
|
||||
<level>0</level>
|
||||
<default>0</default>
|
||||
<constraints>
|
||||
<options>
|
||||
<option label="30018">0</option>
|
||||
<option label="30019">1</option>
|
||||
<option label="30027">2</option>
|
||||
</options>
|
||||
</constraints>
|
||||
<control type="spinner" format="string" />
|
||||
</setting>
|
||||
<!-- folder select path -->
|
||||
<setting id="remote_path" type="path" label="30020" help="">
|
||||
<level>0</level>
|
||||
<default/>
|
||||
<constraints>
|
||||
<allowempty>true</allowempty>
|
||||
</constraints>
|
||||
<dependencies>
|
||||
<dependency type="visible" setting="remote_selection">0</dependency>
|
||||
</dependencies>
|
||||
<control type="button" format="path">
|
||||
<heading>30020</heading>
|
||||
</control>
|
||||
</setting>
|
||||
<!-- type remote path -->
|
||||
<setting id="remote_path_2" type="string" label="30024" help="">
|
||||
<level>0</level>
|
||||
<default></default>
|
||||
<constraints>
|
||||
<allowempty>true</allowempty>
|
||||
</constraints>
|
||||
<dependencies>
|
||||
<dependency type="visible" setting="remote_selection">1</dependency>
|
||||
</dependencies>
|
||||
<control type="edit" format="string">
|
||||
<heading>30024</heading>
|
||||
</control>
|
||||
</setting>
|
||||
<!-- dropbox key and secret -->
|
||||
<setting id="dropbox_key" type="string" label="30028" help="30156">
|
||||
<level>0</level>
|
||||
<default></default>
|
||||
<constraints>
|
||||
<allowempty>true</allowempty>
|
||||
</constraints>
|
||||
<dependencies>
|
||||
<dependency type="visible" setting="remote_selection">2</dependency>
|
||||
</dependencies>
|
||||
<control type="edit" format="string">
|
||||
<heading>30028</heading>
|
||||
</control>
|
||||
</setting>
|
||||
<setting id="dropbox_secret" type="string" label="30029" help="30156">
|
||||
<level>0</level>
|
||||
<default></default>
|
||||
<constraints>
|
||||
<allowempty>true</allowempty>
|
||||
</constraints>
|
||||
<dependencies>
|
||||
<dependency type="visible" setting="remote_selection">2</dependency>
|
||||
</dependencies>
|
||||
<control type="edit" format="string">
|
||||
<heading>30029</heading>
|
||||
</control>
|
||||
</setting>
|
||||
<!-- authorize dropbox -->
|
||||
<setting id="auth_dropbox_button" type="action" label="30104" help="">
|
||||
<level>0</level>
|
||||
<default />
|
||||
<dependencies>
|
||||
<dependency type="visible" setting="remote_selection">2</dependency>
|
||||
</dependencies>
|
||||
<control type="button" format="action">
|
||||
<data>RunScript(special://home/addons/script.xbmcbackup/launcher.py,action=authorize_cloud,provider=dropbox)</data>
|
||||
</control>
|
||||
</setting>
|
||||
</group>
|
||||
<group id="2" label="">
|
||||
<!-- remove auth button -->
|
||||
<setting id="remove_auth_button" type="action" label="30093" help="">
|
||||
<level>2</level>
|
||||
<default />
|
||||
<dependencies>
|
||||
<dependency type="visible" setting="remote_selection">2</dependency>
|
||||
</dependencies>
|
||||
<control type="button" format="action">
|
||||
<data>RunScript(special://home/addons/script.xbmcbackup/launcher.py,action=remove_auth)</data>
|
||||
</control>
|
||||
</setting>
|
||||
</group>
|
||||
</category>
|
||||
<category id="selection" label="30012">
|
||||
<setting id="backup_selection_type" type="enum" lvalues="30014|30015" default="0" label="30023" />
|
||||
<setting id="backup_addon_data" type="bool" label="30031" default="false" visible="eq(-1,0)"/>
|
||||
<setting id="backup_config" type="bool" label="30035" default="true" visible="eq(-2,0)"/>
|
||||
<setting id="backup_database" type="bool" label="30032" default="true" visible="eq(-3,0)"/>
|
||||
<setting id="backup_game_saves" type="bool" label="30133" default="false" visible="eq(-4,0)" />
|
||||
<setting id="backup_playlists" type="bool" label="30033" default="true" visible="eq(-5,0)"/>
|
||||
<setting id="backup_profiles" type="bool" label="30080" default="false" visible="eq(-6,0)"/>
|
||||
<setting id="backup_thumbnails" type="bool" label="30034" default="true" visible="eq(-7,0)"/>
|
||||
<setting id="backup_addons" type="bool" label="30030" default="true" visible="eq(-8,0)" />
|
||||
<setting id="advanced_button" type="action" label="30125" visible="eq(-9,1)" action="RunScript(special://home/addons/script.xbmcbackup/launcher.py,action=advanced_editor)" />
|
||||
<setting id="advanced_defaults" type="action" label="30139" visible="eq(-10,1)" action="RunScript(special://home/addons/script.xbmcbackup/launcher.py,action=advanced_copy_config)" />
|
||||
<group id="1" label="">
|
||||
<!-- selection type (simple/advanced) -->
|
||||
<setting id="backup_selection_type" type="integer" label="30023" help="30157">
|
||||
<level>2</level>
|
||||
<default>0</default>
|
||||
<constraints>
|
||||
<options>
|
||||
<option label="30014">0</option>
|
||||
<option label="30015">1</option>
|
||||
</options>
|
||||
</constraints>
|
||||
<control type="spinner" format="string" />
|
||||
</setting>
|
||||
<!-- simple selection settings -->
|
||||
<setting id="backup_addon_data" type="boolean" label="30031" help="">
|
||||
<level>0</level>
|
||||
<default>false</default>
|
||||
<dependencies>
|
||||
<dependency type="visible" setting="backup_selection_type">0</dependency>
|
||||
</dependencies>
|
||||
<control type="toggle" />
|
||||
</setting>
|
||||
<setting id="backup_config" type="boolean" label="30035" help="">
|
||||
<level>0</level>
|
||||
<default>true</default>
|
||||
<dependencies>
|
||||
<dependency type="visible" setting="backup_selection_type">0</dependency>
|
||||
</dependencies>
|
||||
<control type="toggle" />
|
||||
</setting>
|
||||
<setting id="backup_database" type="boolean" label="30032" help="">
|
||||
<level>0</level>
|
||||
<default>true</default>
|
||||
<dependencies>
|
||||
<dependency type="visible" setting="backup_selection_type">0</dependency>
|
||||
</dependencies>
|
||||
<control type="toggle" />
|
||||
</setting>
|
||||
<setting id="backup_game_saves" type="boolean" label="30133" help="">
|
||||
<level>0</level>
|
||||
<default>false</default>
|
||||
<dependencies>
|
||||
<dependency type="visible" setting="backup_selection_type">0</dependency>
|
||||
</dependencies>
|
||||
<control type="toggle" />
|
||||
</setting>
|
||||
<setting id="backup_playlists" type="boolean" label="30033" help="">
|
||||
<level>0</level>
|
||||
<default>true</default>
|
||||
<dependencies>
|
||||
<dependency type="visible" setting="backup_selection_type">0</dependency>
|
||||
</dependencies>
|
||||
<control type="toggle" />
|
||||
</setting>
|
||||
<setting id="backup_profiles" type="boolean" label="30080" help="">
|
||||
<level>0</level>
|
||||
<default>false</default>
|
||||
<dependencies>
|
||||
<dependency type="visible" setting="backup_selection_type">0</dependency>
|
||||
</dependencies>
|
||||
<control type="toggle" />
|
||||
</setting>
|
||||
<setting id="backup_thumbnails" type="boolean" label="30034" help="">
|
||||
<level>0</level>
|
||||
<default>true</default>
|
||||
<dependencies>
|
||||
<dependency type="visible" setting="backup_selection_type">0</dependency>
|
||||
</dependencies>
|
||||
<control type="toggle" />
|
||||
</setting>
|
||||
<setting id="backup_addons" type="boolean" label="30030" help="">
|
||||
<level>0</level>
|
||||
<default>true</default>
|
||||
<dependencies>
|
||||
<dependency type="visible" setting="backup_selection_type">0</dependency>
|
||||
</dependencies>
|
||||
<control type="toggle" />
|
||||
</setting>
|
||||
<!-- advanced editor options -->
|
||||
<setting id="advanced_button" type="action" label="30125" help="">
|
||||
<level>2</level>
|
||||
<default />
|
||||
<dependencies>
|
||||
<dependency type="visible" setting="backup_selection_type">1</dependency>
|
||||
</dependencies>
|
||||
<control type="button" format="action">
|
||||
<data>RunScript(special://home/addons/script.xbmcbackup/launcher.py,action=advanced_editor)</data>
|
||||
</control>
|
||||
</setting>
|
||||
<setting id="advanced_defaults" type="action" label="30139" help="">
|
||||
<level>2</level>
|
||||
<default />
|
||||
<dependencies>
|
||||
<dependency type="visible" setting="backup_selection_type">1</dependency>
|
||||
</dependencies>
|
||||
<control type="button" format="action">
|
||||
<data>RunScript(special://home/addons/script.xbmcbackup/launcher.py,action=advanced_copy_config)</data>
|
||||
</control>
|
||||
</setting>
|
||||
</group>
|
||||
</category>
|
||||
<category id="scheduling" label="30013">
|
||||
<setting id="enable_scheduler" type="bool" label="30060" default="false" />
|
||||
<setting id="schedule_interval" type="enum" label="30061" lvalues="30079|30072|30073|30074|30075" default="1" enable="eq(-1,true)"/>
|
||||
<setting id="schedule_time" type="labelenum" label="30062" values="00:00|01:00|02:00|03:00|04:00|05:00|06:00|07:00|08:00|09:00|10:00|11:00|12:00|13:00|14:00|15:00|16:00|17:00|18:00|19:00|20:00|21:00|22:00|23:00" default="00:00" visible="!eq(-1,4)" enable="eq(-2,true)"/>
|
||||
<setting id="day_of_week" type="enum" label="30063" lvalues="30065|30066|30067|30068|30069|30070|30071" default="0" visible="eq(-2,2)" enable="eq(-3,true)"/>
|
||||
<setting id="cron_schedule" type="text" label="30064" default="0 0 * * *" visible="eq(-3,4)" enable="eq(-4,true)"/>
|
||||
<setting id="schedule_miss" type="bool" label="30109" default="false" enable="eq(-5,true)" />
|
||||
<setting id="cron_shutdown" type="bool" label="30076" default="false" enable="eq(-6,true)" />
|
||||
<group id="1" label="">
|
||||
<!-- enable scheduler -->
|
||||
<setting id="enable_scheduler" type="boolean" label="30060" help="30158">
|
||||
<level>0</level>
|
||||
<default>false</default>
|
||||
<control type="toggle" />
|
||||
</setting>
|
||||
<!-- schedule interval -->
|
||||
<setting id="schedule_interval" type="integer" label="30061" help="">
|
||||
<level>0</level>
|
||||
<default>1</default>
|
||||
<constraints>
|
||||
<options>
|
||||
<option label="30079">0</option>
|
||||
<option label="30072">1</option>
|
||||
<option label="30073">2</option>
|
||||
<option label="30074">3</option>
|
||||
<option label="30075">4</option>
|
||||
</options>
|
||||
</constraints>
|
||||
<dependencies>
|
||||
<dependency type="enable" setting="enable_scheduler">true</dependency>
|
||||
</dependencies>
|
||||
<control type="spinner" format="string"/>
|
||||
</setting>
|
||||
<!-- hour of the day -->
|
||||
<setting id="schedule_time" type="string" label="30062" help="">
|
||||
<level>0</level>
|
||||
<default>00:00</default>
|
||||
<constraints>
|
||||
<options sort="ascending">
|
||||
<option>00:00</option>
|
||||
<option>01:00</option>
|
||||
<option>02:00</option>
|
||||
<option>03:00</option>
|
||||
<option>04:00</option>
|
||||
<option>05:00</option>
|
||||
<option>06:00</option>
|
||||
<option>07:00</option>
|
||||
<option>08:00</option>
|
||||
<option>09:00</option>
|
||||
<option>10:00</option>
|
||||
<option>11:00</option>
|
||||
<option>12:00</option>
|
||||
<option>13:00</option>
|
||||
<option>14:00</option>
|
||||
<option>15:00</option>
|
||||
<option>16:00</option>
|
||||
<option>17:00</option>
|
||||
<option>18:00</option>
|
||||
<option>19:00</option>
|
||||
<option>20:00</option>
|
||||
<option>21:00</option>
|
||||
<option>22:00</option>
|
||||
<option>23:00</option>
|
||||
</options>
|
||||
<allowempty>false</allowempty>
|
||||
</constraints>
|
||||
<dependencies>
|
||||
<dependency type="visible" setting="schedule_interval" operator="!is">4</dependency>
|
||||
<dependency type="enable" setting="enable_scheduler">true</dependency>
|
||||
</dependencies>
|
||||
<control type="spinner" format="string"/>
|
||||
</setting>
|
||||
<!-- day of the week -->
|
||||
<setting id="day_of_week" type="integer" label="30063" help="">
|
||||
<level>0</level>
|
||||
<default>0</default>
|
||||
<constraints>
|
||||
<options>
|
||||
<option label="30065">0</option>
|
||||
<option label="30066">1</option>
|
||||
<option label="30067">2</option>
|
||||
<option label="30068">3</option>
|
||||
<option label="30069">4</option>
|
||||
<option label="30070">5</option>
|
||||
<option label="30071">6</option>
|
||||
</options>
|
||||
</constraints>
|
||||
<dependencies>
|
||||
<dependency type="visible" setting="schedule_interval">2</dependency>
|
||||
<dependency type="enable" setting="enable_scheduler">true</dependency>
|
||||
</dependencies>
|
||||
<control type="spinner" format="string"/>
|
||||
</setting>
|
||||
<!-- cron schedule -->
|
||||
<setting id="cron_schedule" type="string" label="30064" help="">
|
||||
<level>0</level>
|
||||
<default>0 0 * * *</default>
|
||||
<constraints>
|
||||
<allowempty>false</allowempty>
|
||||
</constraints>
|
||||
<dependencies>
|
||||
<dependency type="visible" setting="schedule_interval">4</dependency>
|
||||
<dependency type="enable" setting="enable_scheduler">true</dependency>
|
||||
</dependencies>
|
||||
<control type="edit" format="string">
|
||||
<heading>30064</heading>
|
||||
</control>
|
||||
</setting>
|
||||
<!-- run if schedule missed -->
|
||||
<setting id="schedule_miss" type="boolean" label="30109" help="">
|
||||
<level>1</level>
|
||||
<default>false</default>
|
||||
<dependencies>
|
||||
<dependency type="enable" setting="enable_scheduler">true</dependency>
|
||||
</dependencies>
|
||||
<control type="toggle" />
|
||||
</setting>
|
||||
<!-- shutdown on complete -->
|
||||
<setting id="cron_shutdown" type="boolean" label="30076" help="">
|
||||
<level>1</level>
|
||||
<default>false</default>
|
||||
<dependencies>
|
||||
<dependency type="enable" setting="enable_scheduler">true</dependency>
|
||||
</dependencies>
|
||||
<control type="toggle" />
|
||||
</setting>
|
||||
</group>
|
||||
</category>
|
||||
</section>
|
||||
</settings>
|
||||
|
||||
198
scheduler.py
@@ -1,198 +0,0 @@
|
||||
import xbmc
|
||||
import xbmcvfs
|
||||
import xbmcgui
|
||||
from datetime import datetime
|
||||
import time
|
||||
import resources.lib.utils as utils
|
||||
from resources.lib.croniter import croniter
|
||||
from resources.lib.backup import XbmcBackup
|
||||
|
||||
UPGRADE_INT = 2 #to keep track of any upgrade notifications
|
||||
|
||||
class BackupScheduler:
|
||||
monitor = None
|
||||
enabled = "false"
|
||||
next_run = 0
|
||||
next_run_path = None
|
||||
restore_point = None
|
||||
|
||||
def __init__(self):
|
||||
self.monitor = UpdateMonitor(update_method = self.settingsChanged)
|
||||
self.enabled = utils.getSetting("enable_scheduler")
|
||||
self.next_run_path = xbmc.translatePath(utils.data_dir()) + 'next_run.txt'
|
||||
|
||||
if(self.enabled == "true"):
|
||||
|
||||
#sleep for 2 minutes so Kodi can start and time can update correctly
|
||||
xbmc.Monitor().waitForAbort(120)
|
||||
|
||||
nr = 0
|
||||
if(xbmcvfs.exists(self.next_run_path)):
|
||||
|
||||
fh = xbmcvfs.File(self.next_run_path)
|
||||
try:
|
||||
#check if we saved a run time from the last run
|
||||
nr = float(fh.read())
|
||||
except ValueError:
|
||||
nr = 0
|
||||
|
||||
fh.close()
|
||||
|
||||
#if we missed and the user wants to play catch-up
|
||||
if(0 < nr <= time.time() and utils.getSetting('schedule_miss') == 'true'):
|
||||
utils.log("scheduled backup was missed, doing it now...")
|
||||
progress_mode = int(utils.getSetting('progress_mode'))
|
||||
|
||||
if(progress_mode == 0):
|
||||
progress_mode = 1 # Kodi just started, don't block it with a foreground progress bar
|
||||
|
||||
self.doScheduledBackup(progress_mode)
|
||||
|
||||
self.setup()
|
||||
|
||||
def setup(self):
|
||||
#scheduler was turned on, find next run time
|
||||
utils.log("scheduler enabled, finding next run time")
|
||||
self.findNextRun(time.time())
|
||||
|
||||
def start(self):
|
||||
|
||||
#display upgrade messages if they exist
|
||||
if(int(utils.getSetting('upgrade_notes')) < UPGRADE_INT):
|
||||
xbmcgui.Dialog().ok(utils.getString(30010),utils.getString(30132))
|
||||
utils.setSetting('upgrade_notes',str(UPGRADE_INT))
|
||||
|
||||
#check if a backup should be resumed
|
||||
resumeRestore = self._resumeCheck()
|
||||
|
||||
if(resumeRestore):
|
||||
restore = XbmcBackup()
|
||||
restore.selectRestore(self.restore_point)
|
||||
#skip the advanced settings check
|
||||
restore.skipAdvanced()
|
||||
restore.restore()
|
||||
|
||||
while(not self.monitor.abortRequested()):
|
||||
|
||||
if(self.enabled == "true"):
|
||||
#scheduler is still on
|
||||
now = time.time()
|
||||
|
||||
if(self.next_run <= now):
|
||||
progress_mode = int(utils.getSetting('progress_mode'))
|
||||
self.doScheduledBackup(progress_mode)
|
||||
|
||||
#check if we should shut the computer down
|
||||
if(utils.getSetting("cron_shutdown") == 'true'):
|
||||
#wait 10 seconds to make sure all backup processes and files are completed
|
||||
time.sleep(10)
|
||||
xbmc.executebuiltin('ShutDown()')
|
||||
else:
|
||||
#find the next run time like normal
|
||||
self.findNextRun(now)
|
||||
|
||||
xbmc.sleep(500)
|
||||
|
||||
#delete monitor to free up memory
|
||||
del self.monitor
|
||||
|
||||
def doScheduledBackup(self,progress_mode):
|
||||
if(progress_mode != 2):
|
||||
utils.showNotification(utils.getString(30053))
|
||||
|
||||
backup = XbmcBackup()
|
||||
|
||||
if(backup.remoteConfigured()):
|
||||
|
||||
if(int(utils.getSetting('progress_mode')) in [0,1]):
|
||||
backup.backup(True)
|
||||
else:
|
||||
backup.backup(False)
|
||||
|
||||
#check if this is a "one-off"
|
||||
if(int(utils.getSetting("schedule_interval")) == 0):
|
||||
#disable the scheduler after this run
|
||||
self.enabled = "false"
|
||||
utils.setSetting('enable_scheduler','false')
|
||||
else:
|
||||
utils.showNotification(utils.getString(30045))
|
||||
|
||||
def findNextRun(self,now):
|
||||
progress_mode = int(utils.getSetting('progress_mode'))
|
||||
|
||||
#find the cron expression and get the next run time
|
||||
cron_exp = self.parseSchedule()
|
||||
|
||||
cron_ob = croniter(cron_exp,datetime.fromtimestamp(now))
|
||||
new_run_time = cron_ob.get_next(float)
|
||||
|
||||
if(new_run_time != self.next_run):
|
||||
self.next_run = new_run_time
|
||||
utils.log("scheduler will run again on " + utils.getRegionalTimestamp(datetime.fromtimestamp(self.next_run),['dateshort','time']))
|
||||
|
||||
#write the next time to a file
|
||||
fh = xbmcvfs.File(self.next_run_path, 'w')
|
||||
fh.write(str(self.next_run))
|
||||
fh.close()
|
||||
|
||||
#only show when not in silent mode
|
||||
if(progress_mode != 2):
|
||||
utils.showNotification(utils.getString(30081) + " " + utils.getRegionalTimestamp(datetime.fromtimestamp(self.next_run),['dateshort','time']))
|
||||
|
||||
def settingsChanged(self):
|
||||
current_enabled = utils.getSetting("enable_scheduler")
|
||||
|
||||
if(current_enabled == "true" and self.enabled == "false"):
|
||||
#scheduler was just turned on
|
||||
self.enabled = current_enabled
|
||||
self.setup()
|
||||
elif (current_enabled == "false" and self.enabled == "true"):
|
||||
#schedule was turn off
|
||||
self.enabled = current_enabled
|
||||
|
||||
if(self.enabled == "true"):
|
||||
#always recheck the next run time after an update
|
||||
self.findNextRun(time.time())
|
||||
|
||||
def parseSchedule(self):
|
||||
schedule_type = int(utils.getSetting("schedule_interval"))
|
||||
cron_exp = utils.getSetting("cron_schedule")
|
||||
|
||||
hour_of_day = utils.getSetting("schedule_time")
|
||||
hour_of_day = int(hour_of_day[0:2])
|
||||
if(schedule_type == 0 or schedule_type == 1):
|
||||
#every day
|
||||
cron_exp = "0 " + str(hour_of_day) + " * * *"
|
||||
elif(schedule_type == 2):
|
||||
#once a week
|
||||
day_of_week = utils.getSetting("day_of_week")
|
||||
cron_exp = "0 " + str(hour_of_day) + " * * " + day_of_week
|
||||
elif(schedule_type == 3):
|
||||
#first day of month
|
||||
cron_exp = "0 " + str(hour_of_day) + " 1 * *"
|
||||
|
||||
return cron_exp
|
||||
|
||||
def _resumeCheck(self):
|
||||
shouldContinue = False
|
||||
if(xbmcvfs.exists(xbmc.translatePath(utils.data_dir() + "resume.txt"))):
|
||||
rFile = xbmcvfs.File(xbmc.translatePath(utils.data_dir() + "resume.txt"),'r')
|
||||
self.restore_point = rFile.read()
|
||||
rFile.close()
|
||||
xbmcvfs.delete(xbmc.translatePath(utils.data_dir() + "resume.txt"))
|
||||
shouldContinue = xbmcgui.Dialog().yesno(utils.getString(30042),utils.getString(30043),utils.getString(30044))
|
||||
|
||||
return shouldContinue
|
||||
|
||||
|
||||
class UpdateMonitor(xbmc.Monitor):
|
||||
update_method = None
|
||||
|
||||
def __init__(self,*args, **kwargs):
|
||||
xbmc.Monitor.__init__(self)
|
||||
self.update_method = kwargs['update_method']
|
||||
|
||||
def onSettingsChanged(self):
|
||||
self.update_method()
|
||||
|
||||
BackupScheduler().start()
|
||||
4
service.py
Normal file
@@ -0,0 +1,4 @@
|
||||
from resources.lib.scheduler import BackupScheduler
|
||||
|
||||
# start the backup scheduler
|
||||
BackupScheduler().start()
|
||||