1
|
1 |
|
from plugin.core.environment import Environment |
2
|
1 |
|
from plugin.sync.core.enums import SyncMode |
3
|
1 |
|
from plugin.sync.modes.core.base import Mode |
4
|
|
|
|
5
|
1 |
|
import json |
6
|
1 |
|
import os |
7
|
|
|
|
8
|
|
|
|
9
|
1 |
|
class Base(Mode): |
10
|
1 |
|
mode = SyncMode.Push |
11
|
|
|
|
12
|
1 |
|
@classmethod |
13
|
|
|
def log_pending(cls, log, message, account, key, items): |
14
|
|
|
if type(items) is set: |
15
|
|
|
items = [ |
16
|
|
|
(k, None) |
17
|
|
|
for k in items |
18
|
|
|
] |
19
|
|
|
elif type(items) is dict: |
20
|
|
|
items = [ |
21
|
|
|
(k, v) |
22
|
|
|
for k, v in items.items() |
23
|
|
|
if len(v) > 0 |
24
|
|
|
] |
25
|
|
|
else: |
26
|
|
|
raise ValueError('Unknown type for "pending" parameter') |
27
|
|
|
|
28
|
|
|
if len(items) < 1: |
29
|
|
|
return |
30
|
|
|
|
31
|
|
|
# Format items |
32
|
|
|
count, keys = cls.format_pending(items) |
33
|
|
|
|
34
|
|
|
# Update pending items report |
35
|
|
|
try: |
36
|
|
|
report_path = cls.write_pending(account, key, keys) |
37
|
|
|
except Exception, ex: |
38
|
|
|
log.warn('Unable to save report: %s', ex, exc_info=True) |
39
|
|
|
report_path = None |
40
|
|
|
|
41
|
|
|
# Display message in log file |
42
|
|
|
log.info(message, count, os.path.relpath(report_path, Environment.path.home)) |
43
|
|
|
|
44
|
1 |
|
@classmethod |
45
|
|
|
def write_pending(cls, account, key, keys): |
46
|
|
|
directory = os.path.join(Environment.path.plugin_data, 'Reports', 'Sync', str(account.id), 'Pending') |
47
|
|
|
path = os.path.join(directory, '%s.json' % key) |
48
|
|
|
|
49
|
|
|
# Ensure directory exists |
50
|
|
|
if not os.path.exists(directory): |
51
|
|
|
os.makedirs(directory) |
52
|
|
|
|
53
|
|
|
# Write items |
54
|
|
|
with open(path, 'w') as fp: |
55
|
|
|
json.dump( |
56
|
|
|
keys, fp, |
57
|
|
|
sort_keys=True, |
58
|
|
|
indent=4, |
59
|
|
|
separators=(',', ': ') |
60
|
|
|
) |
61
|
|
|
|
62
|
|
|
return path |
63
|
|
|
|
64
|
1 |
|
@classmethod |
65
|
|
|
def format_pending(cls, items): |
66
|
|
|
result = {} |
67
|
|
|
child_count = 0 |
68
|
|
|
|
69
|
|
|
for key, children in items: |
70
|
|
|
key = '/'.join([str(k) for k in key]) |
71
|
|
|
|
72
|
|
|
# Set show/movie |
73
|
|
|
result[key] = None |
74
|
|
|
|
75
|
|
|
if children is None: |
76
|
|
|
continue |
77
|
|
|
|
78
|
|
|
# Append keys of children |
79
|
|
|
result[key] = [] |
80
|
|
|
|
81
|
|
|
for c_key in children: |
82
|
|
|
if type(c_key) is tuple and len(c_key) == 2: |
83
|
|
|
c_key = 'S%02dE%02d' % c_key |
84
|
|
|
elif type(c_key) is tuple: |
85
|
|
|
c_key = '/'.join([str(k) for k in c_key]) |
86
|
|
|
|
87
|
|
|
result[key].append(c_key) |
88
|
|
|
child_count += 1 |
89
|
|
|
|
90
|
|
|
# Sort children keys |
91
|
|
|
result[key] = sorted(result[key]) |
92
|
|
|
|
93
|
|
|
if not child_count: |
94
|
|
|
return len(result), sorted(result.keys()) |
95
|
|
|
|
96
|
|
|
return child_count, result |
97
|
|
|
|