-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpadutil.py
139 lines (116 loc) · 3.72 KB
/
padutil.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# padutil.py
from util import *
from datetime import datetime as DT
import csv
def ghtime(s):
#< 151228000000
#> 2015-12-28 00:00:00
#!TODO: Make sensitive to DST history?
if isinstance(s, int):
s = str(s)
return DT.strptime(s, '%y%m%d%H%M%S')
gh_time = ghtime
# Until I figure out which name I want.
def dispID(ID):
"""Permutes internal PAD ID to the displayed form.
"""
ID = str(ID).zfill(9)
return ''.join(ID[x-1] for x in [1,5,9,6,3,8,2,4,7])
# def gh_csv(raw):
# # doesn't correctly deal with newlines in a field
# typ, rest = line.split(';', 1)
# args = [typ]
# try:
# while rest:
# if rest[0] == "'":
# quot = "'"
# var, rest = rest[1:].split("',", 1)
# elif rest[0] == '"':
# quot = '"'
# var, rest = rest[1:].split('",', 1)
# else:
# quot = ''
# var, rest = rest.split(",", 1)
# args.append(var)
# except ValueError:
# if rest[0] in '\'"':
# assert rest[-1] == rest[0]
# rest = rest[1:-1]
# args.append(rest)
# return args
def gh_csv(raw):
"""
Gungho's terrible CSV with unescaped quotes.
? Wait maybe it's valid csv and I just screwed up before??
- Maybe it was enemy_skill_data that ruined things.
- Need to handle newlins within quotes.
CSV is used in:
- enemy_skill_data
- No first semicolon
- Apos within strings.
- No empty fields.
- dungeon_data
- get_dung_sale (without first semi-colon)
- shop_item (without first semi-colon)
- dl_al (without first semi-colon)
"""
if isinstance(raw, str):
lines = raw.splitlines(True)
else:
lines = raw
# # ugh
# # Hopefully they never put a ' at the end of a line.
# lines = (line
# #startquote
# .replace(",'", ",\0")
# .replace("\n'", "\n\0")
# #endquote
# .replace("',", "\0,")
# .replace("'\n", "\0\n")
# #leftover
# .replace("'", "''")
# #now change it back
# .replace("\0", "'")
# for line in lines)
lines = (line.replace(';', ',', 1)
if line[1] == ';' else line
for line in lines if line)
csvraw = csv.reader(lines,
delimiter=',',
quotechar="'",
doublequote=True,
strict=True,
)
result = list(csvraw)
# assert result[-1][0] == 'c' #crc
return result
# def gh_csv(raw):
# """
# Gungho's terrible CSV with unescaped quotes.
# """
# # Escaping quotes on the original raw.
# lines = (line.replace(';', ',', 1)
# if line[1] == ';' else line
# for line in raw
# #startquote
# .replace(",'", ",\0")
# .replace("\n'", "\n\0")
# #endquote
# .replace("',", "\0,")
# .replace("'\n", "\0\n")
# #leftover
# .replace("'", "''")
# #now change it back
# .replace("\0", "'")
# #and split
# .splitlines(True))
# csvraw = csv.reader(lines,
# delimiter=',',
# quotechar="'",
# doublequote=True,
# strict=True)
# bag = Bag(csvraw)
# assert bag[-1][0] == 'c' #crc
# # return bag[:-1]
# return bag[:-1]
# #