[3662b5]: infinity / formats / d2a.py Maximize Restore History

Download this file

d2a.py    197 lines (157 with data), 5.8 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
 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# -*-python-*-
# ie_shell.py - Simple shell for Infinity Engine-based game files
# Copyright (C) 2004-2009 by Jaroslav Benkovsky, <edheldil@users.sf.net>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
import re
import sys
from infinity.format import Format, register_format
num_re = re.compile ('^[0-9]*$')
class D2A_Format (Format):
def __init__ (self):
Format.__init__ (self)
#self.expect_signature = '2DA'
self.rows = []
self.cols = []
self.rows_hash = {}
self.cols_hash = {}
self.cells = []
def read (self, stream):
s = ""
line_no = 0
self.signature = stream.get_line ().strip ()
# FIXME: check the header
self.default_value = stream.get_line ().strip ()
s = stream.get_line ()
s = s.strip ()
# FIXME: canonize the column names
self.cols = s.split (None)
self.cols_hash = dict ([ (i, n.upper ()) for i, n in enumerate (self.cols) ])
line_no = 3
while s is not None:
s = stream.get_line ()
if s is None:
break
line_no = line_no + 1
s = s.strip ()
if s == '':
continue
values = s.split (None)
key = values.pop (0)
self.rows.append (key)
self.rows_hash[key.upper ()] = len (self.rows)
if len (values) < len (self.cols):
values.extend ([''] * (len (self.cols) - len (values)))
self.cells.append (values)
def get (self, row, col, fill_blanks=True):
row = self.get_row_id (row)
col = self.get_col_id (col)
if row >= 0 and col >= 0:
if self.cells[row][col] != '' or not fill_blanks:
return self.cells[row][col]
else:
return self.default_value
elif row != -1:
return self.rows[row]
elif col != -1:
return self.cols[col]
else:
return ''
# def get_row_name (self, row):
# # FIXME: canonize
# if row == -1 or row == '':
# return ''
# elif type (row) == type (''):
# if self.rows.index (row) >= 0:
# return row
# else:
# return self.rows[row]
#
#
# def get_col_name (self, col):
# # FIXME: canonize
# if col == -1 or col == '':
# return ''
# elif type (col) == type (''):
# if self.cols.index (col) >= 0:
# return col
# else:
# return self.cols[col]
def get_row_id (self, name):
if name == '':
return -1
if type (name) == type (''):
return self.rows_hash[name.upper ()]
else:
return name
def get_col_id (self, name):
if name == '':
return -1
if type (name) == type (''):
return self.cols_hash[upper (name)]
else:
return name
def get_row (self, row, include_heading = False):
start = (0, -1)[include_heading == True]
return [ self.get (row, i, False) for i in range (start, len (self.cells[self.get_row_id (row)])) ]
# res = []
# row = self.get_row_id (row)
#
# if include_heading:
# res.append (self.get (row, ''))
#
# for col in self.cols:
# res.append (self.get (row, col))
#
# return res
def get_col (self, col, include_heading = False):
start = (0, -1)[include_heading == True]
return [ self.get (i, col, False) for i in range (start, len (self.cells)) ]
# res = []
# col = self.get_col_id (col)
#
# if include_heading:
# res.append (self.get ('', col))
#
# for row in self.rows:
# res.append (self.get (row, col))
#
# return res
def trim (self):
# TODO: replaces values equal to default with implicit default
pass
def get_col_width (self, col):
cells = self.get_col (col, True)
return reduce (lambda m, n: max (m, len (n)), cells, 0)
def get_col_justification (self, col):
cells = self.get_col (col, False)
return reduce (lambda m, n: max (m, num_re.match (n) is not None), cells, False)
# FIXME: options for same column width, minimal width, using tabs, left alignment, ...
def printme (self):
print 'Signature:', self.signature
print 'Default value:', self.default_value
sizes = map (self.get_col_width, range (-1, len (self.cols)))
rjusts = map (self.get_col_justification, range (-1, len (self.cols)))
#print self.cols
for row in range (-1, len (self.rows)):
values = self.get_row (row, True)
for val, size, rjust in zip (values, sizes, rjusts):
if rjust:
print(val.rjust (size), end=' ')
else:
print(val.ljust (size), end=' ')
#print "%*s" %(size, val),
print()
register_format ('2DA', '', D2A_Format)