1
0
mirror of https://github.com/EDCD/EDMarketConnector.git synced 2025-04-17 17:42:20 +03:00

Removes problematic .encode('utf-8')'s in companion.py

This commit is contained in:
Athanasius 2019-09-12 14:51:34 +01:00
parent a0096c3d6e
commit 2208fdf686

View File

@ -128,19 +128,19 @@ class ServerError(Exception):
def __unicode__(self): def __unicode__(self):
return _('Error: Frontier server is down') # Raised when cannot contact the Companion API server return _('Error: Frontier server is down') # Raised when cannot contact the Companion API server
def __str__(self): def __str__(self):
return str(self).encode('utf-8') return str(self)
class ServerLagging(Exception): class ServerLagging(Exception):
def __unicode__(self): def __unicode__(self):
return _('Error: Frontier server is lagging') # Raised when Companion API server is returning old data, e.g. when the servers are too busy return _('Error: Frontier server is lagging') # Raised when Companion API server is returning old data, e.g. when the servers are too busy
def __str__(self): def __str__(self):
return str(self).encode('utf-8') return str(self)
class SKUError(Exception): class SKUError(Exception):
def __unicode__(self): def __unicode__(self):
return _('Error: Frontier server SKU problem') # Raised when the Companion API server thinks that the user has not purchased E:D. i.e. doesn't have the correct 'SKU' return _('Error: Frontier server SKU problem') # Raised when the Companion API server thinks that the user has not purchased E:D. i.e. doesn't have the correct 'SKU'
def __str__(self): def __str__(self):
return str(self).encode('utf-8') return str(self)
class CredentialsError(Exception): class CredentialsError(Exception):
def __init__(self, message=None): def __init__(self, message=None):
@ -148,13 +148,13 @@ class CredentialsError(Exception):
def __unicode__(self): def __unicode__(self):
return self.message return self.message
def __str__(self): def __str__(self):
return str(self).encode('utf-8') return str(self)
class CmdrError(Exception): class CmdrError(Exception):
def __unicode__(self): def __unicode__(self):
return _('Error: Wrong Cmdr') # Raised when the user has multiple accounts and the username/password setting is not for the account they're currently playing OR the user has reset their Cmdr and the Companion API server is still returning data for the old Cmdr return _('Error: Wrong Cmdr') # Raised when the user has multiple accounts and the username/password setting is not for the account they're currently playing OR the user has reset their Cmdr and the Companion API server is still returning data for the old Cmdr
def __str__(self): def __str__(self):
return str(self).encode('utf-8') return str(self)
class Auth(object): class Auth(object):
@ -187,13 +187,13 @@ class Auth(object):
config.save() # Save settings now for use by command-line app config.save() # Save settings now for use by command-line app
return data.get('access_token') return data.get('access_token')
else: else:
print('Auth\tCan\'t refresh token for %s' % self.cmdr.encode('utf-8')) print('Auth\tCan\'t refresh token for %s' % self.cmdr)
self.dump(r) self.dump(r)
except: except:
print('Auth\tCan\'t refresh token for %s' % self.cmdr.encode('utf-8')) print('Auth\tCan\'t refresh token for %s' % self.cmdr)
print_exc() print_exc()
else: else:
print('Auth\tNo token for %s' % self.cmdr.encode('utf-8')) print('Auth\tNo token for %s' % self.cmdr)
# New request # New request
print('Auth\tNew authorization request') print('Auth\tNew authorization request')
@ -207,16 +207,16 @@ class Auth(object):
def authorize(self, payload): def authorize(self, payload):
# Handle OAuth authorization code callback. Returns access token if successful, otherwise raises CredentialsError # Handle OAuth authorization code callback. Returns access token if successful, otherwise raises CredentialsError
if not '?' in payload: if not '?' in payload:
print('Auth\tMalformed response "%s"' % payload.encode('utf-8')) print('Auth\tMalformed response "%s"' % payload)
raise CredentialsError() # Not well formed raise CredentialsError() # Not well formed
data = urllib.parse.parse_qs(payload[payload.index('?')+1:]) data = urllib.parse.parse_qs(payload[payload.index('?')+1:])
if not self.state or not data.get('state') or data['state'][0] != self.state: if not self.state or not data.get('state') or data['state'][0] != self.state:
print('Auth\tUnexpected response "%s"' % payload.encode('utf-8')) print('Auth\tUnexpected response "%s"' % payload)
raise CredentialsError() # Unexpected reply raise CredentialsError() # Unexpected reply
if not data.get('code'): if not data.get('code'):
print('Auth\tNegative response "%s"' % payload.encode('utf-8')) print('Auth\tNegative response "%s"' % payload)
if data.get('error_description'): if data.get('error_description'):
raise CredentialsError('Error: %s' % data['error_description'][0]) raise CredentialsError('Error: %s' % data['error_description'][0])
elif data.get('error'): elif data.get('error'):
@ -238,7 +238,7 @@ class Auth(object):
r = self.session.post(SERVER_AUTH + URL_TOKEN, data=data, timeout=auth_timeout) r = self.session.post(SERVER_AUTH + URL_TOKEN, data=data, timeout=auth_timeout)
data = r.json() data = r.json()
if r.status_code == requests.codes.ok: if r.status_code == requests.codes.ok:
print('Auth\tNew token for %s' % self.cmdr.encode('utf-8')) print('Auth\tNew token for %s' % self.cmdr)
cmdrs = config.get('cmdrs') cmdrs = config.get('cmdrs')
idx = cmdrs.index(self.cmdr) idx = cmdrs.index(self.cmdr)
tokens = config.get('fdev_apikeys') or [] tokens = config.get('fdev_apikeys') or []
@ -248,12 +248,12 @@ class Auth(object):
config.save() # Save settings now for use by command-line app config.save() # Save settings now for use by command-line app
return data.get('access_token') return data.get('access_token')
except: except:
print('Auth\tCan\'t get token for %s' % self.cmdr.encode('utf-8')) print('Auth\tCan\'t get token for %s' % self.cmdr)
print_exc() print_exc()
if r: self.dump(r) if r: self.dump(r)
raise CredentialsError() raise CredentialsError()
print('Auth\tCan\'t get token for %s' % self.cmdr.encode('utf-8')) print('Auth\tCan\'t get token for %s' % self.cmdr)
self.dump(r) self.dump(r)
if data.get('error_description'): if data.get('error_description'):
raise CredentialsError('Error: %s' % data['error_description']) raise CredentialsError('Error: %s' % data['error_description'])
@ -266,7 +266,7 @@ class Auth(object):
@staticmethod @staticmethod
def invalidate(cmdr): def invalidate(cmdr):
print('Auth\tInvalidated token for %s' % cmdr.encode('utf-8')) print('Auth\tInvalidated token for %s' % cmdr)
cmdrs = config.get('cmdrs') cmdrs = config.get('cmdrs')
idx = cmdrs.index(cmdr) idx = cmdrs.index(cmdr)
tokens = config.get('fdev_apikeys') or [] tokens = config.get('fdev_apikeys') or []
@ -438,7 +438,7 @@ class Session(object):
Auth.invalidate(self.credentials['cmdr']) Auth.invalidate(self.credentials['cmdr'])
def dump(self, r): def dump(self, r):
print('cAPI\t' + r.url, r.status_code, r.reason and r.reason.encode('utf-8') or 'None', r.text.encode('utf-8')) print('cAPI\t' + r.url, r.status_code, r.reason and r.reason or 'None', r.text)
# Returns a shallow copy of the received data suitable for export to older tools - English commodity names and anomalies fixed up # Returns a shallow copy of the received data suitable for export to older tools - English commodity names and anomalies fixed up