Skip to content

Commit 66997bc

Browse files
committed
Standardize the try-except import statements if checking for "do I have the lib?" (fixed #60)
1 parent 4e5c4b7 commit 66997bc

File tree

4 files changed

+24
-27
lines changed

4 files changed

+24
-27
lines changed

db_mysql3.py

+12-14
Original file line numberDiff line numberDiff line change
@@ -15,28 +15,26 @@
1515
"""
1616

1717
__author__ = 'Linuxfabrik GmbH, Zurich/Switzerland'
18-
__version__ = '2022021601'
18+
__version__ = '2022021701'
1919

2020
from .globals3 import STATE_UNKNOWN
2121

2222
try:
2323
import mysql.connector
24-
except ImportError as e:
25-
mysql_connector_found = False
26-
else:
27-
mysql_connector_found = True
24+
HAVE_MYSQL_CONNECTOR = True
25+
except ImportError:
26+
HAVE_MYSQL_CONNECTOR = False
2827

2928
from . import base3
3029

31-
if mysql_connector_found and base3.version(mysql.connector.__version__) < base3.version('2.0.0'):
30+
if HAVE_MYSQL_CONNECTOR and base3.version(mysql.connector.__version__) < base3.version('2.0.0'):
3231
try:
3332
import MySQLdb.cursors
33+
HAVE_MYSQL_CURSORS = True
3434
except ImportError as e:
35-
mysql_cursors_found = False
36-
else:
37-
mysql_cursors_found = True
35+
HAVE_MYSQL_CURSORS = False
3836
else:
39-
mysql_cursors_found = None
37+
HAVE_MYSQL_CURSORS = None
4038

4139

4240
def close(conn):
@@ -74,9 +72,9 @@ def connect(mysql_connection):
7472
>>> conn = connect(mysql_connection)
7573
"""
7674

77-
if mysql_connector_found is False:
75+
if HAVE_MYSQL_CONNECTOR is False:
7876
base3.oao('Python module "mysql.connector" is not installed.', STATE_UNKNOWN)
79-
if mysql_connector_found is False:
77+
if HAVE_MYSQL_CONNECTOR is False:
8078
base3.oao('Python module "MySQLdb.cursors" is not installed.', STATE_UNKNOWN)
8179

8280
try:
@@ -93,9 +91,9 @@ def select(conn, sql, data={}, fetchone=False):
9391
database.
9492
"""
9593

96-
if mysql_connector_found is False:
94+
if HAVE_MYSQL_CONNECTOR is False:
9795
base3.oao('Python module "mysql.connector" is not installed.', STATE_UNKNOWN)
98-
if mysql_connector_found is False:
96+
if HAVE_MYSQL_CONNECTOR is False:
9997
base3.oao('Python module "MySQLdb.cursors" is not installed.', STATE_UNKNOWN)
10098

10199
if base3.version(mysql.connector.__version__) >= base3.version('2.0.0'):

net3.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,16 @@
1313
"""
1414

1515
__author__ = 'Linuxfabrik GmbH, Zurich/Switzerland'
16-
__version__ = '2022021602'
16+
__version__ = '2022021701'
1717

1818
import random
1919
import re
2020
import socket
2121
try:
2222
import netifaces
23-
lib_netifaces = True
24-
except ImportError as e:
25-
lib_netifaces = False
23+
HAVE_NETIFACES = True
24+
except ImportError:
25+
HAVE_NETIFACES = False
2626

2727
from . import txt3 # pylint: disable=C0413
2828
from . import url3 # pylint: disable=C0413
@@ -195,7 +195,7 @@ def get_ip_public():
195195

196196

197197
def get_netinfo():
198-
if lib_netifaces:
198+
if HAVE_NETIFACES:
199199
# Update stats using the netifaces lib
200200
try:
201201
default_gw = netifaces.gateways()['default'][netifaces.AF_INET]
@@ -208,7 +208,7 @@ def get_netinfo():
208208
stats['mask'] = netifaces.ifaddresses(default_gw[1])[netifaces.AF_INET][0]['netmask']
209209
stats['mask_cidr'] = ip_to_cidr(stats['mask'])
210210
stats['gateway'] = netifaces.gateways()['default'][netifaces.AF_INET][0]
211-
except (KeyError, AttributeError) as e:
211+
except (KeyError, AttributeError):
212212
return []
213213

214214
stats['public_address'] = None

psutil3.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"""
1313

1414
__author__ = 'Linuxfabrik GmbH, Zurich/Switzerland'
15-
__version__ = '2021050301'
15+
__version__ = '2022021701'
1616

1717
import sys
1818

@@ -37,5 +37,5 @@ def get_partitions(ignore=[]):
3737
lambda part: not any(
3838
ignore_item in part.mountpoint for ignore_item in ignore),
3939
psutil.disk_partitions(all=False)
40-
)
4140
)
41+
)

winrm3.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,13 @@
1212
"""
1313

1414
__author__ = 'Linuxfabrik GmbH, Zurich/Switzerland'
15-
__version__ = '2022021601'
15+
__version__ = '2022021701'
1616

1717
try:
1818
import winrm
19-
except ImportError as e:
20-
winrm_found = False
21-
else:
22-
winrm_found = True
19+
HAVE_WINRM = True
20+
except ImportError:
21+
HAVE_WINRM = False
2322

2423
from . import txt3
2524

0 commit comments

Comments
 (0)