diff options
Diffstat (limited to 'contrib')
-rw-r--r-- | contrib/CSVReader.cs | 165 | ||||
-rw-r--r-- | contrib/Makefile | 4 | ||||
-rw-r--r-- | contrib/ParseCcStmt.cs | 184 | ||||
-rw-r--r-- | contrib/ledger.vim | 46 | ||||
-rw-r--r-- | contrib/scripts/README | 4 | ||||
-rwxr-xr-x | contrib/scripts/bal | 21 | ||||
-rwxr-xr-x | contrib/scripts/bal-huquq | 21 | ||||
-rwxr-xr-x | contrib/scripts/entry | 16 | ||||
-rwxr-xr-x | contrib/scripts/getquote | 16 | ||||
-rwxr-xr-x | contrib/scripts/ledger-du | 49 | ||||
-rwxr-xr-x | contrib/scripts/report | 21 | ||||
-rwxr-xr-x | contrib/scripts/tc | 7 | ||||
-rwxr-xr-x | contrib/scripts/ti | 5 | ||||
-rwxr-xr-x | contrib/scripts/to | 3 | ||||
-rwxr-xr-x | contrib/scripts/trend | 30 |
15 files changed, 592 insertions, 0 deletions
diff --git a/contrib/CSVReader.cs b/contrib/CSVReader.cs new file mode 100644 index 00000000..a22eab06 --- /dev/null +++ b/contrib/CSVReader.cs @@ -0,0 +1,165 @@ +// This code is in the public domain. I can't remember where I found it on the Web, but it
+// didn't come with any license.
+
+using System;
+using System.Collections;
+using System.IO;
+using System.Text;
+
+namespace CSVReader {
+
+ /// <summary>
+ /// A data-reader style interface for reading CSV files.
+ /// </summary>
+ public class CSVReader : IDisposable {
+
+ #region Private variables
+
+ private Stream stream;
+ private StreamReader reader;
+
+ #endregion
+
+ /// <summary>
+ /// Create a new reader for the given stream.
+ /// </summary>
+ /// <param name="s">The stream to read the CSV from.</param>
+ public CSVReader(Stream s) : this(s, null) { }
+
+ /// <summary>
+ /// Create a new reader for the given stream and encoding.
+ /// </summary>
+ /// <param name="s">The stream to read the CSV from.</param>
+ /// <param name="enc">The encoding used.</param>
+ public CSVReader(Stream s, Encoding enc) {
+
+ this.stream = s;
+ if (!s.CanRead) {
+ throw new CSVReaderException("Could not read the given CSV stream!");
+ }
+ reader = (enc != null) ? new StreamReader(s, enc) : new StreamReader(s);
+ }
+
+ /// <summary>
+ /// Creates a new reader for the given text file path.
+ /// </summary>
+ /// <param name="filename">The name of the file to be read.</param>
+ public CSVReader(string filename) : this(filename, null) { }
+
+ /// <summary>
+ /// Creates a new reader for the given text file path and encoding.
+ /// </summary>
+ /// <param name="filename">The name of the file to be read.</param>
+ /// <param name="enc">The encoding used.</param>
+ public CSVReader(string filename, Encoding enc)
+ : this(new FileStream(filename, FileMode.Open), enc) { }
+
+ /// <summary>
+ /// Returns the fields for the next row of CSV data (or null if at eof)
+ /// </summary>
+ /// <returns>A string array of fields or null if at the end of file.</returns>
+ public string[] GetCSVLine() {
+
+ string data = reader.ReadLine();
+ if (data == null) return null;
+ if (data.Length == 0) return new string[0];
+
+ ArrayList result = new ArrayList();
+
+ ParseCSVFields(result, data);
+
+ return (string[])result.ToArray(typeof(string));
+ }
+
+ // Parses the CSV fields and pushes the fields into the result arraylist
+ private void ParseCSVFields(ArrayList result, string data) {
+
+ int pos = -1;
+ while (pos < data.Length)
+ result.Add(ParseCSVField(data, ref pos));
+ }
+
+ // Parses the field at the given position of the data, modified pos to match
+ // the first unparsed position and returns the parsed field
+ private string ParseCSVField(string data, ref int startSeparatorPosition) {
+
+ if (startSeparatorPosition == data.Length-1) {
+ startSeparatorPosition++;
+ // The last field is empty
+ return "";
+ }
+
+ int fromPos = startSeparatorPosition + 1;
+
+ // Determine if this is a quoted field
+ if (data[fromPos] == '"') {
+ // If we're at the end of the string, let's consider this a field that
+ // only contains the quote
+ if (fromPos == data.Length-1) {
+ fromPos++;
+ return "\"";
+ }
+
+ // Otherwise, return a string of appropriate length with double quotes collapsed
+ // Note that FSQ returns data.Length if no single quote was found
+ int nextSingleQuote = FindSingleQuote(data, fromPos+1);
+ startSeparatorPosition = nextSingleQuote+1;
+ return data.Substring(fromPos+1, nextSingleQuote-fromPos-1).Replace("\"\"", "\"");
+ }
+
+ // The field ends in the next comma or EOL
+ int nextComma = data.IndexOf(',', fromPos);
+ if (nextComma == -1) {
+ startSeparatorPosition = data.Length;
+ return data.Substring(fromPos);
+ }
+ else {
+ startSeparatorPosition = nextComma;
+ return data.Substring(fromPos, nextComma-fromPos);
+ }
+ }
+
+ // Returns the index of the next single quote mark in the string
+ // (starting from startFrom)
+ private int FindSingleQuote(string data, int startFrom) {
+
+ int i = startFrom-1;
+ while (++i < data.Length)
+ if (data[i] == '"') {
+ // If this is a double quote, bypass the chars
+ if (i < data.Length-1 && data[i+1] == '"') {
+ i++;
+ continue;
+ }
+ else
+ return i;
+ }
+ // If no quote found, return the end value of i (data.Length)
+ return i;
+ }
+
+ /// <summary>
+ /// Disposes the CSVReader. The underlying stream is closed.
+ /// </summary>
+ public void Dispose() {
+ // Closing the reader closes the underlying stream, too
+ if (reader != null) reader.Close();
+ else if (stream != null)
+ stream.Close(); // In case we failed before the reader was constructed
+ GC.SuppressFinalize(this);
+ }
+ }
+
+
+ /// <summary>
+ /// Exception class for CSVReader exceptions.
+ /// </summary>
+ public class CSVReaderException : ApplicationException {
+
+ /// <summary>
+ /// Constructs a new exception object with the given message.
+ /// </summary>
+ /// <param name="message">The exception message.</param>
+ public CSVReaderException(string message) : base(message) { }
+ }
+}
diff --git a/contrib/Makefile b/contrib/Makefile new file mode 100644 index 00000000..6e4d367a --- /dev/null +++ b/contrib/Makefile @@ -0,0 +1,4 @@ +all: ParseCcStmt.exe + +ParseCcStmt.exe: ParseCcStmt.cs CSVReader.cs + gmcs -out:ParseCcStmt.exe ParseCcStmt.cs CSVReader.cs diff --git a/contrib/ParseCcStmt.cs b/contrib/ParseCcStmt.cs new file mode 100644 index 00000000..f6b2f20b --- /dev/null +++ b/contrib/ParseCcStmt.cs @@ -0,0 +1,184 @@ +/* + * Copyright (c) 2003-2008, John Wiegley. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * - Neither the name of New Artisans LLC nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +using System; +using System.Collections; +using System.Collections.Generic; +using System.IO; +using System.Text; +using System.Text.RegularExpressions; + +using CSVReader; + +/** + * @file ParseCcStmt.cs + * + * @brief Provides a .NET way to turn a CSV report into Ledger entries. + * + * I use this code for converting the statements from my own credit card + * issuer. I realize it's strange for this to be in C#, but I wrote it + * during a phase of C# contracting. The code is solid enough now -- + * and the Mono project is portable enough -- that I haven't seen the + * need to rewrite it into another language like Python. + */ + +namespace JohnWiegley +{ + public class Transaction + { + public DateTime Date; + public DateTime PostedDate; + public string Code; + public string Payee; + public Decimal Amount; + } + + public interface IStatementConverter + { + List<Transaction> ConvertRecords(Stream s); + } + + public class ConvertGoldMasterCardStatement : IStatementConverter + { + public List<Transaction> ConvertRecords(Stream s) + { + List<Transaction> xacts = new List<Transaction>(); + + using (CSVReader.CSVReader csv = new CSVReader.CSVReader(s)) { + string[] fields; + while ((fields = csv.GetCSVLine()) != null) { + if (fields[0] == "TRANSACTION DATE") + continue; + + Transaction xact = new Transaction(); + + xact.Date = DateTime.ParseExact(fields[0], "mm/dd/yy", null); + xact.PostedDate = DateTime.ParseExact(fields[1], "mm/dd/yy", null); + xact.Payee = fields[2].Trim(); + xact.Code = fields[3].Trim(); + xact.Amount = Convert.ToDecimal(fields[4].Trim()); + + if (xact.Code.Length == 0) + xact.Code = null; + + xacts.Add(xact); + } + } + return xacts; + } + } + + public class ConvertMastercardStatement : IStatementConverter + { + public List<Transaction> ConvertRecords(Stream s) + { + List<Transaction> xacts = new List<Transaction>(); + + using (CSVReader.CSVReader csv = new CSVReader.CSVReader(s)) { + string[] fields; + while ((fields = csv.GetCSVLine()) != null) { + Transaction xact = new Transaction(); + + xact.Date = DateTime.ParseExact(fields[0], "m/dd/yyyy", null); + xact.Payee = fields[2].Trim(); + xact.Code = fields[3].Trim(); + xact.Amount = - Convert.ToDecimal(fields[4].Trim()); + + if (xact.Code.Length == 0) + xact.Code = null; + + xacts.Add(xact); + } + } + return xacts; + } + } + + public class PrintTransactions + { + public string DefaultAccount(Transaction xact) { + if (Regex.IsMatch(xact.Payee, "IGA")) + return "Expenses:Food"; + return "Expenses:Food"; + } + + public void Print(string AccountName, string PayAccountName, + List<Transaction> xacts) + { + foreach (Transaction xact in xacts) { + if (xact.Amount < 0) { + Console.WriteLine("{0} * {1}{2}", xact.Date.ToString("yyyy/mm/dd"), + xact.Code != null ? "(" + xact.Code + ") " : "", + xact.Payee); + Console.WriteLine(" {0,-36}{1,12}", AccountName, + "$" + (- xact.Amount).ToString()); + Console.WriteLine(" {0}", PayAccountName); + } else { + Console.WriteLine("{0} {1}{2}", xact.Date.ToString("yyyy/mm/dd"), + xact.Code != null ? "(" + xact.Code + ") " : "", + xact.Payee); + Console.WriteLine(" {0,-36}{1,12}", DefaultAccount(xact), + "$" + xact.Amount.ToString()); + Console.WriteLine(" * {0}", AccountName); + } + Console.WriteLine(); + } + } + } + + public class ParseCcStmt + { + public static int Main(string[] args) + { + StreamReader reader = new StreamReader(args[0]); + string firstLine = reader.ReadLine(); + + string CardAccount = args[1]; + string BankAccount = args[2]; + + IStatementConverter converter; + + if (firstLine.StartsWith("TRANSACTION DATE")) { + converter = new ConvertGoldMasterCardStatement(); + } else { + converter = new ConvertMastercardStatement(); + } + + reader = new StreamReader(args[0]); + List<Transaction> xacts = converter.ConvertRecords(reader.BaseStream); + + PrintTransactions printer = new PrintTransactions(); + printer.Print(CardAccount, BankAccount, xacts); + + return 0; + } + } +} diff --git a/contrib/ledger.vim b/contrib/ledger.vim new file mode 100644 index 00000000..df63feb8 --- /dev/null +++ b/contrib/ledger.vim @@ -0,0 +1,46 @@ +" Vim syntax file +" filetype: ledger +" Version: 0.0.2 +" by Wolfgang Oertl; Use according to the terms of the GPL>=2. +" Revision history +" 2005-02-05 first version (partly copied from ledger.vim 0.0.1) + +if version < 600 + syntax clear +elseif exists("b:current_sytax") + finish +endif + +" for debugging +syntax clear + +" region: a normal transaction +syn region transNorm start=/^\d/ skip=/^\s/ end=/^/ fold keepend transparent contains=transDate +syn match transDate /^\d\S\+/ contained +syn match Comment /^;.*$/ +" highlight default link transNorm Question +highlight default link Comment SpecialKey +highlight default link transDate Question + +" folding: how to represent a transaction in one line. +function! MyFoldText() + let line = strpart(getline(v:foldstart), 0, 65) + " get the amount at the end of the second line + let line2 = getline(v:foldstart+1) + let pos = match(line2, "[0-9.]*$") + let line2 = strpart(line2, pos) + let pad_len = 80 - strlen(line) - strlen(line2) + if (pad_len < 0) then + pad_len = 0 + endif + let pad = strpart(" ", 0, pad_len) + return line . pad . line2 +endfunction +set foldtext=MyFoldText() +set foldmethod=syntax + +" syncinc is easy: search for the first transaction. +syn sync clear +syn sync match ledgerSync grouphere transNorm "^\d" + +let b:current_syntax = "ledger" diff --git a/contrib/scripts/README b/contrib/scripts/README new file mode 100644 index 00000000..6108afbf --- /dev/null +++ b/contrib/scripts/README @@ -0,0 +1,4 @@ +This scripts are provided just to give some ideas. They probably need +to be modified to better suit your environment. Beware! + +John diff --git a/contrib/scripts/bal b/contrib/scripts/bal new file mode 100755 index 00000000..423e3e41 --- /dev/null +++ b/contrib/scripts/bal @@ -0,0 +1,21 @@ +#!/bin/sh + +switch="-c" +limit="-t (/Liabilities/?a<0:Ua>100)&a" + +if [ "$1" = "-C" -o "$1" = "-U" ]; then + switch="$1" + shift +elif [ "$1" = "-b" -o "$1" = "-e" -o "$1" = "-p" ]; then + switch="$1 $2" + shift 2 +fi + +accts="$@" +if [ -z "$accts" ]; then + accts="-Equity -Income -Expenses" +else + limit="" +fi + +ledger -VQ $switch $limit -s -S "-UT" balance $accts diff --git a/contrib/scripts/bal-huquq b/contrib/scripts/bal-huquq new file mode 100755 index 00000000..fad2854a --- /dev/null +++ b/contrib/scripts/bal-huquq @@ -0,0 +1,21 @@ +#!/bin/sh + +switch="-c" +limit="-t (/Liabilities/?(/Huquq/?a/P{2.22AU}<={-1.0}:a<0):Ua>100)&a" + +if [ "$1" = "-C" -o "$1" = "-U" ]; then + switch="$1" + shift +elif [ "$1" = "-b" -o "$1" = "-e" -o "$1" = "-p" ]; then + switch="$1 $2" + shift 2 +fi + +accts="$@" +if [ -z "$accts" ]; then + accts="-Equity -Income -Expenses" +else + limit="" +fi + +ledger -VQ $switch $limit -s -S "-UT" balance $accts diff --git a/contrib/scripts/entry b/contrib/scripts/entry new file mode 100755 index 00000000..cc030d8e --- /dev/null +++ b/contrib/scripts/entry @@ -0,0 +1,16 @@ +#!/bin/sh + +if [ -z "$LEDGER" -o ! -r "$LEDGER" ]; then + echo Please set your LEDGER environment variable. +fi + +line=`wc -l $LEDGER | awk '{print $1}'` + +if ledger entry "$@" > /tmp/entry; then + cat /tmp/entry >> $LEDGER +else + echo "$@" >> $LEDGER +fi +rm /tmp/entry + +vi +$line $LEDGER diff --git a/contrib/scripts/getquote b/contrib/scripts/getquote new file mode 100755 index 00000000..bed561d6 --- /dev/null +++ b/contrib/scripts/getquote @@ -0,0 +1,16 @@ +#!/usr/bin/perl + +$timeout = 60; + +use Finance::Quote; + +$q = Finance::Quote->new; +$q->timeout($timeout); +$q->require_labels(qw/price/); + +%quotes = $q->fetch("nasdaq", $ARGV[0]); +if ($quotes{$ARGV[0], "price"}) { + print "\$", $quotes{$ARGV[0], "price"}, "\n"; +} else { + exit 1; +} diff --git a/contrib/scripts/ledger-du b/contrib/scripts/ledger-du new file mode 100755 index 00000000..f5d7dd7d --- /dev/null +++ b/contrib/scripts/ledger-du @@ -0,0 +1,49 @@ +#!/usr/bin/env python + +import string +import sys +import os +import time + +from stat import * +from os.path import * + +def report_file(path): + dir_elems = string.split(dirname(path), os.sep) + if dir_elems[0] == "." or dir_elems[0] == "": + dir_elems = dir_elems[1 :] + account = string.join(dir_elems, ":") + + info = os.stat(path) + print time.strftime("%Y/%m/%d", time.localtime(info[ST_MTIME])), + + print basename(path) + print " ", account, " ", info[ST_SIZE], "b" + print " Equity:Files" + print + +def find_files(path): + entries = os.listdir(path) + for entry in entries: + entry = join(path, entry) + if not islink(entry): + if isdir(entry) and entry != "/proc": + find_files(entry) + else: + report_file(entry) + +args = sys.argv[1:] +if len(args): + paths = args +else: + paths = ["."] + +print """ +C 1.00 Kb = 1024 b +C 1.00 Mb = 1024 Kb +C 1.00 Gb = 1024 Mb +C 1.00 Tb = 1024 Gb +""" + +for path in paths: + find_files(path) diff --git a/contrib/scripts/report b/contrib/scripts/report new file mode 100755 index 00000000..24418cdc --- /dev/null +++ b/contrib/scripts/report @@ -0,0 +1,21 @@ +#!/bin/sh + +# This script facilities plotting of a ledger register report. If you +# use OS/X, and have AquaTerm installed, you will probably want to set +# LEDGER_TERM to "aqua". +# +# Examples of use: +# +# report -j -M reg food # plot monthly food costs +# report -J reg checking # plot checking account balance + +if [ -z "$LEDGER_TERM" ]; then + LEDGER_TERM="x11 persist" +fi + +(cat <<EOF; ledger "$@") | gnuplot + set terminal $LEDGER_TERM + set xdata time + set timefmt "%Y/%m/%d" + plot "-" using 1:2 with lines +EOF diff --git a/contrib/scripts/tc b/contrib/scripts/tc new file mode 100755 index 00000000..c24be99a --- /dev/null +++ b/contrib/scripts/tc @@ -0,0 +1,7 @@ +#!/bin/sh + +timeclock out + +proj="$1" +shift +timeclock in "$proj" "$@" diff --git a/contrib/scripts/ti b/contrib/scripts/ti new file mode 100755 index 00000000..a7214e65 --- /dev/null +++ b/contrib/scripts/ti @@ -0,0 +1,5 @@ +#!/bin/sh + +proj="$1" +shift +timeclock in "$proj" "$@" diff --git a/contrib/scripts/to b/contrib/scripts/to new file mode 100755 index 00000000..3198db3c --- /dev/null +++ b/contrib/scripts/to @@ -0,0 +1,3 @@ +#!/bin/sh + +timeclock out "$@" diff --git a/contrib/scripts/trend b/contrib/scripts/trend new file mode 100755 index 00000000..3c189c0b --- /dev/null +++ b/contrib/scripts/trend @@ -0,0 +1,30 @@ +#!/bin/sh + +# This script requires Python support. +# +# To use, just run "trend" with the accounts to compute the trend for: +# +# trend dining +# +# The trend values are not terribly meaningful, but this gives an +# example of how Python can be used to create more complex reports. + +ledger --import-stdin -T "@rdev()" reg "$@" <<EOF +import ledger + +mean = ledger.parse_value_expr ("AT") +last_mean = None +last_dev = None + +def rdev (details): + global last_mean, last_dev + mval = mean.compute (details) + if last_mean is None: + dev = ledger.Value () + else: + dev = mval - last_mean + dev = (last_dev + dev) / 2 + last_mean = mval + last_dev = dev + return dev +EOF |