summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorElena ``of Valhalla'' Grandi <valhalla@trueelena.org>2017-01-02 16:37:16 +0100
committerElena ``of Valhalla'' Grandi <valhalla@trueelena.org>2017-01-02 16:37:16 +0100
commitb2d8d7f2e936c4862ca5554bb5ffe853a65c04e7 (patch)
tree5285246e203c333cb7b201cc7c9a740296b66bea
parent76cc206a8735a22ba605ceb03d48e09984bb43fd (diff)
Move collection initialization to Collection
-rw-r--r--lesana/collection.py59
-rw-r--r--lesana/command.py57
-rw-r--r--tests/test_collection.py35
3 files changed, 108 insertions, 43 deletions
diff --git a/lesana/collection.py b/lesana/collection.py
index d1e651f..5693049 100644
--- a/lesana/collection.py
+++ b/lesana/collection.py
@@ -5,6 +5,12 @@ import uuid
import ruamel.yaml
import xapian
+try:
+ import git
+ git_available = True
+except ImportError:
+ git_available = False
+
class Entry(object):
def __init__(self, collection, data={}, fname=None):
@@ -222,3 +228,56 @@ class Collection(object):
enquire.set_query(query)
# FIXME: if more items are returned, something is wrong?
return self._match_to_entry(enquire.get_mset(0, 1)[0])
+
+ @classmethod
+ def init(cls, directory=None, git_enabled=True, edit_file=None):
+ """
+ Initialize a lesana repository
+
+ directory defaults to .
+ if git_enabled is True, git support is enabled and if possible a git
+ repository is initalized.
+ edit_file is a syncronous function that runs on a filename
+ (possibly opening the file in an editor) and should manage its
+ own errors.
+ """
+ c_dir = os.path.abspath(directory or '.')
+ if git_enabled:
+ # Try to initalize a git repo
+ if git_available:
+ repo = git.Repo.init(c_dir, bare=False)
+ else:
+ logging.warning("python3-git not available, could not initalise the git repository.")
+ repo = None
+ # Add .lesana directory to .gitignore and add it to the
+ # staging
+ lesana_ignored = False
+ try:
+ with open(os.path.join(c_dir, '.gitignore'), 'r') as fp:
+ for line in fp:
+ print("reading line", line)
+ if '.lesana' in line:
+ lesana_ignored = True
+ continue
+ except FileNotFoundError:
+ pass
+ if not lesana_ignored:
+ with open(os.path.join(c_dir, '.gitignore'), 'a') as fp:
+ fp.write('#Added by lesana init\n.lesana')
+ if repo:
+ repo.index.add(['.gitignore'])
+ # TODO: Add hook to index files as they are pulled
+ # If it doesn't exist, create a skeleton of settings.yaml file
+ # and then open it for editing
+ filepath = os.path.join(c_dir, 'settings.yaml')
+ if not os.path.exists(filepath):
+ # TODO: write this in a file and just copy that
+ with open(filepath, 'w') as fp:
+ fp.write('name: \n')
+ fp.write('lang: english\n')
+ fp.write('fields:\n')
+ if edit_file:
+ edit_file(filepath)
+ if git_enabled and repo:
+ repo.index.add(['settings.yaml'])
+ return cls(c_dir)
diff --git a/lesana/command.py b/lesana/command.py
index 706bbf3..513a5b2 100644
--- a/lesana/command.py
+++ b/lesana/command.py
@@ -12,6 +12,15 @@ import gadona
from . import Collection, Entry
+def edit_file_in_external_editor(filepath):
+ try:
+ subprocess.call(['sensible-editor', filepath])
+ except FileNotFoundError as e:
+ logging.warning(
+ "Could not open new file with editor: {}".format(str(e))
+ )
+
+
class New(gadona.Command):
name = 'new'
arguments = [
@@ -148,46 +157,8 @@ class Init(gadona.Command):
]
def main(self):
- c_dir = self.settings.collection
- if self.settings.git:
- # Try to initalize a git repo
- if git_available:
- repo = git.Repo.init(c_dir, bare=False)
- else:
- log.warning("python3-git not available, could not initalise the git repository.")
- repo = None
- # Add .lesana directory to .gitignore and add it to the
- # staging
- lesana_ignored = False
- try:
- with open(os.path.join(c_dir, '.gitignore'), 'r') as fp:
- for line in fp:
- print("reading line", line)
- if '.lesana' in line:
- lesana_ignored = True
- continue
- except FileNotFoundError:
- pass
- if not lesana_ignored:
- with open(os.path.join(c_dir, '.gitignore'), 'a') as fp:
- fp.write('#Added by lesana init\n.lesana')
- if repo:
- repo.index.add(['.gitignore'])
- # TODO: Add hook to index files as they are pulled
- # If it doesn't exist, create a skeleton of settings.yaml file
- # and then open it for editing
- filepath = os.path.join(c_dir, 'settings.yaml')
- if not os.path.exists(filepath):
- # TODO: write this in a file and just copy that
- with open(filepath, 'w') as fp:
- fp.write('name: \n')
- fp.write('lang: english\n')
- fp.write('fields:\n')
- try:
- subprocess.call(['sensible-editor', filepath])
- except FileNotFoundError as e:
- logging.warning(
- "Could not open new file with editor: {}".format(str(e))
- )
- if self.settings.git and repo:
- repo.index.add(['settings.yaml'])
+ Collection.init(
+ self.settings.collection,
+ git_enabled=self.settings.git,
+ edit_file=edit_file_in_external_editor
+ )
diff --git a/tests/test_collection.py b/tests/test_collection.py
index a55261f..9abb3f7 100644
--- a/tests/test_collection.py
+++ b/tests/test_collection.py
@@ -1,6 +1,7 @@
import logging
import os.path
import shutil
+import tempfile
import unittest
import ruamel.yaml
@@ -130,3 +131,37 @@ class testComplexCollection(unittest.TestCase):
def test_index(self):
self.collection.update_cache()
+
+class testCollectionCreation(unittest.TestCase):
+ def test_init(self):
+ tmpdir = tempfile.mkdtemp()
+ collection = lesana.Collection.init(tmpdir)
+ self.assertIsInstance(collection, lesana.Collection)
+ self.assertTrue(os.path.isdir(os.path.join(tmpdir, '.git')))
+ self.assertTrue(os.path.isdir(os.path.join(tmpdir, '.lesana')))
+ self.assertTrue(os.path.isfile(os.path.join(tmpdir, 'settings.yaml')))
+ self.assertTrue(os.path.isfile(os.path.join(tmpdir, '.gitignore')))
+ shutil.rmtree(tmpdir)
+
+ def do_nothing(*args, **kwargs):
+ pass
+
+ def test_init_edit_file(self):
+ tmpdir = tempfile.mkdtemp()
+ collection = lesana.Collection.init(tmpdir, edit_file=self.do_nothing)
+ self.assertIsInstance(collection, lesana.Collection)
+ self.assertTrue(os.path.isdir(os.path.join(tmpdir, '.git')))
+ self.assertTrue(os.path.isdir(os.path.join(tmpdir, '.lesana')))
+ self.assertTrue(os.path.isfile(os.path.join(tmpdir, 'settings.yaml')))
+ self.assertTrue(os.path.isfile(os.path.join(tmpdir, '.gitignore')))
+ shutil.rmtree(tmpdir)
+
+ def test_init_no_git(self):
+ tmpdir = tempfile.mkdtemp()
+ collection = lesana.Collection.init(tmpdir, git_enabled=False)
+ self.assertIsInstance(collection, lesana.Collection)
+ self.assertFalse(os.path.isdir(os.path.join(tmpdir, '.git')))
+ self.assertTrue(os.path.isdir(os.path.join(tmpdir, '.lesana')))
+ self.assertTrue(os.path.isfile(os.path.join(tmpdir, 'settings.yaml')))
+ self.assertFalse(os.path.isfile(os.path.join(tmpdir, '.gitignore')))
+ shutil.rmtree(tmpdir)