Permalink
Browse files

#98: Refactored find_editor into get_editor from recipyCommon.config.…

… Added unit test for find_editor. Merged with Jan's commit.
  • Loading branch information...
trbedwards committed Sep 19, 2016
2 parents 74c7a51 + a0aa0a0 commit d82dfba8f701af220ea010bd52266bc6dab918ab
View
@@ -121,6 +121,7 @@ This simply instructs recipy not to save `git diff` information when it records
* `[general]`
* `debug` - print debug messages
* `editor = vi` - Configure the default text editor that will be used when recipy needs you to type in a message. Use notepad if on Windows, for example
* `quiet` - don't print any messages
* `port` - specify port to use for the GUI
* `[data]`
View
@@ -63,5 +63,24 @@ class PatchNIBabel(PatchSimple):
add_module_to_db(modulename, input_functions, output_functions)
class PatchTifffile(PatchSimple):
modulename = 'tifffile'
input_functions = ['imread']
output_functions = ['imsave']
input_wrapper = create_wrapper(log_input, 0, 'tifffile')
output_wrapper = create_wrapper(log_output, 0, 'tifffile')
class PatchImageio(PatchSimple):
modulename = 'imageio'
input_functions = ['core.functions.get_reader', 'core.functions.read']
output_functions = ['core.functions.get_writer']
input_wrapper = create_wrapper(log_input, 0, 'imageio')
output_wrapper = create_wrapper(log_output, 0, 'imageio')
multiple_insert(sys.meta_path, [PatchGDAL(), PatchSKLearn(),
PatchNIBabel()])
PatchNIBabel(), PatchTifffile(),
PatchImageio()])
View
@@ -38,6 +38,7 @@
from . import __version__
from recipyCommon import config, utils
from recipyCommon.config import get_editor
from recipyCommon.version_control import hash_file
from colorama import init
@@ -140,6 +141,11 @@ def main():
def annotate(args):
# check whether editor is set
editor = get_editor()
if editor is None:
raise RuntimeError('No custom editor set in ~/.recipy/recipyrc file and could not find default $EDITOR.')
# Grab latest run from the DB
run = get_latest_run()
@@ -159,7 +165,7 @@ def annotate(args):
f.close()
# Open your editor
launch_text_editor(f.name)
os.system('%s %s' % (editor, f.name))
# Grab the text
annotation = ""
@@ -180,53 +186,6 @@ def annotate(args):
db.close()
def launch_text_editor(filename):
"""
Launches a text editor on the file called 'filename'. Attempts to find the text editor first through the
recipy config file, then in the environment variables. Falls back to default text editor for the
corresponding operating system.
:param filename: name of file being edited
:return:
"""
def _try_editors(fname, list_of_editors):
for editor in list_of_editors:
status = os.system("%s %s" % (editor, fname))
if status == 0:
break
return status
assert isinstance(filename, six.string_types), \
"%s must be a string type. type = %s" % (filename, type(filename))
# check that $EDITOR is defined
editor = os.environ.get('EDITOR')
if editor is not None:
os.system('$EDITOR %s' % filename)
else:
# create an empty file
touch(filename)
# open the empty file by trying different text editors according to the operating system
if "linux" in sys.platform:
status = _try_editors(filename, ["/usr/bin/editor", "nano", "vi", "emacs"])
elif "darwin" in sys.platform:
status = _try_editors(filename, ["nano", "vi", "emacs", "open -a TextEdit"])
elif "win" in sys.platform:
status = _try_editors(filename, ["edit", "notepad", "notepad.exe"])
else:
raise RuntimeError("Cannot launch text editor for operating system %s. "
"Try setting the EDITOR environment variable" % sys.platform)
if status != 0:
raise RuntimeError("Cannot launch text editor. Try setting the EDITOR environment variable")
def touch(fname):
""" Create an empty file """
with open(fname, 'a'):
os.utime(fname, times=None)
def gui(args):
"""
Loads recipy GUI from the command-line
View
@@ -8,6 +8,9 @@
from configparser import SafeConfigParser, Error
import os
import sys
from distutils.spawn import find_executable
def read_config_file():
@@ -42,6 +45,47 @@ def get_db_path():
return os.path.expanduser('~/.recipy/recipyDB.json')
def get_editor():
try:
editor = conf.get('general', 'editor')
except Error:
if os.environ.get('EDITOR'):
editor = '$EDITOR'
else:
editor = find_editor()
return editor
def find_editor():
""" Attemps to find a valid text editor by trying different executables (according to the operating system) and
checking for existence. """
platform = sys.platform
# open the empty file by trying different text editors according to the operating system
if "linux" in platform:
editor = _try_editors(["/usr/bin/editor", "nano", "vi", "emacs"])
elif "darwin" in platform: # macintosh
editor = _try_editors(["nano", "vi", "emacs", "open -t"])
elif "win" in platform: # windows
editor = _try_editors(["edit", "notepad", "notepad.exe"])
else:
raise RuntimeError("Cannot launch text editor for operating system %s. "
"Try setting the editor in the ~/.recipy/recipyrc file" % platform)
if editor is None:
raise RuntimeError("Try setting the editor in the ~/.recipy/recipyrc file")
return editor
def _try_editors(list_of_commands):
for command in list_of_commands:
exe = command.split(' ')[0] # strip the options
exe_path = find_executable(exe)
if exe_path is not None:
return command
return None # returns None if no editor is found
def get_gui_port():
try:
return int(conf.get('general', 'port'))
@@ -0,0 +1,27 @@
from recipyCommon.config import find_editor
import unittest
import mock
class TestConfig(unittest.TestCase):
def test_find_editor_windows(self):
with mock.patch('sys.platform', 'win32'):
with mock.patch('recipyCommon.config._try_editors', return_value='notepad'):
editor = find_editor()
self.assertEquals(editor, 'notepad')
def test_find_editor_mac(self):
with mock.patch('sys.platform', 'darwin'):
with mock.patch('recipyCommon.config._try_editors', return_value='open -t'):
editor = find_editor()
self.assertEquals(editor, 'open -t')
def test_find_editor_linux(self):
with mock.patch('sys.platform', 'linux'):
with mock.patch('recipyCommon.config._try_editors', return_value='vi'):
editor = find_editor()
self.assertEquals(editor, 'vi')
View
@@ -0,0 +1 @@
mock
View
@@ -0,0 +1,15 @@
year,temperature
2000, 15
2001, 16.2
2002, 17.3
2003, 13.6
2004, 18.6
2005, 14.2
2006, 11.6
2007, 19.0
2008, 24.3
2009, 15.4
2010, 12.3
2011, 18.9
2012, 17.6
2013, 16.3
@@ -0,0 +1,13 @@
import recipy
import imageio
import os
in_path = r'R:\Science\XFM\GaryRuben\git_repos\tmm_model\acsemble\data'
out_path = r'C:\temp'
im1 = imageio.imread(os.path.join(in_path, 'golosio_100-C.tiff'))
im2 = imageio.imread(os.path.join(in_path, 'Ni_test_phantom2.png'))
imageio.imwrite(os.path.join(out_path, 'im1.png'), im1)
imageio.imwrite(os.path.join(out_path, 'im2.png'), im2)
@@ -0,0 +1,11 @@
import recipy
import tifffile
import os
in_path = r'R:\Science\XFM\GaryRuben\git_repos\tmm_model\acsemble\data'
out_path = r'C:\temp'
im1 = tifffile.imread(os.path.join(in_path, 'golosio_100-C.tiff'))
tifffile.imsave(os.path.join(out_path, 'im1.tiff'), im1)

0 comments on commit d82dfba

Please sign in to comment.