~markjtully/askubuntu-lens/packaging

« back to all changes in this revision

Viewing changes to scopes/manpages/unity-manpages-daemon

  • Committer: Mark Tully
  • Date: 2012-07-04 23:20:18 UTC
  • Revision ID: markjtully@gmail.com-20120704232018-fzylppygcve5dj0x
Formatted python files to conform to PEP8 guidelines & fixed some issues found by pylint

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#!/usr/bin/python
 
2
# -*- coding: utf-8 -*-
2
3
 
 
4
"""
3
5
#    Copyright (C) 2012  Pawel Stolowski <stolowski@gmail.com>
4
6
#                  2012  Mark Tully <markjtully@gmail.com>
5
7
 
15
17
 
16
18
#    You should have received a copy of the GNU General Public License
17
19
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
20
"""
18
21
 
19
 
import sys, locale, re
 
22
import sys
 
23
import re
20
24
import os.path
21
25
 
22
26
# Unity imports
38
42
TECHNICAL = 3
39
43
TAGS = 4
40
44
 
 
45
 
41
46
class Daemon (object):
 
47
    """ Manpages Daemon searches manpages for results matching query
 
48
    """
42
49
    def __init__(self):
43
50
        """ Sets up the manpages scope
44
51
        """
45
 
        self.scope = Unity.Scope.new ("/net/launchpad/scope/help/manpages")
 
52
        self.scope = Unity.Scope.new("/net/launchpad/scope/help/manpages")
46
53
 
47
54
        # Listen for changes and requests
48
55
        self.scope.props.search_in_global = False
49
 
        self.scope.connect ("search-changed",  self.on_search_changed)
50
 
        self.scope.connect ("filters-changed", self.on_filters_changed)
51
 
        self.scope.connect ("notify::active",  self.on_lens_active)
52
 
        self.scope.export ()
53
 
    
 
56
        self.scope.connect("search-changed", self.on_search_changed)
 
57
        self.scope.connect("filters-changed", self.on_filters_changed)
 
58
        self.scope.connect("notify::active", self.on_lens_active)
 
59
        self.scope.export()
 
60
 
54
61
    def on_filters_changed(self, *_):
55
62
        """ Called when a filter is clicked.  Queue's a new search
56
63
        """
62
69
        if self.scope.props.active:
63
70
            self.scope.queue_search_changed(Unity.SearchType.DEFAULT)
64
71
 
65
 
    def on_search_changed (self, scope, search=None, search_type=0, cancellable=None):
66
 
        """ Called when the search is changed.  Gets the search string and search 
 
72
    def on_search_changed(self, scope, search=None, search_type=0, cancellable=None):
 
73
        """ Called when the search is changed.  Gets the search string and search
67
74
        type and passes it to update_results_model()
68
75
        Args:
69
76
          scope: a scope object
82
89
            results = scope.props.global_results_model
83
90
 
84
91
        print "Search changed to: '%s'" % search_string
85
 
        self.update_results_model (search_string, results)
 
92
        self.update_results_model(search_string, results)
86
93
        if hasattr(search, "finished"):
87
94
            search.finished()
88
95
 
102
109
        scope_active = False
103
110
        e = self.scope.get_filter("sources")
104
111
        if not e.props.filtering:
105
 
          scope_active = True
 
112
            scope_active = True
106
113
        for source in e.options:
107
 
          if source.props.id == "manpages":
108
 
            if source.props.active:
109
 
              scope_active = True
110
 
              
 
114
            if source.props.id == "manpages":
 
115
                if source.props.active:
 
116
                    scope_active = True
 
117
 
111
118
        if scope_active:
112
 
          import subprocess
113
 
          icon_hint = Gio.ThemedIcon.new("text-x-generic").to_string()
114
 
          result = []
115
 
          if len(search) > 2:   # don't run apropos for strings shorter than 3 chars
116
 
            apropos = subprocess.Popen(["apropos", search], stdout=subprocess.PIPE)
117
 
            os.waitpid(apropos.pid, 0)
118
 
            out = apropos.communicate()[0]
119
 
            for line in out.split("\n"):
120
 
                regex_apropos = re.compile('^(.+?)\s+\((\d+)\)\s+-\s(.+?)$')
121
 
                m = regex_apropos.match(line)
122
 
                if m:
123
 
                    result.append((m.group(1), m.group(2), m.group(3)))
124
 
          for res in result:
125
 
            uri = 'man:' + res[0] + '(' + res[1] + ')'
126
 
            icon_theme = Gtk.IconTheme()
127
 
            icon_info = icon_theme.lookup_icon(res[0], 128, 0)
128
 
            if icon_info:
129
 
              icon_hint = Gio.ThemedIcon.new(res[0]).to_string()
130
 
            model.append(uri, icon_hint, TECHNICAL, 'x-scheme-handler/man', res[0], res[2], uri)
 
119
            import subprocess
 
120
            icon_hint = Gio.ThemedIcon.new("text-x-generic").to_string()
 
121
            result = []
 
122
            if len(search) > 2:   # don't run apropos for strings shorter than 3 chars
 
123
                apropos = subprocess.Popen(["apropos", search], stdout=subprocess.PIPE)
 
124
                os.waitpid(apropos.pid, 0)
 
125
                out = apropos.communicate()[0]
 
126
                for line in out.split("\n"):
 
127
                    regex_apropos = re.compile('^(.+?)\s+\((\d+)\)\s+-\s(.+?)$')
 
128
                    m = regex_apropos.match(line)
 
129
                    if m:
 
130
                        result.append((m.group(1), m.group(2), m.group(3)))
 
131
            for res in result:
 
132
                uri = 'man:' + res[0] + '(' + res[1] + ')'
 
133
                icon_theme = Gtk.IconTheme()
 
134
                icon_info = icon_theme.lookup_icon(res[0], 128, 0)
 
135
                if icon_info:
 
136
                    icon_hint = Gio.ThemedIcon.new(res[0]).to_string()
 
137
                model.append(uri, icon_hint, TECHNICAL, 'x-scheme-handler/man', res[0], res[2], uri)
131
138
 
132
139
if __name__ == "__main__":
133
140
    session_bus_connection = Gio.bus_get_sync(Gio.BusType.SESSION, None)