-
Notifications
You must be signed in to change notification settings - Fork 0
/
sync.py
77 lines (67 loc) · 1.97 KB
/
sync.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import ui
import os
import sys
import config
from anki import Collection as aopen
from anki import sync
def init(_x):
global x
x = _x
def synchronize():
(client, server, hkey) = connect(x)
ret = attempt_incremental_sync(client)
if ret == 'fullSync':
ret = perform_full_sync(x, server, hkey)
print(ret)
perform_media_sync(x, server, hkey)
###
def connect(x):
hkey = config.hkey()
server = sync.RemoteServer(hkey)
hkey = authenticate(hkey, server)
client = sync.Syncer(x, server)
return (client, server, hkey)
def authenticate(hkey, server):
if not hkey:
username = ui.uinput(uprompt='Username', required=True)
password = ui.uinput(uprompt='Password', required=True, password=True)
print('Authenticating...', end=' ', flush=True)
hkey = server.hostKey(username, password)
if not hkey:
print( "bad auth")
raise EOFError()
else:
print("success")
config.save_hkey(hkey)
return hkey
def attempt_incremental_sync(client):
print('Attempting incremental sync...', end=' ', flush=True)
return client.sync()
def perform_full_sync(x, server, hkey):
dir = ui.up_down()
client = sync.FullSyncer(x, hkey, server.client)
try:
if dir == "u":
print('Full upload...', end=' ', flush=True)
if client.upload():
ret = 'success'
else:
ret = 'err'
else:
print('Full download...', end=' ', flush=True)
client.download()
ret = 'success'
except Exception as e:
print(str(e))
raise
x.reopen()
return ret
def perform_media_sync(x, server, hkey):
print('Performing media sync...', end=' ', flush=True)
server = sync.RemoteMediaServer(x, hkey, server.client)
client = sync.MediaSyncer(x, server)
try:
ret = client.sync()
except Exception as e:
ret = str(e)
print(ret)