Config file and CLI arg parser.

This commit is contained in:
Simon Forman
2018-07-21 17:56:53 -07:00
parent ca06c626e1
commit 33a952ff71
9 changed files with 428 additions and 47 deletions
+71 -25
View File
@@ -1,35 +1,81 @@
import os
import argparse, os, sys
from os import listdir, mkdir
from os.path import abspath, exists, expanduser, isfile, join
from dulwich.errors import NotGitRepository
from dulwich.repo import Repo
def init_home():
'''
Find and initialize the Joy home directory and repository.
'''
JOY_HOME = os.environ.get('JOY_HOME')
if JOY_HOME is None:
JOY_HOME = os.path.expanduser('~/.joypy')
if not os.path.isabs(JOY_HOME):
JOY_HOME = os.path.abspath('./JOY_HOME')
#print 'JOY_HOME=' + JOY_HOME
COMMITTER = 'Joy <[email protected]>'
DEFAULT_JOY_HOME = '~/.joypy'
if not os.path.exists(JOY_HOME):
#print 'creating...'
os.makedirs(JOY_HOME, 0700)
#print 'initializing git repository...'
repo = Repo.init(JOY_HOME)
else: # path does exist
try:
repo = Repo(JOY_HOME)
except NotGitRepository:
#print 'initializing git repository...'
repo = Repo.init(JOY_HOME)
#else:
#print 'opened git repository.'
return JOY_HOME, repo
def home_dir(path):
'''Return the absolute path of an existing directory.'''
fullpath = expanduser(path) if path.startswith('~') else abspath(path)
if not exists(fullpath):
if path == DEFAULT_JOY_HOME:
print 'Creating JOY_HOME', repr(fullpath)
mkdir(fullpath, 0700)
else:
print >> sys.stderr, repr(fullpath), "doesn't exist."
raise ValueError(path)
return fullpath
def init_home(fullpath):
'''
Open or create the Repo.
If there are contents in the dir but it's not a git repo, quit.
'''
try:
repo = Repo(fullpath)
except NotGitRepository:
print >> sys.stderr, repr(fullpath), "no repository"
if listdir(fullpath):
print >> sys.stderr, repr(fullpath), "has contents\nQUIT."
sys.exit(2)
print 'Initializing repository in', fullpath
repo = init_repo(fullpath)
print 'Using repository in', fullpath
return repo
def init_repo(repo_dir):
'''
Create a repo, load the initial content, and make the first commit.
Return the Repo object.
'''
repo = Repo.init(repo_dir)
import joy.gui.init_joy_home
joy.gui.init_joy_home.initialize(repo_dir)
repo.stage([
fn
for fn in listdir(repo_dir)
if isfile(join(repo_dir, fn))
])
repo.do_commit('Initial commit.', committer=COMMITTER)
return repo
argparser = argparse.ArgumentParser(
description='Experimental Brutalist UI for Joy.',
)
argparser.add_argument(
'-j', '--joy-home',
help='Use a directory other than %s as JOY_HOME' % DEFAULT_JOY_HOME,
default=DEFAULT_JOY_HOME,
dest='joy_home',
type=home_dir,
)
class FileFaker(object):