aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorElena ``of Valhalla'' Grandi <valhalla@trueelena.org>2017-04-22 19:18:41 +0200
committerElena ``of Valhalla'' Grandi <valhalla@trueelena.org>2017-05-20 11:51:29 +0200
commit9b5d3da213ce261a98109c90160929c70f0a4d04 (patch)
tree1784a02156d7002b333d4c0e83be9edf317d8ab9
parentf6efcd152635aa6655538c7fa316cfc80b6701e9 (diff)
Pass settings to Collection.init
-rw-r--r--lesana/collection.py10
-rw-r--r--tests/test_collection.py19
2 files changed, 28 insertions, 1 deletions
diff --git a/lesana/collection.py b/lesana/collection.py
index 1f488bc..5b80d3b 100644
--- a/lesana/collection.py
+++ b/lesana/collection.py
@@ -304,7 +304,13 @@ class Collection(object):
cache.close()
@classmethod
- def init(cls, directory=None, git_enabled=True, edit_file=None):
+ def init(
+ cls,
+ directory=None,
+ git_enabled=True,
+ edit_file=None,
+ settings={}
+ ):
"""
Initialize a lesana repository
@@ -316,6 +322,7 @@ class Collection(object):
own errors.
"""
c_dir = os.path.abspath(directory or '.')
+ os.makedirs(c_dir, exist_ok=True)
if git_enabled:
# Try to initalize a git repo
if git_available:
@@ -351,6 +358,7 @@ class Collection(object):
).decode('utf-8')
skel_dict = ruamel.yaml.load(skel, ruamel.yaml.RoundTripLoader)
skel_dict['git'] = git_enabled
+ skel_dict.update(settings)
with open(filepath, 'w') as fp:
ruamel.yaml.dump(
skel_dict,
diff --git a/tests/test_collection.py b/tests/test_collection.py
index e17e044..bcd45e7 100644
--- a/tests/test_collection.py
+++ b/tests/test_collection.py
@@ -298,6 +298,25 @@ class testCollectionCreation(unittest.TestCase):
self.assertTrue(self._find_file_in_git_index(fname, repo.index))
shutil.rmtree(tmpdir)
+ def test_init_custom_settings(self):
+ tmpdir = tempfile.mkdtemp()
+ collection = lesana.Collection.init(
+ tmpdir,
+ edit_file=self.do_nothing,
+ settings={
+ 'name': 'A different name',
+ 'fields': [
+ {'name': 'title', 'type': 'string'},
+ {'name': 'author', 'type': 'string'},
+ ],
+ },
+ )
+ self.assertIsInstance(collection, lesana.Collection)
+ self.assertTrue(os.path.isfile(os.path.join(tmpdir, 'settings.yaml')))
+ self.assertEqual(collection.settings['name'], 'A different name')
+ self.assertEqual(len(collection.settings['fields']), 2)
+ shutil.rmtree(tmpdir)
+
if __name__ == '__main__':
unittest.main()