From d323e5f131d19c52d9cb9a2db7fc5ef6c8d57d50 Mon Sep 17 00:00:00 2001 From: Stephan Gabler Date: Wed, 18 Jul 2012 10:38:25 +0200 Subject: [PATCH] test for file path parsing --- jrnl/Journal.py | 22 +++++++++++++++++++++- tests/empty.txt | 0 tests/test_jrnl.py | 42 ++++++++++++++++++++++++++++++++++++++++++ tests/url_test.txt | 6 ++++++ 4 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 tests/empty.txt create mode 100644 tests/url_test.txt diff --git a/jrnl/Journal.py b/jrnl/Journal.py index d88cd3f9..a00dc502 100644 --- a/jrnl/Journal.py +++ b/jrnl/Journal.py @@ -15,7 +15,7 @@ import readline, glob try: from Crypto.Cipher import AES from Crypto.Random import random, atfork -except ImportError: +except ImportError: pass import hashlib import getpass @@ -23,11 +23,13 @@ try: import clint except ImportError: clint = None +import shutil class Journal: def __init__(self, config, **kwargs): config.update(kwargs) self.config = config + self.path_search = re.compile('[\[\(]?(([A-Z]:|/)\S*?\.(tif|tiff|jpg|jpeg|gif|png))[\[\)]?') # Set up date parser consts = pdc.Constants() @@ -233,6 +235,23 @@ class Journal: return date + def parse_for_images(self, body): + print 'in parse for images' + # TODO: parse also for name (like used in markdown [name](url)) + path_start = re.compile('[\[\(]?([A-Z]:|/).*?\.(tif|tiff|jpg|jpeg|gif|png)[\[\)]?') + for word in body.split(): + res = path_start.match(word) + if res and len(res.groups()) == 2 and os.path.exists(word.strip('()[]')): + word = word.strip('()[]') + print word + save_path = os.path.splitext(self.config['journal'])[0] + '_data' + if not os.path.exists(save_path): + os.mkdir(save_path) + new_path = os.path.join(save_path, os.path.basename(word)) + # TODO: replace name by hash + shutil.copyfile(word, new_path) + return body + def new_entry(self, raw, date=None): """Constructs a new entry from some raw text input. If a date is given, it will parse and use this, otherwise scan for a date in the input first.""" @@ -247,6 +266,7 @@ class Journal: title_end = sep_pos title = raw[:title_end+1] body = raw[title_end+1:].strip() + body = self.parse_for_images(body) if not date: if title.find(":") > 0: date = self.parse_date(title[:title.find(":")]) diff --git a/tests/empty.txt b/tests/empty.txt new file mode 100644 index 00000000..e69de29b diff --git a/tests/test_jrnl.py b/tests/test_jrnl.py index 8280fe6e..bca77bb0 100644 --- a/tests/test_jrnl.py +++ b/tests/test_jrnl.py @@ -2,6 +2,8 @@ # encoding: utf-8 import unittest +import os +from jrnl.Journal import Journal class TestClasses(unittest.TestCase): """Test the behavior of the classes. @@ -11,12 +13,52 @@ class TestClasses(unittest.TestCase): """ def setUp(self): + self.data_path = os.path.dirname(os.path.abspath(__file__)) + self.config = { + "timeformat": "%Y-%m-%d %H:%M", + "encrypt": False, + "tagsymbols": "@", + "journal": "" + } + + def tearDown(self): + # TODO: delete copied file, etc pass def test_colon_in_textbody(self): """colons should not cause problems in the text body""" pass + # def test_file_detection(self): + # self.config['journal'] = os.path.join(self.data_path, 'empty.txt') + # journal = Journal(config=self.config) + # with open(os.path.join(self.data_path, 'url_test.txt')) as f: + # journal.new_entry(f.read()) + # # TODO: check that copied file exists + + def test_rendering_md(self): + pass + + def test_rendering_htm(self): + pass + + def test_open_in_browser(self): + pass + + def test_pathsearch_regex(self): + true_positive = ['/Volumes/dedan/bla.png', + '/Users/dedan/projects/jrnl/tests/golden.jpg', + '/Volumes/dedan/test.png'] + false_positive = ['/Volumes/dedan/bla.blub', + 'http://en.wikipedia.org/wiki/Generative_model'] + self.config['journal'] = os.path.join(self.data_path, 'empty.txt') + journal = Journal(config=self.config) + with open(os.path.join(self.data_path, 'url_test.txt')) as f: + results = [res[0] for res in journal.path_search.findall(f.read())] + for tp in true_positive: + self.assertIn(tp, results) + for fp in false_positive: + self.assertNotIn(fp, results) class TestCLI(unittest.TestCase): """test the command-line interaction part of the program""" diff --git a/tests/url_test.txt b/tests/url_test.txt new file mode 100644 index 00000000..222ecd1b --- /dev/null +++ b/tests/url_test.txt @@ -0,0 +1,6 @@ +ein normalser satz. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, (/Volumes/dedan/bla.png +) sed do eiusmod tempor incididunt ut (http://en.wikipedia.org/wiki/Generative_model) labore et dolore magna aliqua. Ut enim ad minim veniam, (/Users/dedan/projects/jrnl/tests/golden.jpg) +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor http://www.dict.cc/?s=descendant in reprehenderit in voluptate velit esse /Volumes/dedan/test.png cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non (/Volumes/dedan/bla.blub) proident, sunt in culpa qui officia deserunt mollit anim id est laborum.