bdgtr-cli 0.1.0 → 0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +3 -3
- data/bin/bdgtr +26 -10
- data/data/bdgtr.sqlite +0 -0
- data/lib/bdgtr/cli/config.rb +188 -0
- data/lib/bdgtr/cli/config.sample.yml +15 -0
- data/lib/bdgtr/cli/gem_home.rb +0 -25
- data/lib/bdgtr/cli/help.rb +168 -0
- data/lib/bdgtr/cli/ledger_sync.rb +102 -0
- data/lib/bdgtr/cli/prompt.rb +47 -0
- data/lib/bdgtr/cli/psd2.rb +530 -0
- data/lib/bdgtr/cli/rows.rb +38 -0
- data/lib/bdgtr/cli/runtime.rb +88 -0
- data/lib/bdgtr/cli/table.rb +458 -0
- data/lib/bdgtr/cli.rb +126 -0
- data/lib/bdgtr/core/binder.rb +79 -0
- data/lib/bdgtr/core/exchange.rb +66 -0
- data/lib/bdgtr/core/models/account.rb +177 -0
- data/lib/bdgtr/core/models/account_requisition.rb +12 -0
- data/lib/bdgtr/core/models/account_sync.rb +12 -0
- data/lib/bdgtr/core/models/import.rb +12 -0
- data/lib/bdgtr/core/models/ledger.rb +10 -0
- data/lib/bdgtr/core/models/requisition.rb +27 -0
- data/lib/bdgtr/core/models/transaction.rb +98 -0
- data/lib/bdgtr/core/models/transaction_category.rb +12 -0
- data/lib/bdgtr/core/record.rb +11 -0
- data/lib/bdgtr/core/schema.rb +290 -0
- data/lib/bdgtr/core/session.rb +76 -0
- data/lib/bdgtr/core/syncable.rb +272 -0
- data/lib/bdgtr/core/transfer.rb +90 -0
- data/lib/bdgtr/core.rb +18 -0
- data/lib/bdgtr/portfolio/Rakefile +20 -0
- data/lib/bdgtr/portfolio/cli/LICENSE +21 -0
- data/lib/bdgtr/portfolio/cli/lib/portfolio_performance_api/cli/app.rb +77 -0
- data/lib/bdgtr/portfolio/cli/lib/portfolio_performance_api/cli/import.rb +30 -0
- data/lib/bdgtr/portfolio/cli/lib/portfolio_performance_api/cli/spreadsheet.rb +25 -0
- data/lib/bdgtr/portfolio/cli/lib/portfolio_performance_api/cli.rb +52 -0
- data/lib/bdgtr/portfolio/exe/bdgtr +7 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/accounts_csv.rb +67 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/app.rb +104 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/auth.rb +52 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/balances.rb +157 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/cache.rb +46 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/checkbox_menu.rb +90 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/checkbox_selection.rb +68 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/config.rb +138 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/decryptor.rb +70 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/drive_client.rb +182 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/errors.rb +10 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/file_reader.rb +80 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/fineco_import.rb +609 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/fineco_xls.rb +235 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/import/fineco.rb +243 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/import.rb +85 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/parser.rb +254 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/portfolio_source.rb +30 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/portfolio_store.rb +124 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/preview_window.rb +98 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/proto/client_pb.rb +29 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/row_preview.rb +174 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/sheets_client.rb +272 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/snapshot.rb +27 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/sync.rb +247 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/sync_preview.rb +63 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/transaction_identity.rb +49 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/transaction_sync.rb +948 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/transaction_types.rb +142 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/version.rb +5 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api.rb +42 -0
- data/lib/bdgtr/portfolio/proto/client.proto +114 -0
- data/lib/bdgtr/psd2/account.rb +24 -0
- data/lib/bdgtr/psd2/account_snapshot.rb +96 -0
- data/lib/bdgtr/psd2/callback.rb +49 -0
- data/lib/bdgtr/psd2/connect.rb +75 -0
- data/lib/bdgtr/psd2/enable_banking.rb +250 -0
- data/lib/bdgtr/psd2/go_cardless.rb +133 -0
- data/lib/bdgtr/psd2/institution.rb +17 -0
- data/lib/bdgtr/psd2/platform.rb +37 -0
- data/lib/bdgtr/psd2/requisition.rb +21 -0
- data/lib/bdgtr/psd2/transaction.rb +29 -0
- data/lib/bdgtr/psd2.rb +25 -0
- data/lib/bdgtr/sync/importer.rb +125 -0
- data/lib/bdgtr/sync/library.rb +18 -0
- data/lib/bdgtr/sync/portfolio.rb +14 -0
- data/lib/bdgtr/sync/projection.rb +38 -0
- data/lib/bdgtr/sync/spreadsheet.rb +23 -0
- data/lib/bdgtr/sync.rb +25 -0
- data/lib/bdgtr/version.rb +1 -1
- metadata +353 -30
|
@@ -0,0 +1,254 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "nokogiri"
|
|
4
|
+
|
|
5
|
+
module PortfolioPerformanceApi
|
|
6
|
+
class Parser
|
|
7
|
+
KIND_DEPOSIT = "deposit"
|
|
8
|
+
KIND_SECURITIES = "securities"
|
|
9
|
+
|
|
10
|
+
Account = Struct.new(
|
|
11
|
+
:uuid, :name, :currency, :retired, :note, :kind, :reference_account_uuid,
|
|
12
|
+
keyword_init: true
|
|
13
|
+
)
|
|
14
|
+
Security = Struct.new(:uuid, :name, :currency, :quote, keyword_init: true)
|
|
15
|
+
Transaction = Struct.new(
|
|
16
|
+
:type, :account_uuid, :other_account_uuid, :portfolio_uuid, :other_portfolio_uuid,
|
|
17
|
+
:amount_cents, :currency, :shares, :security_uuid, :units,
|
|
18
|
+
keyword_init: true
|
|
19
|
+
)
|
|
20
|
+
Unit = Struct.new(:type, :amount_cents, :currency, :fx_amount_cents, :fx_currency, keyword_init: true)
|
|
21
|
+
Client = Struct.new(
|
|
22
|
+
:base_currency, :version, :accounts, :portfolios, :securities, :transactions,
|
|
23
|
+
keyword_init: true
|
|
24
|
+
)
|
|
25
|
+
|
|
26
|
+
def self.parse(result)
|
|
27
|
+
case result.format
|
|
28
|
+
when :xml then parse_xml(result.bytes, version: result.version)
|
|
29
|
+
when :protobuf then parse_protobuf(result.bytes, version: result.version)
|
|
30
|
+
else
|
|
31
|
+
raise Error, "unsupported portfolio format #{result.format}"
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def self.parse_xml(bytes, version: nil)
|
|
36
|
+
doc = Nokogiri::XML(bytes) { |config| config.nonet.noblanks }
|
|
37
|
+
root = doc.at_xpath("/client") || doc.root
|
|
38
|
+
raise NotAPortfolioFile, "XML is not a Portfolio Performance client" unless root
|
|
39
|
+
|
|
40
|
+
accounts = root.xpath("./accounts/account").map { |node| xml_account(node) }
|
|
41
|
+
portfolios = root.xpath("./portfolios/portfolio").map { |node| xml_portfolio(node, accounts) }
|
|
42
|
+
securities = root.xpath("./securities/security").map { |node| xml_security(node) }
|
|
43
|
+
|
|
44
|
+
transactions = []
|
|
45
|
+
root.xpath("./accounts/account").each do |account_node|
|
|
46
|
+
uuid = text_at(account_node, "uuid")
|
|
47
|
+
account_node.xpath("./transactions/account-transaction").each do |tx_node|
|
|
48
|
+
next if tx_node["reference"]
|
|
49
|
+
|
|
50
|
+
transactions << xml_transaction(tx_node, account_uuid: uuid)
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
root.xpath("./portfolios/portfolio").each do |portfolio_node|
|
|
54
|
+
uuid = text_at(portfolio_node, "uuid")
|
|
55
|
+
portfolio_node.xpath("./transactions/portfolio-transaction").each do |tx_node|
|
|
56
|
+
next if tx_node["reference"]
|
|
57
|
+
|
|
58
|
+
transactions << xml_transaction(tx_node, portfolio_uuid: uuid)
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
Client.new(
|
|
63
|
+
base_currency: text_at(root, "baseCurrency") || "EUR",
|
|
64
|
+
version: version || integer_at(root, "version"),
|
|
65
|
+
accounts: accounts,
|
|
66
|
+
portfolios: portfolios,
|
|
67
|
+
securities: securities,
|
|
68
|
+
transactions: transactions
|
|
69
|
+
)
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def self.parse_protobuf(bytes, version: nil)
|
|
73
|
+
client = Proto::PClient.decode(bytes)
|
|
74
|
+
accounts = client.accounts.map do |account|
|
|
75
|
+
Account.new(
|
|
76
|
+
uuid: account.uuid,
|
|
77
|
+
name: account.name,
|
|
78
|
+
currency: account.currencyCode,
|
|
79
|
+
retired: account.isRetired,
|
|
80
|
+
note: account.has_note? ? account.note : nil,
|
|
81
|
+
kind: KIND_DEPOSIT
|
|
82
|
+
)
|
|
83
|
+
end
|
|
84
|
+
accounts_by_uuid = accounts.to_h { |account| [account.uuid, account] }
|
|
85
|
+
|
|
86
|
+
portfolios = client.portfolios.map do |portfolio|
|
|
87
|
+
reference = portfolio.has_referenceAccount? ? portfolio.referenceAccount : nil
|
|
88
|
+
Account.new(
|
|
89
|
+
uuid: portfolio.uuid,
|
|
90
|
+
name: portfolio.name,
|
|
91
|
+
currency: accounts_by_uuid[reference]&.currency,
|
|
92
|
+
retired: portfolio.isRetired,
|
|
93
|
+
note: portfolio.has_note? ? portfolio.note : nil,
|
|
94
|
+
kind: KIND_SECURITIES,
|
|
95
|
+
reference_account_uuid: reference
|
|
96
|
+
)
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
securities = client.securities.map do |security|
|
|
100
|
+
Security.new(
|
|
101
|
+
uuid: security.uuid,
|
|
102
|
+
name: security.name,
|
|
103
|
+
currency: security.has_currencyCode? ? security.currencyCode : nil,
|
|
104
|
+
quote: protobuf_quote(security)
|
|
105
|
+
)
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
transactions = client.transactions.map do |tx|
|
|
109
|
+
Transaction.new(
|
|
110
|
+
type: normalize_type(tx.type.to_s),
|
|
111
|
+
account_uuid: tx.has_account? ? tx.account : nil,
|
|
112
|
+
other_account_uuid: tx.has_otherAccount? ? tx.otherAccount : nil,
|
|
113
|
+
portfolio_uuid: tx.has_portfolio? ? tx.portfolio : nil,
|
|
114
|
+
other_portfolio_uuid: tx.has_otherPortfolio? ? tx.otherPortfolio : nil,
|
|
115
|
+
amount_cents: tx.amount,
|
|
116
|
+
currency: tx.currencyCode,
|
|
117
|
+
shares: tx.has_shares? ? tx.shares : 0,
|
|
118
|
+
security_uuid: tx.has_security? ? tx.security : nil,
|
|
119
|
+
units: tx.units.map do |unit|
|
|
120
|
+
Unit.new(
|
|
121
|
+
type: unit.type.to_s,
|
|
122
|
+
amount_cents: unit.amount,
|
|
123
|
+
currency: unit.currencyCode,
|
|
124
|
+
fx_amount_cents: unit.has_fxAmount? ? unit.fxAmount : nil,
|
|
125
|
+
fx_currency: unit.has_fxCurrencyCode? ? unit.fxCurrencyCode : nil
|
|
126
|
+
)
|
|
127
|
+
end
|
|
128
|
+
)
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
Client.new(
|
|
132
|
+
base_currency: client.baseCurrency.empty? ? "EUR" : client.baseCurrency,
|
|
133
|
+
version: version || client.version,
|
|
134
|
+
accounts: accounts,
|
|
135
|
+
portfolios: portfolios,
|
|
136
|
+
securities: securities,
|
|
137
|
+
transactions: transactions
|
|
138
|
+
)
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
def self.xml_account(node)
|
|
142
|
+
Account.new(
|
|
143
|
+
uuid: text_at(node, "uuid"),
|
|
144
|
+
name: text_at(node, "name"),
|
|
145
|
+
currency: text_at(node, "currencyCode") || "EUR",
|
|
146
|
+
retired: text_at(node, "isRetired") == "true",
|
|
147
|
+
note: text_at(node, "note"),
|
|
148
|
+
kind: KIND_DEPOSIT
|
|
149
|
+
)
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
def self.xml_portfolio(node, accounts)
|
|
153
|
+
reference = referenced_uuid(node.at_xpath("./referenceAccount"))
|
|
154
|
+
Account.new(
|
|
155
|
+
uuid: text_at(node, "uuid"),
|
|
156
|
+
name: text_at(node, "name"),
|
|
157
|
+
currency: accounts.find { |account| account.uuid == reference }&.currency,
|
|
158
|
+
retired: text_at(node, "isRetired") == "true",
|
|
159
|
+
note: text_at(node, "note"),
|
|
160
|
+
kind: KIND_SECURITIES,
|
|
161
|
+
reference_account_uuid: reference
|
|
162
|
+
)
|
|
163
|
+
end
|
|
164
|
+
|
|
165
|
+
def self.xml_security(node)
|
|
166
|
+
latest = node.at_xpath("./latest")
|
|
167
|
+
quote = if latest && latest["v"]
|
|
168
|
+
Integer(latest["v"])
|
|
169
|
+
else
|
|
170
|
+
price = node.xpath("./prices/price").max_by { |item| item["t"].to_s }
|
|
171
|
+
price && price["v"] ? Integer(price["v"]) : 0
|
|
172
|
+
end
|
|
173
|
+
|
|
174
|
+
Security.new(
|
|
175
|
+
uuid: text_at(node, "uuid"),
|
|
176
|
+
name: text_at(node, "name"),
|
|
177
|
+
currency: text_at(node, "currencyCode"),
|
|
178
|
+
quote: quote
|
|
179
|
+
)
|
|
180
|
+
end
|
|
181
|
+
|
|
182
|
+
def self.xml_transaction(node, account_uuid: nil, portfolio_uuid: nil)
|
|
183
|
+
type = normalize_type(text_at(node, "type"), portfolio: !portfolio_uuid.nil?)
|
|
184
|
+
Transaction.new(
|
|
185
|
+
type: type,
|
|
186
|
+
account_uuid: account_uuid,
|
|
187
|
+
other_account_uuid: referenced_uuid(node.at_xpath("./otherAccount")),
|
|
188
|
+
portfolio_uuid: portfolio_uuid,
|
|
189
|
+
other_portfolio_uuid: referenced_uuid(node.at_xpath("./otherPortfolio")),
|
|
190
|
+
amount_cents: integer_at(node, "amount"),
|
|
191
|
+
currency: text_at(node, "currencyCode"),
|
|
192
|
+
shares: integer_at(node, "shares"),
|
|
193
|
+
security_uuid: referenced_uuid(node.at_xpath("./security")),
|
|
194
|
+
units: []
|
|
195
|
+
)
|
|
196
|
+
end
|
|
197
|
+
|
|
198
|
+
def self.protobuf_quote(security)
|
|
199
|
+
return security.latest.close if security.has_latest?
|
|
200
|
+
|
|
201
|
+
prices = security.prices
|
|
202
|
+
return 0 if prices.empty?
|
|
203
|
+
|
|
204
|
+
prices.max_by(&:date).close
|
|
205
|
+
end
|
|
206
|
+
|
|
207
|
+
def self.referenced_uuid(node)
|
|
208
|
+
target = resolve_reference(node)
|
|
209
|
+
return if target.nil?
|
|
210
|
+
|
|
211
|
+
text_at(target, "uuid") || target["uuid"]
|
|
212
|
+
end
|
|
213
|
+
|
|
214
|
+
def self.resolve_reference(node)
|
|
215
|
+
return if node.nil?
|
|
216
|
+
|
|
217
|
+
ref = node["reference"]
|
|
218
|
+
return node unless ref
|
|
219
|
+
|
|
220
|
+
node.at_xpath(ref)
|
|
221
|
+
end
|
|
222
|
+
|
|
223
|
+
def self.normalize_type(type, portfolio: false)
|
|
224
|
+
case type.to_s.upcase
|
|
225
|
+
when "BUY" then "PURCHASE"
|
|
226
|
+
when "SELL" then "SALE"
|
|
227
|
+
when "DIVIDENDS" then "DIVIDEND"
|
|
228
|
+
when "TAXES" then "TAX"
|
|
229
|
+
when "FEES" then "FEE"
|
|
230
|
+
when "FEES_REFUND" then "FEE_REFUND"
|
|
231
|
+
when "DELIVERY_INBOUND" then "INBOUND_DELIVERY"
|
|
232
|
+
when "DELIVERY_OUTBOUND" then "OUTBOUND_DELIVERY"
|
|
233
|
+
when "TRANSFER_OUT" then (portfolio ? "SECURITY_TRANSFER" : "CASH_TRANSFER")
|
|
234
|
+
when "TRANSFER_IN" then "TRANSFER_IN"
|
|
235
|
+
else type.to_s.upcase
|
|
236
|
+
end
|
|
237
|
+
end
|
|
238
|
+
|
|
239
|
+
def self.text_at(node, name)
|
|
240
|
+
child = node.at_xpath("./#{name}")
|
|
241
|
+
return if child.nil?
|
|
242
|
+
|
|
243
|
+
value = child.text
|
|
244
|
+
value.empty? ? nil : value
|
|
245
|
+
end
|
|
246
|
+
|
|
247
|
+
def self.integer_at(node, name)
|
|
248
|
+
Integer(text_at(node, name) || 0)
|
|
249
|
+
end
|
|
250
|
+
private_class_method :xml_account, :xml_portfolio, :xml_security, :xml_transaction,
|
|
251
|
+
:protobuf_quote, :referenced_uuid, :resolve_reference,
|
|
252
|
+
:normalize_type, :text_at, :integer_at
|
|
253
|
+
end
|
|
254
|
+
end
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module PortfolioPerformanceApi
|
|
4
|
+
class PortfolioSource
|
|
5
|
+
def initialize(config: Config, drive_client: nil)
|
|
6
|
+
@config = config
|
|
7
|
+
@drive_client = drive_client
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def fetch
|
|
11
|
+
if @config.drive_configured?
|
|
12
|
+
(@drive_client || DriveClient.new).download
|
|
13
|
+
elsif @config.local_file_configured?
|
|
14
|
+
path = @config.portfolio_file
|
|
15
|
+
{
|
|
16
|
+
bytes: File.binread(path),
|
|
17
|
+
meta: {
|
|
18
|
+
source: "local",
|
|
19
|
+
path: path,
|
|
20
|
+
modified_at: File.mtime(path).utc.iso8601,
|
|
21
|
+
size: File.size(path)
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
else
|
|
25
|
+
raise NotConfigured,
|
|
26
|
+
"configure PORTFOLIO_GOOGLE_DRIVE_FILE_ID + service account, or PORTFOLIO_FILE"
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "stringio"
|
|
4
|
+
require "zip"
|
|
5
|
+
|
|
6
|
+
require_relative "errors"
|
|
7
|
+
require_relative "decryptor"
|
|
8
|
+
require_relative "file_reader"
|
|
9
|
+
require_relative "proto/client_pb"
|
|
10
|
+
|
|
11
|
+
module PortfolioPerformanceApi
|
|
12
|
+
class PortfolioStore
|
|
13
|
+
Loaded = Struct.new(
|
|
14
|
+
:path, :encrypted, :aes256, :password, :version, :content_type, :client, :zip_entries,
|
|
15
|
+
keyword_init: true
|
|
16
|
+
)
|
|
17
|
+
|
|
18
|
+
def self.load(path, password:)
|
|
19
|
+
load_bytes(File.binread(path), password: password, path: path)
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def self.load_bytes(raw, password:, path: nil)
|
|
23
|
+
raw = raw.b
|
|
24
|
+
encrypted = Decryptor.encrypted?(raw)
|
|
25
|
+
if encrypted
|
|
26
|
+
payload = Decryptor.decrypt(raw, password)
|
|
27
|
+
zip_bytes = payload.zip_bytes
|
|
28
|
+
version = payload.version
|
|
29
|
+
content_type = payload.content_type
|
|
30
|
+
aes256 = raw.getbyte(Decryptor::SIGNATURE.bytesize) == 1
|
|
31
|
+
elsif FileReader.zip?(raw)
|
|
32
|
+
zip_bytes = raw
|
|
33
|
+
version = 70
|
|
34
|
+
content_type = 2
|
|
35
|
+
aes256 = true
|
|
36
|
+
else
|
|
37
|
+
raise NotAPortfolioFile, "expected an encrypted or zipped .portfolio file"
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
if content_type == 1
|
|
41
|
+
raise NotAPortfolioFile, "this importer only writes protobuf portfolios"
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
entries = zip_entries(zip_bytes)
|
|
45
|
+
proto = entries["data.portfolio"] || entries.values.first
|
|
46
|
+
raise NotAPortfolioFile, "missing data.portfolio in zip" if proto.nil?
|
|
47
|
+
|
|
48
|
+
client = Proto::PClient.decode(FileReader.strip_proto_magic(proto))
|
|
49
|
+
Loaded.new(
|
|
50
|
+
path: path,
|
|
51
|
+
encrypted: encrypted,
|
|
52
|
+
aes256: aes256,
|
|
53
|
+
password: password,
|
|
54
|
+
version: version,
|
|
55
|
+
content_type: 2,
|
|
56
|
+
client: client,
|
|
57
|
+
zip_entries: entries
|
|
58
|
+
)
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def self.dump(loaded)
|
|
62
|
+
proto = FileReader::PROTO_MAGIC + loaded.client.to_proto
|
|
63
|
+
# Encrypted PP files store protobuf in zip entry "data" and, on open,
|
|
64
|
+
# read only the first entry. Unencrypted zips use "data.portfolio".
|
|
65
|
+
entry_name = loaded.encrypted ? "data" : "data.portfolio"
|
|
66
|
+
zip_bytes = write_zip({ entry_name => proto })
|
|
67
|
+
if loaded.encrypted
|
|
68
|
+
Decryptor.encrypt(
|
|
69
|
+
zip_bytes,
|
|
70
|
+
loaded.password,
|
|
71
|
+
aes256: loaded.aes256,
|
|
72
|
+
content_type: 2,
|
|
73
|
+
version: loaded.version
|
|
74
|
+
)
|
|
75
|
+
else
|
|
76
|
+
zip_bytes
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
def self.save(loaded)
|
|
81
|
+
raise ArgumentError, "portfolio path is missing" if loaded.path.to_s.empty?
|
|
82
|
+
|
|
83
|
+
File.binwrite(loaded.path, dump(loaded))
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
def self.backup_path(path, at: Time.now)
|
|
87
|
+
directory = File.dirname(path)
|
|
88
|
+
basename = File.basename(path, ".portfolio")
|
|
89
|
+
File.join(directory, "#{basename}-#{at.strftime("%Y%m%dT%H%M%S")}.portfolio")
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
def self.zip_entries(zip_bytes)
|
|
93
|
+
entries = {}
|
|
94
|
+
without_zip_date_warnings do
|
|
95
|
+
Zip::File.open_buffer(StringIO.new(zip_bytes)) do |zip|
|
|
96
|
+
zip.each do |entry|
|
|
97
|
+
next if entry.directory?
|
|
98
|
+
|
|
99
|
+
entries[File.basename(entry.name)] = entry.get_input_stream.read
|
|
100
|
+
end
|
|
101
|
+
end
|
|
102
|
+
end
|
|
103
|
+
entries
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
def self.write_zip(entries)
|
|
107
|
+
Zip::OutputStream.write_buffer do |zio|
|
|
108
|
+
entries.each do |name, data|
|
|
109
|
+
zio.put_next_entry(name)
|
|
110
|
+
zio.write(data)
|
|
111
|
+
end
|
|
112
|
+
end.string
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
def self.without_zip_date_warnings
|
|
116
|
+
previous = Zip.warn_invalid_date
|
|
117
|
+
Zip.warn_invalid_date = false
|
|
118
|
+
yield
|
|
119
|
+
ensure
|
|
120
|
+
Zip.warn_invalid_date = previous
|
|
121
|
+
end
|
|
122
|
+
private_class_method :zip_entries, :write_zip, :without_zip_date_warnings
|
|
123
|
+
end
|
|
124
|
+
end
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module PortfolioPerformanceApi
|
|
4
|
+
class PreviewWindow
|
|
5
|
+
attr_reader :offset, :page_size, :lists
|
|
6
|
+
|
|
7
|
+
def initialize(lists, page_size:, page_sizes: nil, locked: nil)
|
|
8
|
+
@lists = Array(lists).map { |list| Array(list) }
|
|
9
|
+
raise ArgumentError, "preview needs at least one list" if @lists.empty?
|
|
10
|
+
|
|
11
|
+
@page_size = [Integer(page_size), 1].max
|
|
12
|
+
@page_sizes = Array(page_sizes)
|
|
13
|
+
@locked = Array(locked)
|
|
14
|
+
@offset = 0
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def up
|
|
18
|
+
move(-1)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def down
|
|
22
|
+
move(1)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def page_up
|
|
26
|
+
move(-@page_size)
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def page_down
|
|
30
|
+
move(@page_size)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def visible(index = 0)
|
|
34
|
+
slice(index)
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def range(index = 0)
|
|
38
|
+
range_for(index)
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def page_size_at(index)
|
|
42
|
+
size = @page_sizes[Integer(index)]
|
|
43
|
+
return @page_size if size.nil?
|
|
44
|
+
|
|
45
|
+
[Integer(size), 1].max
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def max_offset
|
|
49
|
+
@lists.each_index.map { |index| list_max_offset(index) }.max
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
private
|
|
53
|
+
|
|
54
|
+
def locked?(index)
|
|
55
|
+
@locked[Integer(index)] == true
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def list_at(index)
|
|
59
|
+
@lists.fetch(Integer(index))
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def move(delta)
|
|
63
|
+
@offset = [[@offset + delta, 0].max, max_offset].min
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def slice(index)
|
|
67
|
+
lines = list_at(index)
|
|
68
|
+
return [] if lines.empty?
|
|
69
|
+
|
|
70
|
+
lines[start_index(index), page_size_at(index)]
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def start_index(index)
|
|
74
|
+
return 0 if locked?(index)
|
|
75
|
+
|
|
76
|
+
[@offset, last_start(index)].min
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
def last_start(index)
|
|
80
|
+
[0, list_at(index).size - page_size_at(index)].max
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def list_max_offset(index)
|
|
84
|
+
return 0 if locked?(index)
|
|
85
|
+
|
|
86
|
+
[0, list_at(index).size - page_size_at(index)].max
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
def range_for(index)
|
|
90
|
+
visible_rows = slice(index)
|
|
91
|
+
lines = list_at(index)
|
|
92
|
+
return [0, 0, lines.size] if visible_rows.empty?
|
|
93
|
+
|
|
94
|
+
from = start_index(index) + 1
|
|
95
|
+
[from, from + visible_rows.size - 1, lines.size]
|
|
96
|
+
end
|
|
97
|
+
end
|
|
98
|
+
end
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
# Generated by the protocol buffer compiler. DO NOT EDIT!
|
|
3
|
+
# source: client.proto
|
|
4
|
+
|
|
5
|
+
require 'google/protobuf'
|
|
6
|
+
|
|
7
|
+
require 'google/protobuf/timestamp_pb'
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
descriptor_data = "\n\x0c\x63lient.proto\x12\x16name.abuchen.portfolio\x1a\x1fgoogle/protobuf/timestamp.proto\"/\n\x10PHistoricalPrice\x12\x0c\n\x04\x64\x61te\x18\x01 \x01(\x03\x12\r\n\x05\x63lose\x18\x02 \x01(\x03\"3\n\x14PFullHistoricalPrice\x12\x0c\n\x04\x64\x61te\x18\x01 \x01(\x03\x12\r\n\x05\x63lose\x18\x02 \x01(\x03\"\xb6\x02\n\tPSecurity\x12\x0c\n\x04uuid\x18\x01 \x01(\t\x12\x0c\n\x04name\x18\x03 \x01(\t\x12\x19\n\x0c\x63urrencyCode\x18\x04 \x01(\tH\x00\x88\x01\x01\x12\x11\n\x04isin\x18\x07 \x01(\tH\x01\x88\x01\x01\x12\x19\n\x0ctickerSymbol\x18\x08 \x01(\tH\x02\x88\x01\x01\x12\x38\n\x06prices\x18\r \x03(\x0b\x32(.name.abuchen.portfolio.PHistoricalPrice\x12\x41\n\x06latest\x18\x10 \x01(\x0b\x32,.name.abuchen.portfolio.PFullHistoricalPriceH\x03\x88\x01\x01\x12\x11\n\tisRetired\x18\x14 \x01(\x08\x42\x0f\n\r_currencyCodeB\x07\n\x05_isinB\x0f\n\r_tickerSymbolB\t\n\x07_latest\"k\n\x08PAccount\x12\x0c\n\x04uuid\x18\x01 \x01(\t\x12\x0c\n\x04name\x18\x02 \x01(\t\x12\x14\n\x0c\x63urrencyCode\x18\x03 \x01(\t\x12\x11\n\x04note\x18\x04 \x01(\tH\x00\x88\x01\x01\x12\x11\n\tisRetired\x18\x05 \x01(\x08\x42\x07\n\x05_note\"\x8b\x01\n\nPPortfolio\x12\x0c\n\x04uuid\x18\x01 \x01(\t\x12\x0c\n\x04name\x18\x02 \x01(\t\x12\x11\n\x04note\x18\x03 \x01(\tH\x00\x88\x01\x01\x12\x11\n\tisRetired\x18\x04 \x01(\x08\x12\x1d\n\x10referenceAccount\x18\x05 \x01(\tH\x01\x88\x01\x01\x42\x07\n\x05_noteB\x13\n\x11_referenceAccount\";\n\x08PDecimal\x12\r\n\x05scale\x18\x01 \x01(\x05\x12\x11\n\tprecision\x18\x02 \x01(\x05\x12\r\n\x05value\x18\x03 \x01(\x0c\"\xc2\x02\n\x10PTransactionUnit\x12;\n\x04type\x18\x01 \x01(\x0e\x32-.name.abuchen.portfolio.PTransactionUnit.Type\x12\x0e\n\x06\x61mount\x18\x02 \x01(\x03\x12\x14\n\x0c\x63urrencyCode\x18\x03 \x01(\t\x12\x15\n\x08\x66xAmount\x18\x04 \x01(\x03H\x00\x88\x01\x01\x12\x1b\n\x0e\x66xCurrencyCode\x18\x05 \x01(\tH\x01\x88\x01\x01\x12;\n\x0c\x66xRateToBase\x18\x06 \x01(\x0b\x32 .name.abuchen.portfolio.PDecimalH\x02\x88\x01\x01\")\n\x04Type\x12\x0f\n\x0bGROSS_VALUE\x10\x00\x12\x07\n\x03TAX\x10\x01\x12\x07\n\x03\x46\x45\x45\x10\x02\x42\x0b\n\t_fxAmountB\x11\n\x0f_fxCurrencyCodeB\x0f\n\r_fxRateToBase\"\x80\x07\n\x0cPTransaction\x12\x0c\n\x04uuid\x18\x01 \x01(\t\x12\x37\n\x04type\x18\x02 \x01(\x0e\x32).name.abuchen.portfolio.PTransaction.Type\x12\x14\n\x07\x61\x63\x63ount\x18\x03 \x01(\tH\x00\x88\x01\x01\x12\x16\n\tportfolio\x18\x04 \x01(\tH\x01\x88\x01\x01\x12\x19\n\x0cotherAccount\x18\x05 \x01(\tH\x02\x88\x01\x01\x12\x1b\n\x0eotherPortfolio\x18\x06 \x01(\tH\x03\x88\x01\x01\x12\x16\n\totherUuid\x18\x07 \x01(\tH\x04\x88\x01\x01\x12\x32\n\x0eotherUpdatedAt\x18\x08 \x01(\x0b\x32\x1a.google.protobuf.Timestamp\x12(\n\x04\x64\x61te\x18\t \x01(\x0b\x32\x1a.google.protobuf.Timestamp\x12\x14\n\x0c\x63urrencyCode\x18\n \x01(\t\x12\x0e\n\x06\x61mount\x18\x0b \x01(\x03\x12\x13\n\x06shares\x18\x0c \x01(\x03H\x05\x88\x01\x01\x12\x11\n\x04note\x18\r \x01(\tH\x06\x88\x01\x01\x12\x15\n\x08security\x18\x0e \x01(\tH\x07\x88\x01\x01\x12\x37\n\x05units\x18\x0f \x03(\x0b\x32(.name.abuchen.portfolio.PTransactionUnit\x12-\n\tupdatedAt\x18\x10 \x01(\x0b\x32\x1a.google.protobuf.Timestamp\x12\x13\n\x06source\x18\x11 \x01(\tH\x08\x88\x01\x01\"\xf2\x01\n\x04Type\x12\x0c\n\x08PURCHASE\x10\x00\x12\x08\n\x04SALE\x10\x01\x12\x14\n\x10INBOUND_DELIVERY\x10\x02\x12\x15\n\x11OUTBOUND_DELIVERY\x10\x03\x12\x15\n\x11SECURITY_TRANSFER\x10\x04\x12\x11\n\rCASH_TRANSFER\x10\x05\x12\x0b\n\x07\x44\x45POSIT\x10\x06\x12\x0b\n\x07REMOVAL\x10\x07\x12\x0c\n\x08\x44IVIDEND\x10\x08\x12\x0c\n\x08INTEREST\x10\t\x12\x13\n\x0fINTEREST_CHARGE\x10\n\x12\x07\n\x03TAX\x10\x0b\x12\x0e\n\nTAX_REFUND\x10\x0c\x12\x07\n\x03\x46\x45\x45\x10\r\x12\x0e\n\nFEE_REFUND\x10\x0e\x42\n\n\x08_accountB\x0c\n\n_portfolioB\x0f\n\r_otherAccountB\x11\n\x0f_otherPortfolioB\x0c\n\n_otherUuidB\t\n\x07_sharesB\x07\n\x05_noteB\x0b\n\t_securityB\t\n\x07_source\"\x8f\x02\n\x07PClient\x12\x0f\n\x07version\x18\x01 \x01(\x05\x12\x35\n\nsecurities\x18\x02 \x03(\x0b\x32!.name.abuchen.portfolio.PSecurity\x12\x32\n\x08\x61\x63\x63ounts\x18\x03 \x03(\x0b\x32 .name.abuchen.portfolio.PAccount\x12\x36\n\nportfolios\x18\x04 \x03(\x0b\x32\".name.abuchen.portfolio.PPortfolio\x12:\n\x0ctransactions\x18\x05 \x03(\x0b\x32$.name.abuchen.portfolio.PTransaction\x12\x14\n\x0c\x62\x61seCurrency\x18\x0c \x01(\tB \xea\x02\x1dPortfolioPerformanceApi.Protob\x06proto3"
|
|
11
|
+
|
|
12
|
+
pool = ::Google::Protobuf::DescriptorPool.generated_pool
|
|
13
|
+
pool.add_serialized_file(descriptor_data)
|
|
14
|
+
|
|
15
|
+
module PortfolioPerformanceApi
|
|
16
|
+
module Proto
|
|
17
|
+
PHistoricalPrice = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("name.abuchen.portfolio.PHistoricalPrice").msgclass
|
|
18
|
+
PFullHistoricalPrice = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("name.abuchen.portfolio.PFullHistoricalPrice").msgclass
|
|
19
|
+
PSecurity = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("name.abuchen.portfolio.PSecurity").msgclass
|
|
20
|
+
PAccount = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("name.abuchen.portfolio.PAccount").msgclass
|
|
21
|
+
PPortfolio = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("name.abuchen.portfolio.PPortfolio").msgclass
|
|
22
|
+
PDecimal = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("name.abuchen.portfolio.PDecimal").msgclass
|
|
23
|
+
PTransactionUnit = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("name.abuchen.portfolio.PTransactionUnit").msgclass
|
|
24
|
+
PTransactionUnit::Type = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("name.abuchen.portfolio.PTransactionUnit.Type").enummodule
|
|
25
|
+
PTransaction = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("name.abuchen.portfolio.PTransaction").msgclass
|
|
26
|
+
PTransaction::Type = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("name.abuchen.portfolio.PTransaction.Type").enummodule
|
|
27
|
+
PClient = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("name.abuchen.portfolio.PClient").msgclass
|
|
28
|
+
end
|
|
29
|
+
end
|