More flexible CSV -> ODS hyperlinks and pagebreaks; csv2ods.py produces MANIFEST.

Previous version of csv2ods.py simply assumed that fields beyond five
would have links to files.  This obviously lacked flexibility and was a
silly hard-code.  Now, those CSV fields that have link:SOMETHING will
cause a hyperlink to be created to SOMETHING.

Meanwhile, the pagebreak support was similarly hard-coded.  Now, any CSV
field that has the word "pagebreak" in it will generate a pagebreak.

The general ledger and cash receipts/disbursement journals have been
modified to make use of these new features in csv2ods.py.

Finally, the --skip-page-break option is now moot in csv2ods.py, so that
is herein removed.
This commit is contained in:
Bradley M. Kuhn 2013-01-06 19:43:54 -05:00
parent 6d98bc58ae
commit 8cddda4c3e
3 changed files with 32 additions and 21 deletions

View file

@ -113,7 +113,7 @@ foreach my $acct (@accounts) {
my $formatString = '\n"%(date)","%C","%P","%A","%t"\n%/"","","","%A","%t"';
foreach my $tagField (qw/Receipt Invoice Statement Contract PurchaseOrder Approval Check IncomeDistributionAnalysis CurrencyRate/) {
print CSV_OUT ',"', $tagField, '"';
$formatString .= ',"%(tag(\'' . $tagField . '\'))"';
$formatString .= ',"link:%(tag(\'' . $tagField . '\'))"';
}
$formatString .= "\n";
print CSV_OUT "\n";
@ -130,7 +130,7 @@ foreach my $acct (@accounts) {
open(CSV_DATA, "-|", $LEDGER_CMD, @csvRegLedgerOpts)
or die "unable to run ledger command for $fileNameBase.csv: $!";
while (my $line = <CSV_DATA>) { print CSV_OUT $line; }
while (my $line = <CSV_DATA>) { $line =~ s/"link:"/""/g; print CSV_OUT $line; }
close(CSV_DATA); die "Error read from csv ledger command $!" unless $? == 0;
SKIP_REGISTER_COMMANDS:

View file

@ -24,14 +24,13 @@ import sys, os, os.path, optparse
import csv
import ooolib2
file_fields = [ 'Receipt', 'Invoice', 'Statement', 'Contract', 'PurchaseOrder',
'Approval', 'Check', 'IncomeDistributionAnalysis', 'CurrencyRate' ]
def err(msg):
print 'error: %s' % msg
sys.exit(1)
def csv2ods(csvname, odsname, encoding='', verbose = False, skip_page_break = False):
def csv2ods(csvname, odsname, encoding='', verbose = False):
filesSavedinManifest = {}
if verbose:
print 'converting from %s to %s' % (csvname, odsname)
doc = ooolib2.Calc()
@ -45,7 +44,7 @@ def csv2ods(csvname, odsname, encoding='', verbose = False, skip_page_break = Fa
style_currency = doc.styles.get_next_style('cell')
style_data = tuple([style])
doc.styles.style_config[style_data] = style_currency
row = 1
csvdir = os.path.dirname(csvname)
if len(csvdir) == 0:
@ -61,26 +60,39 @@ def csv2ods(csvname, odsname, encoding='', verbose = False, skip_page_break = Fa
if len(val) > 0 and val[0] == '$':
doc.set_cell_value(col + 1, row, 'currency', val[1:])
else:
if ((col >= 5) and (not val in file_fields) and len(val) > 0):
if (len(val) > 0 and val[0:5] == "link:"):
val = val[5:]
linkrel = '../' + val # ../ means remove the name of the *.ods
linkname = os.path.basename(val) # name is just the last component
doc.set_cell_value(col + 1, row, 'link', (linkrel, linkname))
linkpath = csvdir + '/' + val
if not val in filesSavedinManifest:
filesSavedinManifest[val] = col
if not os.path.exists(linkpath):
print "WARNING: link %s DOES NOT EXIST at %s" % (val, linkpath)
if verbose:
if os.path.exists(linkpath):
print 'relative link %s EXISTS at %s' % (val, linkpath)
else:
print 'relative link %s DOES NOT EXIST at %s' % (val, linkpath)
else:
doc.set_cell_value(col + 1, row, 'string', val)
if val == "pagebreak":
doc.sheets[doc.sheet_index].set_sheet_config(('row', row), style_pagebreak)
else:
doc.set_cell_value(col + 1, row, 'string', val)
else:
# enter an empty string for blank lines
doc.set_cell_value(1, row, 'string', '')
# put a pagebreak here
if not skip_page_break:
doc.sheets[doc.sheet_index].set_sheet_config(('row', row), style_pagebreak)
row += 1
# save the file
# save manifest file
if filesSavedinManifest.keys() != []:
manifestFH = open("MANIFEST", "a")
manifestFH.write("# Files from %s\n" % odsname)
for file in filesSavedinManifest.keys():
manifestFH.write("%s\n" % file)
manifestFH.close()
# Save spreadsheet file.
doc.save(odsname)
def main():
@ -97,9 +109,6 @@ def main():
help='ods output filename')
parser.add_option('-e', '--encoding', action='store',
help='unicode character encoding type')
parser.add_option('-s', '--skip-page-break', action='store_true',
dest='skip_page_break',
help='do not add any page breaks')
(options, args) = parser.parse_args()
if len(args) != 0:
parser.error("not expecting extra args")
@ -113,7 +122,7 @@ def main():
print 'csv:', options.csv
print 'ods:', options.ods
print 'ods:', options.encoding
csv2ods(options.csv, options.ods, options.encoding, options.verbose, options.skip_page_break)
csv2ods(options.csv, options.ods, options.encoding, options.verbose)
if __name__ == '__main__':
main()

View file

@ -181,7 +181,7 @@ foreach my $acct (@sortedAccounts) {
my $formatString = '"%(date)","%C","%P","%t",""';
foreach my $tagField (qw/Receipt Invoice Statement Contract PurchaseOrder Approval Check IncomeDistributionAnalysis CurrencyRate/) {
print GL_CSV_OUT ',"', $tagField, '"';
$formatString .= ',"%(tag(\'' . $tagField . '\'))"';
$formatString .= ',"link:%(tag(\'' . $tagField . '\'))"';
}
$formatString .= "\n";
print GL_CSV_OUT "\n";
@ -195,12 +195,14 @@ foreach my $acct (@sortedAccounts) {
or die "Unable to run $LEDGER_CMD @acctLedgerOpts: $!";
foreach my $line (<GL_CSV_DATA>) {
$line =~ s/"link:"/""/g;
print GL_CSV_OUT $line;
next if $line =~ /ACCOUNT:.*PERIOD/; # Skip column header lines
$line =~ s/^"[^"]*","[^"]*","[^"]*","[^"]*","[^"]*",//;
while ($line =~ s/^"([^"]*)"(,|$)//) {
my $file = $1;
next if $file =~ /^\s*$/;
$file =~ s/^link:(.*)$/$1/;
warn "$file does not exist and/or is not readable" unless -r $file;
print MANIFEST "$file\n" if not defined $manifest{$file};
$manifest{$file} = $line;
@ -210,7 +212,7 @@ foreach my $acct (@sortedAccounts) {
$balanceData{totalEnd}{$acct} = $ZERO unless defined $balanceData{totalEnd}{$acct};
print GL_CSV_OUT "\"$formattedEndDate\"", ',"","BALANCE","","$', "$balanceData{totalEnd}{$acct}\"\n";
}
print GL_CSV_OUT "pagebreak\n";
close(GL_CSV_DATA); die "error reading ledger output for chart of accounts: $!" unless $? == 0;
}
close(GL_TEXT_OUT); die "error writing to general-ledger.txt: $!" unless $? == 0;