From d4d4120ed6362e15f55d83525515c959713ff4ba Mon Sep 17 00:00:00 2001 From: Elena ``of Valhalla'' Grandi Date: Sat, 4 Mar 2017 18:20:16 +0100 Subject: Save in the settings whether git support should be enabled --- lesana/collection.py | 8 +++++++- lesana/data/settings.yaml | 2 ++ tests/test_collection.py | 5 +++++ 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/lesana/collection.py b/lesana/collection.py index 92ffcbb..8be3ed0 100644 --- a/lesana/collection.py +++ b/lesana/collection.py @@ -328,8 +328,14 @@ class Collection(object): skel = resource_string( 'lesana', 'data/settings.yaml' ).decode('utf-8') + skel_dict = ruamel.yaml.load(skel, ruamel.yaml.RoundTripLoader) + skel_dict['git'] = git_enabled with open(filepath, 'w') as fp: - fp.write(skel) + ruamel.yaml.dump( + skel_dict, + stream=fp, + Dumper=ruamel.yaml.RoundTripDumper + ) if edit_file: edit_file(filepath) if git_enabled and repo: diff --git a/lesana/data/settings.yaml b/lesana/data/settings.yaml index ea83e8a..31653d0 100644 --- a/lesana/data/settings.yaml +++ b/lesana/data/settings.yaml @@ -1,5 +1,7 @@ name: 'My Collection' lang: english +entry_label: '{{ uid }}: {{ name }}' +git: true fields: - name: name type: string diff --git a/tests/test_collection.py b/tests/test_collection.py index a197237..265db09 100644 --- a/tests/test_collection.py +++ b/tests/test_collection.py @@ -203,9 +203,12 @@ class testCollectionCreation(unittest.TestCase): 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'))) + created = lesana.Collection(tmpdir) + self.assertTrue(created.settings['git']) shutil.rmtree(tmpdir) def do_nothing(*args, **kwargs): + # A function that does nothing instead of editing a file pass def test_init_edit_file(self): @@ -233,6 +236,8 @@ class testCollectionCreation(unittest.TestCase): 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'))) + created = lesana.Collection(tmpdir) + self.assertFalse(created.settings['git']) shutil.rmtree(tmpdir) def test_deletion(self): -- cgit v1.2.3 From 2e1e99bdb18368de7cb30623c4381baa076f4307 Mon Sep 17 00:00:00 2001 From: Elena ``of Valhalla'' Grandi Date: Sat, 4 Mar 2017 19:10:37 +0100 Subject: Auto add edited files to git --- TODO.rst | 1 - lesana/collection.py | 21 +++++++++++++++++++++ lesana/command.py | 14 ++++++++++++++ tests/test_collection.py | 34 ++++++++++++++++++++++++++++++++++ 4 files changed, 69 insertions(+), 1 deletion(-) diff --git a/TODO.rst b/TODO.rst index 0dbaeac..f8256c8 100644 --- a/TODO.rst +++ b/TODO.rst @@ -1,7 +1,6 @@ The following features are already planned. * Allow to identify entries with just the beginning of the uid. -* Auto add edited file to git (but don't commit). * Write some infrastructure to make it easy to write import scripts from other formats (and write an import script from tellico to lesana). diff --git a/lesana/collection.py b/lesana/collection.py index 8be3ed0..a4b9c63 100644 --- a/lesana/collection.py +++ b/lesana/collection.py @@ -193,6 +193,27 @@ class Collection(object): with open(complete_name, 'w') as fp: fp.write(e.yaml_data) + def git_add_files(self, files=[]): + if not git_available: + logging.warning( + "python3-git not available, could not initalise " + + "the git repository.") + return False + if not self.settings.get('git', False): + logging.info( + "This collection is configured not to use git" + ) + return False + try: + repo = git.Repo(self.basedir) + except git.exc.InvalidGitRepositoryError as e: + logging.warning( + "Could not find a git repository in {}".format( + self.basedir)) + return False + repo.index.add(files) + return True + def _get_cache(self): try: cache = xapian.Database( diff --git a/lesana/command.py b/lesana/command.py index 56c855a..41448b3 100644 --- a/lesana/command.py +++ b/lesana/command.py @@ -64,6 +64,11 @@ class New(guacamole.Command): (['--collection', '-c'], dict( help='The collection to work on (default .)' )), + (['--no-git'], dict( + help="Don't add the new entry to git", + action="store_false", + dest='git' + )), ] def register_arguments(self, parser): @@ -80,6 +85,8 @@ class New(guacamole.Command): ) if edit_file_in_external_editor(filepath): collection.update_cache([filepath]) + if ctx.args.git: + collection.git_add_files([filepath]) print(new_entry) @@ -88,6 +95,11 @@ class Edit(guacamole.Command): (['--collection', '-c'], dict( help='The collection to work on (default .)' )), + (['--no-git'], dict( + help="Don't add the new entry to git", + action="store_false", + dest='git' + )), (['uid'], dict( help='uid of an entry to edit', )), @@ -108,6 +120,8 @@ class Edit(guacamole.Command): ) if edit_file_in_external_editor(filepath): collection.update_cache([filepath]) + if ctx.args.git: + collection.git_add_files([filepath]) print(entry) diff --git a/tests/test_collection.py b/tests/test_collection.py index 265db09..e17e044 100644 --- a/tests/test_collection.py +++ b/tests/test_collection.py @@ -4,6 +4,7 @@ import shutil import tempfile import unittest +import git import ruamel.yaml import lesana @@ -264,6 +265,39 @@ class testCollectionCreation(unittest.TestCase): mset = collection._enquire.get_mset(0, 10) self.assertEqual(mset.get_matches_estimated(), 0) + def _find_file_in_git_index(self, fname, index): + found = False + for (path, stage) in index.entries: + if fname in path: + found = True + break + return found + + def test_git_adding(self): + tmpdir = tempfile.mkdtemp() + shutil.copy('tests/data/simple/settings.yaml', tmpdir) + shutil.copytree( + 'tests/data/simple/items', + os.path.join(tmpdir, 'items'), + ) + collection = lesana.Collection.init(tmpdir) + fname = '11189ee47ddf4796b718a483b379f976.yaml' + repo = git.Repo(tmpdir) + # By default, this collection doesn't have any git entry in the + # settings (but there is a repo) + collection.git_add_files([os.path.join(collection.itemdir, fname)]) + self.assertFalse(self._find_file_in_git_index(fname, repo.index)) + # Then we set it to false + collection.settings['git'] = False + collection.git_add_files([os.path.join(collection.itemdir, fname)]) + self.assertFalse(self._find_file_in_git_index(fname, repo.index)) + # And only when it's set to true we should find the file in the + # staging area + collection.settings['git'] = True + collection.git_add_files([os.path.join(collection.itemdir, fname)]) + self.assertTrue(self._find_file_in_git_index(fname, repo.index)) + shutil.rmtree(tmpdir) + if __name__ == '__main__': unittest.main() -- cgit v1.2.3 From f9788793d0b0d384eef3725b13ab3134c1cc46c0 Mon Sep 17 00:00:00 2001 From: Elena ``of Valhalla'' Grandi Date: Sat, 4 Mar 2017 19:13:53 +0100 Subject: pep8 cleanup --- lesana/collection.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lesana/collection.py b/lesana/collection.py index a4b9c63..1f488bc 100644 --- a/lesana/collection.py +++ b/lesana/collection.py @@ -295,7 +295,7 @@ class Collection(object): ) for uid in uids: entry = self.entry_from_uid(uid) - if not entry is None: + if entry is not None: cache.delete_document(entry.idterm) os.remove(os.path.join(self.itemdir, entry.fname)) else: -- cgit v1.2.3