Adding Josh Lamos's end-of-year reports scripts.
authorChris Sharp <csharp@georgialibraries.org>
Tue, 5 Aug 2014 18:11:19 +0000 (14:11 -0400)
committerChris Sharp <csharp@georgialibraries.org>
Tue, 5 Aug 2014 18:11:19 +0000 (14:11 -0400)
fy-report-scripts/README [new file with mode: 0644]
fy-report-scripts/doall.sh [new file with mode: 0755]
fy-report-scripts/split_fy.py [new file with mode: 0755]

diff --git a/fy-report-scripts/README b/fy-report-scripts/README
new file mode 100644 (file)
index 0000000..7ba8d10
--- /dev/null
@@ -0,0 +1,16 @@
+This script makes the following assumptions (and doesn't check if these reqs are not met):
+
+*library system names don't have parens AND the word 'row' in their name
+*the first column of every line identifies what file we want to write THIS line to
+*if that item has a '-' in it, remove the contents after and including the '-'
+*there exists an 'output' directory within the current directory
+*there exists a 'csv' directory within the current directory that contains all of the .csv files you want to process
+*All of said csv files contain a line at the top containing the column headers
+*that the scripts will be run from the current directory
+*that file system is in the right state for us to write to (permissions, space, actually exists etc)
+
+you can run the python script on an individual file:
+
+#INSERT EXAMPLE
+
+or you can just run ./do_all.sh to iterate over all files in the csv folder
diff --git a/fy-report-scripts/doall.sh b/fy-report-scripts/doall.sh
new file mode 100755 (executable)
index 0000000..530b6f4
--- /dev/null
@@ -0,0 +1,6 @@
+#!/bin/bash
+
+for F in csv/* 
+do
+       ./split_fy.py "$F"
+done
diff --git a/fy-report-scripts/split_fy.py b/fy-report-scripts/split_fy.py
new file mode 100755 (executable)
index 0000000..1b5fbfc
--- /dev/null
@@ -0,0 +1,54 @@
+#!/usr/bin/python
+
+import os
+import sys
+
+if len(sys.argv) != 2:
+       print "Usage: <scriptname> csv/blah.csv"
+       quit()
+fname = sys.argv[1].split('/')[1]
+report_name = fname.split('.csv')[0]
+
+f = open ('csv/'+fname,'r')
+header = f.readline()
+lines = f.readlines()
+output=dict()
+for line in lines:
+       #check for just systems e.g. STATELIB instead of the expected STATELIB-L
+       tmp = line.split(',')[0]
+       if tmp.find('-') == -1:
+               libsys = tmp
+       else:
+               libsys = line.split('-')[0]
+       #remove any whitespace junk that may have made it thus far:
+       libsys = libsys.strip()
+       
+       #if there are any lines that don't look right or lack a lib name, notify us, and discard the line by
+       #continuing on to the next item in the for loop
+       if libsys == "":
+               print "Couldn't identify library for this line:", line
+               print "This was contained in:", fname 
+               continue
+       #attempt to detect row count lines and skip to the next item if we think we got one
+       #probably could have regex matched it
+       if libsys.find('(') != -1 or libsys.find(')') != -1:
+               if libsys.find('row') != -1:
+                       print "We most linkely encountered a row count line:"+line+" in file:"+fname
+                       continue                
+
+
+       #check to make sure we don't try to append to a non-existant object, and then add to the list object
+       if libsys not in output:
+               output[libsys]=list()
+       output[libsys].append(line)
+
+#iterate through the dictionary we just created, and generate the files
+for k,v in output.items():
+       outfile = open('output/'+k+'-'+fname,'w')
+       outfile.write(header)
+       for line in v:
+               outfile.write(line)
+       outfile.close()
+
+
+f.close()