import argparse
import os
import sys
from collections import defaultdict

import sys

from postprocess import processdata
from utility import utility

parser = argparse.ArgumentParser(description="Counts the valid queries")
parser.add_argument("--monthsFolder", "-m", default="/a/akrausetud/months", type=str,
					help="the folder in which the months directory are residing")
parser.add_argument("--ignoreLock", "-i", help="Ignore locked file and execute anyways", action="store_true")
parser.add_argument("month", type=str, help="the month which we're interested in")

if (len(sys.argv[1:]) == 0):
	parser.print_help()
	parser.exit()

args = parser.parse_args()

if os.path.isfile(utility.addMissingSlash(args.monthsFolder) + utility.addMissingSlash(args.month) + "locked") and not args.ignoreLock:
	print "ERROR: The month " + args.month + " is being edited at the moment. Use -i if you want to force the execution of this script."
	sys.exit()


class CountValidityHandler:
	validCounter = defaultdict(int)

	def handle(self, sparqlQuery, processed):
		self.validCounter[processed['#Valid']] += 1

	def __str__(self):
		if 'VALID' in self.validCounter:
			validCount = self.validCounter['VALID']
			invalidCount = self.validCounter['INVALID']
		else:
			validCount = self.validCounter['1']
			invalidCount = self.validCounter['-1']
		return "Valid: \t\t" + str(validCount) + " " + str(
			float(validCount) / (
				validCount + invalidCount) * 100) + "%" + "\n" + \
			   "Invalid:\t" + str(invalidCount) + " " + str(float(invalidCount) / (
			validCount + invalidCount) * 100) + "%"


handler = CountValidityHandler()

processdata.processMonth(handler, args.month, args.monthsFolder)

print(handler)

@@ -70,21 +70,8 @@
 ict%0A
-%0Aimport sys%0A%0A
 from
@@ -129,16 +129,44 @@
  utility
+%0Aimport pprint%0Aimport config
 %0A%0Aparser
@@ -288,31 +288,48 @@
 ult=
-%22/a/akrausetud/months%22,
+config.monthsFolder,%0A                   
  typ
@@ -338,14 +338,9 @@
 str,
-%0A%09%09%09%09%09
+ 
 help
@@ -381,16 +381,41 @@
 irectory
+%22%0A                    + %22
  are res
@@ -463,16 +463,36 @@
 %22, %22-i%22,
+%0A                   
  help=%22I
@@ -530,16 +530,36 @@
 nyways%22,
+%0A                   
  action=
@@ -602,32 +602,52 @@
 onth%22, type=str,
+%0A                   
  help=%22the month
@@ -705,17 +705,20 @@
  == 0):%0A
-%09
+    
 parser.p
@@ -729,17 +729,20 @@
 _help()%0A
-%09
+    
 parser.e
@@ -836,16 +836,34 @@
 sFolder)
+%0A                 
  + utili
@@ -904,16 +904,21 @@
 locked%22)
+ %5C%0A  
  and not
@@ -939,15 +939,18 @@
 ck:%0A
-%09
+    
 print
- 
+(
 %22ERR
@@ -1012,16 +1012,31 @@
  moment.
+%22%0A          + %22
  Use -i 
@@ -1086,18 +1086,22 @@
 script.%22
-%0A%09
+)%0A    
 sys.exit
@@ -1133,17 +1133,20 @@
 andler:%0A
-%09
+    
 validCou
@@ -1170,17 +1170,20 @@
 t(int)%0A%0A
-%09
+    
 def hand
@@ -1216,18 +1216,24 @@
 essed):%0A
-%09%09
+        
 self.val
@@ -1269,17 +1269,20 @@
 %5D += 1%0A%0A
-%09
+    
 def __st
@@ -1296,473 +1296,56 @@
 f):%0A
-%09%09if 'VALID' in self.validCounter:%0A%09%09%09validCount = self.validCounter%5B'VALID'%5D%0A%09%09%09invalidCount = self.validCounter%5B'INVALID'%5D%0A%09%09else:%0A%09%09%09validCount = self.validCounter%5B'1'%5D%0A%09%09%09invalidCount = self.validCounter%5B'-1'%5D%0A%09%09return %22Valid: %5Ct%5Ct%22 + str(validCount) + %22 %22 + str(%0A%09%09%09float(validCount) / (%0A%09%09%09%09validCount + invalidCount) * 100) + %22%25%22 + %22%5Cn%22 + %5C%0A%09%09%09   %22Invalid:%5Ct%22 + str(invalidCount) + %22 %22 + str(float(invalidCount) / (%0A%09%09%09validCount + invalidCount) * 100) + %22%25%22
+        return pprint.pformat(self.validCounter)
 %0A%0A%0Ah

Update countValid