Skip to content

Commit

Permalink
[#328] Menu commands to show cookie cutter and metadata template files
Browse files Browse the repository at this point in the history
  • Loading branch information
quicklizard99 committed Jun 17, 2016
1 parent 48f2a5c commit 15fd011
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 28 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ This is an overview of major changes. Refer to the git repository for a full log
Version 0.1.34
-------------
- #325 Command-line option to set window size
- #328 Menu commands to show cookie cutter and metadata template files

Version 0.1.33
-------------
Expand Down
8 changes: 8 additions & 0 deletions inselect/gui/cookie_cutter_choice.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,14 @@ def create_and_use(self, boxes, path):
cookie_cutter.save(path)
self.load(path)

@property
def current_path(self):
"""The path to the selected UserTemplate or None, if the default
template is selected
"""
current = QSettings().value(self.PATH_KEY)
return Path(current) if current else None

@property
def current(self):
"The selected CookieCutter"
Expand Down
11 changes: 10 additions & 1 deletion inselect/gui/cookie_cutter_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from inselect.lib.utils import debug_print

from .cookie_cutter_choice import cookie_cutter_choice
from .utils import report_to_user, load_icon
from .utils import report_to_user, load_icon, reveal_path


class CookieCutterWidget(QObject):
Expand All @@ -32,6 +32,9 @@ def _create_actions(self):
"Choose...", self, triggered=self.choose,
icon=load_icon(':/icons/open.png')
)
self.reveal_action = QAction(
"Reveal cookie cutter", self, triggered=self.reveal
)
self.clear_action = QAction(
"Do not use a cookie cutter", self, triggered=self.clear,
icon=load_icon(':/icons/close.png')
Expand All @@ -42,6 +45,7 @@ def inject_actions(self, menu):
"Adds cookie cutter actions to menu"
menu.addAction(self.choose_action)
menu.addAction(self.apply_current_action)
menu.addAction(self.reveal_action)
menu.addSeparator()
menu.addAction(self.clear_action)
menu.addSeparator()
Expand All @@ -66,6 +70,10 @@ def choose(self):
# Save the user's choice
cookie_cutter_choice().load(path)

@report_to_user
def reveal(self):
reveal_path(cookie_cutter_choice().current_path)

def sync_ui(self, button, has_document, has_rows):
"Sync state of actions"
debug_print('CookieCutterWidget.sync_ui')
Expand All @@ -82,4 +90,5 @@ def sync_ui(self, button, has_document, has_rows):

self.save_to_new_action.setEnabled(has_rows)
self.clear_action.setEnabled(has_current)
self.reveal_action.setEnabled(has_current)
self.apply_current_action.setEnabled(has_document and has_current)
26 changes: 1 addition & 25 deletions inselect/gui/info_widget.py
Original file line number Diff line number Diff line change
@@ -1,39 +1,15 @@
import humanize
import locale
import platform
import subprocess
import sys

from PySide.QtCore import Qt
from PySide.QtGui import QFormLayout, QLabel, QWidget

from inselect.lib.utils import format_dt_display
from inselect.gui.utils import BoldLabel, HorizontalLine
from inselect.gui.utils import BoldLabel, HorizontalLine, reveal_path

from .popup_panel import PopupPanel
from .utils import report_to_user


def reveal_path(path):
"""Shows path in Finder (on Mac) or in Explorer (on Windows)
"""
# http://stackoverflow.com/a/3546503
path = path.resolve()
if sys.platform.startswith("win"):
res = subprocess.call(["explorer.exe", u"/select,{0}".format(path)])
if 1 != res:
raise ValueError('Unexpected exit code [{0}]'.format(res))
elif 'Darwin' == platform.system():
reveal = u'tell application "Finder" to reveal POSIX file "{0}"'
activate = u'tell application "Finder" to activate "{0}"'
args = ['/usr/bin/osascript', '-e']
subprocess.check_call(args + [reveal.format(path)])
subprocess.check_call(args + [activate.format(path)])
else:
# What to do on Linux?
pass


class RevealPathLabel(QLabel):
"""A QLabel that, when clicked, reveals a path in Finder / Explorer
"""
Expand Down
8 changes: 8 additions & 0 deletions inselect/gui/user_template_choice.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,14 @@ def refresh(self):
# Using the default (DWC) template - no need to do anything
pass

@property
def current_path(self):
"""The path to the selected UserTemplate or None, if the default
template is selected
"""
current = QSettings().value(self.PATH_KEY)
return Path(current) if current else None

@property
def current(self):
"The selected UserTemplate"
Expand Down
13 changes: 11 additions & 2 deletions inselect/gui/user_template_popup_button.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from inselect.lib.utils import debug_print

from .user_template_choice import user_template_choice
from .utils import report_to_user, load_icon
from .utils import report_to_user, load_icon, reveal_path


class UserTemplatePopupButton(QPushButton):
Expand Down Expand Up @@ -42,15 +42,19 @@ def _create_actions(self):
"Reload", self, triggered=self.refresh,
icon=load_icon(':/icons/refresh.png')
)
self._reveal_template_action = QAction(
"Reveal template", self, triggered=self.reveal
)
self._default_action = QAction(
u"Default ({0})".format(user_template_choice().DEFAULT.name),
self, triggered=self.default
self, triggered=self.default, icon=load_icon(':/icons/close.png')
)

def inject_actions(self, menu):
"Adds user template actions to menu"
menu.addAction(self._choose_action)
menu.addAction(self._refresh_action)
menu.addAction(self._reveal_template_action)
menu.addSeparator()
menu.addAction(self._default_action)

Expand Down Expand Up @@ -78,10 +82,15 @@ def refresh(self):
debug_print('UserTemplateWidget.refresh')
user_template_choice().refresh()

@report_to_user
def reveal(self):
reveal_path(user_template_choice().current_path)

def changed(self):
"Slot for UserTemplateChoice.template_changed"
debug_print('UserTemplateWidget.changed')
choice = user_template_choice()
self.setText(choice.current.name)
self._default_action.setEnabled(not choice.current_is_default)
self._refresh_action.setEnabled(not choice.current_is_default)
self._reveal_template_action.setEnabled(not choice.current_is_default)
23 changes: 23 additions & 0 deletions inselect/gui/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import platform
import subprocess
import sys
import traceback

from contextlib import contextmanager
Expand Down Expand Up @@ -199,3 +202,23 @@ class BoldLabel(QLabel):
"""A label in a bold font
"""
pass


def reveal_path(path):
"""Shows path in Finder (on Mac) or in Explorer (on Windows)
"""
# http://stackoverflow.com/a/3546503
path = path.resolve()
if sys.platform.startswith("win"):
res = subprocess.call(["explorer.exe", u"/select,{0}".format(path)])
if 1 != res:
raise ValueError('Unexpected exit code [{0}]'.format(res))
elif 'Darwin' == platform.system():
reveal = u'tell application "Finder" to reveal POSIX file "{0}"'
activate = u'tell application "Finder" to activate "{0}"'
args = ['/usr/bin/osascript', '-e']
subprocess.check_call(args + [reveal.format(path)])
subprocess.check_call(args + [activate.format(path)])
else:
# What to do on Linux?
pass
44 changes: 44 additions & 0 deletions inselect/tests/gui/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import platform
import unittest
import subprocess
import sys

from mock import patch

from inselect.gui.utils import reveal_path
from inselect.tests.utils import temp_directory_with_files


class TestUtils(unittest.TestCase):
"""Test GUI utils
"""
@unittest.skipUnless(sys.platform.startswith("win"), "requires Windows")
@patch.object(subprocess, 'call', return_value=1)
def test_reveal_path_windows(self, mock_subprocess):
with temp_directory_with_files() as tempdir:
path = tempdir / 'xyz'
path.open('w')
reveal_path(path)
expected = [
"explorer.exe",
u"/select,{0}".format(str(path.resolve()))
]
mock_subprocess.assert_called_once_with(expected)

@unittest.skipUnless('Darwin' == platform.system(), "requires OS X")
@patch.object(subprocess, 'check_call')
def test_reveal_path_os_x(self, mock_subprocess):
with temp_directory_with_files() as tempdir:
path = tempdir / 'xyz'
path.open('w')
reveal_path(path)
expected = [
'/usr/bin/osascript',
'-e',
u'tell application "Finder" to reveal POSIX file "{0}"'.format(str(path.resolve()))
]
mock_subprocess.assert_any_call(expected)


if __name__ == '__main__':
unittest.main()

0 comments on commit 15fd011

Please sign in to comment.