1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
|
# Copyright (C) 2009 Canonical
#
# Authors:
# Michael Vogt
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation; version 3.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
import gettext
import glob
import locale
import logging
import os
import xapian
from xml.etree import ElementTree as ET
from xml.sax.saxutils import escape as xml_escape
from xml.sax.saxutils import unescape as xml_unescape
from softwarecenter.enums import SortMethods
# not possible not use local logger
LOG = logging.getLogger(__name__)
def get_category_by_name(categories, untrans_name):
# find a specific category
cat = [cat for cat in categories if cat.untranslated_name == untrans_name]
if cat:
return cat[0]
return None
def categories_sorted_by_name(categories):
# sort categories by name
sorted_catnames = []
# first pass, sort by translated names
for cat in categories:
sorted_catnames.append(cat.name)
sorted_catnames = sorted(sorted_catnames, cmp=locale.strcoll)
# second pass, assemble cats by sorted their sorted catnames
sorted_cats = []
for name in sorted_catnames:
for cat in categories:
if cat.name == name:
sorted_cats.append(cat)
break
return sorted_cats
class Category(object):
"""represents a menu category"""
def __init__(self, untranslated_name, name, iconname, query,
only_unallocated=True, dont_display=False, flags=[],
subcategories=[], sortmode=SortMethods.BY_ALPHABET,
item_limit=0):
if type(name) == str:
self.name = unicode(name, 'utf8').encode('utf8')
else:
self.name = name.encode('utf8')
self.untranslated_name = untranslated_name
self.iconname = iconname
for subcategory in subcategories:
query = xapian.Query(xapian.Query.OP_OR, query, subcategory.query)
self.query = query
self.only_unallocated = only_unallocated
self.subcategories = subcategories
self.dont_display = dont_display
self.flags = flags
self.sortmode = sortmode
self.item_limit = item_limit
@property
def is_forced_sort_mode(self):
return (self.sortmode != SortMethods.BY_ALPHABET)
def __str__(self):
return "<Category: name='%s', sortmode='%s', "\
"item_limit='%s'>" % (
self.name, self.sortmode, self.item_limit)
class CategoriesParser(object):
"""
Parser that is able to read the categories from a menu file
"""
def __init__(self, db):
self.db = db
def parse_applications_menu(self, datadir):
""" parse a application menu and return a list of Category objects """
categories = []
# we support multiple menu files and menu drop ins
menu_files = [datadir+"/desktop/software-center.menu"]
menu_files += glob.glob(datadir+"/menu.d/*.menu")
for f in menu_files:
tree = ET.parse(f)
root = tree.getroot()
for child in root.getchildren():
category = None
if child.tag == "Menu":
category = self._parse_menu_tag(child)
if category:
categories.append(category)
# post processing for <OnlyUnallocated>
# now build the unallocated queries, once for top-level,
# and for the subcategories. this means that subcategories
# can have a "OnlyUnallocated/" that applies only to
# unallocated entries in their sublevel
for cat in categories:
self._build_unallocated_queries(cat.subcategories)
self._build_unallocated_queries(categories)
# debug print
for cat in categories:
LOG.debug("%s %s %s" % (cat.name, cat.iconname, cat.query))
return categories
def _cat_sort_cmp(self, a, b):
"""sort helper for the categories sorting"""
#print "cmp: ", a.name, b.name
if a.untranslated_name == "System":
return 1
elif b.untranslated_name == "System":
return -1
elif a.untranslated_name == "Developer Tools":
return 1
elif b.untranslated_name == "Developer Tools":
return -1
return locale.strcoll(a.name, b.name)
def _parse_directory_tag(self, element):
from softwarecenter.db.update import DesktopConfigParser
cp = DesktopConfigParser()
fname = "/usr/share/desktop-directories/%s" % element.text
if not os.path.exists(fname):
return None
LOG.debug("reading '%s'" % fname)
cp.read(fname)
try:
untranslated_name = name = cp.get("Desktop Entry","Name")
except Exception:
LOG.warn("'%s' has no name" % fname)
return None
try:
gettext_domain = cp.get("Desktop Entry", "X-Ubuntu-Gettext-Domain")
except:
gettext_domain = None
try:
icon = cp.get("Desktop Entry","Icon")
except Exception:
icon = "applications-other"
name = cp.get_desktop("Name", translated=True)
return (untranslated_name, name, gettext_domain, icon)
def _parse_flags_tag(self, element):
flags = []
for an_elem in element.getchildren():
flags.append(an_elem.text)
return flags
def _parse_and_or_not_tag(self, element, query, xapian_op):
"""parse a <And>, <Or>, <Not> tag """
for and_elem in element.getchildren():
if and_elem.tag == "Not":
query = self._parse_and_or_not_tag(and_elem, query, xapian.Query.OP_AND_NOT)
elif and_elem.tag == "Or":
or_elem = self._parse_and_or_not_tag(and_elem, xapian.Query(), xapian.Query.OP_OR)
query = xapian.Query(xapian.Query.OP_AND, or_elem, query)
elif and_elem.tag == "Category":
LOG.debug("adding: %s" % and_elem.text)
q = xapian.Query("AC"+and_elem.text.lower())
query = xapian.Query(xapian_op, query, q)
elif and_elem.tag == "SCSection":
LOG.debug("adding section: %s" % and_elem.text)
# we have the section once in apt-xapian-index and once
# in our own DB this is why we need two prefixes
# FIXME: ponder if it makes sense to simply write
# out XS in update-software-center instead of AE?
q = xapian.Query(xapian.Query.OP_OR,
xapian.Query("XS"+and_elem.text.lower()),
xapian.Query("AE"+and_elem.text.lower()))
query = xapian.Query(xapian_op, query, q)
elif and_elem.tag == "SCType":
LOG.debug("adding type: %s" % and_elem.text)
q = xapian.Query("AT"+and_elem.text.lower())
query = xapian.Query(xapian_op, query, q)
elif and_elem.tag == "SCChannel":
LOG.debug("adding channel: %s" % and_elem.text)
q = xapian.Query("AH"+and_elem.text.lower())
query = xapian.Query(xapian_op, query, q)
elif and_elem.tag == "SCOrigin":
LOG.debug("adding origin: %s" % and_elem.text)
# FIXME: origin is currently case-sensitive?!?
q = xapian.Query("XOO"+and_elem.text)
query = xapian.Query(xapian_op, query, q)
elif and_elem.tag == "SCPkgname":
LOG.debug("adding tag: %s" % and_elem.text)
# query both axi and s-c
q1 = xapian.Query("AP"+and_elem.text.lower())
q = xapian.Query(xapian.Query.OP_OR, q1,
xapian.Query("XP"+and_elem.text.lower()))
query = xapian.Query(xapian_op, query, q)
elif and_elem.tag == "SCPkgnameWildcard":
LOG.debug("adding tag: %s" % and_elem.text)
# query both axi and s-c
s = "pkg_wildcard:%s" % and_elem.text.lower()
q = self.db.xapian_parser.parse_query(s, xapian.QueryParser.FLAG_WILDCARD)
query = xapian.Query(xapian_op, query, q)
else:
LOG.warn("UNHANDLED: %s %s" % (and_elem.tag, and_elem.text))
return query
def _parse_include_tag(self, element):
for include in element.getchildren():
if include.tag == "Or":
query = xapian.Query()
return self._parse_and_or_not_tag(include, query, xapian.Query.OP_OR)
if include.tag == "And":
query = xapian.Query("")
return self._parse_and_or_not_tag(include, query, xapian.Query.OP_AND)
# without "and" tag we take the first entry
elif include.tag == "Category":
return xapian.Query("AC"+include.text.lower())
else:
LOG.warn("UNHANDLED: _parse_include_tag: %s" % include.tag)
# empty query matches all
return xapian.Query("")
def _parse_menu_tag(self, item):
name = None
untranslated_name = None
query = None
icon = None
only_unallocated = False
dont_display = False
flags = []
subcategories = []
sortmode = SortMethods.BY_ALPHABET
item_limit = 0
for element in item.getchildren():
# ignore inline translations, we use gettext for this
if (element.tag == "Name" and
'{http://www.w3.org/XML/1998/namespace}lang' in element.attrib):
continue
if element.tag == "Name":
untranslated_name = element.text
# gettext/xml writes stuff from software-center.menu
# out into the pot as escaped xml, so we need to escape
# the name first, get the translation and unscape it again
escaped_name = xml_escape(untranslated_name)
name = xml_unescape(gettext.gettext(escaped_name))
elif element.tag == "SCIcon":
icon = element.text
elif element.tag == 'Flags':
flags = self._parse_flags_tag(element)
elif element.tag == "Directory":
l = self._parse_directory_tag(element)
if l:
(untranslated_name, name, gettext_domain, icon) = l
elif element.tag == "Include":
query = self._parse_include_tag(element)
elif element.tag == "OnlyUnallocated":
only_unallocated = True
elif element.tag == "SCDontDisplay":
dont_display = True
elif element.tag == "SCSortMode":
sortmode = int(element.text)
if not self._verify_supported_sort_mode(sortmode):
return None
elif element.tag == "SCItemLimit":
item_limit = int(element.text)
elif element.tag == "Menu":
subcat = self._parse_menu_tag(element)
if subcat:
subcategories.append(subcat)
else:
LOG.warn("UNHANDLED tag in _parse_menu_tag: %s" % element.tag)
if untranslated_name and query:
return Category(untranslated_name, name, icon, query, only_unallocated, dont_display, flags, subcategories, sortmode, item_limit)
else:
LOG.warn("UNHANDLED entry: %s %s %s %s" % (name,
untranslated_name,
icon,
query))
return None
def _verify_supported_sort_mode(self, sortmode):
""" verify that we use a sortmode that we know and can handle """
# always supported
if sortmode in (SortMethods.UNSORTED,
SortMethods.BY_ALPHABET,
SortMethods.BY_TOP_RATED,
SortMethods.BY_SEARCH_RANKING):
return True
# only supported with a apt-xapian-index version that has the
# "catalogedtime" value
elif sortmode == SortMethods.BY_CATALOGED_TIME:
if self.db._axi_values and "catalogedtime" in self.db._axi_values:
return True
else:
LOG.warn("sort by cataloged time requested but your a-x-i "
"does not seem to support that yet")
return False
# we don't know this sortmode
LOG.error("unknown sort mode '%i'" % sortmode)
return False
def _build_unallocated_queries(self, categories):
for cat_unalloc in categories:
if not cat_unalloc.only_unallocated:
continue
for cat in categories:
if cat.name != cat_unalloc.name:
cat_unalloc.query = xapian.Query(xapian.Query.OP_AND_NOT, cat_unalloc.query, cat.query)
#print cat_unalloc.name, cat_unalloc.query
return
# static category mapping for the tiles
category_cat = {
'Utility': 'Accessories',
'System': 'Accessories',
'Education': 'Education',
'Game': 'Games',
'Sports': 'Games',
'Graphics': 'Graphics',
'Network': 'Internet',
'Office': 'Office',
'Science': 'Science & Engineering',
'Audio': 'Sound & Video',
'AudioVideo': 'Sound & Video',
'Video': 'Sound & Video',
'Settings': 'Themes & Tweaks',
'Accessibility': 'Universal Access',
'Development': 'Developer Tools',}
category_subcat = {
'BoardGame': 'Games;Board Games',
'CardGame': 'Games;Card Games',
'LogicGame': 'Games;Puzzles',
'RolePlaying': 'Games;Role Playing',
'SportsGame': 'Games;Sports',
'3DGraphics': 'Graphics;3D Graphics',
'VectorGraphics': 'Graphics;Drawing',
'RasterGraphics': 'Graphics;Painting',
'Photography': 'Graphics;Photography',
'Publishing': 'Graphics;Publishing',
'Scanning': 'Graphics;Scanning & OCR',
'OCR': 'Graphics;Scanning & OCR',
'Viewer': 'Graphics;Viewers',
'InstantMessaging': 'Internet;Chat',
'IRCClient': 'Internet;Chat',
'FileTransfer': 'Internet;File Sharing',
'Email': 'Internet;Mail',
'WebBrowser': 'Internet;Web Browsers',
'Astronomy': 'Science & Engineering;Astronomy',
'Biology': 'Science & Engineering;Biology',
'Chemistry': 'Science & Engineering;Chemistry',
'ArtificialIntelligence': 'Science & Engineering;Computing & Robotics',
'ComputerScience': 'Science & Engineering;Computing & Robotics',
'Robotics': 'Science & Engineering;Computing & Robotics',
'Electronics': 'Science & Engineering;Electronics',
'Engineering': 'Science & Engineering;Engineering',
'Geography': 'Science & Engineering;Geography',
'Geology': 'Science & Engineering;Geology',
'Geoscience': 'Science & Engineering;Geology',
'DataVisualization': 'Science & Engineering;Mathematics',
'Math': 'Science & Engineering;Mathematics',
'NumericalAnalysis': 'Science & Engineering;Mathematics',
'MedicalSoftware': 'Science & Engineering;Medicine',
'Electricity': 'Science & Engineering;Physics',
'Physics': 'Science & Engineering;Physics',
'Debugger': 'Developer Tools;Debugging',
'GUIDesigner': 'Developer Tools;Graphic Interface Design',
'IDE': 'Developer Tools;IDEs',
'Translation': 'Developer Tools;Localization',
'Profiling': 'Developer Tools;Profiling',
'RevisionControl': 'Developer Tools;Version Control',
'WebDevelopment': 'Developer Tools;Web Development',}
|