forked from platformio/platformio-core
Do not load automaically JSON from cached resources
This commit is contained in:
@ -12,6 +12,7 @@
|
|||||||
# See the License for the specific language governing permissions and
|
# See the License for the specific language governing permissions and
|
||||||
# limitations under the License.
|
# limitations under the License.
|
||||||
|
|
||||||
|
import codecs
|
||||||
import hashlib
|
import hashlib
|
||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
@ -106,7 +107,7 @@ class State(object):
|
|||||||
def __exit__(self, type_, value, traceback):
|
def __exit__(self, type_, value, traceback):
|
||||||
if self._prev_state != self._state:
|
if self._prev_state != self._state:
|
||||||
try:
|
try:
|
||||||
with open(self.path, "w") as fp:
|
with codecs.open(self.path, "w") as fp:
|
||||||
if "dev" in __version__:
|
if "dev" in __version__:
|
||||||
json.dump(self._state, fp, indent=4)
|
json.dump(self._state, fp, indent=4)
|
||||||
else:
|
else:
|
||||||
@ -187,11 +188,8 @@ class ContentCache(object):
|
|||||||
cache_path = self.get_cache_path(key)
|
cache_path = self.get_cache_path(key)
|
||||||
if not isfile(cache_path):
|
if not isfile(cache_path):
|
||||||
return None
|
return None
|
||||||
with open(cache_path, "rb") as fp:
|
with codecs.open(cache_path, "rb") as fp:
|
||||||
data = fp.read()
|
return fp.read()
|
||||||
if data and data[0] in ("{", "["):
|
|
||||||
return json.loads(data)
|
|
||||||
return data
|
|
||||||
|
|
||||||
def set(self, key, data, valid):
|
def set(self, key, data, valid):
|
||||||
if not get_setting("enable_cache"):
|
if not get_setting("enable_cache"):
|
||||||
@ -212,12 +210,9 @@ class ContentCache(object):
|
|||||||
|
|
||||||
if not isdir(dirname(cache_path)):
|
if not isdir(dirname(cache_path)):
|
||||||
os.makedirs(dirname(cache_path))
|
os.makedirs(dirname(cache_path))
|
||||||
with open(cache_path, "wb") as fp:
|
with codecs.open(cache_path, "wb") as fp:
|
||||||
if isinstance(data, (dict, list)):
|
fp.write(str(data))
|
||||||
json.dump(data, fp)
|
with codecs.open(self._db_path, "a") as fp:
|
||||||
else:
|
|
||||||
fp.write(str(data))
|
|
||||||
with open(self._db_path, "a") as fp:
|
|
||||||
fp.write("%s=%s\n" % (str(expire_time), cache_path))
|
fp.write("%s=%s\n" % (str(expire_time), cache_path))
|
||||||
|
|
||||||
return self._unlock_dbindex()
|
return self._unlock_dbindex()
|
||||||
@ -233,7 +228,7 @@ class ContentCache(object):
|
|||||||
paths_for_delete = [self.get_cache_path(k) for k in keys]
|
paths_for_delete = [self.get_cache_path(k) for k in keys]
|
||||||
found = False
|
found = False
|
||||||
newlines = []
|
newlines = []
|
||||||
with open(self._db_path) as fp:
|
with codecs.open(self._db_path) as fp:
|
||||||
for line in fp.readlines():
|
for line in fp.readlines():
|
||||||
if "=" not in line:
|
if "=" not in line:
|
||||||
continue
|
continue
|
||||||
@ -253,7 +248,7 @@ class ContentCache(object):
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
if found and self._lock_dbindex():
|
if found and self._lock_dbindex():
|
||||||
with open(self._db_path, "w") as fp:
|
with codecs.open(self._db_path, "w") as fp:
|
||||||
fp.write("\n".join(newlines) + "\n")
|
fp.write("\n".join(newlines) + "\n")
|
||||||
self._unlock_dbindex()
|
self._unlock_dbindex()
|
||||||
|
|
||||||
|
@ -607,8 +607,9 @@ def _get_api_result(
|
|||||||
headers=headers,
|
headers=headers,
|
||||||
auth=auth,
|
auth=auth,
|
||||||
verify=verify_ssl)
|
verify=verify_ssl)
|
||||||
result = r.json()
|
|
||||||
r.raise_for_status()
|
r.raise_for_status()
|
||||||
|
r.json()
|
||||||
|
result = r.text
|
||||||
except requests.exceptions.HTTPError as e:
|
except requests.exceptions.HTTPError as e:
|
||||||
if result and "message" in result:
|
if result and "message" in result:
|
||||||
raise exception.APIRequestError(result['message'])
|
raise exception.APIRequestError(result['message'])
|
||||||
@ -637,7 +638,7 @@ def get_api_result(url, params=None, data=None, auth=None, cache_valid=None):
|
|||||||
if cache_key:
|
if cache_key:
|
||||||
result = cc.get(cache_key)
|
result = cc.get(cache_key)
|
||||||
if result is not None:
|
if result is not None:
|
||||||
return result
|
return json.loads(result)
|
||||||
|
|
||||||
# check internet before and resolve issue with 60 seconds timeout
|
# check internet before and resolve issue with 60 seconds timeout
|
||||||
internet_on(raise_exception=True)
|
internet_on(raise_exception=True)
|
||||||
@ -646,7 +647,7 @@ def get_api_result(url, params=None, data=None, auth=None, cache_valid=None):
|
|||||||
if cache_valid:
|
if cache_valid:
|
||||||
with ContentCache() as cc:
|
with ContentCache() as cc:
|
||||||
cc.set(cache_key, result, cache_valid)
|
cc.set(cache_key, result, cache_valid)
|
||||||
return result
|
return json.loads(result)
|
||||||
except (requests.exceptions.ConnectionError,
|
except (requests.exceptions.ConnectionError,
|
||||||
requests.exceptions.Timeout) as e:
|
requests.exceptions.Timeout) as e:
|
||||||
from platformio.maintenance import in_silence
|
from platformio.maintenance import in_silence
|
||||||
|
Reference in New Issue
Block a user