qubespolicy: make pylint happy

This include refactoring out one-function-class GtkIconGetter.

QubesOS/qubes-issues#910
This commit is contained in:
Marek Marczykowski-Górecki 2017-04-06 16:29:49 +02:00
parent a3da85bfda
commit e5ad26c090
No known key found for this signature in database
GPG Key ID: 063938BA42CFA724
6 changed files with 35 additions and 30 deletions

View File

@ -6,3 +6,4 @@ jinja2
lxml
pylint
sphinx
pydbus

View File

@ -23,13 +23,17 @@
decisions.'''
import pydbus
# pylint: disable=import-error,wrong-import-position
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import GLib
# pylint: enable=import-error
import qubespolicy.rpcconfirmation
# pylint: enable=wrong-import-position
class PolicyAgent(object):
# pylint: disable=too-few-public-methods
dbus = """
<node>
<interface name='org.qubesos.PolicyAgent'>
@ -45,8 +49,10 @@ class PolicyAgent(object):
</node>
"""
def Ask(self, source, service_name, targets, default_target,
@staticmethod
def Ask(source, service_name, targets, default_target,
icons):
# pylint: disable=invalid-name
entries_info = {}
for target in targets:
entries_info[target] = {}

View File

@ -19,40 +19,36 @@
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
import gi
import itertools
# pylint: disable=import-error,wrong-import-position
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Gdk, GdkPixbuf, GObject, GLib
# pylint: enable=import-error
from qubespolicy.utils import sanitize_domain_name
# pylint: enable=wrong-import-position
class GtkIconGetter:
def __init__(self, size):
class VMListModeler:
def __init__(self, domains_info=None):
self._entries = {}
self._domains_info = domains_info
self._icons = {}
self._size = size
self._icon_size = 16
self._theme = Gtk.IconTheme.get_default()
self._create_entries()
def get_icon(self, name):
def _get_icon(self, name):
if name not in self._icons:
try:
icon = self._theme.load_icon(name, self._size, 0)
except GLib.Error:
icon = self._theme.load_icon("edit-find", self._size, 0)
icon = self._theme.load_icon(name, self._icon_size, 0)
except GLib.Error: # pylint: disable=catching-non-exception
icon = self._theme.load_icon("edit-find", self._icon_size, 0)
self._icons[name] = icon
return self._icons[name]
class VMListModeler:
def __init__(self, domains_info=None):
self._icon_getter = GtkIconGetter(16)
self._entries = {}
self._domains_info = domains_info
self._create_entries()
def _create_entries(self):
for name, vm in self._domains_info.items():
if name.startswith('$dispvm:'):
@ -63,7 +59,7 @@ class VMListModeler:
dispvm = False
sanitize_domain_name(vm_name, assert_sanitized=True)
icon = self._icon_getter.get_icon(vm.get('icon', None))
icon = self._get_icon(vm.get('icon', None))
if dispvm:
display_name = 'Disposable VM ({})'.format(vm_name)
@ -125,7 +121,7 @@ class VMListModeler:
list_store = Gtk.ListStore(int, str, GdkPixbuf.Pixbuf)
for entry_no, display_name in zip(itertools.count(),
sorted(self._entries.keys())):
sorted(self._entries)):
entry = self._entries[display_name]
if entry['api_name'] in vm_list:
list_store.append([entry_no, display_name, entry['icon']])
@ -199,6 +195,7 @@ class VMListModeler:
class GtkOneTimerHelper:
# pylint: disable=too-few-public-methods
def __init__(self, wait_seconds):
self._wait_seconds = wait_seconds
self._current_timer_id = 0

View File

@ -18,15 +18,17 @@
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
import pkg_resources
from gi.repository import Gtk, Gdk, GLib
import os
from gi.repository import Gtk, Gdk, GLib # pylint: disable=import-error
import pkg_resources
from qubespolicy.gtkhelpers import VMListModeler, FocusStealingHelper
from qubespolicy.utils import sanitize_domain_name, \
sanitize_service_name
class RPCConfirmationWindow:
# pylint: disable=too-few-public-methods
_source_file = pkg_resources.resource_filename('qubespolicy',
os.path.join('glade', "RPCConfirmationWindow.glade"))
_source_id = {'window': "RPCConfirmationWindow",
@ -110,7 +112,8 @@ class RPCConfirmationWindow:
def _can_perform_action(self):
return self._focus_helper.can_perform_action()
def _escape_and_format_rpc_text(self, rpc_operation):
@staticmethod
def _escape_and_format_rpc_text(rpc_operation):
escaped = GLib.markup_escape_text(rpc_operation)
partitioned = escaped.partition('.')
@ -161,7 +164,7 @@ class RPCConfirmationWindow:
self._escape_and_format_rpc_text(rpc_operation))
self._entries_info = entries_info
list_modeler = self._new_VM_list_modeler()
list_modeler = self._new_vm_list_modeler()
list_modeler.apply_model(self._rpc_combo_box, targets_list,
selection_trigger=self._update_ok_button_sensitivity,
@ -186,7 +189,7 @@ class RPCConfirmationWindow:
Gtk.main()
def _new_VM_list_modeler(self):
def _new_vm_list_modeler(self):
return VMListModeler(self._entries_info)
def _new_focus_stealing_helper(self):
@ -209,4 +212,3 @@ def confirm_rpc(entries_info, source, rpc_operation, targets_list, target=None):
targets_list, target)
return window.confirm_rpc()

View File

@ -30,7 +30,7 @@ from qubespolicy.rpcconfirmation import RPCConfirmationWindow
class MockRPCConfirmationWindow(RPCConfirmationWindow):
def _new_VM_list_modeler(self):
def _new_vm_list_modeler(self):
return VMListModeler(mock_domains_info)
def _new_focus_stealing_helper(self):

View File

@ -56,4 +56,3 @@ def sanitize_domain_name(input_string, assert_sanitized=False):
def sanitize_service_name(input_string, assert_sanitized=False):
return _sanitize_name(input_string, {'+'}, assert_sanitized)