[tor-commits] [ooni-probe/master] Open YAML report in createReport

isis at torproject.org isis at torproject.org
Sun Mar 10 01:57:02 UTC 2013


commit f94ad6bc7a01b78c3b6a63f8ad965bb5dea23f07
Author: aagbsn <aagbsn at extc.org>
Date:   Thu Feb 28 15:57:23 2013 +0100

    Open YAML report in createReport
    
    Also adds reporter exceptions ReportAlreadyClosed and ReportNotCreated
---
 ooni/errors.py   |    6 ++++++
 ooni/reporter.py |   12 +++++++++---
 2 files changed, 15 insertions(+), 3 deletions(-)

diff --git a/ooni/errors.py b/ooni/errors.py
index 2023dbf..36a042f 100644
--- a/ooni/errors.py
+++ b/ooni/errors.py
@@ -126,3 +126,9 @@ class InvalidOONIBCollectorAddress(Exception):
 class AllReportersFailed(Exception):
     pass
 
+class ReportNotCreated(Exception):
+    pass
+
+class ReportAlreadyClosed(Exception):
+    pass
+
diff --git a/ooni/reporter.py b/ooni/reporter.py
index 69d4892..c01e904 100644
--- a/ooni/reporter.py
+++ b/ooni/reporter.py
@@ -27,6 +27,7 @@ except ImportError:
 
 
 from ooni.errors import InvalidOONIBCollectorAddress
+from ooni.errors import ReportNotCreated, ReportAlreadyClosed
 
 from ooni import otime
 from ooni.utils import geodata, pushFilenameStack
@@ -184,15 +185,17 @@ class YAMLReporter(OReporter):
             log.msg("Report already exists with filename %s" % report_path)
             pushFilenameStack(report_path)
 
-        log.debug("Creating %s" % report_path)
-        self._stream = open(report_path, 'w+')
-
+        self.report_path = report_path
         OReporter.__init__(self, test_details)
 
     def _writeln(self, line):
         self._write("%s\n" % line)
 
     def _write(self, format_string, *args):
+        if not self._stream:
+            raise ReportNotCreated
+        if self._stream.closed:
+            raise ReportAlreadyClosed
         s = str(format_string)
         assert isinstance(s, type(''))
         if args:
@@ -214,6 +217,9 @@ class YAMLReporter(OReporter):
         """
         Writes the report header and fire callbacks on self.created
         """
+        log.debug("Creating %s" % self.report_path)
+        self._stream = open(self.report_path, 'w+')
+
         self._writeln("###########################################")
 
         self._writeln("# OONI Probe Report for %s (%s)" % (self.testDetails['test_name'],





More information about the tor-commits mailing list