-
Notifications
You must be signed in to change notification settings - Fork 27
/
generate_constants.py
executable file
·253 lines (213 loc) · 9.87 KB
/
generate_constants.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
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#!/usr/bin/env python
enums = {
# OFX spec version
"OfxVersion": (["102", "103", "151", "160", "200", "201", "202", "203", "210", "211", "220"], "the OFX specification version in use"),
# Bank/general
"AcctType": (["Checking", "Savings", "MoneyMrkt", "CreditLine", "CD"], "types of bank accounts"),
"TrnType": (["Credit", "Debit", "Int", "Div", "Fee", "SrvChg", "Dep", "ATM", "POS", "Xfer", "Check", "Payment", "Cash", "DirectDep", "DirectDebit", "RepeatPmt", "Hold", "Other"], "types of transactions. INT, ATM, and POS depend on the signage of the account."),
"ImageType": (["Statement", "Transaction", "Tax"], "what this image contains"),
"ImageRefType": (["Opaque", "URL", "FormURL"], "the type of reference to the image"),
"CheckSup": (["FrontOnly", "BackOnly", "FrontAndBack"], "what portions of the check this image contains"),
"CorrectAction": (["Delete", "Replace"], "whether this transaction correction replaces or deletes the transaction matching its CORRECTFITID"),
"BalType": (["Dollar", "Percent", "Number"], "how this BAL's VALUE field should be interpreted"),
# InvStmt
"Inv401kSource": (["PreTax", "AfterTax", "Match", "ProfitSharing", "Rollover", "OtherVest", "OtherNonVest"], "the source of money used for this security in a 401(k) account. Default if not present is OTHERNONVEST. The following cash source types are subject to vesting: MATCH, PROFITSHARING, and OTHERVEST."),
"SubAcctType": (["Cash", "Margin", "Short", "Other"], "the sub-account type for a source and/or destination of a transaction. Used in fields named SubAcctFrom, SubAcctTo, SubAcctSec, SubAcctFund, HeldInAcct."),
"BuyType": (["Buy", "BuyToCover"], "types of purchases"),
"OptAction": (["Exercise", "Assign", "Expire"], "types of actions for options"),
"TferAction": (["In", "Out"], "whether the transfer is into or out of this account"),
"PosType": (["Long", "Short"], "position type"),
"Secured": (["Naked", "Covered"], "how an option is secured"),
"Duration": (["Day", "GoodTilCancel", "Immediate"], "how long the investment order is good for"),
"Restriction": (["AllOrNone", "MinUnits", "None"], "a special restriction on an investment order"),
"UnitType": (["Shares", "Currency"], "type of the UNITS value"),
"OptBuyType": (["BuyToOpen", "BuyToClose"], "types of purchases for options"),
"SellType": (["Sell", "SellShort"], "types of sales"),
"LoanPmtFreq": (["Weekly", "Biweekly", "TwiceMonthly", "Monthly", "FourWeeks", "BiMonthly", "Quarterly", "Semiannually", "Annually", "Other"], "the frequency of loan payments"),
"IncomeType": (["CGLong", "CGShort", "Div", "Interest", "Misc"], "types of investment income"),
"SellReason": (["Call", "Sell", "Maturity"], "the reason the sell of a debt security was generated: CALL (the debt was called), SELL (the debt was sold), MATURITY (the debt reached maturity)"),
"OptSellType": (["SellToClose", "SellToOpen"], "types of sales for options"),
"RelType": (["Spread", "Straddle", "None", "Other"], "related option transaction types"),
# Prof
"CharType": (["AlphaOnly", "NumericOnly", "AlphaOrNumeric", "AlphaAndNumeric"], "types of characters allowed in password"),
"SyncMode": (["Full", "Lite"], "data synchronization mode supported (see OFX spec for more details)"),
"OfxSec": (["None", "Type 1"], "the type of application-level security required for the message set"),
# SecList
"DebtType": (["Coupon", "Zero"], "debt type"),
"DebtClass": (["Treasury", "Municipal", "Corporate", "Other"], "the class of debt"),
"CouponFreq": (["Monthly", "Quarterly", "Semiannual", "Annual", "Other"], "when debt coupons mature"),
"CallType": (["Call", "Put", "Prefund", "Maturity"], "type of next call (for a debt)"),
"AssetClass": (["DomesticBond", "IntlBond", "LargeStock", "SmallStock", "IntlStock", "MoneyMrkt", "Other"], "type of asset classes"),
"MfType": (["OpenEnd", "CloseEnd", "Other"], "types of mutual funds"),
"OptType": (["Put", "Call"], "whether the option is a PUT or a CALL"),
"StockType": (["Common", "Preferred", "Convertible", "Other"], "types of stock"),
# Signup
"HolderType": (["Individual", "Joint", "Custodial", "Trust", "Other"], "how the account is held"),
"AcctClassification": (["Personal", "Business", "Corporate", "Other"], "the type of an account"),
"SvcStatus": (["Avail", "Pend", "Active"], "the status of the account: AVAIL = Available, but not yet requested, PEND = Requested, but not yet available, ACTIVE = In use"),
"UsProductType": (["401K", "403B", "IRA", "KEOGH", "Other", "SARSEP", "Simple", "Normal", "TDA", "Trust", "UGMA"], "type of investment account (in the US)"),
}
header = """package ofxgo
/*
* Do not edit this file by hand. It is auto-generated by calling `go generate`.
* To make changes, edit generate_constants.py, re-run `go generate`, and check
* in the result.
*/
import (
"errors"
"fmt"
"strings"
"github.com/aclindsa/xml"
)
"""
template = """
type {enumLower} uint
// {enum}* constants represent {comment}
const (
{constNames})
var {enumLower}s = [...]string{{"{upperValueString}"}}
func (e {enumLower}) Valid() bool {{
// This check is mostly out of paranoia, ensuring e != 0 should be
// sufficient
return e >= {firstValue} && e <= {lastValue}
}}
func (e {enumLower}) String() string {{
if e.Valid() {{
return {enumLower}s[e-1]
}}
return fmt.Sprintf("invalid {enumLower} (%d)", e)
}}
func (e *{enumLower}) FromString(in string) error {{
value := strings.TrimSpace(in)
for i, s := range {enumLower}s {{
if s == value {{
*e = {enumLower}(i + 1)
return nil
}}
}}
*e = 0
return errors.New("Invalid {enum}: \\\"" + in + "\\\"")
}}
func (e *{enumLower}) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {{
var value string
err := d.DecodeElement(&value, &start)
if err != nil {{
return err
}}
return e.FromString(value)
}}
func (e {enumLower}) MarshalXML(enc *xml.Encoder, start xml.StartElement) error {{
if !e.Valid() {{
return nil
}}
enc.EncodeElement({enumLower}s[e-1], start)
return nil
}}
// New{enum} returns returns an 'enum' value of type {enumLower} given its
// string representation
func New{enum}(s string) ({enumLower}, error) {{
var e {enumLower}
err := e.FromString(s)
if err != nil {{
return 0, err
}}
return e, nil
}}
"""
with open("constants.go", 'w') as f:
f.write(header)
for enum in enums:
enumLower = enum[:1].lower() + enum[1:].replace(" ", "")
firstValue = enum+enums[enum][0][0].replace(" ", "")
lastValue = enum+enums[enum][0][-1].replace(" ", "")
comment = enums[enum][1]
constNames = "\t{firstValue} {enumLower} = 1 + iota\n".format(
enum=enum,
firstValue=firstValue,
enumLower=enumLower)
for value in enums[enum][0][1:]:
constNames += "\t{enum}{value}\n".format(
enum=enum,
value=value.replace(" ", ""))
upperValueString = "\", \"".join([s.upper() for s in enums[enum][0]])
f.write(template.format(enum=enum,
enumLower=enumLower,
comment=comment,
firstValue=firstValue,
lastValue=lastValue,
constNames=constNames,
upperValueString=upperValueString))
test_header = """package ofxgo
/*
* Do not edit this file by hand. It is auto-generated by calling `go generate`.
* To make changes, edit generate_constants.py, re-run `go generate`, and check
* in the result.
*/
import (
"strings"
"testing"
"github.com/aclindsa/xml"
)
"""
test_template = """
func Test{enum}(t *testing.T) {{
e, err := New{enum}("{firstValueUpper}")
if err != nil {{
t.Fatalf("Unexpected error creating new {enum} from string \\\"{firstValueUpper}\\\"\\n")
}}
if !e.Valid() {{
t.Fatalf("{enum} unexpectedly invalid\\n")
}}
err = e.FromString("{lastValueUpper}")
if err != nil {{
t.Fatalf("Unexpected error on {enum}.FromString(\\\"{lastValueUpper}\\\")\\n")
}}
if e.String() != "{lastValueUpper}" {{
t.Fatalf("{enum}.String() expected to be \\\"{lastValueUpper}\\\"\\n")
}}
marshalHelper(t, "{lastValueUpper}", &e)
overwritten, err := New{enum}("THISWILLNEVERBEAVALIDENUMSTRING")
if err == nil {{
t.Fatalf("Expected error creating new {enum} from string \\\"THISWILLNEVERBEAVALIDENUMSTRING\\\"\\n")
}}
if overwritten.Valid() {{
t.Fatalf("{enum} created with string \\\"THISWILLNEVERBEAVALIDENUMSTRING\\\" should not be valid\\n")
}}
if !strings.Contains(strings.ToLower(overwritten.String()), "invalid") {{
t.Fatalf("{enum} created with string \\\"THISWILLNEVERBEAVALIDENUMSTRING\\\" should not return valid string from String()\\n")
}}
b, err := xml.Marshal(&overwritten)
if err != nil {{
t.Fatalf("Unexpected error on xml.Marshal({enum}): %s\\n", err)
}}
if string(b) != "" {{
t.Fatalf("Expected empty string, got '%s'\\n", string(b))
}}
unmarshalHelper(t, "{lastValueUpper}", &e, &overwritten)
err = xml.Unmarshal([]byte("<GARBAGE><!LALDK>"), &overwritten)
if err == nil {{
t.Fatalf("Expected error unmarshalling garbage value\\n")
}}
type SC struct {{
E {enumLower}
}}
sc := SC{{E: e}}
b, err = xml.Marshal(sc)
if err != nil {{
t.Fatalf("Unexpected error on xml.Marshal(struct {enum}): %s\\n", err)
}}
if string(b) != "<SC><E>{lastValueUpper}</E></SC>" {{
t.Fatalf("Expected '%s', got '%s'\\n", "<SC><E>{lastValueUpper}</E></SC>", string(b))
}}
}}
"""
with open("constants_test.go", 'w') as f:
f.write(test_header)
for enum in enums:
enumLower = enum[:1].lower() + enum[1:].replace(" ", "")
firstValueUpper = enums[enum][0][0].upper()
lastValueUpper = enums[enum][0][-1].upper()
f.write(test_template.format(enum=enum,
enumLower=enumLower,
firstValueUpper=firstValueUpper,
lastValueUpper=lastValueUpper))