From 0b92c6ef9af6569e762aad2181aaea4d9ee49db1 Mon Sep 17 00:00:00 2001 From: Lakshmi Vyasarajan Date: Fri, 14 Jan 2011 16:00:59 +0530 Subject: [PATCH] Added time tests for fs --- hyde/fs.py | 46 +++++++++++++++++++++++++++++++++---------- hyde/tests/test_fs.py | 18 ++++++++++++++--- 2 files changed, 51 insertions(+), 13 deletions(-) diff --git a/hyde/fs.py b/hyde/fs.py index 3c6fbde..31cbf98 100644 --- a/hyde/fs.py +++ b/hyde/fs.py @@ -7,6 +7,7 @@ for common operations to provide a single interface. """ import codecs +from datetime import datetime import mimetypes import os import shutil @@ -202,6 +203,41 @@ class File(FS): break return False + @property + def is_text(self): + """Return true if this is a text file.""" + return (not self.is_binary) + + @property + def is_image(self): + """Return true if this is an image file.""" + return self.mimetype.split("/")[0] == "image" + + + @property + def last_modified(self): + """ + Returns a datetime object representing the last modified time. + Calls os.path.getmtime. + + """ + return datetime.fromtimestamp(os.path.getmtime(self.path)) + + def has_changed_since(self, basetime): + """ + Returns True if the file has been changed since the given time. + + """ + return self.last_modified > basetime + + def older_than(self, another_file): + """ + Checks if this file is older than the given file. Uses last_modified to + determine age. + + """ + return another_file.last_modified > self.last_modified + @staticmethod def make_temp(text): """ @@ -214,16 +250,6 @@ class File(FS): f.write(text) return f - @property - def is_text(self): - """Return true if this is a text file.""" - return (not self.is_binary) - - @property - def is_image(self): - """Return true if this is an image file.""" - return self.mimetype.split("/")[0] == "image" - def read_all(self, encoding='utf-8'): """ Reads from the file and returns the content as a string. diff --git a/hyde/tests/test_fs.py b/hyde/tests/test_fs.py index d0ac137..3c97073 100644 --- a/hyde/tests/test_fs.py +++ b/hyde/tests/test_fs.py @@ -56,6 +56,16 @@ def test_can_create_temp_file(): f.delete() assert not f.exists +def test_time_functions(): + f1 = File(__file__) + t1 = f1.last_modified + f2 = File.make_temp("I am new") + t2 = f2.last_modified + assert t1 < t2 + assert f2.has_changed_since(t1) + assert f1.older_than(f2) + + def test_path_expands_user(): f = File("~/abc/def") assert f.path == os.path.expanduser("~/abc/def") @@ -243,16 +253,18 @@ def test_move_folder(): DATA_JUNK = DATA_ROOT.child_folder('junk') assert not DATA_JUNK.exists JINJA2.copy_contents_to(DATA_JUNK) + assert DATA_JUNK.exists for f in [HELPERS, INDEX, LAYOUT]: assert File(DATA_JUNK.child(f.name)).exists - DATA_JUNK2 = DATA_ROOT.child_folder('junk2') - assert DATA_JUNK.exists + DATA_JUNK2 = DATA_ROOT.child_folder('second_junk') assert not DATA_JUNK2.exists DATA_JUNK.move_to(DATA_JUNK2) assert not DATA_JUNK.exists assert DATA_JUNK2.exists for f in [HELPERS, INDEX, LAYOUT]: - assert File(DATA_JUNK2.child_folder('junk').child(f.name)).exists + assert File(DATA_JUNK2.child_folder( + DATA_JUNK.name).child( + f.name)).exists @with_setup(setup_data, cleanup_data) def test_rename_folder():