forked from jessegrosjean/Documents.com.client.python
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdocuments_sync.py
More file actions
155 lines (125 loc) · 5.01 KB
/
documents_sync.py
File metadata and controls
155 lines (125 loc) · 5.01 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/usr/bin/env python
# BEEP BEEP THIS module is NOT DONE YET. NOT WORKING.
# BEEP BEEP THIS module is NOT DONE YET. NOT WORKING.
# BEEP BEEP THIS module is NOT DONE YET. NOT WORKING.
# BEEP BEEP THIS module is NOT DONE YET. NOT WORKING.
import os
import sys
import sqlite3
import optparse
import simplejson
import documents_service
parser = optparse.OptionParser(usage="usage: %prog [options]")
parser.add_option("-u", "--user", action="store", dest="user", help="Google ID for Google Authentication.")
parser.add_option("-p", "--password", action="store", dest="password", help="Password for Google Authentication.")
parser.add_option("-c", "--service", action="store", dest="service", default="simpletextws", help="Service name for Google authentication.")
parser.add_option("-s", "--server", action="store", dest="server", default="www.simpletext.ws", help="Documents service server URL.")
options = parser.parse_args()[0]
service_instance = documents_service.DocumentsService(options.service, options.server, options.user, options.password)
class SyncedDocument(object):
def __init__(self, path):
self.serverVersion = -1
self.name = s.path.split(path)[1]
self.user_deleted = False
self.content = data.get('content')
self.shadowID = data.get('id')
self.shadowVersion = data.get('version')
self.shadowContent = data.get('content')
def __init__(self, data):
self.serverVersion = -1
self.name = data.get('name')
self.content = data.get('content')
self.shadowID = data.get('id')
self.shadowVersion = data.get('version')
self.shadowContent = data.get('content')
def local_edits(self):
if self.shadowID != None and self.shadowVersion != None:
edits = {}
edits['version'] = self.shadowVersion
if self.content != self.shadowContent:
edits['patches'] = patches
return edits
return None
def has_server_edits(self):
return self.shadowVersion != self.serverVersion
def isServerDocument(self):
return self.shadowID != None
def isDeletedFromServer(self):
pass
def isInsertedFromServer(self):
return self.serverVersion != -1 and self.shadowVersion == None
class SyncedDocumentController(object):
def __init__(self):
self.connection = sqlite3.connect('./.syncedDocuments.sqlite')
c = self.connection.cursor()
c.execute('''create table documents
(content TEXT, name TEXT, shadowContent TEXT, shadowID TEXT, shadowName TEXT,
userDeleted INTEGER, version INTEGER)''')
self.connection.commit()
c.close()
def sync_document(self, document):
pass
def sync_documents(self):
syncing_documents = []
# 1. Get server documents index, mapped by id
server_documents_state_by_id = self.read_server_documents_state()
# 2. Map local documents to server documents
for each_document in self.read_local_documents():
if each_document.isSyncedToServer():
each_server_document_state = server_documents_state_by_id[each_document.shadowID]
if each_server_document_state:
each_document.serverVersion = each_server_document_state.get('version')
del server_documents_state_by_id[each_document.shadowID]
each_document.isDeletedFromServer = False
else:
each_document.isDeletedFromServer = True
syncing_documents.append(each_document)
for each in service_instance.GET_documents():
servers[each['id']] = SyncedDocument(each)
def read_server_documents_state(self):
documents_state_by_id = {}
for each in service_instance.GET_documents():
documents_state_by_id[each['id']] = each
return documents_state_by_id
def read_local_documents(self):
c = self.connection.cursor()
c.execute('select * from documents')
for row in c:
print row
c.close()
locals = {}
locals_path = os.getcwd()
for each in os.listdir(locals_path):
if each.endswith('.py'):
each_path = "%s/%s" % (locals_path, each)
if not os.path.isdir(each_path):
data = {}
data['name'] = each
data['content'] = open(each_path).read()
shadows_path = locals_path + ".shadows"
#for each in os.listdir(locals_path):
#for each in
#return locals
def read_shadows(self):
shadows = {}
shadows_path = os.getcwd() + ".shadows"
if not os.path.isdir(shadows_path):
os.mkdir(shadows_path)
for each in os.listdir(locals_path):
if each.endswith('.shadow'):
shadow_id, shadow_version = os.path.splitext[0].partition('_')
shadow_data = {}
shadow_data['shadowID'] = shadow_id
shadow_data['shadowVersion'] = shadow_version
shadow_data['shadowContent'] = open("%s/%s" % (shadows_path, each)).read()
shadows[shadow_id] = SyncedDocument(shadow_data)
return shadows
print service_instance.GET_documents()
print service_instance.GET_document("p2")
print service_instance.PUT_document("p2", None, name="kimchi", content="look! monkeys are here.")
#synced_document_controller = SyncedDocumentController()
#synced_document_controller.sync_documents()
# BEEP BEEP THIS module is NOT DONE YET. NOT WORKING.
# BEEP BEEP THIS module is NOT DONE YET. NOT WORKING.
# BEEP BEEP THIS module is NOT DONE YET. NOT WORKING.
# BEEP BEEP THIS module is NOT DONE YET. NOT WORKING.