Skip to content

Commit 67528f6

Browse files
committed
More fixes
1 parent 0846333 commit 67528f6

File tree

2 files changed

+41
-26
lines changed

2 files changed

+41
-26
lines changed

edr/edrcmdrs.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212

1313
class EDRCmdrs(object):
1414
#TODO these should be player and/or squadron specific
15-
EDR_CMDRS_CACHE = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'cache', 'cmdrs.v7.p')
16-
EDR_INARA_CACHE = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'cache', 'inara.v7.p')
15+
EDR_CMDRS_CACHE = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'cache', 'cmdrs.v8.p')
16+
EDR_INARA_CACHE = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'cache', 'inara.v8.p')
1717
EDR_SQDRDEX_CACHE = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'cache', 'sqdrdex.v2.p')
1818

1919
def __init__(self, edrserver, opsec_config=None):
@@ -116,6 +116,7 @@ def evict(self, cmdr):
116116
pass
117117

118118
def __edr_cmdr(self, cmdr_name, autocreate):
119+
backup_profile = self.cmdrs_cache.peek(cmdr_name.lower())
119120
profile = self.cmdrs_cache.get(cmdr_name.lower())
120121
cached = self.cmdrs_cache.has_key(cmdr_name.lower())
121122
if cached or profile:
@@ -128,11 +129,18 @@ def __edr_cmdr(self, cmdr_name, autocreate):
128129
profile = self.server.cmdr(cmdr_name, autocreate)
129130
except:
130131
EDR_LOG.log("Exception during call to EDR server cmdr.", "WARNING")
132+
pass
131133

132134
if not profile:
133-
self.cmdrs_cache.set(cmdr_name.lower(), None)
134-
EDR_LOG.log(u"No match on EDR. Temporary entry to be nice on EDR's server.", "DEBUG")
135-
return None
135+
if backup_profile:
136+
self.cmdrs_cache.set(cmdr_name.lower(), backup_profile)
137+
self.cmdrs_cache.refresh(cmdr_name.lower())
138+
EDR_LOG.log(u"No updated profile on EDR. Refresh old profile", "DEBUG")
139+
return backup_profile
140+
else:
141+
self.cmdrs_cache.set(cmdr_name.lower(), None)
142+
EDR_LOG.log(u"No match on EDR. Temporary entry to be nice on EDR's server.", "DEBUG")
143+
return None
136144
dex_profile = self.server.cmdrdex(profile.cid)
137145
if dex_profile:
138146
EDR_LOG.log(u"EDR CmdrDex entry found for {cmdr}: {id}".format(cmdr=cmdr_name,

edr/edrserver.py

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ def __process_inara_response(self, resp):
141141

142142
def __get(self, endpoint, service, params=None, headers=None, attempts=3):
143143
if self.backoff[service].throttled():
144-
EDR_LOG.log("Exponential backoff active for {} API calls: attempts={}, until={}".format(service, self.backoff[service].attempts, EDTime.t_plus_py(self.self.backoff[service].backoff_until)), "DEBUG")
144+
EDR_LOG.log("Exponential backoff active for {} API calls: attempts={}, until={}".format(service, self.backoff[service].attempts, EDTime.t_plus_py(self.backoff[service].backoff_until)), "DEBUG")
145145
return None
146146

147147
while attempts:
@@ -155,7 +155,7 @@ def __get(self, endpoint, service, params=None, headers=None, attempts=3):
155155

156156
def __put(self, endpoint, service, json, params=None, headers=None, attempts=3):
157157
if self.backoff[service].throttled():
158-
EDR_LOG.log("Exponential backoff active for {} API calls: attempts={}, until={}".format(service, self.backoff[service].attempts, EDTime.t_plus_py(self.self.backoff[service].backoff_until)), "DEBUG")
158+
EDR_LOG.log("Exponential backoff active for {} API calls: attempts={}, until={}".format(service, self.backoff[service].attempts, EDTime.t_plus_py(self.backoff[service].backoff_until)), "DEBUG")
159159
return None
160160

161161
while attempts:
@@ -169,7 +169,7 @@ def __put(self, endpoint, service, json, params=None, headers=None, attempts=3):
169169

170170
def __delete(self, endpoint, service, params=None, attempts=3):
171171
if self.backoff[service].throttled():
172-
EDR_LOG.log("Exponential backoff active for {} API calls: attempts={}, until={}".format(service, self.backoff[service].attempts, EDTime.t_plus_py(self.self.backoff[service].backoff_until)), "DEBUG")
172+
EDR_LOG.log("Exponential backoff active for {} API calls: attempts={}, until={}".format(service, self.backoff[service].attempts, EDTime.t_plus_py(self.backoff[service].backoff_until)), "DEBUG")
173173
return None
174174

175175
while attempts:
@@ -184,7 +184,7 @@ def __delete(self, endpoint, service, params=None, attempts=3):
184184

185185
def __post(self, endpoint, service, json, params=None, attempts=3):
186186
if self.backoff[service].throttled():
187-
EDR_LOG.log("Exponential backoff active for {} API calls: attempts={}, until={}".format(service, self.backoff[service].attempts, EDTime.t_plus_py(self.self.backoff[service].backoff_until)), "DEBUG")
187+
EDR_LOG.log("Exponential backoff active for {} API calls: attempts={}, until={}".format(service, self.backoff[service].attempts, EDTime.t_plus_py(self.backoff[service].backoff_until)), "DEBUG")
188188
return None
189189

190190
while attempts:
@@ -344,15 +344,15 @@ def pledged_to(self, power, since):
344344
endpoint = "{server}/v1/pledges/{uid}/.json".format(server=self.EDR_SERVER, uid=self.uid())
345345
EDR_LOG.log(u"Endpoint: {}".format(endpoint), "DEBUG")
346346
resp = self.__delete(endpoint, "EDR", params=params)
347-
EDR_LOG.log(u"resp= {}".format(resp.status_code), "DEBUG")
347+
EDR_LOG.log(u"resp= {}".format("None" if resp is None else resp.status_code), "DEBUG")
348348
return self.__check_response(resp, "EDR", "Delete pledge")
349349

350350
EDR_LOG.log(u"Pledge info for uid {uid} with power:{power}".format(uid=self.uid(), power=power), "INFO")
351351
endpoint = "{server}/v1/pledges/{uid}/.json".format(server=self.EDR_SERVER, uid=self.uid())
352352
json = { "cpower": self.nodify(power), "since": int(since*1000), "heartbeat": {".sv": "timestamp"} }
353353
EDR_LOG.log(u"Endpoint: {}".format(endpoint), "DEBUG")
354354
resp = self.__put(endpoint, "EDR", params=params, json=json)
355-
EDR_LOG.log(u"resp= {}".format(resp.status_code), "DEBUG")
355+
EDR_LOG.log(u"resp= {}".format("None" if resp is None else resp.status_code), "DEBUG")
356356
return self.__check_response(resp, "EDR", "Put pledge")
357357

358358
def cmdr(self, cmdr, autocreate=True):
@@ -373,7 +373,14 @@ def cmdr(self, cmdr, autocreate=True):
373373
EDR_LOG.log(u"{error}, {content}".format(error=resp.status_code, content=resp.text), "DEBUG")
374374
return None
375375

376-
if resp.content == 'null' or resp.content == b'null':
376+
json_cmdr = None
377+
try:
378+
json_cmdr = json.loads(resp.content)
379+
except:
380+
json_cmdr = None
381+
pass
382+
383+
if json_cmdr is None:
377384
if autocreate and not self.is_anonymous():
378385
params = { "auth" : self.auth_token() }
379386
endpoint = "{}/v1/cmdrs.json".format(self.EDR_SERVER)
@@ -428,7 +435,7 @@ def inara_cmdr(self, cmdr):
428435

429436
def __post_json(self, endpoint, json_payload, service):
430437
if self.backoff[service].throttled():
431-
EDR_LOG.log("Exponential backoff active for {} API calls: attempts={}, until={}".format(service, self.backoff[service].attempts, EDTime.t_plus_py(self.self.backoff[service].backoff_until)), "DEBUG")
438+
EDR_LOG.log("Exponential backoff active for {} API calls: attempts={}, until={}".format(service, self.backoff[service].attempts, EDTime.t_plus_py(self.backoff[service].backoff_until)), "DEBUG")
432439
return None
433440

434441
params = { "auth" : self.auth_token()}
@@ -479,7 +486,7 @@ def legal_stats(self, cmdr_id):
479486
endpoint = "{server}/v1/stats/legal/{cmdr_id}/.json".format(server=self.EDR_SERVER,cmdr_id=cmdr_id)
480487
params = {"auth": self.auth_token()}
481488
resp = self.__get(endpoint, "EDR", params)
482-
EDR_LOG.log(u"resp= {}".format(resp.status_code), "DEBUG")
489+
EDR_LOG.log(u"resp= {}".format("None" if resp is None else resp.status_code), "DEBUG")
483490

484491
if self.__check_response(resp, "EDR", "Legal_Stats"):
485492
return json.loads(resp.content)
@@ -534,7 +541,7 @@ def report_fcs(self, system_id, report):
534541
endpoint = "{server}/v1/fc_reports/{system_id}/{uid}/.json".format(server=self.EDR_SERVER, system_id=system_id, uid=self.uid())
535542
EDR_LOG.log(u"Endpoint: {}".format(endpoint), "DEBUG")
536543
resp = self.__put(endpoint, "EDR", params=params, json=report)
537-
EDR_LOG.log(u"resp= {}".format(resp.status_code), "DEBUG")
544+
EDR_LOG.log(u"resp= {}".format("None" if resp is None else resp.status_code), "DEBUG")
538545
return self.__check_response(resp, "EDR", "Put fcs report")
539546

540547
def fc_presence(self, star_system):
@@ -545,7 +552,7 @@ def fc_presence(self, star_system):
545552
EDR_LOG.log(u"Querying Fleet Carriers in system {}".format(star_system), "INFO")
546553
params = {"orderBy": '"starSystem"', "equalTo": json.dumps(star_system), "limitToFirst": 1, "auth": self.auth_token()}
547554
resp = self.__get("{}/v1/fc_presence.json".format(self.EDR_SERVER), "EDR", params)
548-
EDR_LOG.log(u"resp= {}".format(resp.status_code), "DEBUG")
555+
EDR_LOG.log(u"resp= {}".format("None" if resp is None else resp.status_code), "DEBUG")
549556
if self.__check_response(resp, "EDR", "FC_Presence"):
550557
result = json.loads(resp.content)
551558
sid = list(result)[0] if result else None
@@ -562,7 +569,7 @@ def report_fc_materials(self, fc_id, report):
562569
endpoint = "{server}/v1/fc_materials_reports/{fc_id}/{uid}/.json".format(server=self.EDR_SERVER, fc_id=fc_id, uid=self.uid())
563570
EDR_LOG.log(u"Endpoint: {}".format(endpoint), "DEBUG")
564571
resp = self.__put(endpoint, "EDR", params=params, json=report)
565-
EDR_LOG.log(u"resp= {}".format(resp.status_code), "DEBUG")
572+
EDR_LOG.log(u"resp= {}".format("None" if resp is None else resp.status_code), "DEBUG")
566573
return self.__check_response(resp, "EDR", "Put fcs materials report")
567574

568575
def report_fc_market(self, fc_id, report):
@@ -574,7 +581,7 @@ def report_fc_market(self, fc_id, report):
574581
endpoint = "{server}/v1/fc_market_reports/{fc_id}/{uid}/.json".format(server=self.EDR_SERVER, fc_id=fc_id, uid=self.uid())
575582
EDR_LOG.log(u"Endpoint: {}".format(endpoint), "DEBUG")
576583
resp = self.__put(endpoint, "EDR", params=params, json=report)
577-
EDR_LOG.log(u"resp= {}".format(resp.status_code), "DEBUG")
584+
EDR_LOG.log(u"resp= {}".format("None" if resp is None else resp.status_code), "DEBUG")
578585
return self.__check_response(resp, "EDR", "Put fcs market report")
579586

580587
def __get_recent(self, path, timespan_seconds, limitToLast=None):
@@ -709,7 +716,7 @@ def __update_dex(self, dex_path, cmdr_id, dex_entry):
709716
endpoint = "{server}{dex}{cid}/.json".format(server=self.EDR_SERVER, dex=dex_path, cid=cmdr_id)
710717
EDR_LOG.log(u"Endpoint: {} with {}".format(endpoint, dex_entry), "DEBUG")
711718
resp = self.__put(endpoint, "EDR", json=dex_entry, params=params)
712-
EDR_LOG.log(u"resp= {}".format(resp.status_code), "DEBUG")
719+
EDR_LOG.log(u"resp= {}".format("None" if resp is None else resp.status_code), "DEBUG")
713720
return self.__check_response(resp, "EDR")
714721

715722
def __remove_dex(self, dex_path, cmdr_id):
@@ -718,7 +725,7 @@ def __remove_dex(self, dex_path, cmdr_id):
718725
endpoint = "{server}{dex}{cid}.json".format(server=self.EDR_SERVER, dex=dex_path, cid=cmdr_id)
719726
EDR_LOG.log(u"Endpoint: {}".format(endpoint), "DEBUG")
720727
resp = self.__delete(endpoint, "EDR", params=params)
721-
EDR_LOG.log(u"resp= {}".format(resp.status_code), "DEBUG")
728+
EDR_LOG.log(u"resp= {}".format("None" if resp is None else resp.status_code), "DEBUG")
722729
return self.__check_response(resp, "EDR")
723730

724731
def __dex(self, dex_path, cmdr_id):
@@ -727,7 +734,7 @@ def __dex(self, dex_path, cmdr_id):
727734
endpoint = "{server}{dex}{cid}/.json".format(server=self.EDR_SERVER, dex=dex_path, cid=cmdr_id)
728735
EDR_LOG.log(u"Endpoint: {}".format(endpoint), "DEBUG")
729736
resp = self.__get(endpoint, "EDR", params)
730-
EDR_LOG.log(u"resp= {}".format(resp.status_code), "DEBUG")
737+
EDR_LOG.log(u"resp= {}".format("None" if resp is None else resp.status_code), "DEBUG")
731738

732739
if self.__check_response(resp, "EDR", "Dex"):
733740
return json.loads(resp.content)
@@ -743,7 +750,7 @@ def contracts(self):
743750
endpoint = "{server}{con}.json".format(server=self.EDR_SERVER, con=contracts_path)
744751
EDR_LOG.log(u"Endpoint: {}".format(endpoint), "DEBUG")
745752
resp = self.__get(endpoint, "EDR", params)
746-
EDR_LOG.log(u"resp= {}".format(resp.status_code), "DEBUG")
753+
EDR_LOG.log(u"resp= {}".format("None" if resp is None else resp.status_code), "DEBUG")
747754

748755
if self.__check_response(resp, "EDR", "Contracts"):
749756
return json.loads(resp.content)
@@ -759,7 +766,7 @@ def contract_for(self, cmdr_id):
759766
endpoint = "{server}{con}{cid}/.json".format(server=self.EDR_SERVER, con=contracts_path, cid=cmdr_id)
760767
EDR_LOG.log(u"Endpoint: {}".format(endpoint), "DEBUG")
761768
resp = self.__get(endpoint, "EDR", params)
762-
EDR_LOG.log(u"resp= {}".format(resp.status_code), "DEBUG")
769+
EDR_LOG.log(u"resp= {}".format("None" if resp is None else resp.status_code), "DEBUG")
763770

764771
if self.__check_response(resp, "EDR", "Contract_for"):
765772
return json.loads(resp.content)
@@ -785,7 +792,7 @@ def __update_contract(self, contract_path, cmdr_id, contract_entry):
785792
endpoint = "{server}{contract}{cid}/.json".format(server=self.EDR_SERVER, contract=contract_path, cid=cmdr_id)
786793
EDR_LOG.log(u"Endpoint: {} with {}".format(endpoint, contract_entry), "DEBUG")
787794
resp = self.__put(endpoint, "EDR", json=contract_entry, params=params)
788-
EDR_LOG.log(u"resp= {}".format(resp.status_code), "DEBUG")
795+
EDR_LOG.log(u"resp= {}".format("None" if resp is None else resp.status_code), "DEBUG")
789796
return self.__check_response(resp, "EDR", "Update_contract")
790797

791798
def __remove_contract(self, contract_path, cmdr_id):
@@ -794,7 +801,7 @@ def __remove_contract(self, contract_path, cmdr_id):
794801
endpoint = "{server}{contract}{cid}.json".format(server=self.EDR_SERVER, contract=contract_path, cid=cmdr_id)
795802
EDR_LOG.log(u"Endpoint: {}".format(endpoint), "DEBUG")
796803
resp = self.__delete(endpoint, "EDR", params=params)
797-
EDR_LOG.log(u"resp= {}".format(resp.status_code), "DEBUG")
804+
EDR_LOG.log(u"resp= {}".format("None" if resp is None else resp.status_code), "DEBUG")
798805
return self.__check_response(resp, "EDR", "Remove_contract")
799806

800807
def preflight_realtime(self, kind):
@@ -809,7 +816,7 @@ def __preflight(self, api_name, param):
809816
EDR_LOG.log(u"Preflight request for {} with {}".format(api_name, json), "DEBUG")
810817
endpoint = "{server_functions}/edr/v1/preflight/{uid}".format(server_functions=self.EDR_SERVER_FUNCTIONS, uid=self.uid())
811818
resp = self.__put(endpoint, "EDR", json=json, headers=headers)
812-
EDR_LOG.log(u"resp= {}".format(resp.status_code), "DEBUG")
819+
EDR_LOG.log(u"resp= {}".format("None" if resp is None else resp.status_code), "DEBUG")
813820
return self.__check_response(resp, "EDR", "Preflight {}".format(api_name))
814821

815822
class CommsJammedError(Exception):

0 commit comments

Comments
 (0)