Created
December 13, 2012 10:58
-
-
Save them0nk/4275715 to your computer and use it in GitHub Desktop.
Sample ninja ide plugin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: UTF-8 -*- | |
#=============================================================================== | |
# IMPORTS | |
#=============================================================================== | |
import os | |
import json | |
import datetime | |
import string | |
import socket | |
import getpass | |
import shutil | |
import re | |
from PyQt4 import uic | |
from PyQt4.QtCore import Qt | |
from PyQt4.QtCore import pyqtSignal | |
from PyQt4.QtGui import QVBoxLayout | |
from PyQt4.QtGui import QInputDialog | |
from PyQt4.QtGui import QTextCursor | |
from PyQt4.QtGui import QMessageBox | |
from PyQt4.QtGui import QListWidgetItem | |
from PyQt4.QtGui import QFileDialog | |
from PyQt4.QtGui import QTableWidgetItem | |
from PyQt4.QtGui import QLineEdit | |
from PyQt4.QtGui import QDialog | |
from PyQt4.QtCore import QDateTime | |
from ninja_ide.core import plugin | |
from ninja_ide.tools import json_manager | |
from ninja_ide.gui.editor import editor | |
import ninja_ide | |
class QLogSortWidget(QDialog): | |
def __init__(self,locator,parent=None): | |
super(QLogSortWidget,self).__init__(parent) | |
self.locator = locator | |
self.ui = uic.loadUi('/home/chaos/work/crs/scripts/ninja-ide/plugins/LogAnalyser/loganalyser/ui/search.ui', self) | |
self.buffer = None | |
def buffer_init(self): | |
SERVICE_NAME = "editor" | |
editor_service = self.locator.get_service(SERVICE_NAME) | |
if not self.buffer: | |
logs = editor_service.get_text() | |
if logs: | |
self.buffer = logs | |
def grep(self): | |
pass | |
def display(self): | |
self.buffer_init() | |
if self.buffer: | |
logs = self.filter(self.buffer) | |
SERVICE_NAME = "editor" | |
editor_service = self.locator.get_service(SERVICE_NAME) | |
editor_service.get_editor().setPlainText(logs) | |
pass | |
def restore(self): | |
if self.buffer: | |
print("Restoring") | |
SERVICE_NAME = "editor" | |
editor_service = self.locator.get_service(SERVICE_NAME) | |
editor_service.get_editor().setPlainText(self.buffer) | |
def add_to_slot(self): | |
self.ui.list_regex.addItem(self.ui.le_regex.displayText()) | |
def remove_from_slot(self): | |
for item in self.ui.list_regex.selectedItems(): | |
self.ui.list_regex.removeItemWidget(item) | |
def filter_timestamp(self,logs): | |
start_date = self.ui.dte_start.dateTime() | |
end_date = self.ui.dte_end.dateTime() | |
ret_log = [] | |
for log in logs: | |
date = QDateTime.fromString(':'.join(log.split(':')[1:4]).strip(),'MMM dd HH:mm:ss.zzz') | |
date = date.addYears(112) | |
if date >= start_date and date <= end_date: | |
print(date.toString()) | |
ret_log.append(log) | |
return ret_log | |
def filter_regex(self,logs): | |
regex = self.ui.le_regex.displayText() | |
if len(regex): | |
return [log for log in logs if re.search(regex,log) ] | |
return logs | |
def filter(self,logs): | |
logs_list = logs.split('\n') | |
logs_list = self.filter_timestamp(logs_list) | |
logs_list = self.filter_regex(logs_list) | |
if len(logs_list) != 0: | |
return '\n'.join(logs_list) | |
return "" | |
class LogAnalyser(plugin.Plugin): | |
def initialize(self): | |
# Init your plugin | |
SERVICE_NAME = "misc" | |
misc_service = self.locator.get_service(SERVICE_NAME) | |
misc_service.add_widget(QLogSortWidget(self.locator),'/home/chaos/work/oss/source/ninja-ide/ninja_ide/img/play.png', "desc") | |
def finish(self): | |
# Shutdown your plugin | |
pass | |
def get_preferences_widget(self): | |
# Return a widget for customize your plugin | |
pass |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<ui version="4.0"> | |
<class>Dialog</class> | |
<widget class="QDialog" name="Dialog"> | |
<property name="geometry"> | |
<rect> | |
<x>0</x> | |
<y>0</y> | |
<width>670</width> | |
<height>300</height> | |
</rect> | |
</property> | |
<property name="windowTitle"> | |
<string>Dialog</string> | |
</property> | |
<property name="autoFillBackground"> | |
<bool>true</bool> | |
</property> | |
<property name="sizeGripEnabled"> | |
<bool>false</bool> | |
</property> | |
<property name="modal"> | |
<bool>false</bool> | |
</property> | |
<widget class="QLineEdit" name="le_regex"> | |
<property name="geometry"> | |
<rect> | |
<x>40</x> | |
<y>50</y> | |
<width>211</width> | |
<height>27</height> | |
</rect> | |
</property> | |
</widget> | |
<widget class="QPushButton" name="pb_grep"> | |
<property name="geometry"> | |
<rect> | |
<x>270</x> | |
<y>50</y> | |
<width>98</width> | |
<height>27</height> | |
</rect> | |
</property> | |
<property name="text"> | |
<string>grep</string> | |
</property> | |
</widget> | |
<widget class="QPushButton" name="pb_display"> | |
<property name="geometry"> | |
<rect> | |
<x>100</x> | |
<y>220</y> | |
<width>98</width> | |
<height>27</height> | |
</rect> | |
</property> | |
<property name="text"> | |
<string>display</string> | |
</property> | |
</widget> | |
<widget class="QListWidget" name="list_regex"> | |
<property name="geometry"> | |
<rect> | |
<x>390</x> | |
<y>60</y> | |
<width>256</width> | |
<height>192</height> | |
</rect> | |
</property> | |
</widget> | |
<widget class="QDateTimeEdit" name="dte_start"> | |
<property name="geometry"> | |
<rect> | |
<x>20</x> | |
<y>120</y> | |
<width>194</width> | |
<height>27</height> | |
</rect> | |
</property> | |
<property name="displayFormat"> | |
<string>d MMM yyyy HH:mm:ss.zzz</string> | |
</property> | |
</widget> | |
<widget class="QDateTimeEdit" name="dte_end"> | |
<property name="geometry"> | |
<rect> | |
<x>20</x> | |
<y>170</y> | |
<width>194</width> | |
<height>27</height> | |
</rect> | |
</property> | |
<property name="displayFormat"> | |
<string>d MMM yyyy HH:mm:ss.zzz</string> | |
</property> | |
</widget> | |
<widget class="QPushButton" name="pb_add_to_list"> | |
<property name="geometry"> | |
<rect> | |
<x>270</x> | |
<y>90</y> | |
<width>98</width> | |
<height>27</height> | |
</rect> | |
</property> | |
<property name="text"> | |
<string>Add To List</string> | |
</property> | |
</widget> | |
<widget class="QPushButton" name="pb_restore"> | |
<property name="geometry"> | |
<rect> | |
<x>250</x> | |
<y>220</y> | |
<width>98</width> | |
<height>27</height> | |
</rect> | |
</property> | |
<property name="text"> | |
<string>Restore</string> | |
</property> | |
</widget> | |
<widget class="QPushButton" name="pb_remove"> | |
<property name="geometry"> | |
<rect> | |
<x>550</x> | |
<y>260</y> | |
<width>98</width> | |
<height>27</height> | |
</rect> | |
</property> | |
<property name="text"> | |
<string>Remove</string> | |
</property> | |
</widget> | |
</widget> | |
<resources/> | |
<connections> | |
<connection> | |
<sender>pb_grep</sender> | |
<signal>clicked()</signal> | |
<receiver>Dialog</receiver> | |
<slot>display()</slot> | |
<hints> | |
<hint type="sourcelabel"> | |
<x>338</x> | |
<y>63</y> | |
</hint> | |
<hint type="destinationlabel"> | |
<x>199</x> | |
<y>149</y> | |
</hint> | |
</hints> | |
</connection> | |
<connection> | |
<sender>pb_display</sender> | |
<signal>clicked()</signal> | |
<receiver>Dialog</receiver> | |
<slot>display()</slot> | |
<hints> | |
<hint type="sourcelabel"> | |
<x>308</x> | |
<y>233</y> | |
</hint> | |
<hint type="destinationlabel"> | |
<x>199</x> | |
<y>149</y> | |
</hint> | |
</hints> | |
</connection> | |
<connection> | |
<sender>pb_add_to_list</sender> | |
<signal>clicked()</signal> | |
<receiver>Dialog</receiver> | |
<slot>add_to_slot()</slot> | |
<hints> | |
<hint type="sourcelabel"> | |
<x>318</x> | |
<y>113</y> | |
</hint> | |
<hint type="destinationlabel"> | |
<x>334</x> | |
<y>149</y> | |
</hint> | |
</hints> | |
</connection> | |
<connection> | |
<sender>pb_restore</sender> | |
<signal>clicked()</signal> | |
<receiver>Dialog</receiver> | |
<slot>restore()</slot> | |
<hints> | |
<hint type="sourcelabel"> | |
<x>298</x> | |
<y>233</y> | |
</hint> | |
<hint type="destinationlabel"> | |
<x>334</x> | |
<y>149</y> | |
</hint> | |
</hints> | |
</connection> | |
<connection> | |
<sender>pb_remove</sender> | |
<signal>clicked()</signal> | |
<receiver>Dialog</receiver> | |
<slot>remove_from_slot()</slot> | |
<hints> | |
<hint type="sourcelabel"> | |
<x>598</x> | |
<y>273</y> | |
</hint> | |
<hint type="destinationlabel"> | |
<x>334</x> | |
<y>149</y> | |
</hint> | |
</hints> | |
</connection> | |
</connections> | |
<slots> | |
<slot>add_to_slot()</slot> | |
<slot>display()</slot> | |
<slot>restore()</slot> | |
<slot>remove_from_slot()</slot> | |
</slots> | |
</ui> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment