summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--lps_gen.py42
-rw-r--r--tests/test_lps_gen.py79
2 files changed, 120 insertions, 1 deletions
diff --git a/lps_gen.py b/lps_gen.py
index 69ec4c8..44fb5a6 100644
--- a/lps_gen.py
+++ b/lps_gen.py
@@ -31,7 +31,7 @@ from jinja2.exceptions import TemplateNotFound
from mistune import Renderer, Markdown
from unidecode import unidecode
-__version__ = '0.2.0'
+__version__ = '0.3.0.dev1'
# unicode magic
reload(sys)
@@ -61,9 +61,49 @@ def read_file(filename):
return content
+def write_file(filename, filecontent):
+ """Write `filecontent` to `filename`.
+
+ :param str filename:
+ Absolute pathname of the file.
+ :param str filecontent:
+ Data to write to `filename`.
+
+ """
+ file_ = None
+ try:
+ file_ = open(filename, 'wb')
+ file_.write(filecontent)
+ file_.close()
+ except IOError:
+ print "Error creating and writing content to %s" % filename
+ exit(1)
+
+
+
+def json_write(filename, obj):
+ """Serialize `obj` to JSON formatted `str` to `filename`.
+
+ `filename` is written relative to the current working directory.
+
+ """
+ write_file(filename, json.dumps(obj, ensure_ascii=False, indent=4))
+
+
+def json_read(filename):
+ """Deserialize JSON formatted `str` from `filename` into Python object.
+ """
+ if not path.isfile(filename):
+ return False
+
+ return json.loads(read_file(filename),
+ object_pairs_hook=OrderedDict)
+
+
class LPSRenderer(Renderer):
"""Helps in converting Markdown version of LP schedule to a dictionary.
"""
+
def __init__(self, **kwargs):
super(LPSRenderer, self).__init__(**kwargs)
self.last_day = None
diff --git a/tests/test_lps_gen.py b/tests/test_lps_gen.py
index ad66aa9..00b21ef 100644
--- a/tests/test_lps_gen.py
+++ b/tests/test_lps_gen.py
@@ -19,11 +19,13 @@
# <http://www.gnu.org/licenses/>.
import json
+import os
import pprint
import mistune
import mock
+from collections import OrderedDict
from os import path
from StringIO import StringIO
@@ -31,6 +33,83 @@ from nose.tools import *
from lps_gen import *
+
+class TestJSONUtils(object):
+ """Class that tests json utils in `lps_gen` module.
+ """
+ @classmethod
+ def setup_class(self):
+ """Runs before running any tests in this class."""
+ self.speakers_ids = OrderedDict({
+ unicode('Daniel Kahn Gillmor'): 'gillmor',
+ unicode('Edward Snowden'): 'snowden',
+ unicode('Richard Stallman'): 'stallman',
+ unicode('Clara Snowden'): 'clara_snowden',
+ unicode('Ludovic Courtès'): 'courtes',
+ unicode('Jonas Öberg'): 'aberg',
+ })
+ self.ids_filename = 'speakers.ids'
+
+ self.speakers_noids = [
+ unicode('Daniel Kahn Gillmor'),
+ unicode('Richard Stallman'),
+ unicode('Ludovic Courtès'),
+ unicode('Jonas Öberg'),
+ ]
+ self.noids_filename = 'speakers.noids'
+
+ # Change current working directory to the tests directory.
+ self.old_cwd = os.getcwd()
+ os.chdir('tests')
+
+
+ def setup(self):
+ """Runs before each test in this class."""
+ pass
+
+
+ def test_json_write(self):
+ """Testing json_write function."""
+ json_write(self.ids_filename, self.speakers_ids)
+ assert_equal(json.loads(read_file(self.ids_filename),
+ object_pairs_hook=OrderedDict),
+ self.speakers_ids)
+
+ json_write(self.noids_filename, self.speakers_noids)
+ assert_equal(json.loads(read_file(self.noids_filename),
+ object_pairs_hook=OrderedDict),
+ self.speakers_noids)
+
+
+ def test_json_read(self):
+ """Testing json_read function."""
+ write_file(self.ids_filename, json.dumps(self.speakers_ids,
+ indent=4))
+ assert_equal(json_read(self.ids_filename), self.speakers_ids)
+
+ write_file(self.noids_filename, json.dumps(self.speakers_noids,
+ indent=4))
+ assert_equal(json_read(self.noids_filename), self.speakers_noids)
+
+
+ def teardown(self):
+ """Cleans up things after each test in this class."""
+ # Remove `speaker.ids` file if it exists.
+ if path.isfile(self.ids_filename):
+ os.remove(self.ids_filename)
+
+ # Remove `speaker.noids` file if it exists.
+ if path.isfile(self.noids_filename):
+ os.remove(self.noids_filename)
+
+
+ @classmethod
+ def teardown_class(self):
+ """Purge the mess created by this test."""
+ # Change back to the old cwd
+ os.chdir(self.old_cwd)
+
+
class TestLPS(object):
"""
Class that tests everything related LP Schedule.