This repository has been archived by the owner on May 22, 2021. It is now read-only.
/
drake.py
470 lines (351 loc) · 12.7 KB
/
drake.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
import traceback,os
import kivy
import base64
import webbrowser,urllib
from kivy.config import Config
Config.set('graphics', 'show_cursor', '1')
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.textinput import TextInput
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.popup import Popup
from kivy.uix.filechooser import FileChooserListView
from kivy.uix.recycleview import RecycleView
from kivy.lang import Builder
Builder.load_string('''
<RecycleView, Label, Button>
font_size: 18
canvas.before:
Color:
rgba: 0.8, 0.8, 0.8, 1
Rectangle:
pos: self.pos
size: self.size
canvas.after:
Color:
rgba: 0.55, 0.65, 0.4, 1
Line:
points: [self.pos[0],self.pos[1]+1, self.pos[0]+self.size[0]-1,self.pos[1], self.pos[0]+self.size[0]-1, self.pos[1]+self.size[1]+1 ]
width: dp(2)
Color:
rgba: 0.35, 0.45, 0.3, 1
Line:
points: [self.pos[0],self.pos[1], self.pos[0],self.pos[1]+self.size[1], self.pos[0]+self.size[0], self.pos[1]+self.size[1] ]
width: dp(3)
Color:
rgba: 0.1, 0.2, 0.08, 1
Line:
points: [self.pos[0]+dp(2),self.pos[1], self.pos[0]+dp(2),self.pos[1]+self.size[1]-dp(2), self.pos[0]+self.size[0]+dp(2), self.pos[1]+self.size[1]-dp(2) ]
width: dp(2)
[FileListEntry@FloatLayout+TreeViewNode]:
locked: False
entries: []
path: ctx.path
# FIXME: is_selected is actually a read_only treeview property. In this
# case, however, we're doing this because treeview only has single-selection
# hardcoded in it. The fix to this would be to update treeview to allow
## multiple selection.nam
is_selected: self.path in ctx.controller().selection
orientation: 'horizontal'
size_hint_y: None
height: '48dp' if dp(1) > 1 else '24dp'
# Don't allow expansion of the ../ node
is_leaf: not ctx.isdir or ctx.name.endswith('..' + ctx.sep) or self.locked
on_touch_down: self.collide_point(*args[1].pos) and ctx.controller().entry_touched(self, args[1])
on_touch_up: self.collide_point(*args[1].pos) and ctx.controller().entry_released(self, args[1])
BoxLayout:
pos: root.pos
size_hint_x: None
width: root.width - dp(10)
Label:
canvas.before:
Color:
rgba: 0.8, 0.8, 0.8, 1
Rectangle:
pos: self.pos
size: self.size
# --------------
# CHANGE FONT COLOR
# --------------
color: 0, 0, 0, 1
id: filename
text_size: self.width, None
halign: 'left'
shorten: True
text: ctx.name
''')
import drayer, sys, os,time
import rv
db = None
#The UI redefines this. It's the callback for what drayer should
#call to refresh the UI
def rl():
pass
rlfun = rl
class DrayerRefresher(drayer.DrayerStream):
def onChange(self, op, x,y,z):
rlfun()
drayer.startServer()
drayer.startLocalDiscovery()
import atexit
def cleanup():
db.close()
atexit.register(cleanup)
def openStream(x,pk=None):
global db
if(db):
db.close()
db = DrayerRefresher(x,pk)
rlfun()
def presentError(x=None):
x=x or traceback.format_exc()
layout = BoxLayout(orientation='vertical')
yes = Button(text='Ok', font_size=18, size_hint=(1,0.1))
layout.add_widget(TextInput(text=x))
layout.add_widget(yes)
popup = Popup(title='Error', content=layout, size_hint=(None, None), size=(600, 400))
def y(*a,**k):
popup.dismiss()
yes.bind(on_press=y)
popup.open()
try:
openStream(sys.argv[1])
print("Opened "+sys.argv[1])
except:
print(traceback.format_exc())
presentError(traceback.format_exc())
db = None
def FilePopup(x):
layout = BoxLayout(orientation='vertical')
fch = FileChooserListView()
fch.path= os.getcwd()
pubkey = TextInput(hint_text="Public Key of the stream, leave blank to create new stream or accept the one in the file", multiline=False,size_hint=(1,0.1))
filename = TextInput(hint_text="Filename", multiline=False,size_hint=(1,0.1))
button = Button(text='Select', font_size=14, size_hint=(1,0.1))
def s(x,y):
try:
filename.text = os.path.basename(str(fch.selection[0]))
except:
filename.text =''
fch.bind(selection=s)
n = []
def f(j):
try:
if filename.text:
openStream(os.path.join(fch.path,filename.text) ,pubkey.text)
n.extend(fch.selection)
else:
presentError("Nothing selected!s")
except:
presentError(traceback.format_exc())
popup.dismiss()
layout.add_widget(pubkey)
layout.add_widget(fch)
layout.add_widget(filename)
layout.add_widget(button)
popup = Popup(title='Open or Create a Stream', content=layout, size_hint=(None, None), size=(600, 400))
button.bind(on_press=f)
popup.open()
def ImportFilesPopup(x=None):
layout = BoxLayout(orientation='vertical')
hlp = Label(text="""Choose a directory to sync with.
All files in the directory will be imported,
(dir/foo.txt will map to foo.txt, and all files in the stream that
are not in the folder will be deleted from the stream.
Include an index.html to create a website.
""")
fch = FileChooserListView()
fch.path= os.getcwd()
fch.dirselect=True
filename = TextInput(hint_text="Foldername", multiline=False,size_hint=(1,0.1))
button = Button(text='Select', font_size=14, size_hint=(1,0.1))
def s(x,y):
try:
filename.text = os.path.basename(str(fch.selection[0]))
except:
filename.text =''
fch.bind(selection=s)
n = []
def f(j):
try:
fn = os.path.join(fch.path, filename.text)
if fn:
if os.path.isdir(fn):
db.importFiles(fn, True)
else:
presentError("Not a directory")
else:
presentError("Nothing selected!s")
except:
presentError(traceback.format_exc())
popup.dismiss()
layout.add_widget(hlp)
layout.add_widget(fch)
layout.add_widget(filename)
layout.add_widget(button)
popup = Popup(title='Open or Create a Stream', content=layout, size_hint=(None, None), size=(600, 400))
button.bind(on_press=f)
popup.open()
def ConfirmPopup(x, cb):
layout = BoxLayout(orientation='vertical')
yes = Button(text='Confirm', font_size=14, size_hint=(1,0.2))
no = Button(text='Cancel', font_size=14, size_hint=(1,0.2))
layout.add_widget(Label(text=x))
layout.add_widget(yes)
layout.add_widget(no)
popup = Popup(title='Confirm?', content=layout, size_hint=(None, None), size=(600, 400))
def y(*a,**k):
cb(True)
popup.dismiss()
def n(*a,**k):
cb(False)
popup.dismiss()
yes.bind(on_press=y)
no.bind(on_press=n)
popup.open()
def PubkeyPopup(x):
layout = BoxLayout(orientation='vertical')
yes = Button(text='Ok', font_size=18, size_hint=(1,0.2))
layout.add_widget(TextInput(text=x))
layout.add_widget(yes)
popup = Popup(title='Confirm?', content=layout, size_hint=(None, None), size=(600, 400))
def y(*a,**k):
popup.dismiss()
yes.bind(on_press=y)
popup.open()
def getPublicSocialposts():
c = db.getConn().cursor()
c.execute('SELECT key,value FROM record WHERE type="publicSocialPost" ORDER BY id desc')
return c
def getOneSocialPost(key):
d=db.rawGetItemByKey(key,'publicSocialPost')
k=key.split(":",1)
t = 12345
try:
t = float(k[0])
title=k[1]
except:
title="Untitled"
return (title, t, d)
class MyApp(App):
def build(self):
layout = BoxLayout(orientation='vertical')
body = BoxLayout(orientation='horizontal')
buttonbar=BoxLayout(orientation="horizontal",size_hint=(1,0.05))
c1 = BoxLayout(orientation='vertical')
c2 = BoxLayout(orientation='vertical')
bwid = 1/5.0
sel = Button(text='Select a File', font_size=14, size_hint=(bwid,1))
showpubkey = Button(text='Show the pubkey', font_size=14, size_hint=(bwid,1))
sync = Button(text='Sync!', font_size=14, size_hint=(bwid,1))
brows = Button(text='Web Browser', font_size=14, size_hint=(bwid,1))
imfils = Button(text='Import Folder', font_size=14, size_hint=(bwid,1))
buttonbar.add_widget(showpubkey)
buttonbar.add_widget(sel)
buttonbar.add_widget(sync)
buttonbar.add_widget(brows)
buttonbar.add_widget(imfils)
layout.add_widget(buttonbar)
layout.add_widget(body)
body.add_widget(c1)
body.add_widget(c2)
#
newposttitle = TextInput(hint_text="Title", multiline=False,size_hint=(1,0.1))
#Edit the selected post
newpost = TextInput()
submit = Button(text='Update!', font_size=14, size_hint=(1,0.1))
c1.add_widget(newposttitle)
c1.add_widget(newpost)
c1.add_widget(submit)
allposts = rv.RV()
allposts.selected="newpost"
c2.add_widget(allposts)
delb = Button(text='Delete Selected', font_size=14, size_hint=(1,0.1))
c2.add_widget(delb)
global rlfun
def rf():
if db:
try:
d = [{"text":"Create New Post", "value":"newpost"}]
for i in getPublicSocialposts():
k=i["key"].split(":",1)
t = 12345
try:
t = float(k[0])
title=k[1]
except:
title="Untitled"
d.append({"text":time.strftime("%Y %b %d %I:%M%p",time.gmtime(t))+" "+title,"value":i['key']})
allposts.data = d
except:
presentError()
allposts.data=[{"text":"foo"}, {"text":"bar"}]
rlfun = rf
rf()
oldsel = [newpost]
def dels(c):
if allposts.selected=="newpost":
return
#Copy, so it doesn't get changed before they click
x = allposts.selected
def f(y):
if y:
try:
db.rawDelete(allposts.selected,"publicSocialPost")
rf()
except:
presentError(traceback.format_exc())
ConfirmPopup("Really delete "+newposttitle.text+"?", f)
delb.bind(on_press=dels)
def onSelect(post):
if post=="newpost":
newposttitle.readonly = False
if not oldsel[0]==post:
newposttitle.text=''
newpost.text=''
else:
newposttitle.readonly=True
try:
p = getOneSocialPost(post)
newposttitle.text = p[0]
newpost.text= p[2]
except:
presentError()
oldsel[0] = newpost
allposts.onSelect= onSelect
def post(instance):
if allposts.selected=="newpost":
db.rawSetItem(str(time.time())+":"+newposttitle.text, newpost.text.encode("utf8"),"publicSocialPost")
newpost.text = ''
newposttitle.text =''
rf()
else:
db.rawSetItem(allposts.selected, newpost.text.encode("utf8"),"publicSocialPost")
def spk(inst):
if db:
PubkeyPopup(base64.b64encode(db.pubkey))
else:
presentError("No stream loaded!")
def sf(inst):
if db:
db.sync()
else:
presentError("No stream loaded!")
def imfilscb(inst):
if db:
ImportFilesPopup()
else:
presentError("No stream loaded!")
def wbcb(inst):
webbrowser.open("http://localhost:"+str(drayer.http_port)+"/webAccess/"+
urllib.parse.quote_plus(base64.b64encode(db.pubkey).decode("utf8"))+"/"+"index.html")
sync.bind(on_press=sf)
showpubkey.bind(on_press=spk)
submit.bind(on_press=post)
sel.bind(on_press=FilePopup)
brows.bind(on_press=wbcb)
imfils.bind(on_press=imfilscb)
return layout
if __name__ == '__main__':
MyApp().run()