14
14
# You should have received a copy of the GNU General Public License along
15
15
# with this program. If not, see <http://www.gnu.org/licenses/>.
17
from gi.repository import GLib, Gio
18
from gi.repository import Unity
22
from xml.dom import minidom
21
from xml.etree import cElementTree as ET
23
from gi.repository import GLib, Gio
24
from gi.repository import Unity
24
26
APP_NAME = 'unity-scope-yelp'
25
27
LOCAL_PATH = '/usr/share/locale/'
55
57
EXTRA_METADATA = []
60
def _get_manuals_in_dir(dir, manuals):
61
"""Yield the manuals found in the given directory, omitting those
62
that have already been discovered."""
63
if not os.path.isdir(dir):
65
for manual in os.listdir(dir):
68
manualdir = os.path.join(dir, manual)
69
if os.path.isdir(manualdir):
74
def get_manuals(helpdir):
75
"""Get the manuals found in the given directory according to the
77
language, encoding = locale.getlocale()
79
if language is not None:
80
languagedir = os.path.join(helpdir, language)
81
yield from _get_manuals_in_dir(languagedir, manuals)
82
# If the locale includes a country, look for manuals
83
if language[:2] != language:
84
languagedir = os.path.join(helpdir, language[:2])
85
yield from _get_manuals_in_dir(languagedir, manuals)
86
# Now return untranslated versions of remaining manuals.
87
languagedir = os.path.join(helpdir, 'C')
88
yield from _get_manuals_in_dir(languagedir, manuals)
58
91
def get_yelp_documents():
60
93
Parses local help files for <desc> and 1st <title> tags and associates
61
94
them with the page's uri in self.data as: {uri, page description, page title}
62
95
If a help file has no <desc> or <title> tag, it is excluded.
64
language, encoding = locale.getlocale()
69
help_location = '%s%s' % (HELP_DIR, language)
70
if not os.path.exists(help_location):
71
help_location = '%s%s' % (HELP_DIR, language[:2])
72
if not os.path.exists(help_location):
73
help_location = '%s%s' % (HELP_DIR, "C")
74
for dirname in os.listdir(help_location):
75
directory = help_location + "/" + dirname
76
for filename in os.listdir(directory):
77
filename = directory + "/" + filename
78
if not os.path.isdir(filename):
79
if os.path.isfile(filename):
80
xmldoc = minidom.parse(filename)
82
pagenode = xmldoc.getElementsByTagName('page')[0]
83
if pagenode.attributes["type"].value == "topic":
85
nodes = xmldoc.getElementsByTagName('desc').item(0).childNodes
88
desc += str(node.wholeText)
90
desc += str(node.childNodes[0].wholeText)
91
desc = desc.strip(' \t\n\r')
92
title = xmldoc.getElementsByTagName('title').item(0).childNodes[0].data
96
record.append(filename)
99
record.append(dirname)
98
namespaces = {'m': 'http://projectmallard.org/1.0/'}
99
for manualdir in get_manuals(HELP_DIR):
100
for filename in os.listdir(manualdir):
101
filename = os.path.join(manualdir, filename)
102
if not (filename.endswith('page') and os.path.isfile(filename)):
105
tree = ET.parse(filename)
106
except ET.ParseError:
109
if (tree.getroot().tag != '{http://projectmallard.org/1.0/}page' or
110
tree.getroot().get('type') != 'topic'):
111
# Not a Mallard documentation file.
114
node = tree.find('m:title', namespaces)
117
node = tree.find('m:info/m:desc', namespaces)
119
desc = ''.join(node.itertext())
120
desc = desc.strip(' \t\n\r')
124
data.append((filename, title, desc, os.path.basename(manualdir)))
116
138
YELP_CACHE = get_yelp_documents()
117
139
help_data = YELP_CACHE
119
for data in help_data:
141
search = search.lower()
143
for (filename, title, desc, manual) in help_data:
120
144
if len(results) >= MAX_RESULTS:
123
if data[3] == "ubuntu-help":
147
if manual == "ubuntu-help":
124
148
icon_hint = Gio.ThemedIcon.new("distributor-logo").to_string()
126
icon_hint = Gio.ThemedIcon.new(data[3]).to_string()
150
icon_hint = Gio.ThemedIcon.new(manual).to_string()
128
152
icon_hint = Gio.ThemedIcon.new("help").to_string()
130
if search.lower() in data[1].lower():
131
results.append({'uri': data[0],
134
elif search.lower() in data[2].lower():
135
results.append({'uri': data[0],
138
elif search.lower() in data[3].lower():
139
results.append({'uri': data[0],
154
if (search in title.lower() or
155
search in desc.lower() or
156
search in manual.lower()):
157
results.append({'uri': filename,
164
class Previewer(Unity.ResultPreviewer):
167
image = Gio.ThemedIcon.new(self.result.icon_hint)
168
preview = Unity.GenericPreview.new(
169
self.result.title, html.escape(self.result.comment), image)
170
action = Unity.PreviewAction.new("open", _("Open"), None)
171
preview.add_action(action)
147
175
# Classes below this point establish communication
148
176
# with Unity, you probably shouldn't modify them.