core-admin-client/qubesadmin/label.py

86 lines
2.7 KiB
Python
Raw Permalink Normal View History

# -*- encoding: utf8 -*-
#
# The Qubes OS Project, http://www.qubes-os.org
#
# Copyright (C) 2017 Marek Marczykowski-Górecki
# <marmarek@invisiblethingslab.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# 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 Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License along
# with this program; if not, see <http://www.gnu.org/licenses/>.
'''VM Labels'''
import qubesadmin.exc
2017-04-14 13:26:05 +02:00
class Label(object):
'''Label definition for virtual machines
Label specifies colour of the padlock displayed next to VM's name.
:param str color: colour specification as in HTML (``#abcdef``)
:param str name: label's name like "red" or "green"
'''
def __init__(self, app, name):
self.app = app
self._name = name
self._color = None
2017-04-14 13:26:05 +02:00
self._index = None
@property
def color(self):
'''color specification as in HTML (``#abcdef``)'''
if self._color is None:
try:
qubesd_response = self.app.qubesd_call(
'dom0', 'admin.label.Get', self._name, None)
except qubesadmin.exc.QubesDaemonNoResponseError:
raise qubesadmin.exc.QubesPropertyAccessError('label.color')
self._color = qubesd_response.decode()
return self._color
@property
def name(self):
'''label's name like "red" or "green"'''
return self._name
2017-04-14 13:26:05 +02:00
@property
def icon(self):
'''freedesktop icon name, suitable for use in
:py:meth:`PyQt4.QtGui.QIcon.fromTheme`'''
return 'appvm-' + self.name
@property
def index(self):
'''label numeric identifier'''
2017-04-14 13:26:05 +02:00
if self._index is None:
try:
qubesd_response = self.app.qubesd_call(
'dom0', 'admin.label.Index', self._name, None)
except qubesadmin.exc.QubesDaemonNoResponseError:
raise qubesadmin.exc.QubesPropertyAccessError('label.index')
2017-04-14 13:26:05 +02:00
self._index = int(qubesd_response.decode())
return self._index
def __str__(self):
return self._name
def __eq__(self, other):
if isinstance(other, Label):
return self.name == other.name
return NotImplemented
def __hash__(self):
return hash(self.name)