From a84966cf9b02697f13e56371375929b7d7fa7727 Mon Sep 17 00:00:00 2001 From: Simon Forman Date: Sat, 14 Jul 2018 18:51:22 -0700 Subject: [PATCH] Split some code into a utils.py module. --- joy/gui/main.py | 36 ++---------------------------------- joy/gui/utils.py | 45 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 34 deletions(-) create mode 100644 joy/gui/utils.py diff --git a/joy/gui/main.py b/joy/gui/main.py index fea0104..6508b33 100755 --- a/joy/gui/main.py +++ b/joy/gui/main.py @@ -14,32 +14,13 @@ from dulwich.errors import NotGitRepository from dulwich.repo import Repo from joy.gui.textwidget import TextViewerWidget, tk, get_font, TEXT_BINDINGS +from joy.gui.utils import init_home, FileFaker from joy.gui.world import World from joy.library import initialize from joy.utils.stack import stack_to_string -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 - -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.' +JOY_HOME, repo = init_home() def repo_relative_path(path): @@ -229,19 +210,6 @@ for event, command in GLOBAL_COMMANDS.items(): t.bind_all(event, lambda _, _command=command: w.interpret(_command)) -class FileFaker(object): - - def __init__(self, T): - self.T = T - - def write(self, text): - self.T.insert('end', text) - self.T.see('end') - - def flush(self): - pass - - def main(): sys.stdout, old_stdout = FileFaker(log), sys.stdout try: diff --git a/joy/gui/utils.py b/joy/gui/utils.py new file mode 100644 index 0000000..e7b5a93 --- /dev/null +++ b/joy/gui/utils.py @@ -0,0 +1,45 @@ +import os + +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 + + 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 + + +class FileFaker(object): + + def __init__(self, T): + self.T = T + + def write(self, text): + self.T.insert('end', text) + self.T.see('end') + + def flush(self): + pass