Class: Reckon::App
- Inherits:
-
Object
- Object
- Reckon::App
- Defined in:
- lib/reckon/app.rb
Constant Summary collapse
- @@cli =
HighLine.new
Instance Attribute Summary collapse
-
#csv_parser ⇒ Object
Returns the value of attribute csv_parser.
-
#matcher ⇒ Object
Returns the value of attribute matcher.
-
#options ⇒ Object
Returns the value of attribute options.
-
#regexps ⇒ Object
Returns the value of attribute regexps.
-
#seen ⇒ Object
Returns the value of attribute seen.
Class Method Summary collapse
Instance Method Summary collapse
- #add_description(row) ⇒ Object
- #add_note(row) ⇒ Object
- #add_regexp(account, regex_str) ⇒ Object
- #already_seen?(row) ⇒ Boolean
- #ask_account_question(msg, row) ⇒ Object
- #each_row_backwards ⇒ Object
-
#extract_account_tokens(subtree, account = nil) ⇒ Object
Add tokens from account_tokens_file to accounts.
- #finish ⇒ Object
-
#initialize(opts = {}) ⇒ App
constructor
A new instance of App.
- #interactive_output(str) ⇒ Object
- #learn! ⇒ Object
- #learn_from_account_tokens(filename) ⇒ Object
- #learn_from_ledger(ledger) ⇒ Object
- #learn_from_ledger_file(ledger_file) ⇒ Object
- #ledger_format(row, line1, line2) ⇒ Object
- #most_specific_regexp_match(row) ⇒ Object
- #output(ledger_line) ⇒ Object
- #output_table ⇒ Object
- #print_transaction(rows) ⇒ Object
- #seen_key(date, amount) ⇒ Object
- #suggest(row) ⇒ Object
- #walk_backwards ⇒ Object
Constructor Details
#initialize(opts = {}) ⇒ App
Returns a new instance of App.
11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/reckon/app.rb', line 11 def initialize(opts = {}) self. = opts LOGGER.level = Logger::INFO if [:verbose] self.regexps = {} self.seen = Set.new self.[:currency] ||= '$' @csv_parser = CSVParser.new( ) @matcher = CosineSimilarity.new() learn! end |
Instance Attribute Details
#csv_parser ⇒ Object
Returns the value of attribute csv_parser.
8 9 10 |
# File 'lib/reckon/app.rb', line 8 def csv_parser @csv_parser end |
#matcher ⇒ Object
Returns the value of attribute matcher.
8 9 10 |
# File 'lib/reckon/app.rb', line 8 def matcher @matcher end |
#options ⇒ Object
Returns the value of attribute options.
8 9 10 |
# File 'lib/reckon/app.rb', line 8 def @options end |
#regexps ⇒ Object
Returns the value of attribute regexps.
8 9 10 |
# File 'lib/reckon/app.rb', line 8 def regexps @regexps end |
#seen ⇒ Object
Returns the value of attribute seen.
8 9 10 |
# File 'lib/reckon/app.rb', line 8 def seen @seen end |
Class Method Details
.parse_opts(args = ARGV) ⇒ Object
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 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 |
# File 'lib/reckon/app.rb', line 289 def self.parse_opts(args = ARGV) = { :output_file => STDOUT } parser = OptionParser.new do |opts| opts. = "Usage: Reckon.rb [options]" opts.separator "" opts.on("-f", "--file FILE", "The CSV file to parse") do |file| [:file] = file end opts.on("-a", "--account NAME", "The Ledger Account this file is for") do |a| [:bank_account] = a end opts.on("-v", "--[no-]verbose", "Run verbosely") do |v| [:verbose] = v end opts.on("-i", "--inverse", "Use the negative of each amount") do |v| [:inverse] = v end opts.on("-p", "--print-table", "Print out the parsed CSV in table form") do |p| [:print_table] = p end opts.on("-o", "--output-file FILE", "The ledger file to append to") do |o| [:output_file] = File.open(o, 'a') end opts.on("-l", "--learn-from FILE", "An existing ledger file to learn accounts from") do |l| [:existing_ledger_file] = l end opts.on("", "--ignore-columns 1,2,5", "Columns to ignore in the CSV file - the first column is column 1") do |ignore| [:ignore_columns] = ignore.split(",").map { |i| i.to_i } end opts.on("", "--money-column 2", Integer, "Specify the money column instead of letting Reckon guess - the first column is column 1") do |column_number| [:money_column] = column_number end opts.on("", "--raw-money", "Don't format money column (for stocks)") do |n| [:raw] = n end opts.on("", "--date-column 3", Integer, "Specify the date column instead of letting Reckon guess - the first column is column 1") do |column_number| [:date_column] = column_number end opts.on("", "--contains-header [N]", "The first row of the CSV is a header and should be skipped. Optionally add the number of rows to skip.") do |contains_header| [:contains_header] = 1 [:contains_header] = contains_header.to_i if contains_header end opts.on("", "--csv-separator ','", "Separator for parsing the CSV - default is comma.") do |csv_separator| [:csv_separator] = csv_separator end opts.on("", "--comma-separates-cents", "Use comma instead of period to deliminate dollars from cents when parsing ($100,50 instead of $100.50)") do |c| [:comma_separates_cents] = c end opts.on("", "--encoding 'UTF-8'", "Specify an encoding for the CSV file; not usually needed") do |e| [:encoding] = e end opts.on("-c", "--currency '$'", "Currency symbol to use, defaults to $ (£, EUR)") do |e| [:currency] = e end opts.on("", "--date-format '%d/%m/%Y'", "Force the date format (see Ruby DateTime strftime)") do |d| [:date_format] = d end opts.on("-u", "--unattended", "Don't ask questions and guess all the accounts automatically. Used with --learn-from or --account-tokens options.") do |n| [:unattended] = n end opts.on("-t", "--account-tokens FILE", "YAML file with manually-assigned tokens for each account (see README)") do |a| [:account_tokens_file] = a end opts.on("", "--default-into-account NAME", "Default into account") do |a| [:default_into_account] = a end opts.on("", "--default-outof-account NAME", "Default 'out of' account") do |a| [:default_outof_account] = a end opts.on("", "--suffixed", "If --currency should be used as a suffix. Defaults to false.") do |e| [:suffixed] = e end opts.on_tail("-h", "--help", "Show this message") do puts opts exit end opts.on_tail("--version", "Show version") do puts VERSION exit end opts.parse!(args) end unless [:file] [:file] = @@cli.ask("What CSV file should I parse? ") unless [:file].length > 0 puts "\nYou must provide a CSV file to parse.\n" puts parser exit end end unless [:bank_account] fail "Please specify --account in unattended mode" if [:unattended] [:bank_account] = @@cli.ask("What is the account name of this bank account in Ledger? ") do |q| q.readline = true q.validate = /^.{2,}$/ q.default = "Assets:Bank:Checking" end end end |
Instance Method Details
#add_description(row) ⇒ Object
221 222 223 224 225 226 227 228 229 |
# File 'lib/reckon/app.rb', line 221 def add_description(row) desc_answer = @@cli.ask("Enter a new description for this transaction (empty line aborts)\n") do |q| q.overwrite = true q.readline = true q.default = row[:description] end row[:description] = desc_answer unless desc_answer.empty? end |
#add_note(row) ⇒ Object
231 232 233 234 235 236 237 238 239 |
# File 'lib/reckon/app.rb', line 231 def add_note(row) desc_answer = @@cli.ask("Enter a new note for this transaction (empty line aborts)\n") do |q| q.overwrite = true q.readline = true q.default = row[:note] end row[:note] = desc_answer unless desc_answer.empty? end |
#add_regexp(account, regex_str) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/reckon/app.rb', line 91 def add_regexp(account, regex_str) # https://github.com/tenderlove/psych/blob/master/lib/psych/visitors/to_ruby.rb match = regex_str.match(/^\/(.*)\/([ix]*)$/m) fail "failed to parse regexp #{regex_str}" unless match = 0 (match[2] || '').split('').each do |option| case option when 'x' then |= Regexp::EXTENDED when 'i' then |= Regexp::IGNORECASE end end regexps[Regexp.new(match[1], )] = account end |
#already_seen?(row) ⇒ Boolean
271 272 273 |
# File 'lib/reckon/app.rb', line 271 def already_seen?(row) seen.include?(seen_key(row[:pretty_date], row[:pretty_money])) end |
#ask_account_question(msg, row) ⇒ Object
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 |
# File 'lib/reckon/app.rb', line 191 def ask_account_question(msg, row) possible_answers = suggest(row) LOGGER.info "possible_answers===> #{possible_answers.inspect}" if [:unattended] default = if row[:pretty_money][0] == '-' [:default_into_account] || 'Expenses:Unknown' else [:default_outof_account] || 'Income:Unknown' end return possible_answers[0] || default end answer = @@cli.ask(msg) do |q| q.completion = possible_answers q.readline = true q.default = possible_answers.first end # if answer isn't n/note/d/description, must be an account name, or skip, or quit return answer unless %w[n note d description].include?(answer) add_description(row) if %w[d description].include?(answer) add_note(row) if %w[n note].include?(answer) print_transaction([row]) # give user a chance to set account name or retry description return ask_account_question(msg, row) end |
#each_row_backwards ⇒ Object
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/reckon/app.rb', line 147 def each_row_backwards rows = [] (0...@csv_parser.columns.first.length).to_a.each do |index| if @csv_parser.date_for(index).nil? LOGGER.warn("Skipping row: '#{@csv_parser.row(index)}' that doesn't have a valid date") next end rows << { :date => @csv_parser.date_for(index), :pretty_date => @csv_parser.pretty_date_for(index), :pretty_money => @csv_parser.pretty_money_for(index), :pretty_money_negated => @csv_parser.pretty_money_for(index, :negate), :money => @csv_parser.money_for(index), :description => @csv_parser.description_for(index) } end rows.sort_by { |n| [n[:date], -n[:money], n[:description]] }.each { |row| yield row } end |
#extract_account_tokens(subtree, account = nil) ⇒ Object
Add tokens from account_tokens_file to accounts
76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/reckon/app.rb', line 76 def extract_account_tokens(subtree, account = nil) if subtree.nil? puts "Warning: empty #{account} tree" {} elsif subtree.is_a?(Array) { account => subtree } else at = subtree.map do |k, v| merged_acct = [account, k].compact.join(':') extract_account_tokens(v, merged_acct) end at.inject({}) { |memo, e| memo.merge!(e)} end end |
#finish ⇒ Object
275 276 277 278 279 |
# File 'lib/reckon/app.rb', line 275 def finish [:output_file].close unless [:output_file] == STDOUT interactive_output "Exiting." exit end |
#interactive_output(str) ⇒ Object
23 24 25 26 27 |
# File 'lib/reckon/app.rb', line 23 def interactive_output(str) return if [:unattended] puts str end |
#learn! ⇒ Object
29 30 31 32 |
# File 'lib/reckon/app.rb', line 29 def learn! learn_from_account_tokens([:account_tokens_file]) learn_from_ledger_file([:existing_ledger_file]) end |
#learn_from_account_tokens(filename) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/reckon/app.rb', line 34 def learn_from_account_tokens(filename) return unless filename raise "#{filename} doesn't exist!" unless File.exist?(filename) extract_account_tokens(YAML.load_file(filename)).each do |account, tokens| tokens.each do |t| if t.start_with?('/') add_regexp(account, t) else @matcher.add_document(account, t) end end end end |
#learn_from_ledger(ledger) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/reckon/app.rb', line 58 def learn_from_ledger(ledger) LOGGER.info "learning from #{ledger}" LedgerParser.new(ledger).entries.each do |entry| entry[:accounts].each do |account| str = [entry[:desc], account[:amount]].join(" ") if account[:name] != [:bank_account] LOGGER.info "adding document #{account[:name]} #{str}" @matcher.add_document(account[:name], str) end pretty_date = entry[:date].iso8601 if account[:name] == [:bank_account] seen << seen_key(pretty_date, @csv_parser.pretty_money(account[:amount])) end end end end |
#learn_from_ledger_file(ledger_file) ⇒ Object
50 51 52 53 54 55 56 |
# File 'lib/reckon/app.rb', line 50 def learn_from_ledger_file(ledger_file) return unless ledger_file raise "#{ledger_file} doesn't exist!" unless File.exist?(ledger_file) learn_from_ledger(File.read(ledger_file)) end |
#ledger_format(row, line1, line2) ⇒ Object
255 256 257 258 259 260 |
# File 'lib/reckon/app.rb', line 255 def ledger_format(row, line1, line2) out = "#{row[:pretty_date]}\t#{row[:description]}#{row[:note] ? "\t; " + row[:note]: ""}\n" out += "\t#{line1.first}\t\t\t#{line1.last}\n" out += "\t#{line2.first}\t\t\t#{line2.last}\n\n" out end |
#most_specific_regexp_match(row) ⇒ Object
241 242 243 244 245 246 247 248 |
# File 'lib/reckon/app.rb', line 241 def most_specific_regexp_match(row) matches = regexps.map { |regexp, account| if match = regexp.match(row[:description]) [account, match[0]] end }.compact matches.sort_by! { |_account, matched_text| matched_text.length }.map(&:first) end |
#output(ledger_line) ⇒ Object
262 263 264 265 |
# File 'lib/reckon/app.rb', line 262 def output(ledger_line) [:output_file].puts ledger_line [:output_file].flush end |
#output_table ⇒ Object
281 282 283 284 285 286 287 |
# File 'lib/reckon/app.rb', line 281 def output_table rows = [] each_row_backwards do |row| rows << row end print_transaction(rows) end |
#print_transaction(rows) ⇒ Object
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 |
# File 'lib/reckon/app.rb', line 164 def print_transaction(rows) str = "\n" header = %w[Date Amount Description Note] maxes = header.map(&:length) rows = rows.map { |r| [r[:pretty_date], r[:pretty_money], r[:description], r[:note]] } rows.each do |r| r.length.times { |i| l = r[i] ? r[i].length : 0; maxes[i] = l if maxes[i] < l } end header.each_with_index do |n, i| str += " #{n.center(maxes[i])} |" end str += "\n" rows.each do |row| row.each_with_index do |_, i| just = maxes[i] str += sprintf(" %#{just}s |", row[i]) end str += "\n" end interactive_output str end |
#seen_key(date, amount) ⇒ Object
267 268 269 |
# File 'lib/reckon/app.rb', line 267 def seen_key(date, amount) return [date, amount].join("|") end |
#suggest(row) ⇒ Object
250 251 252 253 |
# File 'lib/reckon/app.rb', line 250 def suggest(row) most_specific_regexp_match(row) + @matcher.find_similar(row[:description]).map { |n| n[:account] } end |
#walk_backwards ⇒ Object
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 |
# File 'lib/reckon/app.rb', line 105 def walk_backwards = "[account]/[q]uit/[s]kip/[n]ote/[d]escription" seen_anything_new = false each_row_backwards do |row| print_transaction([row]) if already_seen?(row) interactive_output "NOTE: This row is very similar to a previous one!" if !seen_anything_new interactive_output "Skipping..." next end else seen_anything_new = true end if row[:money] > 0 # out_of_account answer = ask_account_question("Which account provided this income? (#{})", row) line1 = [[:bank_account], row[:pretty_money]] line2 = [answer, ""] else # into_account answer = ask_account_question("To which account did this money go? (#{})", row) # line1 = [answer, row[:pretty_money_negated]] line1 = [answer, ""] line2 = [[:bank_account], row[:pretty_money]] end finish if %w[quit q].include?(answer) if %w[skip s].include?(answer) interactive_output "Skipping" next end ledger = ledger_format(row, line1, line2) LOGGER.info "ledger line: #{ledger}" learn_from_ledger(ledger) unless [:account_tokens_file] output(ledger) end end |