|
Lines 121-128
import copy
Link Here
|
| 121 |
import os |
121 |
import os |
| 122 |
import sys |
122 |
import sys |
| 123 |
import re |
123 |
import re |
| 124 |
import xml.parsers.expat |
124 |
from lxml import etree |
| 125 |
import xml.etree.cElementTree as ET |
|
|
| 126 |
|
125 |
|
| 127 |
from .tools import JSON_Object, JSON_List |
126 |
from .tools import JSON_Object, JSON_List |
| 128 |
from .log import RESOURCES |
127 |
from .log import RESOURCES |
|
Lines 272-288
class Link(Module):
Link Here
|
| 272 |
pass |
271 |
pass |
| 273 |
|
272 |
|
| 274 |
|
273 |
|
| 275 |
class XML_Definition(ET.ElementTree): |
274 |
class XML_Definition(object): |
| 276 |
|
275 |
|
| 277 |
'''container for the interface description of a module''' |
276 |
'''container for the interface description of a module''' |
| 278 |
|
277 |
|
| 279 |
def __init__(self, root=None, filename=None): |
278 |
def __init__(self, root=None): |
| 280 |
ET.ElementTree.__init__(self, element=root, file=filename) |
279 |
self.tree = etree.ElementTree(root) |
| 281 |
self.root = self.getroot() |
280 |
self.root = root |
| 282 |
|
281 |
|
| 283 |
@property |
282 |
@property |
| 284 |
def name(self): |
283 |
def name(self): |
| 285 |
return self.findtext('name') |
284 |
return self.tree.findtext('name') |
| 286 |
|
285 |
|
| 287 |
@property |
286 |
@property |
| 288 |
def version(self): |
287 |
def version(self): |
|
Lines 290-304
class XML_Definition(ET.ElementTree):
Link Here
|
| 290 |
|
289 |
|
| 291 |
@property |
290 |
@property |
| 292 |
def url(self): |
291 |
def url(self): |
| 293 |
return self.findtext('url') |
292 |
return self.tree.findtext('url') |
| 294 |
|
293 |
|
| 295 |
@property |
294 |
@property |
| 296 |
def description(self): |
295 |
def description(self): |
| 297 |
return self.findtext('description') |
296 |
return self.tree.findtext('description') |
| 298 |
|
297 |
|
| 299 |
@property |
298 |
@property |
| 300 |
def keywords(self): |
299 |
def keywords(self): |
| 301 |
return KEYWORD_PATTERN.split(self.findtext('keywords', '')) + [self.name] |
300 |
return KEYWORD_PATTERN.split(self.tree.findtext('keywords', '')) + [self.name] |
| 302 |
|
301 |
|
| 303 |
@property |
302 |
@property |
| 304 |
def id(self): |
303 |
def id(self): |
|
Lines 338-344
class XML_Definition(ET.ElementTree):
Link Here
|
| 338 |
@property |
337 |
@property |
| 339 |
def flavors(self): |
338 |
def flavors(self): |
| 340 |
'''Retrieve list of flavor objects''' |
339 |
'''Retrieve list of flavor objects''' |
| 341 |
for elem in self.findall('flavor'): |
340 |
for elem in self.tree.findall('flavor'): |
| 342 |
name = elem.findtext('name') |
341 |
name = elem.findtext('name') |
| 343 |
priority = None |
342 |
priority = None |
| 344 |
try: |
343 |
try: |
|
Lines 366-376
class XML_Definition(ET.ElementTree):
Link Here
|
| 366 |
|
365 |
|
| 367 |
@property |
366 |
@property |
| 368 |
def categories(self): |
367 |
def categories(self): |
| 369 |
return [elem.get('name') for elem in self.findall('categories/category')] |
368 |
return [elem.get('name') for elem in self.tree.findall('categories/category')] |
| 370 |
|
369 |
|
| 371 |
def commands(self): |
370 |
def commands(self): |
| 372 |
'''Generator to iterate over the commands''' |
371 |
'''Generator to iterate over the commands''' |
| 373 |
for command in self.findall('command'): |
372 |
for command in self.tree.findall('command'): |
| 374 |
yield command.get('name') |
373 |
yield command.get('name') |
| 375 |
|
374 |
|
| 376 |
def get_module(self): |
375 |
def get_module(self): |
|
Lines 383-389
class XML_Definition(ET.ElementTree):
Link Here
|
| 383 |
priority=self.priority, |
382 |
priority=self.priority, |
| 384 |
keywords=self.keywords, |
383 |
keywords=self.keywords, |
| 385 |
translationId=self.translationId, |
384 |
translationId=self.translationId, |
| 386 |
required_commands=[cat.get('name') for cat in self.findall('requiredCommands/requiredCommand')], |
385 |
required_commands=[cat.get('name') for cat in self.tree.findall('requiredCommands/requiredCommand')], |
| 387 |
version=self.version, |
386 |
version=self.version, |
| 388 |
) |
387 |
) |
| 389 |
|
388 |
|
|
Lines 395-406
class XML_Definition(ET.ElementTree):
Link Here
|
| 395 |
|
394 |
|
| 396 |
def get_command(self, name): |
395 |
def get_command(self, name): |
| 397 |
'''Retrieves details of a command''' |
396 |
'''Retrieves details of a command''' |
| 398 |
for command in self.findall('command'): |
397 |
for command in self.tree.findall('command'): |
| 399 |
if command.get('name') == name: |
398 |
if command.get('name') == name: |
| 400 |
return Command(name, command.get('function'), command.get('allow_anonymous', '0').lower() in ('yes', 'true', '1')) |
399 |
return Command(name, command.get('function'), command.get('allow_anonymous', '0').lower() in ('yes', 'true', '1')) |
| 401 |
|
400 |
|
| 402 |
def __bool__(self): |
401 |
def __bool__(self): |
| 403 |
module = self.find('module') |
402 |
module = self.tree.find('module') |
| 404 |
return module is not None and len(module) != 0 |
403 |
return module is not None and len(module) != 0 |
| 405 |
__nonzero__ = __bool__ |
404 |
__nonzero__ = __bool__ |
| 406 |
|
405 |
|
|
Lines 433-448
class Manager(dict):
Link Here
|
| 433 |
if not filename.endswith('.xml'): |
432 |
if not filename.endswith('.xml'): |
| 434 |
continue |
433 |
continue |
| 435 |
try: |
434 |
try: |
| 436 |
parsed_xml = ET.parse(os.path.join(Manager.DIRECTORY, filename)) |
435 |
parsed_xml = etree.parse(os.path.join(Manager.DIRECTORY, filename)) |
| 437 |
RESOURCES.info('Loaded module %s' % filename) |
436 |
RESOURCES.info('Loaded module %s' % filename) |
| 438 |
for mod_tree in parsed_xml.getroot(): |
437 |
for mod_element in parsed_xml.getroot().iterchildren(tag=etree.Element): |
| 439 |
mod = XML_Definition(root=mod_tree) |
438 |
mod = XML_Definition(root=mod_element) |
| 440 |
if mod.deactivated: |
439 |
if mod.deactivated: |
| 441 |
RESOURCES.info('Module is deactivated: %s' % filename) |
440 |
RESOURCES.info('Module is deactivated: %s' % filename) |
| 442 |
continue |
441 |
continue |
| 443 |
# save list of definitions in self |
442 |
# save list of definitions in self |
| 444 |
self.setdefault(mod.id, []).append(mod) |
443 |
self.setdefault(mod.id, []).append(mod) |
| 445 |
except (xml.parsers.expat.ExpatError, ET.ParseError) as exc: |
444 |
except (etree.XMLSyntaxError,) as exc: |
| 446 |
RESOURCES.warn('Failed to load module %s: %s' % (filename, exc)) |
445 |
RESOURCES.warn('Failed to load module %s: %s' % (filename, exc)) |
| 447 |
continue |
446 |
continue |
| 448 |
|
447 |
|