Permalink
Browse files

Startet with autotagging option -> orgwriter part finished

Signed-off-by: Armin Wieser <armin.wieser@gmail.com>
  • Loading branch information...
1 parent 2c1423c commit 71c954f9d8bd96357d386449c8ccb5cbf761acea @awieser awieser committed Apr 16, 2012
Showing with 84 additions and 6 deletions.
  1. +44 −5 memacs/lib/orgwriter.py
  2. +40 −1 memacs/lib/tests/orgwriter_test.py
View
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
-# Time-stamp: <2011-12-30 12:01:41 armin>
+# Time-stamp: <2012-04-16 20:13:03 armin>
import codecs
import sys
@@ -28,7 +28,8 @@ def __init__(self,
tag,
file_name=None,
test=False,
- append=False):
+ append=False,
+ autotag_dict={}):
"""
@param file_name:
"""
@@ -40,6 +41,9 @@ def __init__(self,
self.__tag = tag
self.__file_name = file_name
self.__existing_ids = []
+ self.__autotag_dict=autotag_dict
+
+ self.__lower_autotag_dict()
if file_name:
if append and os.path.exists(file_name):
@@ -149,7 +153,7 @@ def write_org_subitem(self,
output,
note="",
properties=OrgProperties(),
- tags=[]):
+ tags=None):
"""
Writes an org item line.
@@ -164,16 +168,22 @@ def write_org_subitem(self,
written to file
@param timestamp: str/unicode
- @param output: str/unicode
+ @param output: st tar/unicode
@param note: str/unicode
@param tags: list of tags
@param properties: OrgProperties object
"""
assert (timestamp.__class__ == str or timestamp.__class__ == unicode)
- assert tags.__class__ == list
+ assert tags.__class__ == list or tags == None
assert properties.__class__ == OrgProperties
assert (output.__class__ == str or output.__class__ == unicode)
assert (note.__class__ == str or note.__class__ == unicode)
+
+ if tags == None:
+ tags = []
+
+ if self.__autotag_dict != {}:
+ self.__get_autotags(tags, output)
if self.__append:
self.__append_org_subitem(timestamp,
@@ -243,3 +253,32 @@ def close(self):
self.__write_footer()
if self.__handler != None:
self.__handler.close()
+
+ def __lower_autotag_dict(self):
+ """
+ lowers all values of dict
+ """
+ for tag in self.__autotag_dict.iterkeys():
+ values = []
+
+ for value in self.__autotag_dict[tag]:
+ values.append(value.lower())
+
+ self.__autotag_dict[tag] = values
+
+ def __get_autotags(self, tags, string):
+ """
+ Searches for tags in a given wordlist.
+ Append them to tags
+
+ @param tags: list to append the matched tags
+ @param string: string to look for matching values
+ """
+ string = string.lower()
+
+ for autotag_tag in self.__autotag_dict.iterkeys():
+ for matching_word in self.__autotag_dict[autotag_tag]:
+ if matching_word in string:
+ if autotag_tag not in tags:
+ tags.append(autotag_tag)
+ continue
@@ -1,6 +1,6 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
-# Time-stamp: <2011-10-26 22:13:31 awieser>
+# Time-stamp: <2012-04-16 20:13:26 armin>
import unittest
import os
@@ -35,6 +35,7 @@ def test_ouput_to_file(self):
writer.write_comment("abc\n")
writer.write_commentln("abc")
writer.write_org_item("begin")
+
timestamp = OrgFormat.datetime(time.gmtime(0))
writer.write_org_subitem(timestamp=timestamp,
output="sub",
@@ -104,6 +105,7 @@ def test_ouput_to_file(self):
data[16],
" :END:\n")
#cleaning up
+ file_handler.close()
os.remove(self.TMPFOLDER + "testfile.org")
def test_utf8(self):
@@ -117,12 +119,49 @@ def test_utf8(self):
# read and check the file_handler
file_handler = codecs.open(test_filename, "r", "utf-8")
input_handler = file_handler.readlines()
+ file_handler.close()
self.assertEqual(input_handler[4],
u"☁☂☃☄★☆☇☈☉☊☋☌☍☎☏☐☑☒☓☔☕☖☗♞♟♠♡♢♣♤♥♦♧♨♩♪♫♬♭♮♯♰♱♲♳♴♵\n",
"utf-8 failure")
#cleaning up
+
os.remove(self.TMPFOLDER + "testutf8.org")
+
+
+ def test_autotag(self):
+ test_filename = self.TMPFOLDER + "testautotag.org"
+
+ autotag_dict = {}
+ autotag_dict["TUG"] = ["tugraz", "university"]
+ autotag_dict["programming"] = ["programming", "python", "java"]
+
+ output = "Programming for my bachelor thesis at University"
+
+ # writing test output
+ writer = OrgOutputWriter(short_description="short-des",
+ tag="tag",
+ file_name=test_filename,
+ autotag_dict=autotag_dict)
+ timestamp = OrgFormat.datetime(time.gmtime(0))
+
+ properties = OrgProperties("data_for_hashing")
+
+ writer.write_org_subitem(timestamp=timestamp,
+ output=output,
+ properties=properties)
+ writer.close()
+
+ # read and check the file_handler
+ file_handler = codecs.open(test_filename, "r", "utf-8")
+ input_handler = file_handler.readlines()
+ file_handler.close()
+
+ self.assertEqual(input_handler[4],
+ u"** <1970-01-01 Thu 00:00> Programming for my bachelor thesis at University\t:programming:TUG:\n")
+
+ #cleaning up
+ os.remove(self.TMPFOLDER + "testautotag.org")
if __name__ == '__main__':
unittest.main()

0 comments on commit 71c954f

Please sign in to comment.