On 2012-12-02 06:25, Cristian Marchi wrote:
> I've now a script that downloads a json file for each release of GnuCash
> in the 2.4 series with data from 2010-11-01 to 2012-11-30. It is like this:
>
> for i in {0..11}; do wget
> "http://sourceforge.net/projects/gnucash/files/gnucash%20%28stable%29/2.4.$i/stats/json?start_date=2010-11-01&end_date=2012-11-30"
> -O gnucash2.4.$i.json; done
>
> I can then convert each json file to a csv file with your script so that
> I can copy-paste data in a single spreadsheet to generate the graph.
>
> In order to make the process more automatic, would it be possible to
> create a csv file with only ["downloads"] data ordered as shown in the
> attached downloads.csv file?
Cristian,
Hopefully the attached is what you want.
usage: stats01.py [options] jsonPath csvOpath
GnuCash Stats for Cristian Marchi
positional arguments:
jsonPath Pathname of the directory containing all the json files
containing stats.
csvOpath Pathname of csv file to create
optional arguments:
-h, --help show this help message and exit
I used the bash file you supplied to download all the json files into a
directory. I CD'ed to this directory and ran command line:
stats01.py ./ stats1.csv
This produced the stats1.csv file that is attached.
Note: I sort the columns into ascending order first by major release
level then by minor release level, then by bug fix level, treating all
release levels as integers.
I sort the rows into ascending date order. I allow each json file to
contain a different set of dates.
SegundoBob
['downloads'],Date,GnuCash 2.4.0,GnuCash 2.4.1,GnuCash 2.4.2,GnuCash 2.4.3,GnuCash 2.4.4,GnuCash 2.4.5,GnuCash 2.4.6,GnuCash 2.4.7,GnuCash 2.4.8,GnuCash 2.4.9,GnuCash 2.4.10,GnuCash 2.4.11
,2010-11-01 00:00:00,0,0,0,0,0,0,0,0,0,0,0,0
,2010-12-01 00:00:00,40827,0,0,0,0,0,0,0,0,0,0,0
,2011-01-01 00:00:00,83031,0,0,0,0,0,0,0,0,0,0,0
,2011-02-01 00:00:00,24539,903,46299,1773,0,0,0,0,0,0,0,0
,2011-03-01 00:00:00,2612,421,8730,24294,28668,0,0,0,0,0,0,0
,2011-04-01 00:00:00,885,1,7284,271,16389,70976,0,0,0,0,0,0
,2011-05-01 00:00:00,725,3,7178,289,452,57856,3167,0,0,0,0,0
,2011-06-01 00:00:00,332,3,2337,319,267,814,50641,0,0,0,0,0
,2011-07-01 00:00:00,215,0,162,215,167,302,5103,43942,0,0,0,0
,2011-08-01 00:00:00,322,0,211,257,162,1220,1508,45469,0,0,0,0
,2011-09-01 00:00:00,181,1,2716,305,151,166,1346,50184,0,0,0,0
,2011-10-01 00:00:00,239,13,147,165,178,272,1028,34253,55650,0,0,0
,2011-11-01 00:00:00,440,0,110,129,135,143,165,555,65191,0,0,0
,2011-12-01 00:00:00,183,0,73,153,81,145,122,445,61077,0,0,0
,2012-01-01 00:00:00,313,1,135,112,111,178,127,415,37658,48472,504,0
,2012-02-01 00:00:00,222,1,131,93,111,103,93,222,8905,12044,49872,0
,2012-03-01 00:00:00,159,0,107,130,114,104,56,310,8163,791,54499,0
,2012-04-01 00:00:00,187,1,109,118,69,93,52,204,6819,796,56121,0
,2012-05-01 00:00:00,100,0,71,86,71,69,36,113,1972,636,40694,0
,2012-06-01 00:00:00,164,0,64,76,46,68,19,140,384,609,38079,0
,2012-07-01 00:00:00,241,0,58,74,34,64,14,76,282,353,22475,27518
,2012-08-01 00:00:00,38,0,60,91,27,70,16,63,209,108,7802,53411
,2012-09-01 00:00:00,56,0,50,113,69,60,15,66,179,94,7901,45029
,2012-10-01 00:00:00,57,0,69,89,58,57,18,66,211,138,770,44544
,2012-11-01 00:00:00,36,0,61,8,32,39,12,61,206,134,676,46931
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
Created on Sat Dec 1 10:04:12 2012
@author: bob02
"""
import codecs
import csv
import argparse
import glob
import json
import os
FileNamePattern = 'gnucash2.4.*.json'
CsvHdrLine = ["['downloads']", 'Date']
class RelStatObj(object):
"""
Release Stats Object
"""
def __init__(self, pathname, monthCntList):
"""
Initialize a Release Stats Object
@param self: instance to initialize
@param pathname: Pathname of the json file containing the stats.
@param monthCntList: The month, count list
"""
_, filename = os.path.split(pathname)
params = filename[len('gnucash'):]
partsList = params.split('.')
major, minor, bf, _ = partsList
self.major = int(major)
self.minor = int(minor)
self.bf = int(bf)
monthCntList.sort()
self.monthCntList = monthCntList
def cmdLineHandler():
"""
Command Line Handler
@param return: Instance of class argparse.Namespace containing all the parsed command line arguments.
"""
parser = argparse.ArgumentParser(description="GnuCash Stats for Cristian Marchi",
usage='%(prog)s [options] jsonPath csvOpath')
parser.add_argument('jsonPath', help='Pathname of the directory containing all the '
'json files containing stats.')
parser.add_argument('csvOpath', help='Pathname of csv file to create')
args = parser.parse_args()
return args
"""
http://docs.python.org/2/library/csv.html#examples
The csv module doesnât directly support reading and writing Unicode,
but it is 8-bit-clean save for some problems with ASCII NUL characters.
So you can write functions or classes that handle the encoding and decoding
for you as long as you avoid encodings like UTF-16 that use NULs.
UTF-8 is recommended.
"""
def main():
"""
Function executed when this file is executed.
This function is NOT executed when this module is imported.
"""
args = cmdLineHandler()
pathNamePattern = os.path.join(args.jsonPath, FileNamePattern)
globx = glob.glob(pathNamePattern)
relStatObjList = list()
for pathName in globx:
jsonFD = codecs.open(pathName, 'r', encoding='UTF-8')
try:
jsonTree = json.load(jsonFD)
except Exception as exception:
raise type(exception)('File: {0} - {1}'.format(pathName, exception.message))
finally:
jsonFD.close()
relStatObjList.append(RelStatObj(pathName, jsonTree['downloads']))
# Sort into ascending major, then ascending minor, then ascending bf.
relStatObjList.sort(key=lambda x: x.bf)
relStatObjList.sort(key=lambda x: x.minor)
relStatObjList.sort(key=lambda x: x.major)
# Calculate the header line and the column for each json file.
for rso in relStatObjList:
CsvHdrLine.append('GnuCash {0}.{1}.{2}'.format(rso.major, rso.minor, rso.bf))
# Allow the files to contain different sets of dates.
dateSet = set()
for rso in relStatObjList:
dateSet = dateSet | set([mcl[0] for mcl in rso.monthCntList])
dateList = list(dateSet)
dateList.sort()
# Set the dates column
outList = [CsvHdrLine]
for datx in dateList:
outList.append([None, datx])
# Set the counts Columns
for rso in relStatObjList:
mcl = rso.monthCntList
idxMcl = 0
for idx1, datx in enumerate(dateList):
linex = outList[1 + idx1]
if datx == mcl[idxMcl][0]:
linex.append(mcl[idxMcl][1])
idxMcl = idxMcl + 1
else:
linex.append(None)
csvFD = open(args.csvOpath, 'w')
csvWriter = csv.writer(csvFD, dialect='excel')
csvWriter.writerows(outList)
csvFD.close()
if __name__ == "__main__":
main()_______________________________________________
gnucash-devel mailing list
[email protected]
https://lists.gnucash.org/mailman/listinfo/gnucash-devel