Skip to content
Browse files

Add Path.newfile() and Path.newdir()

  • Loading branch information...
1 parent a0e8143 commit e9eb518846e95bc51f607a8f0fc0cdb9b341caf5 @davidmiller committed
Showing with 165 additions and 21 deletions.
  1. +7 −3 HISTORY.rst
  2. +6 −0 Rakefile
  3. +1 −1 ffs/_version.py
  4. +29 −0 ffs/contrib/mold.py
  5. +51 −0 ffs/path.py
  6. +25 −0 test/contrib/test_mold.py
  7. +46 −17 test/test_path.py
View
10 HISTORY.rst
@@ -1,11 +1,16 @@
History
-------
+0.0.7.3 (Nov 19 2012)
++++++++++++++++++++++
+
+Add Path.newfile() and Path.newdir() - non destructive versions of the
+contextmanager tempfile() && tempath()
+
0.0.7.2 (Nov 14 2012)
+++++++++++++++++++++
Add __version__ to main module
-
0.0.7.1 (Oct 12 2012)
+++++++++++++++++++++
@@ -20,11 +25,10 @@ relative to the parent.
Expose Path.decode (Useful when using Paths as Django template directories)
-
0.0.6 (Oct 09 2012)
+++++++++++++++++++
-Add ffs.contrib.cast for templating helpers.
+Add ffs.contrib.mold for templating helpers.
0.0.5 (Sep 22 2012)
+++++++++++++++++++
View
6 Rakefile
@@ -2,6 +2,12 @@
PROJ = "ffs"
+task :coverage, :python do |t, args|
+ p "Running coverage check for #{PROJ}"
+ args.with_defaults :python => "python"
+ sh "#{args[:python]} -m pytest --cov=ffs test"
+end
+
task :test, :python do |t, args|
p "Running unit tests for #{PROJ}"
args.with_defaults :python => "python"
View
2 ffs/_version.py
@@ -1 +1 @@
-__version__ = "0.0.7.2"
+__version__ = "0.0.7.3"
View
29 ffs/contrib/mold.py
@@ -0,0 +1,29 @@
+"""
+ffs.contrib.mold
+
+Useful for easy template rendering
+"""
+
+import jinja2
+
+__all__ = [
+ 'cast'
+ ]
+
+def cast(template, **context):
+ """
+ Render TEMPLATE using **CONTEXT.
+
+ Assume TEMPLATE is a Jinja2 template.
+
+
+ Arguments:
+ - `template`: Path
+ - `**context`: dict
+
+ Return: unicode
+ Exceptions: None
+ """
+ tpl = jinja2.Template(template.contents)
+ return tpl.render(**context)
+
View
51 ffs/path.py
@@ -664,6 +664,57 @@ def temp(klass):
finally:
fs.rm(tmpath, recursive=True)
+ @classmethod
+ @contextlib.contextmanager
+ def tempfile(klass):
+ """
+ Create a temporary file.
+
+ If this is called as a contextmanager, delete
+ the file on exiting the block, else leave it to the
+ user to delete as appropriate.
+
+ Return: Path
+ Exceptions: None
+ """
+ fs = klass.fsflavour()
+ tmpath = fs.tempfile()
+ pth = klass(tmpath)
+ pth.touch()
+ try:
+ yield pth
+ finally:
+ fs.rm(tmpath)
+ return
+
+ @classmethod
+ def newdir(klass):
+ """
+ Create a directory that previously did not exist.
+ (Typically in the system's temp dir)
+
+ Return: klass()
+ Exceptions: None
+ """
+ fs = klass.fsflavour()
+ tmpath = fs.tempdir()
+ return klass(tmpath)
+
+ @classmethod
+ def newfile(klass):
+ """
+ Create a file that previously did not exist.
+ (typically in the system's temp dir)
+
+ Return: klass()
+ Exceptions: None
+ """
+ fs = klass.fsflavour()
+ tmpfile = fs.tempfile()
+ pth = klass(tmpfile)
+ pth.touch()
+ return pth
+
def touch(self, *args):
"""
Equivalent to calling the *nix command touch on SELF.
View
25 test/contrib/test_mold.py
@@ -0,0 +1,25 @@
+"""
+Unittests for the ffs.contrib.mold module
+"""
+import sys
+import unittest
+
+from mock import MagicMock, patch
+
+if sys.version_info < (2, 7): import unittest2 as unittest
+
+from ffs.contrib import mold
+
+class CastTestCase(unittest.TestCase):
+
+ def test_renderit(self):
+ "Render a template"
+ tpl = MagicMock(name='Mock template')
+ tpl.contents = '{{one}}:{{two}}'
+ res = mold.cast(tpl, one=1, two=2)
+ self.assertEqual('1:2', res)
+
+
+if __name__ == '__main__':
+ unittest.main()
+
View
63 test/test_path.py
@@ -371,23 +371,6 @@ def test_open_mkpath(self):
self.assertEqual(filename, fh.name)
pass
- def test_temp_contextmanager(self):
- "should yeild a path that exists"
- with Path.temp() as p:
- val = str(p)
- self.assertTrue(os.path.exists(val))
- self.assertTrue(os.path.isdir(val))
- self.assertFalse(os.path.isdir(val))
- self.assertFalse(os.path.exists(val))
-
- def test_with_tmp_contents(self):
- "Should kill directory contents"
- with Path.temp() as p:
- val = str(p)
- touch(p + 'my.txt')
- self.assertTrue(os.path.exists(str(p + 'my.txt')))
- self.assertFalse(os.path.exists(val))
-
class PropertiesTestCase(PathTestCase):
"Properties of instances"
@@ -671,6 +654,52 @@ def test_mv_nonexistent(self):
with self.assertRaises(exceptions.DoesNotExistError):
p.mv(self.tdir)
+class TemporaryTestCase(unittest.TestCase):
+ def setUp(self):
+ pass
+
+ def tearDown(self):
+ pass
+
+ def test_temp_contextmanager(self):
+ "should yeild a path that exists"
+ with Path.temp() as p:
+ val = str(p)
+ self.assertTrue(os.path.exists(val))
+ self.assertTrue(os.path.isdir(val))
+ self.assertFalse(os.path.isdir(val))
+ self.assertFalse(os.path.exists(val))
+
+ def test_with_tmp_contents(self):
+ "Should kill directory contents"
+ with Path.temp() as p:
+ val = str(p)
+ touch(p + 'my.txt')
+ self.assertTrue(os.path.exists(str(p + 'my.txt')))
+ self.assertFalse(os.path.exists(val))
+
+ def test_tmp_function(self):
+ "Should leave the tempdir"
+ p = Path.newdir()
+ self.assertTrue(os.path.exists(p))
+ self.assertTrue(os.path.isdir(p))
+
+ def test_tempfile_contextmanager(self):
+ "should yeild a file that exists"
+ with Path.tempfile() as p:
+ val = str(p)
+ self.assertTrue(os.path.exists(val))
+ self.assertTrue(os.path.isfile(val))
+ self.assertFalse(os.path.isfile(val))
+ self.assertFalse(os.path.exists(val))
+
+ def test_tmpfile_function(self):
+ "Should leave the tempdir"
+ p = Path.newfile()
+ self.assertTrue(os.path.exists(p))
+ self.assertTrue(os.path.isfile(p))
+
+
class JsonishTestCase(unittest.TestCase):
"Tests for the JSON operations"
def setUp(self):

0 comments on commit e9eb518

Please sign in to comment.
Something went wrong with that request. Please try again.