forked from camicroscope/SlideLoader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdev_utils.py
73 lines (61 loc) · 1.81 KB
/
dev_utils.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
import hashlib
import os
import json
import requests
import sys
import ImageReader
post_url = "http://ca-back:4010/data/Slide/post"
# deleteme
# todo deleteme
import threading
class X:
def __del__(self):
print("dying thread more effort later?", flush=True)
ll = threading.local()
# given a path, get metadata
# if raise_exception is false, returns an object with attribute "error"
def getMetadata(filepath, extended, raise_exception):
print("getMetadata called", file=sys.stderr)
# TODO consider restricting filepath
metadata = {}
if not os.path.isfile(filepath):
msg = {"error": "No such file"}
print(msg)
return msg
try:
reader = ImageReader.construct_reader(filepath)
except BaseException as e:
if raise_exception:
raise e
# here, e has attribute "error"
return str(e)
return reader.get_basic_metadata(extended)
def postslide(img, url, token=''):
if token != '':
url = url + '?token='+token
payload = json.dumps(img)
res = requests.post(url, data=payload, headers={'content-type': 'application/json'})
if res.status_code < 300:
img['_status'] = 'success'
else:
img['_status'] = str(res.status_code)
print('status ' + img['_status'])
return img
# given a list of path, get metadata for each
def getMetadataList(filenames, extended, raise_exception):
allData = []
for filename in filenames:
allData.append(getMetadata(filename, extended, raise_exception))
return allData
def file_md5(fileName):
m = hashlib.md5()
blocksize = 2 ** 20
with open(fileName, "rb") as f:
while True:
buf = f.read(blocksize)
if not buf:
break
m.update(buf)
return m.hexdigest()
def hello():
print('hello!')