-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmoh.rb
329 lines (279 loc) · 7.1 KB
/
moh.rb
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
# coding: utf-8
require 'date'
require 'optparse'
class Transaction
include Comparable
@@pool = []
def Transaction.each
@@pool.each { |t| yield t }
end
def Transaction.clear
@@pool = []
end
private
#amount is plus when the money moves from [source] to [target]
attr_writer :date, :source, :target, :sort, :amount, :comment
public
attr_reader :date, :source, :target, :sort, :amount, :comment
# amount > 0
def initialize(date, source, target, amount, comment)
self.date = date
self.comment = comment
if amount > 0 then
self.source = source
self.target = target
self.amount = amount
elsif amount < 0 then
self.source = target
self.target = source
self.amount = -amount
end
if not self.source then p amount end
@@pool << self
end
def <=>(other)
(self.date <=> other.date) != 0 ? self.date <=> other.date :
(self.target <=> other.target) != 0 ? self.target <=> other.target :
(self.amount <=> other.amount) != 0 ? self.amount <=> other.amount :
self.comment <=> other.comment
end
def is_in_dates(date1, date2)
(not date1 or date >= date1) && (not date2 or date <= date2)
end
end
def initial_of(path1, path2)
if path1 == [] then
return true
elsif path2 == [] then
return false
elsif path1[0] == path2[0] then
return initial_of(path1[1..-1], path2[1..-1])
else
return false
end
end
class Book
include Comparable
public
attr_reader :parent, :children
attr_writer :name, :children, :parent
public
attr_reader :name
def add_child(book)
if self.children then
self.children[book.name] = book
else
self.children = {book.name => book}
end
end
def get_child(name)
self.children ? self.children[name] : nil
end
def get_children
if self.children then self.children.map {|name, b| b} else [] end
end
def initialize(name, book = nil)
self.name = name
if book then book.add_child(self) end
self.parent = book
end
def full_path
if parent then
parent.full_path << self.name
else
self.name.length == 0 ? [] : [self.name]
end
end
def fqdn
full_path.inject{|s, n| s + ':' + n}
end
def get_grandchild(path, create = false)
if path.length > 0 then
path = path.dup
name = path.shift
get_child(name) ? get_child(name).get_grandchild(path, create) :
(create ? Book.new(name, self).get_grandchild(path, create) : nil)
else
self
end
end
def <=>(other)
full_path <=> other.full_path
end
def is_contained(book)
initial_of(book.full_path, self.full_path)
end
def debit_sum
sum = 0
Transaction.each do |t|
if yield(t) & t.source.is_contained(self) then
sum += t.amount
end
end
return sum
end
def credit_sum
sum = 0
Transaction.each do |t|
if yield(t) & t.target.is_contained(self) then
sum += t.amount
end
end
return sum
end
def balance(date=nil)
debit_sum = debit_sum{|t| t.is_in_dates(nil, date)}
credit_sum = credit_sum {|t| t.is_in_dates(nil, date)}
credit_sum - debit_sum
end
end
#IO
##Input
LINE_START = /^\[(\d{4}-\d{2}-\d{2})\]\$\s+/
DAYONE_LINE = /^(.*<string>)?(.*)(<\/string>.*)?$/
class BookReader
protected
attr_writer :root_book
public
attr_reader :root_book
def initialize()
self.root_book = Book.new('')
end
def add_line(date_string, path1, path2, amount)
begin
date = Date.parse(date_string)
book1 = self.root_book.get_grandchild(path1, true)
book2 = self.root_book.get_grandchild(path2, true)
if not amount == 0 then
t = Transaction.new(date, book1, book2, amount, "")
end
true
rescue ArgumentError
false
end
end
def parse_line(line)
md = LINE_START.match(line)
if md then
entries = line.chomp.split(/\s+/)
if entries.length >= 4 then
add_line(md[1],
entries[1].split(':'),
entries[2].split(':'),
entries[-1].to_i)
end
end
end
def parse_dayone_line(line)
md = DAYONE_LINE.match(line)
if md then
parse_line(md[2])
end
end
end
##Output
class BookWriter
protected
attr_writer :book
def print_transaction(t)
puts "[#{t.date.to_s}]$\t#{t.source.fqdn}\t#{t.target.fqdn}\t#{t.comment}\t#{t.amount.to_s}"
end
public
attr_reader :book
def initialize(book)
self.book = book
end
def print_summary(date1, date2, depth=-1)
debit = 0
Transaction.each do |t|
if
t.is_in_dates(date1, date2) \
and t.source.is_contained(book)
then
debit += t.amount
end
end
credit = 0
Transaction.each do |t|
if
t.is_in_dates(date1, date2) \
and t.target.is_contained(book)
then
credit += t.amount
end
end
if credit != 0 or debit != 0 then
puts "#{book.fqdn}\t#{credit}\t#{debit}\t#{credit - debit}"
end
if depth != 0 then
book.get_children.sort.each{|b| BookWriter.new(b).print_summary(date1, date2, depth-1)}
end
end
def print_transactions(date1=nil, date2=nil)
transactions = []
Transaction.each do |t|
if
t.is_in_dates(date1, date2) \
and (t.source.is_contained(book) or t.target.is_contained(book))
then
transactions << t
end
end
transactions.sort.each{|t| print_transaction(t)}
end
end
## Main
if $0 == __FILE__ then
puts 'moh -- simple, commandline-based accounting software'
puts '(C) 2012, 2013, 2014, 2015: Yoriyuki Yamagata'
puts 'See LICENSE.txt for the licence.'
opt = OptionParser.new
book_reader = BookReader.new
txt_dir = nil
txt_suffix = 'txt'
print_summary = false
summary_depth = -1
print_transactions = false
Version = "0.3.0"
def dir_scanner(path, suffix)
Dir.glob("#{File.expand_path(path)}/**{,/*/**}/*.#{suffix}") {|path| yield(path)}
end
opt.on('-d dir'){|dir|
dir_scanner(dir, txt_suffix) do |path|
File.open(path){ |file| file.each{|line|
line.force_encoding('ascii-8bit')
book_reader.parse_line(line)
}}
end
}
opt.on('--dayone=dir'){|dir|
dir_scanner(dir, 'doentry') do |path|
File.open(path){ |file| file.each{|line|
line.force_encoding('ascii-8bit')
book_reader.parse_dayone_line(line)
}}
end
}
opt.on("-D depth"){|depth| summary_depth = Integer(depth) - 1}
opt.on('--txt_suffix=suffix'){ |suffix| txt_suffix=suffix }
opt.on('-s', '--summary'){ |b| print_summary = true }
opt.on('-t', '--transactions'){ |b| print_transactions = true }
values = []
opt.order!{ |v| values << v }
if values.length == 3 then
bookname = values[0].dup
bookname.force_encoding('ascii-8bit')
book = book_reader.root_book.get_grandchild(bookname.split(':'))
book_writer = BookWriter.new(book)
if print_summary then
puts '*Summary'
book_writer.print_summary(Date.parse(values[1]), Date.parse(values[2]), summary_depth)
end
if print_transactions then
puts '*Transactions'
book_writer.print_transactions(Date.parse(values[1]), Date.parse(values[2]))
end
else
puts "Too few arguments\n"
end
end