~ubuntu-branches/ubuntu/natty/pida/natty

« back to all changes in this revision

Viewing changes to pida-plugins/man/man.py

  • Committer: Bazaar Package Importer
  • Author(s): Jan Luebbe
  • Date: 2007-09-05 17:54:09 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20070905175409-ty9f6qpuctyjv1sd
Tags: 0.5.1-2
* Depend on librsvg2-common, which is not pulled in by the other depends
  (closes: #394860)
* gvim is no alternative for python-gnome2 and python-gnome2-extras
  (closes: #436431)
* Pida now uses ~/.pida2, so it can no longer be confused by old
  configurations (closes: #421378)
* Culebra is no longer supported by upstream (closes: #349009)
* Update manpage (closes: #440375)
* Update watchfile (pida is now called PIDA)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*- 
 
2
 
 
3
# Copyright (c) 2007 The PIDA Project
 
4
 
 
5
#Permission is hereby granted, free of charge, to any person obtaining a copy
 
6
#of this software and associated documentation files (the "Software"), to deal
 
7
#in the Software without restriction, including without limitation the rights
 
8
#to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 
9
#copies of the Software, and to permit persons to whom the Software is
 
10
#furnished to do so, subject to the following conditions:
 
11
 
 
12
#The above copyright notice and this permission notice shall be included in
 
13
#all copies or substantial portions of the Software.
 
14
 
 
15
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 
16
#IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
17
#FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 
18
#AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 
19
#LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 
20
#OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 
21
#SOFTWARE.
 
22
 
 
23
import os
 
24
import gtk
 
25
import commands
 
26
import re
 
27
import cgi
 
28
 
 
29
from kiwi.ui.objectlist import ObjectList, Column
 
30
 
 
31
# PIDA Imports
 
32
from pida.core.service import Service
 
33
from pida.core.actions import ActionsConfig
 
34
from pida.core.actions import TYPE_TOGGLE
 
35
 
 
36
from pida.ui.views import PidaView
 
37
 
 
38
from pida.utils.gthreads import GeneratorSubprocessTask
 
39
 
 
40
# locale
 
41
from pida.core.locale import Locale
 
42
locale = Locale('man')
 
43
_ = locale.gettext
 
44
 
 
45
class ManItem(object):
 
46
 
 
47
    def __init__(self, pattern, number, description, search):
 
48
        self.pattern = pattern
 
49
        self.number = number
 
50
        self.description = self._color_match(cgi.escape(description), search)
 
51
        patternmark = self._color_match(cgi.escape(pattern), search)
 
52
        self.markup = '%s(<span color="#0000cc">%d</span>)' % (
 
53
            patternmark, int(self.number))
 
54
 
 
55
    def _color_match(self, data, match):
 
56
        return data.replace(match, '<span color="#c00000"><b>%s</b></span>' % match)
 
57
 
 
58
 
 
59
class ManView(PidaView):
 
60
 
 
61
    icon_name = 'gtk-library'
 
62
    label_text = 'Man'
 
63
 
 
64
    def create_ui(self):
 
65
        self._count = 0
 
66
        self.__vbox = gtk.VBox(spacing=3)
 
67
        self.__vbox.set_border_width(6)
 
68
        self.__hbox = gtk.HBox()
 
69
        self.__entry = gtk.Entry()
 
70
        self.__entry.connect('changed', self.cb_entry_changed)
 
71
        self.__check = gtk.CheckButton(label='-k')
 
72
        self.__check.connect('toggled', self.cb_entry_changed)
 
73
        self.__list = ObjectList([
 
74
                   Column('markup', title=_('Man page'), sorted=True,
 
75
                       use_markup=True),
 
76
                   Column('description', title=_('Description'),
 
77
                       use_markup=True),
 
78
               ])
 
79
        self.__list.connect('double-click', self._on_man_double_click)
 
80
        self.__list.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
 
81
        self.__hbox.pack_start(self.__entry)
 
82
        self.__hbox.pack_start(self.__check, expand=False)
 
83
        self.__vbox.pack_start(self.__hbox, expand=False)
 
84
        self.__vbox.pack_start(self.__list)
 
85
        self.add_main_widget(self.__vbox)
 
86
        self.__vbox.show_all()
 
87
 
 
88
    def clear_items(self):
 
89
        self._count = 0
 
90
        self.__list.clear()
 
91
 
 
92
    def add_item(self, item):
 
93
        self._count += 1
 
94
        self.__list.append(item)
 
95
 
 
96
    def _on_man_double_click(self, olist, item):
 
97
        commandargs = ['/usr/bin/env', 'man', item.number, item.pattern]
 
98
        directory = os.path.dirname(commandargs[0])
 
99
        self.svc.boss.cmd('commander', 'execute',
 
100
                commandargs=commandargs,
 
101
                cwd=directory,
 
102
                icon='gnome-library',
 
103
                title='%(pattern)s(%(number)d)' % dict(
 
104
                    pattern=item.pattern,
 
105
                    number=int(item.number)
 
106
                ))
 
107
 
 
108
    def cb_entry_changed(self, w):
 
109
        options = '-f'
 
110
        if self.__check.get_active():
 
111
            options = '-k'
 
112
        self.svc.cmd_find(options=options, pattern=self.__entry.get_text())
 
113
 
 
114
    def can_be_closed(self):
 
115
        self.svc.get_action('show_man').set_active(False)
 
116
 
 
117
 
 
118
class ManActions(ActionsConfig):
 
119
 
 
120
    def create_actions(self):
 
121
        self.create_action(
 
122
            'show_man',
 
123
            TYPE_TOGGLE,
 
124
            _('Man Viewer'),
 
125
            _('Show the man'),
 
126
            '',
 
127
            self.on_show_man,
 
128
            '<Shift><Control>m',
 
129
        )
 
130
 
 
131
    def on_show_man(self, action):
 
132
        if action.get_active():
 
133
            self.svc.show_man()
 
134
        else:
 
135
            self.svc.hide_man()
 
136
 
 
137
# Service class
 
138
class Man(Service):
 
139
    """Show manpage of command"""
 
140
 
 
141
    actions_config = ManActions
 
142
 
 
143
    def start(self):
 
144
        self._view = ManView(self)
 
145
        self._has_loaded = False
 
146
        self.task = None
 
147
 
 
148
    def show_man(self):
 
149
        self.boss.cmd('window', 'add_view', paned='Terminal', view=self._view)
 
150
        if not self._has_loaded:
 
151
            self._has_loaded = True
 
152
 
 
153
    def hide_man(self):
 
154
        self.boss.cmd('window', 'remove_view', view=self._view)
 
155
 
 
156
    def cmd_find(self, options, pattern):
 
157
 
 
158
        # stop and clear task
 
159
        if self.task:
 
160
            self.task.stop()
 
161
        self._view.clear_items()
 
162
 
 
163
        # don't make empty search
 
164
        if len(pattern) <= 0:
 
165
            return
 
166
 
 
167
        # prepare command
 
168
        cmd = '/usr/bin/env man %s "%s"' % (options, pattern)
 
169
        reman = re.compile('[(]([\d]+)[)]')
 
170
 
 
171
        # match and add each line
 
172
        def _line(result):
 
173
            list = reman.findall(result)
 
174
            if not len(list):
 
175
                return
 
176
            name = result.split('(')[0].strip()
 
177
            res = result.split('- ',1)
 
178
 
 
179
            # avoid too much result
 
180
            if self._view._count > 100:
 
181
                self.task.stop()
 
182
 
 
183
            # add in list
 
184
            self._view.add_item(ManItem(name, list[0], res[1], pattern))
 
185
 
 
186
        # launch man subprocess
 
187
        self.task = GeneratorSubprocessTask(_line)
 
188
        self.task.start(cmd, shell=True)
 
189
 
 
190
    def stop(self):
 
191
        if self.task:
 
192
            self.task.stop()
 
193
        if self.get_action('show_man').get_active():
 
194
            self.hide_man()
 
195
 
 
196
 
 
197
# Required Service attribute for service loading
 
198
Service = Man
 
199
 
 
200
 
 
201
 
 
202
# vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79: