Commits

Rodrigo Pinheiro Matias committed db17830

Iniciando o projeto.

Comments (0)

Files changed (10)

+^(build|dist).*$
+^.*sublime.*$

LICENSE.txt

Empty file added.

QSupervisorControl.py

+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+'''
+Copyright 2012 Rodrigo Pinheiro Matias <rodrigopmatias@gmail.com>
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+   http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+'''
+from PyQt4 import QtGui
+from PyQt4 import QtCore
+from supervisor import supervisorctl
+
+import os
+import sys
+import re
+
+try:
+    from cStringIO import StringIO
+except ImportError:
+    from StringIO import StringIO
+
+app_dir  = os.path.dirname(os.path.abspath(__file__))
+data_dir = os.path.join(app_dir, 'data')
+
+def proxy_func(func, *args, **kargs):
+    def wrapper():
+        func(*args, **kargs)
+    return wrapper
+
+class StatusObserve(QtCore.QThread):
+
+    def __init__(self, controller):
+        QtCore.QThread.__init__(self)
+        self._controller = controller
+
+    def run(self):
+        while True:
+            self._controller.emit(QtCore.SIGNAL('observe'))
+            self.sleep(1)
+
+class Controller(QtGui.QSystemTrayIcon):
+
+    def _void(self):
+        pass
+
+    def get_programs(self):
+        swap = sys.stdout
+        out = StringIO()
+        sys.stdout = out
+        supervisorctl.main(('status',))
+        sys.stdout = swap
+
+        for line in out.getvalue().split('\n'):
+            rst = re.match(r'^(?P<name>\w+)\s+(?P<status>\w+).*$', line.replace('\n', ''))
+            if rst is not None:
+                yield rst.groupdict()
+
+    def get_status(self, program):
+        swap = sys.stdout
+        out = StringIO()
+        sys.stdout = out
+        supervisorctl.main(('status', program))
+        sys.stdout = swap
+
+        rst = re.match(
+            r'^(?P<name>\w+)\s+(?P<status>\w+).*$', 
+            out.getvalue().replace('\n', '\0')
+        )
+
+        if rst is not None:
+            return rst.groupdict().get('status')
+        else:
+            return None
+
+    def _exit(self):
+        self._app.quit()
+
+    def do(self, program = 'all', action = None):
+        supervisorctl.main((action, program))
+
+    def register_to_observe(self, program, menu, action):
+        if hasattr(self, '_to_observe') is False:
+            self._to_observe = {}
+        if program not in self._to_observe.keys():
+            self._to_observe.update({
+                program: {
+                    'menu': menu,
+                    'action': action
+                }
+            })
+
+    def changeActionIcon(self, action, icon):
+        action.setIcon(QtGui.QIcon(os.path.join(data_dir, '%s.png' % icon.lower())))
+
+    def do_observe(self):
+        translate = {
+            'STOPPED': 'Parado',
+            'RUNNING': 'Rodando',
+            'FATAL': 'Falhou'
+        }
+
+        for program, info in self._to_observe.items():
+            status = self.get_status(program)
+            if status is not None:
+                info.get('action').setText(translate.get(status, 'Desconhecido'))
+                self.changeActionIcon(info.get('menu'), status)
+
+    def _createContextMenu(self):
+        menu = QtGui.QMenu()
+
+        menu.addAction('Iniciar todos', proxy_func(self.do, **{'action': 'start'}))
+        menu.addAction('Parar todos', proxy_func(self.do, **{'action': 'stop'}))
+        menu.addAction('Reiniciar todos', proxy_func(self.do, **{'action': 'restart'}))
+
+        menu.addSeparator()
+        for program in self.get_programs():
+            sub_menu = menu.addMenu(program.get('name').capitalize())
+            self.register_to_observe(
+                program.get('name'),
+                sub_menu,
+                sub_menu.addAction(program.get('status')),
+            )
+
+            sub_menu.addSeparator()
+            sub_menu.addAction('Iniciar', proxy_func(self.do, **{'program': program.get('name'),'action': 'start'}))
+            sub_menu.addAction('Parar', proxy_func(self.do, **{'program': program.get('name'),'action': 'stop'}))
+            sub_menu.addAction('Reiniciar', proxy_func(self.do, **{'program': program.get('name'),'action': 'restart'}))
+        menu.addSeparator()
+
+        menu.addAction('&Finalizar' + (' ' * 50), self._app.quit)
+
+        return menu
+
+    def __init__(self, app):
+        QtGui.QSystemTrayIcon.__init__(
+            self, 
+            QtGui.QIcon(os.path.join(data_dir, 'icon.png')),
+            app
+        )
+
+        self._app = app
+        self.setContextMenu(self._createContextMenu())
+        self._thread = StatusObserve(self)
+        QtCore.QObject.connect(self, QtCore.SIGNAL('observe'), self.do_observe)
+        self._thread.start()
+
+def main():
+    app = QtGui.QApplication([])
+    
+    ctl = Controller(app)
+    ctl.show()
+
+    app.exec_()
+
+__name__ == '__main__' and main()

README.rst

Empty file added.
+0.1.1

data/fatal.png

Added
New image

data/icon.png

Added
New image

data/running.png

Added
New image

data/stopped.png

Added
New image
+# -*- coding: utf-8 -*-
+'''
+Copyright 2012 Rodrigo Pinheiro Matias <rodrigopmatias@gmail.com>
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+   http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+'''
+
+from distutils.core import setup
+
+setup(
+    name = 'QSupervisorControl',
+    description = 'The simples tray system that observe status of services managed with supervisor.',
+    long_description = open('README.rst', 'r').read(),
+    version = open('VERSION.txt', 'r').read(),
+    author = 'Rodrigo Pinheiro Matias',
+    author_email = 'rodrigopmatias@gmail.com',
+    maintainer = 'Rodrigo Pinheiro Matias',
+    maintainer_email = 'rodrigopmatias@gmail.com',
+    url = 'https://bitbucket.org/rodrigopmatias/QSupervisorControl/downloads',
+    py_modules = ['QSupervisorControl'],
+    scripts = ['QSupervisorControl.py'],
+    install_requires = ['PyQt4','supervisor'],
+    packages = [''],
+    package_data = {'': ['data/*.png']}
+)