Skip to content

Commit

Permalink
Fix the detection of netifaces
Browse files Browse the repository at this point in the history
Didn't properly handle this case.
  • Loading branch information
mrworf committed Jun 27, 2021
1 parent c6a0613 commit 9ddcf45
Showing 1 changed file with 18 additions and 15 deletions.
33 changes: 18 additions & 15 deletions modules/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
import random
import time

try:
import netifaces
except ImportError:
logging.error('User has not installed python-netifaces, using checkNetwork() instead (depends on internet)')

# A regular expression to determine whether a url is valid or not (e.g. "www.example.de/someImg.jpg" is missing "http://")
VALID_URL_REGEX = re.compile(
r'^(?:http|ftp)s?://' # http:// or https://
Expand All @@ -33,7 +38,7 @@

class helper:
TOOL_ROTATE = '/usr/bin/jpegtran'
NETWORK_CHECK = None
NETWORK_CHECK = True

MIMETYPES = {
'image/jpeg' : 'jpg',
Expand Down Expand Up @@ -74,24 +79,22 @@ def getResolution():

@staticmethod
def getDeviceIp():
if helper.NETWORK_CHECK is None:
if helper.NETWORK_CHECK:
try:
import netifaces
helper.NETWORK_CHECK = True
except ImportError:
logging.error('User has not installed python-netifaces, using checkNetwork() instead (depends on internet)')
if 'default' in netifaces.gateways() and netifaces.AF_INET in netifaces.gateways()['default']:
dev = netifaces.gateways()['default'][netifaces.AF_INET][1]
if netifaces.ifaddresses(dev) and netifaces.AF_INET in netifaces.ifaddresses(dev):
net = netifaces.ifaddresses(dev)[netifaces.AF_INET]
if len(net) > 0 and 'addr' in net[0]:
return net[0]['addr']
except NameError:
# We weren't able to import, so switch it up
helper.NETWORK_CHECK = False
return helper._checkNetwork()
except:
logging.exception('netifaces call failed, using checkNetwork() instead (depends on internet)')
helper.NETWORK_CHECK = False

if helper.NETWORK_CHECK:
if 'default' in netifaces.gateways() and netifaces.AF_INET in netifaces.gateways()['default']:
dev = netifaces.gateways()['default'][netifaces.AF_INET][1]
if netifaces.ifaddresses(dev) and netifaces.AF_INET in netifaces.ifaddresses(dev):
net = netifaces.ifaddresses(dev)[netifaces.AF_INET]
if len(net) > 0 and 'addr' in net[0]:
return net[0]['addr']
logging.exception('netifaces call failed, using checkNetwork() instead (depends on internet)')
return helper._checkNetwork()
else:
return helper._checkNetwork()

Expand Down

0 comments on commit 9ddcf45

Please sign in to comment.