about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorhut <hut@lavabit.com>2012-03-18 17:50:10 +0100
committerhut <hut@lavabit.com>2012-03-18 17:50:10 +0100
commit45dd1a435f91ee2f0a98747dbccf1a77896cc000 (patch)
tree73bf284901927b72ce6b01ded602fedf66c9fe98
parent8134bd5f41875540cdfdd0030e8611864a8cec08 (diff)
downloadranger-45dd1a435f91ee2f0a98747dbccf1a77896cc000.tar.gz
ext.rifle: added hooks, fixed bugs.
-rw-r--r--ranger/core/actions.py2
-rw-r--r--ranger/core/fm.py4
-rw-r--r--ranger/ext/rifle.py40
3 files changed, 40 insertions, 6 deletions
diff --git a/ranger/core/actions.py b/ranger/core/actions.py
index 5b824476..d5d38de9 100644
--- a/ranger/core/actions.py
+++ b/ranger/core/actions.py
@@ -304,10 +304,8 @@ class Actions(FileManagerAware, EnvironmentAware, SettingsAware):
 		mimetype = files[0].mimetype if files else None
 		label = kw['label'] if 'label' in kw else None
 		try:
-			self.ui.suspend()
 			return self.rifle.execute(filenames, mode, label, mimetype)
 		finally:
-			self.ui.initialize()
 			self.signal_emit('execute.after')
 
 	# --------------------------
diff --git a/ranger/core/fm.py b/ranger/core/fm.py
index 1e134854..7858c67e 100644
--- a/ranger/core/fm.py
+++ b/ranger/core/fm.py
@@ -79,6 +79,10 @@ class FM(Actions, SignalDispatcher):
 			self.ui = UI()
 			self.ui.initialize()
 
+		self.rifle.hook_before_executing = lambda a, b, flags: self.ui.suspend() if 'f' not in flags else None
+		self.rifle.hook_after_executing = lambda a, b, flags: self.ui.initialize() if 'f' not in flags else None
+		self.rifle.hook_logger = self.notify
+
 		def mylogfunc(text):
 			self.notify(text, bad=True)
 		self.run = Runner(ui=self.ui, logfunc=mylogfunc, fm=self)
diff --git a/ranger/ext/rifle.py b/ranger/ext/rifle.py
index 563968ef..a61fb2a3 100644
--- a/ranger/ext/rifle.py
+++ b/ranger/ext/rifle.py
@@ -22,6 +22,24 @@ class Rifle(object):
 	delimiter1 = '='
 	delimiter2 = ','
 
+	def hook_before_executing(self, command, mimetype, flags):
+		pass
+
+	def hook_after_executing(self, command, mimetype, flags):
+		pass
+
+	def hook_command_preprocessing(self, command):
+		return command
+
+	def hook_command_postprocessing(self, command):
+		return command
+
+	def hook_environment(self, env):
+		return env
+
+	def hook_logger(self, string):
+		sys.stderr.write(string + "\n")
+
 	def __init__(self, config_file):
 		self.config_file = config_file
 		self._app_flags = False
@@ -31,22 +49,27 @@ class Rifle(object):
 			config_file = self.config_file
 		f = open(config_file, 'r')
 		self.rules = []
+		lineno = 1
 		for line in f:
 			if line.startswith('#') or line == '\n':
 				continue
 			line = line.strip()
 			if self.delimiter1 not in line:
-				print("Syntax error foo")
+				self.hook_logger("Syntax error in %s line %d" % \
+						(config_file, lineno))
 			tests, command = line.split(self.delimiter1, 1)
 			tests = tests.split(self.delimiter2)
 			tests = tuple(tuple(f.strip().split(None, 1)) for f in tests)
 			tests = tuple(tests)
 			command = command.strip()
 			self.rules.append((command, tests))
+			lineno += 1
 
 	def _eval_rule(self, rule, files, label):
 		function = rule[0]
 		argument = rule[1] if len(rule) > 1 else ''
+		if not files:
+			return False
 
 		self._app_flags = ''
 
@@ -89,7 +112,7 @@ class Rifle(object):
 		flags = self._app_flags
 		_filenames = "' '".join(f.replace("'", "'\\\''") for f in files)
 		command = "set -- '%s'" % _filenames + '\n'
-		if 'p' in flags and not 'f' in flags and is_terminal():
+		if 'p' in flags and not 'f' in flags and _is_terminal():
 			action += '| less'
 		if 'f' in flags:
 			action = "nohup %s >&/dev/null &" % action
@@ -130,12 +153,21 @@ class Rifle(object):
 				elif count != number:
 					count += 1
 				else:
+					command = self.hook_command_preprocessing(command)
 					command = self._build_command(files, cmd)
 					break
 		#print(command)
 		if command is not None:
-			p = Popen(command, shell=True)
-			p.wait()
+			command = self.hook_command_postprocessing(command)
+			self.hook_before_executing(command, self._mimetype, self._app_flags)
+			try:
+				p = Popen(command, shell=True)
+				p.wait()
+			finally:
+				self.hook_after_executing(command, self._mimetype, self._app_flags)
+
+		else:
+			self.hook_logger("No action found.")
 
 if __name__ == '__main__':
 	import sys