/
sdpp.py
258 lines (219 loc) · 8.6 KB
/
sdpp.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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
import os
import shutil
import sys
import time
from tinydb import TinyDB, Query
from watchdog import events, observers
PAPERPILE = os.path.expanduser('~/Google Drive/Paperpile/All Papers')
SONY_BOX = os.path.expanduser('~/Box Sync/Paperpile')
forbidden_chars = '"<> ,;:|*?'
def to_sony_filename(fn : str) -> str:
"""Remove tricky characters from Paperpile filenames.
The Sony Digital Paper user guide warns that filenames containing
these characters may not sync correctly. (The space character is
added just in case.)
Parameters
----------
fn : string
The input filename, from the Paperpile Google Drive folder.
Returns
-------
sanitised : string
The sanitised filename.
Examples
--------
>>> fn = 'A paper by Jones; and Smith: Are angle brackets <> needed?.pdf'
>>> to_sony_filename(fn)
'A_paper_by_Jones__and_Smith__Are_angle_brackets____needed_.pdf'
"""
sanitised = fn
for char in forbidden_chars:
sanitised = sanitised.replace(char, '_')
return sanitised
def rscandir(directory):
"""Recursively scan subdirectories and yield simple files."""
for entry in os.scandir(directory):
if entry.is_dir():
yield from rscandir(entry.path)
elif not entry.name.startswith('.'):
yield entry
def init_paperpile_to_dpt(paperpile_path : str = None,
sony_path : str = None,
dry_run : bool = True,
verbose : bool = True):
"""Make the initial copy from Paperpile to the Box directory.
This creates a tinyDB database that will store modification times
for documents, enabling later sync.
"""
home = os.path.expanduser('~/')
truncate = len(home)
if paperpile_path is None and sony_path is None:
paperpile_path = PAPERPILE
sony_path = SONY_BOX
os.chdir(home)
db = TinyDB('.sdpp-sync.json')
for entry in rscandir(paperpile_path):
new_paperpile_to_box(paperpile_path, db,
dry_run=dry_run, verbose=verbose)
def new_paperpile_to_box(path_to_file_in_paperpile, db, *,
dry_run=True, verbose=True):
"""Copy a new Paperpile file to Box, and add to database.
"""
home = os.path.expanduser('~/')
truncate = len(home)
os.chdir(home)
original_path = path_to_file_in_paperpile
original_name = os.path.basename(original_path)
if original_path.startswith(home):
original_path = original_path[truncate:]
new_path = os.path.join(SONY_BOX, to_sony_filename(original_name))
if new_path.startswith(home):
new_path = new_path[truncate:]
if verbose or dry_run:
print('copying %s to %s' % (original_path, new_path))
if not dry_run:
shutil.copy2(original_path, new_path)
db.insert({'paperpile': original_path, 'box': new_path,
'modtime': os.stat(original_path).st_mtime})
def sync_existing(dry_run=True, verbose=True):
"""Manually sync files by comparing modification time with last known.
"""
home = os.path.expanduser('~/')
truncate = len(home)
os.chdir(home)
db = TinyDB('.sdpp-sync.json')
for elem in db.all():
sony, paperpile, modtime = [elem[k]
for k in ['box', 'paperpile', 'modtime']]
modified_action(elem, sony, paperpile, modtime,
dry_run=dry_run, verbose=verbose)
def modified_action(elem, sony, paperpile, modtime, *,
dry_run=True, verbose=True):
"""Perform appropriate action when a file has been modified.
Parameters
----------
elem : db element (dictionary)
sony, paperpile : string
The paths to the Sony Box file and corresponding Paperpile file.
modtime : number
The last known modification time (seconds from Epoch) of the file.
"""
smod = os.stat(sony).st_mtime
pmod = os.stat(paperpile).st_mtime
print('current: %i, sony: %i, paperpile: %i' % (modtime, smod, pmod))
if smod > modtime + 10: # 10 second resolution
print('sony newer')
if pmod > modtime + 10:
print('both %s and %s changed; favoring Sony.' %
(sony, paperpile))
if dry_run or verbose:
print('copying updated %s to %s' % (sony, paperpile))
if not dry_run:
elem['modtime'] = smod
shutil.copy2(sony, paperpile)
elif pmod > modtime + 10:
print('paperpile newer')
if dry_run or verbose:
print('copying update %s to %s' % (paperpile, sony))
if not dry_run:
elem['modtime'] = pmod
shutil.copy2(paperpile, sony)
class FileModifiedHandler(events.FileSystemEventHandler):
def __init__(self, *args, **kwargs):
self.dry_run = kwargs.pop('dry_run', True)
self.verbose = kwargs.pop('verbose', True)
super().__init__(*args, **kwargs)
os.chdir(os.path.expanduser('~'))
self.db = TinyDB('.sdpp-sync.json')
def on_modified(self, event):
home = os.path.expanduser('~/')
path = event.src_path
if path.startswith(home):
path = path[len(home):]
q = Query()
result = (self.db.search(q.box == path) +
self.db.search(q.paperpile == path))
print(result)
if len(result) != 0:
result = result[0]
modified_action(result, result['box'], result['paperpile'],
result['modtime'], dry_run=self.dry_run,
verbose=self.verbose)
class NewFileHandler(events.FileSystemEventHandler):
def __init__(self, *args, **kwargs):
self.dry_run = kwargs.pop('dry_run', True)
self.verbose = kwargs.pop('verbose', True)
super().__init__(*args, **kwargs)
os.chdir(os.path.expanduser('~'))
self.db = TinyDB('.sdpp-sync.json')
def on_created(self, event):
home = os.path.expanduser('~/')
path = event.src_path
if path.startswith(home):
path = path[len(home):]
if path.endswith('.pdf') or path.endswith('.PDF'):
new_paperpile_to_box(event.src_path, self.db,
dry_run=self.dry_run, verbose=self.verbose)
class MovedFileHandler(events.FileSystemEventHandler):
def __init__(self, *args, **kwargs):
self.dry_run = kwargs.pop('dry_run', True)
self.verbose = kwargs.pop('verbose', True)
super().__init__(*args, **kwargs)
os.chdir(os.path.expanduser('~'))
self.db = TinyDB('.sdpp-sync.json')
def on_moved(self, event):
path = event.src_path
if not path.endswith('.pdf') or path.endswith('.PDF'):
return
home = os.path.expanduser('~/')
dest = event.dest_path
if path.startswith(home):
path = path[len(home):]
if dest.startswith(home):
dest = dest[len(home):]
box_old = to_sony_filename(path)
box_new = to_sony_filename(dest)
os.rename(box_old, box_new)
q = Query()
elem = self.db.search(q.box == box_old)
elem['box'] = box_new
elem['modtime'] = os.stat(box_new).st_mtime
elem['paperpile'] = dest
def watch(dry_run=True, verbose=True):
"""Watch both directories for changes and copy when detected.
"""
os.chdir(os.path.expanduser('~'))
obs = observers.Observer()
mod = FileModifiedHandler(dry_run=dry_run, verbose=verbose)
new = NewFileHandler(dry_run=dry_run, verbose=verbose)
mov = MovedFileHandler(dry_run=dry_run, verbose=verbose)
obs.schedule(mod, SONY_BOX, recursive=True)
obs.schedule(new, PAPERPILE, recursive=True)
obs.schedule(mov, PAPERPILE, recursive=True)
obs.start()
try:
while True:
time.sleep(10)
except KeyboardInterrupt:
obs.stop()
obs.join()
def main():
import argparse
p = argparse.ArgumentParser(description='sync paperpile and box')
p.add_argument('--init', action='store_true',
help='Run initial sync')
p.add_argument('--do', action='store_true',
help='Actually sync, not just dry run.')
p.add_argument('--syncx', action='store_true',
help='Manually sync existing files.')
p.add_argument('--watch', action='store_true',
help='Watch the relevant directories and sync when needed.')
args = p.parse_args()
if args.init:
init_paperpile_to_dpt(dry_run=(not args.do))
elif args.syncx:
sync_existing(dry_run=(not args.do))
elif args.watch:
watch(dry_run=(not args.do))
if __name__ == '__main__':
main()