-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmodelutils.py
111 lines (95 loc) · 3.46 KB
/
modelutils.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
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
"""Utility functions for the model."""
import re
import logging
import datetime
from typing import Optional
import orm
import constants as const
def logline_to_dict(logline: str) -> dict:
"""Convert a logline into a sanitized dict"""
data = {}
pairs = re.split('(?<!:):(?!:)', logline.strip().strip('\0'))
for p in pairs:
p = p.replace('::',':')
keyval = p.split('=')
try:
data[keyval[0]] = keyval[1]
except IndexError as e:
logging.error('error "{}" in keyval "{}", logline "{}"'.format(e,keyval,logline))
data["v"] = re.match(r"(0.\d+)", data["v"]).group()
if "god" not in data:
data["god"] = "GOD_NO_GOD"
if "status" not in data:
data["status"] = ""
data["god"] = const.GOD_NAME_FIXUPS.get(data["god"],data["god"])
if "end" in data:
data["time"] = data["end"]
data["ktyp"] = const.KTYP_FIXUPS.get(data["ktyp"], data["ktyp"])
data["type"] = "death.final"
data["milestone"] = data["tmsg"]
data["runes"] = data.get("urune", 0)
# D:0 is D:$ in logfile so we came from D:1 in that case
data["oplace"] = data.get("oplace",
data["place"].translate(str.maketrans("$", "1")))
return data
def crawl_date_to_datetime(d: str) -> datetime.datetime:
"""Converts a crawl date string to a datetime object.
Note: crawl dates use a 0-indexed month... I think you can blame struct_tm
for this.
"""
# Increment the month by one
d = d[:4] + "%02d" % (int(d[4:6]) + 1) + d[6:]
return datetime.datetime(
year=int(d[:4]),
month=int(d[4:6]),
day=int(d[6:8]),
hour=int(d[8:10]),
minute=int(d[10:12]),
second=int(d[12:14]),
)
def _morgue_prefix(src: str, version: str) -> Optional[str]:
src = src.lower()
if src == "cao":
prefix = "http://crawl.akrasiac.org/rawdata"
elif src == "cdo":
prefix = "http://crawl.develz.org/morgues"
prefix += "/" + version_url(version)
elif src == "cue" or src == "clan":
prefix = "http://underhound.eu/crawl/morgue"
elif src == "cbro":
prefix = "http://crawl.berotato.org/crawl/morgue"
elif src == "cxc":
prefix = "http://crawl.xtahua.com/crawl/morgue"
elif src == "lld":
prefix = "http://lazy-life.ddo.jp:8080/morgue"
prefix += "-" + version_url(version)
elif src == "cpo":
prefix = "https://crawl.project357.org/morgue"
elif src == "cko":
prefix = "http://crawl.kelbi.org/crawl/morgue"
elif src == "cwz":
prefix = "https://webzook.net/soup/morgue/"
prefix += "/" + version_url(version)
elif src in ("ckr", "csn", "rhf", "cjr", "cszo"):
return None
else:
raise ValueError("No prefix for %s" % src)
return prefix
def morgue_url(game: orm.Game) -> Optional[str]:
"""Generates a morgue URL from a game."""
src = game.account.server.name
prefix = _morgue_prefix(src, game.version.v)
if not prefix or game.alive:
return None
name = game.account.name
timestamp = game.end.strftime("%Y%m%d-%H%M%S")
return "%s/%s/morgue-%s-%s.txt" % (prefix, name, name, timestamp)
def version_url(version: str) -> str:
"""Cleans up version strings for use in morgue URLs."""
if version[-2:] == "a0":
return "trunk"
if len(version) > 4:
for i in range(len(version)):
if version[-(i + 1)] == ".":
return version[: -(i + 1)]
return version