-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimport_cstimer.py
84 lines (71 loc) · 2.75 KB
/
import_cstimer.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
# CubingB, copyright 2021 Zach Wegner
#
# This file is part of CubingB.
#
# CubingB is free software: you can redistribute it and/or modify it under the
# terms of the GNU Affero General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option) any
# later version.
#
# CubingB is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License
# along with CubingB. If not, see <https://www.gnu.org/licenses/>.
import argparse
import json
import datetime
import sys
import config
import db
# XXX add more scrambles
SCRAMBLE_TYPE = {
'222so': '2x2',
'sqrs': 'sq1',
None: '3x3',
}
def main():
parser = argparse.ArgumentParser()
parser.add_argument(action='store', dest='backup_file', help='filename of CSTimer backup')
parser.add_argument('-p', '--prop-file', action='store', help='separate CSTimer backup '
'file with session properties, in case a backup lost names')
args = parser.parse_args()
# Load backup file JSON and properties
with open(args.backup_file) as f:
data = json.load(f)
if args.prop_file:
with open(args.prop_file) as f:
properties = json.load(f)
session_data = json.loads(properties['properties']['sessionData'])
else:
session_data = json.loads(data['properties']['sessionData'])
db.init_db(config.DB_PATH)
# Parse solve records
with db.get_session() as session:
for [k, v] in data.items():
if not k.startswith('session'):
continue
s_id = k[7:]
name = k
scr_type = None
if s_id in session_data:
s_data = session_data[s_id]
name = s_data['name']
scr_type = s_data['opt'].get('scrType')
scr_type = SCRAMBLE_TYPE[scr_type]
sesh = session.insert(db.CubeSession, name=name, scramble_type=scr_type)
for solve in v:
[result, scramble, notes, ts] = solve
dnf = (result[0] == -1)
plus_2 = (result[0] == 2000)
time_ms = result[1]
seg_ms = result[-1:1:-1] or None
dt = datetime.datetime.fromtimestamp(ts)
session.insert(db.Solve, session=sesh, scramble=scramble,
time_ms=time_ms, segment_time_ms=seg_ms, dnf=dnf, plus_2=plus_2,
created_at=dt, notes=notes)
print('imported %s solves from %s' % (len(v), name))
if __name__ == '__main__':
main()