QubesVmLabel: XML parsing

This commit is contained in:
Wojtek Porczyk 2014-11-21 16:51:59 +01:00
parent 6c68bd062e
commit 6146c8e466
2 changed files with 60 additions and 0 deletions

View File

@ -8,6 +8,8 @@ __author__ = 'Invisible Things Lab'
__license__ = 'GPLv2 or later'
__version__ = 'R3'
import ast
import qubes._pluginloader
class QubesException(Exception):
@ -168,6 +170,21 @@ class QubesVmLabel(object):
#: freedesktop icon name, suitable for use in :py:meth:`PyQt4.QtGui.QIcon.fromTheme`
self.icon = '{}-{}'.format(('dispvm' if dispvm else 'appvm'), name)
@classmethod
def fromxml(cls, xml):
'''Create label definition from XML node
:param :py:class:`lxml.etree._Element` xml: XML node reference
:rtype: :py:class:`qubes.QubesVmLabel`
'''
index = int(xml.get('id').split('-', 1)[1])
color = xml.get('color')
name = xml.text
dispvm = ast.literal_eval(xml.get('dispvm', 'False'))
return cls(index, color, name, dispvm)
def __repr__(self):
return '{}({!r}, {!r}, {!r}, dispvm={!r})'.format(
self.__class__.__name__,

43
tests/init.py Normal file
View File

@ -0,0 +1,43 @@
#!/usr/bin/python2 -O
import sys
import unittest
import lxml.etree
sys.path.insert(0, '../')
import qubes
class TC_QubesVmLabel(unittest.TestCase):
def test_000_appvm(self):
xml = lxml.etree.XML('''
<qubes version="3">
<labels>
<label id="label-1" color="#cc0000">red</label>
</labels>
</qubes>
''')
node = xml.xpath('//label')[0]
label = qubes.QubesVmLabel.fromxml(node)
self.assertEqual(label.index, 1)
self.assertEqual(label.color, '#cc0000')
self.assertEqual(label.name, 'red')
self.assertEqual(label.dispvm, False)
self.assertEqual(label.icon, 'appvm-red')
def test_001_dispvm(self):
xml = lxml.etree.XML('''
<qubes version="3">
<labels>
<label id="label-2" color="#cc0000" dispvm="True">red</label>
</labels>
</qubes>
''')
node = xml.xpath('//label')[0]
label = qubes.QubesVmLabel.fromxml(node)
self.assertEqual(label.dispvm, True)
self.assertEqual(label.icon, 'dispvm-red')