Skip to content
This repository was archived by the owner on Jun 20, 2024. It is now read-only.

Commit c8908ef

Browse files
Merge pull request #68 from SethMichaelLarson/flake8
Start Flake8 testing the virtualbox package
2 parents 5f69f12 + 9d7a3f7 commit c8908ef

23 files changed

+635
-543
lines changed

.gitignore

Lines changed: 100 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,102 @@
1-
*.pyc
2-
*.xidl
1+
# Byte-compiled / optimized / DLL files
2+
__pycache__/
3+
*.py[cod]
4+
*$py.class
5+
6+
# C extensions
7+
*.so
8+
9+
# Distribution / packaging
10+
.Python
11+
env/
12+
build/
13+
develop-eggs/
14+
dist/
15+
downloads/
16+
eggs/
17+
.eggs/
18+
lib/
19+
lib64/
20+
parts/
21+
sdist/
22+
var/
23+
*.egg-info/
24+
.installed.cfg
325
*.egg
4-
*.zip
5-
*.swp
626

7-
# PyCharm / IntelliJ projects
8-
.idea/**
27+
# PyInstaller
28+
# Usually these files are written by a python script from a template
29+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
30+
*.manifest
31+
*.spec
32+
33+
# Installer logs
34+
pip-log.txt
35+
pip-delete-this-directory.txt
36+
37+
# Unit test / coverage reports
38+
htmlcov/
39+
.tox/
40+
.coverage
41+
.coverage.*
42+
.cache
43+
nosetests.xml
44+
coverage.xml
45+
*,cover
46+
.hypothesis/
47+
48+
# Translations
49+
*.mo
50+
*.pot
51+
52+
# Django stuff:
53+
*.log
54+
local_settings.py
55+
56+
# Flask stuff:
57+
instance/
58+
.webassets-cache
59+
60+
# Scrapy stuff:
61+
.scrapy
62+
63+
# Sphinx documentation
64+
docs/_build/
65+
66+
# PyBuilder
67+
target/
68+
69+
# IPython Notebook
70+
.ipynb_checkpoints
71+
72+
# pyenv
73+
.python-version
74+
75+
# celery beat schedule file
76+
celerybeat-schedule
77+
78+
# dotenv
79+
.env
80+
81+
# virtualenv
82+
venv/
83+
ENV/
84+
85+
# Spyder project settings
86+
.spyderproject
87+
88+
# Rope project settings
89+
.ropeproject
90+
91+
# PyCharm
92+
.idea/
93+
94+
# Benchmarks
95+
.benchmarks/
96+
97+
# PyCharm
98+
.idea/
99+
100+
*.swp
101+
*.zip
102+
*.xidl

setup.cfg

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,8 @@ all_files = 1
55

66
[upload_sphinx]
77
upload-dir = docs/build/html
8+
9+
[flake8]
10+
exclude = virtualbox/library.py
11+
ignore = E502,E241
12+
max-line-length = 100

tox.ini

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[tox]
2+
envlist = lint
3+
4+
[testenv:lint]
5+
commands =
6+
python -m pip install flake8
7+
flake8 virtualbox/

virtualbox/__init__.py

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import copy
2323
import atexit
2424
from multiprocessing import current_process
25-
from threading import current_thread
2625

2726
from virtualbox.library_ext import library
2827

@@ -34,6 +33,7 @@
3433
VirtualBox = library.IVirtualBox
3534
Session = library.ISession
3635

36+
3737
@contextmanager
3838
def import_vboxapi():
3939
"""This import is designed to help when loading vboxapi inside of
@@ -47,28 +47,26 @@ def import_vboxapi():
4747
system = platform.system()
4848
py_mm_ver = sys.version_info[:2]
4949
packages = ['vboxapi']
50+
5051
if system == 'Windows':
5152
packages.extend(['win32com', 'win32', 'win32api', 'pywintypes', 'win32comext'])
52-
search = [
53-
'C:\\Python%s%s\\Lib\\site-packages' % py_mm_ver,
54-
'C:\\Python%s%s\\Lib\\site-packages\\win32' % py_mm_ver,
55-
'C:\\Python%s%s\\Lib\\site-packages\\win32\\lib' % py_mm_ver,
56-
'C:\\Program Files\\Oracle\\VirtualBox\\sdk\\install',
57-
'C:\\Program Files (x86)\\Oracle\\VirtualBox\\sdk\\install',
58-
]
53+
search = ['C:\\Python%s%s\\Lib\\site-packages' % py_mm_ver,
54+
'C:\\Python%s%s\\Lib\\site-packages\\win32' % py_mm_ver,
55+
'C:\\Python%s%s\\Lib\\site-packages\\win32\\lib' % py_mm_ver,
56+
'C:\\Program Files\\Oracle\\VirtualBox\\sdk\\install',
57+
'C:\\Program Files (x86)\\Oracle\\VirtualBox\\sdk\\install']
58+
5959
elif system == 'Linux':
60-
search = [
61-
'/usr/lib/python%s.%s/dist-packages' % py_mm_ver,
62-
'/usr/lib/python%s.%s/site-packages' % py_mm_ver,
63-
'/usr/share/pyshared',
64-
]
60+
search = ['/usr/lib/python%s.%s/dist-packages' % py_mm_ver,
61+
'/usr/lib/python%s.%s/site-packages' % py_mm_ver,
62+
'/usr/share/pyshared']
63+
6564
elif system == 'Darwin':
66-
search = [
67-
'/Library/Python/%s.%s/site-packages' % py_mm_ver,
68-
]
65+
search = ['/Library/Python/%s.%s/site-packages' % py_mm_ver]
6966
else:
7067
# No idea where to look...
7168
raise
69+
7270
packages = set(packages)
7371
original_path = copy.copy(sys.path)
7472
for path in search:
@@ -81,7 +79,7 @@ def import_vboxapi():
8179
if not packages:
8280
break
8381
else:
84-
# After search each path we still failed to find
82+
# After search each path we still failed to find
8583
# the required set of packages.
8684
raise
8785
import vboxapi
@@ -93,13 +91,15 @@ def import_vboxapi():
9391
yield vboxapi
9492

9593

96-
_managers = {}
94+
_managers = {}
95+
96+
9797
class Manager(object):
9898
"""The Manager maintains a single point of entry into vboxapi.
99-
99+
100100
This object is responsible for the construction of
101101
:py:class:`virtualbox.library_ext.ISession` and
102-
:py:class:`virtualbox.library_ext.IVirtualBox`.
102+
:py:class:`virtualbox.library_ext.IVirtualBox`.
103103
104104
:param mtype: Type of manager i.e. WEBSERVICE.
105105
:type mtype: str (Default None)
@@ -117,12 +117,12 @@ def __init__(self, mtype=None, mparams=None):
117117
@property
118118
def manager(self):
119119
"""Create a default Manager object
120-
120+
121121
Builds a singleton VirtualBoxManager object.
122122
123123
Note: It is not necessary to build this object when defining a
124124
Session or VirtualBox object as both of these classes will default
125-
to this object's global singleton during construction.
125+
to this object's global singleton during construction.
126126
"""
127127
if _managers is None:
128128
raise RuntimeError("Can not get the manager following a system exit.")
@@ -135,20 +135,20 @@ def manager(self, value):
135135
if _managers is None:
136136
raise RuntimeError("Can not set the manager following a system exit.")
137137
if pid not in _managers:
138-
_managers[pid] = value
138+
_managers[pid] = value
139139
else:
140140
raise Exception("Manager already set for pid %s" % pid)
141141

142142
def get_virtualbox(self):
143143
"""Return a VirtualBox interface
144-
144+
145145
:rtype: library.IVirtualBox
146146
"""
147147
return VirtualBox(interface=self.manager.getVirtualBox())
148148

149149
def get_session(self):
150150
"""Return a Session interface
151-
151+
152152
:rtype: library.ISession
153153
"""
154154
# The inconsistent vboxapi implementation makes this annoying...
@@ -160,7 +160,7 @@ def get_session(self):
160160

161161
def cast_object(self, interface_object, interface_class):
162162
"""Cast the obj to the interface class
163-
163+
164164
:rtype: interface_class(interface_object)
165165
"""
166166
name = interface_class.__name__
@@ -170,17 +170,17 @@ def cast_object(self, interface_object, interface_class):
170170
@property
171171
def bin_path(self):
172172
"""return the virtualbox install directory
173-
173+
174174
:rtype: str
175175
"""
176176
return self.manager.getBinDir()
177177

178178

179-
# Attempt to close left over manager objects cleanly.
179+
# Attempt to close left over manager objects cleanly.
180180
def _cleanup_managers():
181181
global _managers
182182
managers = _managers
183-
_managers = None
183+
_managers = None
184184
for manager in managers.values():
185185
try:
186186
del manager
@@ -198,9 +198,9 @@ class WebServiceManager(Manager):
198198
"""
199199
def __init__(self, url='http://localhost/', user='', password=''):
200200
"""Create a VirtualBoxManager WEBSERVICE manager for IVirtualBox
201-
201+
202202
Options:
203-
url - url to connect with the VirtualBox server
203+
url - url to connect with the VirtualBox server
204204
user - username used to auth to the VirtualBox server service
205205
password - password used to auth to the VirtualBox server service
206206
@@ -209,11 +209,11 @@ def __init__(self, url='http://localhost/', user='', password=''):
209209
vbox = VirtualBox(manager=manager)
210210
...
211211
"""
212-
params = {"url":url, "user":user, "password":password}
212+
params = {"url": url, "user": user, "password": password}
213213
super(WebServiceManager, self).__init__("WEBSERVICE", params)
214214

215215

216216
# Lazy include...
217-
from virtualbox import pool
218-
from virtualbox import events
219-
from virtualbox import version
217+
from virtualbox import pool # noqa: F401
218+
from virtualbox import events # noqa: F401
219+
from virtualbox import version # noqa: F401

virtualbox/events.py

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,17 @@
55
"""
66
from __future__ import print_function
77
import sys
8-
import atexit
9-
import inspect
8+
import atexit
9+
import inspect
1010
import traceback
1111
import threading
1212

13-
import virtualbox
1413
from virtualbox import library
1514

1615

17-
_lookup = {}
16+
_lookup = {}
17+
18+
1819
def type_to_interface(event_type):
1920
"""Return the event interface object that corresponds to the event type
2021
enumeration"""
@@ -38,6 +39,8 @@ def type_to_interface(event_type):
3839

3940

4041
_callbacks = {}
42+
43+
4144
def _event_monitor(callback, event_source, listener, event_interface, quit):
4245
global _callbacks
4346
try:
@@ -46,27 +49,27 @@ def _event_monitor(callback, event_source, listener, event_interface, quit):
4649
event = event_source.get_event(listener, 1000)
4750
except library.VBoxError:
4851
print("Unregistering %s due to VBoxError on get_event" %
49-
listener, file=sys.stderr)
52+
listener, file=sys.stderr)
5053
break
5154
if event:
5255
try:
5356
callback(event_interface(event))
5457
except:
55-
print("Unhanded exception in callback: \n%s" % \
56-
traceback.format_exc(), file=sys.stderr)
58+
print("Unhanded exception in callback: \n%s" %
59+
traceback.format_exc(), file=sys.stderr)
5760
event_source.event_processed(listener, event)
5861
finally:
5962
_callbacks.pop(threading.current_thread().ident, None)
6063
try:
6164
event_source.unregister_listener(listener)
62-
except Exception as exc:
65+
except Exception:
6366
print("Failed to unregister listener %s" % listener,
64-
file=sys.stderr)
67+
file=sys.stderr)
6568

6669

6770
def register_callback(callback, event_source, event_type):
6871
"""register a callback function against an event_source for a given
69-
event_type.
72+
event_type.
7073
7174
Arguments:
7275
callback - function to call when the event occurs
@@ -77,11 +80,11 @@ def register_callback(callback, event_source, event_type):
7780
"""
7881
global _callbacks
7982
event_interface = type_to_interface(event_type)
80-
listener = event_source.create_listener()
83+
listener = event_source.create_listener()
8184
event_source.register_listener(listener, [event_type], False)
8285
quit = threading.Event()
83-
t = threading.Thread(target=_event_monitor, args=(callback,
84-
event_source,
86+
t = threading.Thread(target=_event_monitor, args=(callback,
87+
event_source,
8588
listener,
8689
event_interface,
8790
quit))
@@ -113,7 +116,3 @@ def _remove_all_callbacks():
113116

114117

115118
atexit.register(_remove_all_callbacks)
116-
117-
118-
119-

0 commit comments

Comments
 (0)