Permalink
Browse files

refactoring -> own class for reading files/urls ...

Signed-off-by: Armin Wieser <armin.wieser@gmail.com>
  • Loading branch information...
1 parent 8580c02 commit e5050f79d4d916f59a4cb442939ac469970971db @awieser awieser committed Dec 22, 2011
@@ -14,6 +14,7 @@
from common.memacs import Memacs
from common.orgformat import OrgFormat
from common.orgproperty import OrgProperties
+from common.reader import CommonReader
try:
@@ -209,9 +210,9 @@ def __handle_vevent(self, component):
def _main(self):
# getting data
if self._args.calendar_file:
- data = self.__read_file(self._args.calendar_file)
+ data = CommonReader.get_data_from_file(self._args.calendar_file)
elif self._args.calendar_url:
- data = self.__read_url(self._args.calendar_url)
+ data = CommonReader.get_data_from_url(self._args.calendar_url)
# read and go through calendar
cal = Calendar.from_string(data)
View
@@ -0,0 +1,55 @@
+# -*- coding: utf-8 -*-
+# Time-stamp: <2011-12-19 15:13:31 aw>
+
+import codecs
+import logging
+import sys
+from urllib2 import urlopen
+from urllib2 import HTTPError
+from urllib2 import URLError
+
+
+class CommonReader:
+ """
+ Class for reading
+ * files
+ * url's
+ """
+
+ @staticmethod
+ def get_data_from_file(path):
+ """
+ reads a file
+
+ @param file: path to file
+ @return: returns data
+ """
+ try:
+ input_file = codecs.open(path, 'rb')
+ data = input_file.read()
+ input_file.close()
+ return data
+ except IOError:
+ logging.error("Error at opening file: %s" % path)
+ sys.exit(1)
+
+ @staticmethod
+ def get_data_from_url(self, url):
+ """
+ reads from a url
+
+ @param url: url to read
+ @returns: returns data
+ """
+ try:
+ req = urlopen(self._args.calendar_url, None, 10)
+ return req.read()
+ except HTTPError:
+ logging.error("ValueError: " % url)
+ sys.exit(1)
+ except URLError:
+ logging.error("ValueError: " % url)
+ sys.exit(1)
+ except ValueError:
+ logging.error("ValueError: %s" % url)
+ sys.exit(1)
@@ -0,0 +1,19 @@
+import unittest
+import sys
+import os
+sys.path.append(
+ os.path.dirname(
+ os.path.dirname(
+ os.path.dirname(os.path.abspath(__file__)))))
+from common.reader import CommonReader
+
+
+class TestArgParser(unittest.TestCase):
+
+ def test_file_no_path(self):
+ try:
+ CommonReader.get_data_from_file("")
+ self.assertTrue(False, "false path failed")
+ except SystemExit:
+ pass
+
File renamed without changes.

0 comments on commit e5050f7

Please sign in to comment.