This repository was archived by the owner on Nov 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdevice.py
More file actions
executable file
·91 lines (72 loc) · 2.94 KB
/
device.py
File metadata and controls
executable file
·91 lines (72 loc) · 2.94 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
import os
import re
import shutil
from uuid import UUID
from db.__config import Union,e,MODEL,CALIBRATION
class Model:
def __init__(self,uuid:str)->None:
self._id=uuid
def __getitem__(self,key:str)->Union[str,None]:
if not re.match('^[a-z0-9]+(-[a-z0-9]+)*$',key):
raise ValueError('algo contains invalid characters')
return e('select path from model where uuid=? and algo=?',(self._id,key,))
def __setitem__(self,key:str,value:Union[str,None])->None:
if not re.match('^[a-z0-9]+(-[a-z0-9]+)*$',key):
raise ValueError('algo contains invalid characters')
s=self[key]
if value is None:
if s is not None:
shutil.rmtree(s,ignore_errors=True)
e('delete from model where uuid=? and algo=?',(self._id,key,))
else:
_model=MODEL%(self._id,key,)
if s is None:
e('insert into model(uuid,algo,path) values(?,?,?)',(self._id,key,_model,))
os.makedirs(os.path.dirname(_model),exist_ok=True)
shutil.copyfile(value,_model)
class Device:
def __init__(self,uuid:str)->None:
self._id=uuid
@property
def id(self)->UUID:
return UUID(self._id)
@property
def email(self)->Union[str,None]:
return e('select email from device where uuid=?',(self._id,))
@email.setter
def email(self,value:Union[str,None])->None:
if value and len(value)>254:
raise ValueError('email is too long')
e('update device set email=? where uuid=?',(value,self._id,))
@property
def model(self)->Model:
return Model(self._id)
@property
def calibration(self)->Union[str,None]:
return e('select calibration from device where uuid=?',(self._id,))
@calibration.setter
def calibration(self,value:Union[str,None])->None:
s=self.calibration
if value is None:
if s is not None:
shutil.rmtree(s,ignore_errors=True)
e('update device set calibration=? where uuid=?',(None,self._id,))
else:
_calibration=CALIBRATION%(self._id,)
if s is None:
e('update device set calibration=? where uuid=?',(_calibration,self._id,))
shutil.copytree(value,_calibration,dirs_exist_ok=True)
def exists(devid:Union[str,UUID])->bool:
devid=UUID(str(devid)).hex
return e('select 1 from device where uuid=?',(devid,)) is not None
def get(devid:Union[str,UUID])->Device:
devid=UUID(str(devid)).hex
if e('select 1 from device where uuid=?',(devid,)) is None:
e('insert into device(uuid,email,calibration) values(?,?,?)',(devid,None,None))
return Device(devid)
def remove(devid:Union[str,UUID])->None:
devid=UUID(str(devid)).hex
e('delete from device where uuid=?',(devid,))
e('delete from model where uuid=?',(devid,))
_dir=os.path.dirname(CALIBRATION%devid)
shutil.rmtree(_dir,ignore_errors=True)