Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathutils.py
More file actions
Latest commit
578 lines (467 loc) · 15.7 KB
/
Copy pathutils.py
File metadata and controls
578 lines (467 loc) · 15.7 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
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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
importos
importerrno
importjson
importre
importhashlib
importtime
importwebbrowser
fromfunctoolsimportwraps
try:
fromurllib.parseimporturlparse
asserturlparse
exceptImportError:
fromurlparseimporturlparse
try:
from .. importeditor
from . importsharedasG
from .exc_fmtimportstr_e
from . importmsg
from .libimportDMP
assertGandDMP
exceptImportError:
importeditor
importmsg
fromexc_fmtimportstr_e
importsharedasG
fromlibimportDMP
classJOIN_ACTION(object):
PROMPT=1
UPLOAD=2
DOWNLOAD=3
classFlooPatch(object):
def__init__(self, current, buf):
self.buf=buf
self.current=current
self.previous=buf['buf']
ifbuf['encoding'] =='base64':
self.md5_before=hashlib.md5(self.previous).hexdigest()
self.md5_after=hashlib.md5(self.current).hexdigest()
else:
try:
self.md5_before=hashlib.md5(self.previous.encode('utf-8')).hexdigest()
exceptExceptionase:
# Horrible fallback if for some reason encoding doesn't agree with actual object
self.md5_before=hashlib.md5(self.previous).hexdigest()
msg.log('Error calculating md5_before for ', str(self), ': ', str_e(e))
try:
self.md5_after=hashlib.md5(self.current.encode('utf-8')).hexdigest()
exceptExceptionase:
# Horrible fallback if for some reason encoding doesn't agree with actual object
self.md5_after=hashlib.md5(self.current).hexdigest()
msg.log('Error calculating md5_after for ', str(self), ': ', str_e(e))
def__str__(self):
return'%s - %s'% (self.buf['id'], self.buf['path'])
defpatches(self):
returnDMP.patch_make(self.previous, self.current)
defto_json(self):
patches=self.patches()
iflen(patches) ==0:
returnNone
patch_str=''
forpatchinpatches:
patch_str+=str(patch)
return {
'id': self.buf['id'],
'md5_after': self.md5_after,
'md5_before': self.md5_before,
'path': self.buf['path'],
'patch': patch_str,
'name': 'patch'
}
defreload_settings():
floorc_settings=load_floorc_json()
forname, valinfloorc_settings.items():
setattr(G, name, val)
validate_auth(G.AUTH)
ifG.SHARE_DIR:
G.BASE_DIR=G.SHARE_DIR
G.BASE_DIR=os.path.realpath(os.path.expanduser(G.BASE_DIR))
G.COLAB_DIR=os.path.join(G.BASE_DIR, 'share')
G.COLAB_DIR=os.path.realpath(G.COLAB_DIR)
ifG.DEBUG:
msg.LOG_LEVEL=msg.LOG_LEVELS['DEBUG']
else:
msg.LOG_LEVEL=msg.LOG_LEVELS['MSG']
mkdir(G.COLAB_DIR)
returnfloorc_settings
defload_floorc_json():
# Expose a few settings for curious users to tweak
s= {
'expert_mode': False,
'debug': False,
}
try:
withopen(G.FLOORC_JSON_PATH, 'r') asfd:
floorc_json=fd.read()
exceptIOErrorase:
ife.errno==errno.ENOENT:
returns
raise
try:
default_settings=json.loads(floorc_json)
exceptValueError:
returns
fork, vindefault_settings.items():
s[k.upper()] =v
returns
defsave_floorc_json(s):
floorc_json= {}
fork, vins.items():
floorc_json[k.lower()] =v
msg.log('Writing ', floorc_json)
withopen(G.FLOORC_JSON_PATH, 'w') asfd:
fd.write(json.dumps(floorc_json, indent=4, sort_keys=True, separators=(',', ': ')))
defvalidate_auth(auth):
iftype(auth) !=dict:
msg.error('floorc.json validation error: Auth section is not an object!')
returnFalse
to_delete= []
fork, vinauth.items():
iftype(v) !=dict:
msg.error('floorc.json validation error: host "', k, '" has invalid auth credentials. Did you put a setting in the auth section?')
to_delete.append(k)
break
forkeyin ['username', 'api_key', 'secret']:
ifnotv.get(key):
msg.error('floorc.json validation error: host "', k, '" missing "', key, '"')
to_delete.append(k)
break
forkinto_delete:
delauth[k]
returnlen(to_delete) ==0
defcan_auth(host=None):
ifhostisNone:
host=len(G.AUTH) andlist(G.AUTH.keys())[0] orG.DEFAULT_HOST
auth=G.AUTH.get(host)
iftype(auth) ==dict:
returnbool((auth.get('username') orauth.get('api_key')) andauth.get('secret'))
returnFalse
cancelled_timeouts=set()
timeout_ids=set()
defset_timeout(func, timeout, *args, **kwargs):
return_set_timeout(func, timeout, False, *args, **kwargs)
defset_interval(func, timeout, *args, **kwargs):
return_set_timeout(func, timeout, True, *args, **kwargs)
def_set_timeout(func, timeout, repeat, *args, **kwargs):
timeout_id=set_timeout._top_timeout_id
iftimeout_id>100000:
set_timeout._top_timeout_id=0
else:
set_timeout._top_timeout_id+=1
try:
from . importapi
exceptImportError:
importapi
@api.send_errors
deftimeout_func():
timeout_ids.discard(timeout_id)
iftimeout_idincancelled_timeouts:
cancelled_timeouts.remove(timeout_id)
return
func(*args, **kwargs)
ifrepeat:
editor.set_timeout(timeout_func, timeout)
timeout_ids.add(timeout_id)
editor.set_timeout(timeout_func, timeout)
timeout_ids.add(timeout_id)
returntimeout_id
set_timeout._top_timeout_id=0
defcancel_timeout(timeout_id):
iftimeout_idintimeout_ids:
cancelled_timeouts.add(timeout_id)
rate_limits= {}
defrate_limit(name, timeout, func, *args, **kwargs):
ifrate_limits.get(name):
return
rate_limits[name] =time.time()
func(*args, **kwargs)
defdelete_limit():
delrate_limits[name]
set_timeout(delete_limit, timeout, *args, **kwargs)
defparse_url(workspace_url):
secure=G.SECURE
owner=None
workspace_name=None
# owner/workspacename
result=re.match('^([-\@\+\.\w]+)/([-\.\w]+)$', workspace_url)
ifresult:
workspace_url='https://'+G.DEFAULT_HOST+'/'+workspace_url
parsed_url=urlparse(workspace_url)
port=parsed_url.port
ifG.DEBUGandparsed_url.scheme=='http':
# Only obey http if we're debugging
ifnotport:
port=3148
secure=False
ifnotport:
port=G.DEFAULT_PORT
# Allow /file/...
result=re.match('^/([-\@\+\.\w]+)/([-\.\w]+)/?.*$', parsed_url.path)
ifnotresult:
# Old style URL. Do not remove. People still have these in their persistent.json
result=re.match('^/r/([-\@\+\.\w]+)/([-\.\w]+)/?$', parsed_url.path)
ifresult:
(owner, workspace_name) =result.groups()
else:
raiseValueError('%s is not a valid Floobits URL'%workspace_url)
return {
'host': parsed_url.hostname,
'owner': owner,
'port': port,
'workspace': workspace_name,
'secure': secure,
}
defto_workspace_url(r):
port=int(r.get('port', 3448))
ifr['secure']:
proto='https'
ifport==3448:
port=''
else:
proto='http'
ifport==3148:
port=''
ifport!='':
port=':%s'%port
host=r.get('host', G.DEFAULT_HOST)
workspace_url='%s://%s%s/%s/%s'% (proto, host, port, r['owner'], r['workspace'])
p=r.get('path')
ifp:
workspace_url+='/file/%s'%p
line=r.get('line')
ifline:
workspace_url+=':%s'%line
returnworkspace_url
defnormalize_url(workspace_url):
returnto_workspace_url(parse_url(workspace_url))
defget_full_path(p):
full_path=os.path.join(G.PROJECT_PATH, p)
returnunfuck_path(full_path)
defunfuck_path(p):
returnos.path.normpath(p)
defto_rel_path(p):
returnos.path.relpath(p, G.PROJECT_PATH).replace(os.sep, '/')
defto_scheme(secure):
ifsecureisTrue:
return'https'
return'http'
defis_shared(p):
ifnotG.AGENTornotG.AGENT.joined_workspace:
returnFalse
p=unfuck_path(p)
try:
ifto_rel_path(p).find('../') ==0:
returnFalse
exceptValueError:
returnFalse
returnTrue
defupdate_floo_file(path, data):
try:
floo_json=json.loads(open(path, 'r').read())
exceptException:
pass
try:
floo_json.update(data)
exceptException:
floo_json=data
try:
withopen(path, 'w') asfloo_fd:
floo_fd.write(json.dumps(floo_json, indent=4, sort_keys=True, separators=(',', ': ')))
exceptExceptionase:
msg.warn('Couldn\'t update .floo file: ', floo_json, ': ', str_e(e))
defread_floo_file(path):
floo_file=os.path.join(path, '.floo')
info= {}
try:
floo_info=open(floo_file, 'rb').read().decode('utf-8')
info=json.loads(floo_info)
except (IOError, OSError):
pass
exceptExceptionase:
msg.warn('Couldn\'t read .floo file: ', floo_file, ': ', str_e(e))
returninfo
defget_persistent_data(per_path=None):
per_data= {'recent_workspaces': [], 'workspaces': {}}
per_path=per_pathoros.path.join(G.BASE_DIR, 'persistent.json')
try:
per=open(per_path, 'rb')
except (IOError, OSError):
msg.debug('Failed to open ', per_path, '. Recent workspace list will be empty.')
returnper_data
try:
data=per.read().decode('utf-8')
persistent_data=json.loads(data)
exceptExceptionase:
msg.debug('Failed to parse ', per_path, '. Recent workspace list will be empty.')
msg.debug(str_e(e))
msg.debug(data)
returnper_data
if'recent_workspaces'notinpersistent_data:
persistent_data['recent_workspaces'] = []
if'workspaces'notinpersistent_data:
persistent_data['workspaces'] = {}
returnpersistent_data
defupdate_persistent_data(data):
seen=set()
recent_workspaces= []
forxindata['recent_workspaces']:
try:
ifx['url'] inseen:
continue
seen.add(x['url'])
recent_workspaces.append(x)
exceptExceptionase:
msg.debug(str_e(e))
data['recent_workspaces'] =recent_workspaces
per_path=os.path.join(G.BASE_DIR, 'persistent.json')
withopen(per_path, 'wb') asper:
per.write(json.dumps(data, indent=2).encode('utf-8'))
# Cleans up URLs in persistent.json
defnormalize_persistent_data():
persistent_data=get_persistent_data()
forrwinpersistent_data['recent_workspaces']:
rw['url'] =normalize_url(rw['url'])
forowner, workspacesinpersistent_data['workspaces'].items():
forname, workspaceinworkspaces.items():
workspace['url'] =normalize_url(workspace['url'])
workspace['path'] =unfuck_path(workspace['path'])
update_persistent_data(persistent_data)
defadd_workspace_to_persistent_json(owner, name, url, path):
d=get_persistent_data()
workspaces=d['workspaces']
ifownernotinworkspaces:
workspaces[owner] = {}
workspaces[owner][name] = {'url': url, 'path': path}
update_persistent_data(d)
defupdate_recent_workspaces(workspace_url):
d=get_persistent_data()
recent_workspaces=d.get('recent_workspaces', [])
recent_workspaces.insert(0, {'url': workspace_url})
recent_workspaces=recent_workspaces[:100]
seen=set()
new= []
forrinrecent_workspaces:
string=json.dumps(r)
ifstringnotinseen:
new.append(r)
seen.add(string)
d['recent_workspaces'] =new
update_persistent_data(d)
defget_workspace_by_path(path, _filter):
path=unfuck_path(path)
forowner, workspacesinget_persistent_data()['workspaces'].items():
forname, workspaceinworkspaces.items():
ifunfuck_path(workspace['path']) ==path:
r=_filter(workspace['url'])
ifr:
returnr
defrm(path):
"""removes path and dirs going up until a OSError"""
os.remove(path)
try:
os.removedirs(os.path.split(path)[0])
exceptOSError:
pass
defmkdir(path):
try:
os.makedirs(path)
exceptOSErrorase:
ife.errno!=errno.EEXIST:
editor.error_message('Cannot create directory {0}.\n{1}'.format(path, str_e(e)))
raise
defget_line_endings(path):
try:
withopen(path, 'rb') asfd:
line=fd.readline()
exceptException:
return
ifnotline:
return
chunk=line[-2:]
ifchunk=="\r\n":
return"\r\n"
ifchunk[-1:] =="\n":
return"\n"
defsave_buf(buf):
path=get_full_path(buf['path'])
mkdir(os.path.split(path)[0])
ifbuf['encoding'] =='utf8':
newline=get_line_endings(path) oreditor.get_line_endings(path)
try:
withopen(path, 'wb') asfd:
ifbuf['encoding'] =='utf8':
out=buf['buf']
ifnewline!='\n':
out=out.split('\n')
out=newline.join(out)
fd.write(out.encode('utf-8'))
else:
fd.write(buf['buf'])
exceptExceptionase:
msg.error('Error saving buf: ', str_e(e))
def_unwind_generator(gen_expr, cb=None, res=None):
try:
whileTrue:
maybe_func=res
args= []
# if the first arg is callable, we need to call it (and assume the last argument is a callback)
iftype(res) ==tuple:
maybe_func=len(res) andres[0]
ifnotcallable(maybe_func):
# send only accepts one argument... this is slightly dangerous if
# we ever just return a tuple of one elemetn
# TODO: catch no generator
iftype(res) ==tupleandlen(res) ==1:
res=gen_expr.send(res[0])
else:
res=gen_expr.send(res)
continue
deff(*args):
return_unwind_generator(gen_expr, cb, args)
try:
args=list(res)[1:]
exceptException:
# assume not iterable
args= []
args.append(f)
returnmaybe_func(*args)
# TODO: probably shouldn't catch StopIteration to return since that can occur by accident...
exceptStopIteration:
pass
except__StopUnwindingExceptionase:
res=e.ret_val
ifcb:
returncb(res)
returnres
class__StopUnwindingException(BaseException):
def__init__(self, ret_val):
self.ret_val=ret_val
defreturn_value(args):
raise__StopUnwindingException(args)
definlined_callbacks(f):
""" Branching logic in async functions generates a callback nightmare.
Use this decorator to inline the results. If you yield a function, it must
accept a callback as its final argument that it is responsible for firing.
example usage:
"""
@wraps(f)
defwrap(*args, **kwargs):
return_unwind_generator(f(*args, **kwargs))
returnwrap
defhas_browser():
valid_browsers= [
"MacOSX", # Default mac browser.
"Chrome",
"Chromium",
"Firefox",
"Safari",
"Opera",
"windows-default",
]
forbrowserinvalid_browsers:
try:
webbrowser.get(browser)
returnTrue
exceptException:
continue
returnFalse