-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy path__main__.py
More file actions
52 lines (38 loc) · 1.54 KB
/
Copy path__main__.py
File metadata and controls
52 lines (38 loc) · 1.54 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
from cmd import Cmd
import objectbox
import datetime
from example.model import *
# objectbox expects date timestamp in milliseconds since UNIX epoch
def now_ms() -> int:
seconds: float = datetime.datetime.utcnow().timestamp()
return round(seconds * 1000)
def format_date(timestamp_ms: int) -> str:
return "" if timestamp_ms == 0 else str(datetime.datetime.fromtimestamp(timestamp_ms / 1000))
class TasklistCmd(Cmd):
prompt = "> "
_ob = objectbox.Builder().model(get_objectbox_model()).directory("tasklist-db").build()
_box = objectbox.Box(_ob, Task)
def do_ls(self, _):
"""list tasks"""
tasks = self._box.get_all()
print("%3s %-29s %-29s %s" % ("ID", "Created", "Finished", "Text"))
for task in tasks:
print("%3d %-29s %-29s %s" % (
task.id, format_date(task.date_created), format_date(task.date_finished), task.text))
def do_new(self, text: str):
"""create a new task with the given text (all arguments concatenated)"""
task = Task()
task.text = text
task.date_created = now_ms()
self._box.put(task)
def do_done(self, id: str):
"""mark task with the given ID as done"""
task = self._box.get(int(id))
task.date_finished = now_ms()
self._box.put(task)
def do_exit(self, _):
"""close the program"""
raise SystemExit()
if __name__ == '__main__':
app = TasklistCmd()
app.cmdloop('Welcome to the ObjectBox tasks-list app example. Type help or ? for a list of commands.')