aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorElena ``of Valhalla'' Grandi <valhalla@trueelena.org>2016-12-31 14:21:56 +0100
committerElena ``of Valhalla'' Grandi <valhalla@trueelena.org>2016-12-31 14:21:56 +0100
commit5e63c3127cd82a187fdb6c227e5e0538212763b2 (patch)
tree29ff4b4bab075d7bca8d24ffa36c4d27fff8e030
parent42531b8d4e8735a5cc44220a38d51540f6882242 (diff)
lesana init: new command
-rw-r--r--lesana/command.py66
-rwxr-xr-xscripts/lesana3
2 files changed, 68 insertions, 1 deletions
diff --git a/lesana/command.py b/lesana/command.py
index 23b9b6b..706bbf3 100644
--- a/lesana/command.py
+++ b/lesana/command.py
@@ -2,6 +2,12 @@ import logging
import os
import subprocess
+try:
+ import git
+ git_available = True
+except ImportError:
+ git_available = False
+
import gadona
from . import Collection, Entry
@@ -125,3 +131,63 @@ class Search(gadona.Command):
offset,
pagesize):
print(entry.fname)
+
+
+class Init(gadona.Command):
+ name = 'init'
+ arguments = [
+ (['--collection', '-c'], dict(
+ help='The directory to work on (default .)',
+ default='.'
+ )),
+ (['--no-git'], dict(
+ help='Skip setting up git in this directory',
+ action="store_false",
+ dest='git'
+ )),
+ ]
+
+ 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'])
diff --git a/scripts/lesana b/scripts/lesana
index a452c64..a500bb8 100755
--- a/scripts/lesana
+++ b/scripts/lesana
@@ -1,7 +1,7 @@
#!/usr/bin/env python3
import gadona
-from lesana.command import New, Edit, Index, Search
+from lesana.command import New, Edit, Index, Search, Init
if __name__ == '__main__':
app = gadona.App()
@@ -11,5 +11,6 @@ if __name__ == '__main__':
Edit(),
Index(),
Search(),
+ Init(),
]
app.main()