[kupfer: 2/20] + putty plugin



commit 19351d6183e6af76a03d7733d38c13d29c0b8f36
Author: Karol BÄ?dkowski <karol bedkowski gmail com>
Date:   Sat Oct 3 10:54:19 2009 +0200

    + putty plugin
    Plugin index session saved in putty.

 kupfer/plugin/putty.py |   99 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 99 insertions(+), 0 deletions(-)
---
diff --git a/kupfer/plugin/putty.py b/kupfer/plugin/putty.py
new file mode 100644
index 0000000..c30a2f8
--- /dev/null
+++ b/kupfer/plugin/putty.py
@@ -0,0 +1,99 @@
+# -*- coding: utf8 -*-
+from __future__ import with_statement
+
+import os
+import urllib
+
+from kupfer.objects import Leaf, Action, Source
+from kupfer.utils import spawn_async
+
+__kupfer_name__ = _("PuTTY sessions")
+__kupfer_sources__ = ("PuttySessionSource", )
+__description__ = _("Session saved in PuTTY")
+__version__ = "0.1"
+__author__ = "Karol BÄ?dkowski <karol bedkowski gmail com>"
+
+
+
+class PuttySessionLeaf(Leaf):
+	""" Leaf represent session saved in PuTTy"""
+
+	def __init__(self, name, description):
+		Leaf.__init__(self, name, name)
+		self._description = description
+
+	def get_actions(self):
+		yield PuttyOpenSession()
+
+	def get_description(self):
+		return self._description
+
+	def get_icon_name(self):
+		return "computer"
+
+
+
+class PuttyOpenSession(Action):
+	''' opens putty session '''
+	def __init__(self):
+		super(PuttyOpenSession, self).__init__(_('Open PuTTy session'))
+
+	def activate(self, leaf):
+		cli = ("putty", "-load", leaf.object)
+		spawn_async(cli)
+
+	def get_icon_name(self):
+		return 'putty'
+
+
+
+class PuttySessionSource(Source):
+	''' indexes session saved in putty '''
+	def __init__(self, name=_("PuTTy Session")):
+		super(PuttySessionSource, self).__init__(name)
+		self._putty_sessions_dir = os.path.expanduser('~/.putty/sessions')
+
+	def is_dynamic(self):
+		return True
+
+	def get_items(self):
+		for filename in os.listdir(self._putty_sessions_dir):
+			if filename == 'Default%20Settings':
+				continue
+
+			obj_path = os.path.join(self._putty_sessions_dir, filename)
+			if os.path.isfile(obj_path):
+				name = urllib.unquote(filename)
+				description = self._load_host_from_session_file(obj_path)
+				yield PuttySessionLeaf(name, description)
+
+	def get_description(self):
+		return _("Session saved in Putty")
+
+	def get_icon_name(self):
+		return "putty"
+
+	def provides(self):
+		yield PuttySessionLeaf
+
+	def _load_host_from_session_file(self, filepath):
+		user = None
+		host = None
+		try:
+			with open(filepath, 'r') as session_file:
+				for line in session_file:
+					if line.startswith('HostName='):
+						host = line.split('=', 2)[1].strip()
+					elif line.startswith('UserName='):
+						user = line.split('=', 2)[1].strip()
+		except Exception, err:
+			print err
+
+		else:
+			return user + '@' + host if user else host
+
+		return 'PuTTY session'
+
+
+
+



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]