From 0dbcdb8c0db282a60f85e79c3697ce380a5b4554 Mon Sep 17 00:00:00 2001 From: Jason Mehring Date: Wed, 7 Jan 2015 08:22:12 -0500 Subject: [PATCH] qubes: pep8 fixes ------------------------------------------------------------------------------- ISSUES: ------------------------------------------------------------------------------- - Some auto-corrected code (when line is too long) may still be over-indented. It can be manually chaged and it will be left alone, or is it acceptable as I am not sure how strict your rule is for under-indented lines for which context. If you want this only indented 4 spaces, I can work on it some more. [Also @ ~line:385 in new file] For example, __init__.py:382 OLD: def __contains__(self, key): return any((key == vm or key == vm.qid or key == vm.name) for vm in self) NEW: def __contains__(self, key): return any((key == vm or key == vm.qid or key == vm.name) for vm in self) - will not detect if there are more than 2 spaces between function methods ------------------------------------------------------------------------------- FIXED: ------------------------------------------------------------------------------- - Now uses the most horizontial space and does not use excessive lines when splitting a line - __init__:489 - '#' comments being indented for some lines and not others; would like no indent - Only happens if line preceeding comment ends in a ':' E128 - Fix visual indentation E128 - Fix a badly indented line [Now allows under-indented lines] E309 - Add missing blank line (after class declaration) [No longer adds it] E303 - Remove extra blank lines [Now allows 2 blank lines between function defs] [TODO: Create definition to enforce this] Conflicts: qubes/__init__.py --- qubes/__init__.py | 74 ++++++++++++++++++++++++++++++----------------- 1 file changed, 47 insertions(+), 27 deletions(-) diff --git a/qubes/__init__.py b/qubes/__init__.py index 06b6db0a..6a396f5c 100644 --- a/qubes/__init__.py +++ b/qubes/__init__.py @@ -41,7 +41,7 @@ elif os.name == 'nt': import win32file import pywintypes else: - raise RuntimeError, "Qubes works only on POSIX or WinNT systems" + raise RuntimeError("Qubes works only on POSIX or WinNT systems") import libvirt try: @@ -61,8 +61,10 @@ class QubesException(Exception): '''Exception that can be shown to the user''' pass + class VMMConnection(object): '''Connection to Virtual Machine Manager (libvirt)''' + def __init__(self): self._libvirt_conn = None self._xs = None @@ -77,7 +79,8 @@ class VMMConnection(object): @offline_mode.setter def offline_mode(self, value): if value and self._libvirt_conn is not None: - raise QubesException("Cannot change offline mode while already connected") + raise QubesException( + "Cannot change offline mode while already connected") self._offline_mode = value @@ -157,7 +160,7 @@ class QubesHost(object): (model, memory, cpus, mhz, nodes, socket, cores, threads) = \ self._app.vmm.libvirt_conn.getInfo() - self._total_mem = long(memory)*1024 + self._total_mem = long(memory) * 1024 self._no_cpus = cpus self.app.log.debug('QubesHost: no_cpus={} memory_total={}'.format(self.no_cpus, self.memory_total)) @@ -219,7 +222,7 @@ class QubesHost(object): for vm in info: previous[vm['domid']] = {} previous[vm['domid']]['cpu_time'] = ( - vm['cpu_time'] / vm['online_vcpus']) + vm['cpu_time'] / vm['online_vcpus']) previous[vm['domid']]['cpu_usage'] = 0 time.sleep(wait_time) @@ -233,12 +236,12 @@ class QubesHost(object): for vm in info: current[vm['domid']] = {} current[vm['domid']]['cpu_time'] = ( - vm['cpu_time'] / max(vm['online_vcpus'], 1)) + vm['cpu_time'] / max(vm['online_vcpus'], 1)) if vm['domid'] in previous.keys(): current[vm['domid']]['cpu_usage'] = ( float(current[vm['domid']]['cpu_time'] - previous[vm['domid']]['cpu_time']) / - long(1000**3) / (current_time-previous_time) * 100) + long(1000 ** 3) / (current_time - previous_time) * 100) if current[vm['domid']]['cpu_usage'] < 0: # VM has been rebooted current[vm['domid']]['cpu_usage'] = 0 @@ -294,7 +297,8 @@ class Label(object): def __xml__(self): - element = lxml.etree.Element('label', id='label-' + self.index, color=self.color) + element = lxml.etree.Element( + 'label', id='label-' + self.index, color=self.color) element.text = self.name return element @@ -324,7 +328,8 @@ class Label(object): .. deprecated:: 2.0 use :py:meth:`PyQt4.QtGui.QIcon.fromTheme` and :py:attr:`icon_dispvm` ''' - return os.path.join(system_path['qubes_icon_dir'], self.icon_dispvm) + ".png" + return os.path.join( + system_path['qubes_icon_dir'], self.icon_dispvm) + ".png" class VMCollection(object): @@ -342,7 +347,8 @@ class VMCollection(object): def __repr__(self): - return '<{} {!r}>'.format(self.__class__.__name__, list(sorted(self.keys()))) + return '<{} {!r}>'.format( + self.__class__.__name__, list(sorted(self.keys()))) def items(self): @@ -394,7 +400,8 @@ class VMCollection(object): # this violates duck typing, but is needed # for VMProperty to function correctly if not isinstance(value, qubes.vm.BaseVM): - raise TypeError('{} holds only BaseVM instances'.format(self.__class__.__name__)) + raise TypeError( + '{} holds only BaseVM instances'.format(self.__class__.__name__)) if not hasattr(value, 'qid'): value.qid = self.domains.get_new_unused_qid() @@ -435,7 +442,8 @@ class VMCollection(object): def __contains__(self, key): - return any((key == vm or key == vm.qid or key == vm.name) for vm in self) + return any((key == vm or key == vm.qid or key == vm.name) + for vm in self) def __len__(self): @@ -478,7 +486,7 @@ class VMCollection(object): def get_new_unused_netid(self): - used_ids = set([vm.netid for vm in self]) # if vm.is_netvm()]) + used_ids = set([vm.netid for vm in self]) # if vm.is_netvm()]) for i in range(1, MAX_NETID): if i not in used_ids: return i @@ -536,7 +544,8 @@ class property(object): load_stage=2, order=0, save_via_ref=False, doc=None): self.__name__ = name self._setter = setter - self._saver = saver if saver is not None else (lambda self, prop, value: str(value)) + self._saver = saver if saver is not None else ( + lambda self, prop, value: str(value)) self._type = type self._default = default self.order = order @@ -560,7 +569,8 @@ class property(object): except AttributeError: if self._default is self._NO_DEFAULT: - raise AttributeError('property {!r} not set'.format(self.__name__)) + raise AttributeError( + 'property {!r} not set'.format(self.__name__)) elif isinstance(self._default, collections.Callable): return self._default(instance) else: @@ -584,14 +594,16 @@ class property(object): value = self._type(value) if has_oldvalue: - instance.fire_event_pre('property-pre-set:' + self.__name__, value, oldvalue) + instance.fire_event_pre( + 'property-pre-set:' + self.__name__, value, oldvalue) else: instance.fire_event_pre('property-pre-set:' + self.__name__, value) instance._init_property(self, value) if has_oldvalue: - instance.fire_event('property-set:' + self.__name__, value, oldvalue) + instance.fire_event( + 'property-set:' + self.__name__, value, oldvalue) else: instance.fire_event('property-set:' + self.__name__, value) @@ -692,9 +704,12 @@ class property(object): ''' lcvalue = value.lower() - if lcvalue in ('0', 'no', 'false'): return False - if lcvalue in ('1', 'yes', 'true'): return True - raise ValueError('Invalid literal for boolean property: {!r}'.format(value)) + if lcvalue in ('0', 'no', 'false'): + return False + if lcvalue in ('1', 'yes', 'true'): + return True + raise ValueError( + 'Invalid literal for boolean property: {!r}'.format(value)) @@ -765,7 +780,8 @@ class PropertyHolder(qubes.events.Emitter): if load_stage is not None: props = set(prop for prop in props if prop.load_stage == load_stage) - return sorted(props, key=lambda prop: (prop.load_stage, prop.order, prop.__name__)) + return sorted(props, + key=lambda prop: (prop.load_stage, prop.order, prop.__name__)) def _init_property(self, prop, value): @@ -825,7 +841,8 @@ class PropertyHolder(qubes.events.Emitter): ''' self.events_enabled = False - all_names = set(prop.__name__ for prop in self.get_props_list(load_stage)) + all_names = set( + prop.__name__ for prop in self.get_props_list(load_stage)) for node in self.xml.xpath('./properties/property'): name = node.get('name') value = node.get('ref') or node.text @@ -852,8 +869,9 @@ class PropertyHolder(qubes.events.Emitter): for prop in self.get_props_list(): try: - value = getattr(self, (prop.__name__ if with_defaults else prop._attr_name)) - except AttributeError, e: + value = getattr( + self, (prop.__name__ if with_defaults else prop._attr_name)) + except AttributeError as e: continue try: @@ -1033,7 +1051,8 @@ class Qubes(PropertyHolder): def __init__(self, store='/var/lib/qubes/qubes.xml'): - self._extensions = set(ext(self) for ext in qubes.ext.Extension.register.values()) + self._extensions = set(ext(self) + for ext in qubes.ext.Extension.register.values()) #: collection of all VMs managed by this Qubes instance self.domains = VMCollection() @@ -1054,7 +1073,8 @@ class Qubes(PropertyHolder): except IOError: self._init() - super(Qubes, self).__init__(xml=lxml.etree.parse(self.qubes_store_file)) + super(Qubes, self).__init__( + xml=lxml.etree.parse(self.qubes_store_file)) def _open_store(self): @@ -1064,7 +1084,7 @@ class Qubes(PropertyHolder): self._storefd = open(self._store, 'r+') if os.name == 'posix': - fcntl.lockf (self.qubes_store_file, fcntl.LOCK_EX) + fcntl.lockf(self.qubes_store_file, fcntl.LOCK_EX) elif os.name == 'nt': overlapped = pywintypes.OVERLAPPED() win32file.LockFileEx(win32file._get_osfhandle(self.qubes_store_file.fileno()), @@ -1163,7 +1183,7 @@ class Qubes(PropertyHolder): lxml.etree.ElementTree(self.__xml__()).write( self._storefd, encoding='utf-8', pretty_print=True) self._storefd.sync() - os.chmod(self._store, 0660) + os.chmod(self._store, 0o660) os.chown(self._store, -1, grp.getgrnam('qubes').gr_gid)