Browse Source

Added time tests for fs

main
Lakshmi Vyasarajan 14 years ago
parent
commit
0b92c6ef9a
2 changed files with 51 additions and 13 deletions
  1. +36
    -10
      hyde/fs.py
  2. +15
    -3
      hyde/tests/test_fs.py

+ 36
- 10
hyde/fs.py View File

@@ -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.


+ 15
- 3
hyde/tests/test_fs.py View File

@@ -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():


Loading…
Cancel
Save