Skip to content

Commit

Permalink
use utf-8 when loading json data
Browse files Browse the repository at this point in the history
  • Loading branch information
xavluiz committed Nov 25, 2021
1 parent 0668447 commit 66fb3f7
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 38 deletions.
4 changes: 1 addition & 3 deletions Software.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ def on_modified_async(self, view):
fileInfoData = PluginData.get_file_info_and_initialize_if_none(active_data, full_file_path)
if fileInfoData is None:
return

# project data
fileInfoData['project_name'] = active_data.project['name'] or NO_PROJ_NAME
fileInfoData['project_directory'] = active_data.project['directory']
Expand Down Expand Up @@ -421,8 +421,6 @@ def initializePlugin(initializedAnonUser):
logIt('Code Time: Loaded v%s of package name: %s' % (version, name))
showStatus("Code Time")

setItem("sublime_lastUpdateTime", None)

displayReadmeIfNotExists(False)

initWallClockThread = Thread(target=wallClockMgrInit, args=())
Expand Down
Binary file removed Software.pyc
Binary file not shown.
2 changes: 1 addition & 1 deletion Software.sublime_settings
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
"software_dashboard_url": "https://app.software.com",
"plugin": "code-time",
"open_tree_on_startup": true,
"plugin_version": "2.4.0"
"plugin_version": "2.4.2"
}
18 changes: 8 additions & 10 deletions lib/CommonUtil.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def getIsNewDay():
currentDay = getItem('currentDay')
if (currentDay != day):
return True
else:
else:
return False

def getOsUsername():
Expand All @@ -54,7 +54,7 @@ def getOs():
return system

def getTimezone():
myTimezone = None
myTimezone = None
try:
myTimezone = datetime.now(timezone.utc).astimezone().tzname()
except Exception:
Expand Down Expand Up @@ -140,10 +140,9 @@ def setAuthCallbackState(value):

def getSoftwareSessionAsJson():
try:
with open(getSoftwareSessionFile()) as sessionFile:
loadedSessionFile = json.load(sessionFile)
return loadedSessionFile
except Exception:
with open(getSoftwareSessionFile(), encoding='utf-8') as sessionFile:
return json.load(sessionFile)
except Exception as ex:
return {}

def getSoftwareSessionFile():
Expand All @@ -152,9 +151,8 @@ def getSoftwareSessionFile():

def getDeviceAsJson():
try:
with open(getDeviceFile()) as deviceFile:
loadedDeviceFile = json.load(deviceFile)
return loadedDeviceFile
with open(getDeviceFile(), encoding='utf-8') as deviceFile:
return json.load(deviceFile)
except Exception:
return {}

Expand All @@ -181,7 +179,7 @@ def getIntegrationsFile():

def getIntegrations():
try:
with open(getIntegrationsFile()) as integrationsFile:
with open(getIntegrationsFile(), encoding='utf-8') as integrationsFile:
loadedIntegrationsFile = json.load(integrationsFile)
return loadedIntegrationsFile
except Exception:
Expand Down
4 changes: 2 additions & 2 deletions lib/SoftwareFileDataManager.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from threading import Lock
from .SoftwareUtil import *
from .SoftwareModels import SessionSummary
import os
import os
import json
from .Logger import *

Expand All @@ -24,7 +24,7 @@ def clearSessionSummaryFile():
def getSessionSummaryFileAsJson():
sessionSummaryLock.acquire()
try:
with open(getSessionSummaryFile()) as sessionSummaryFile:
with open(getSessionSummaryFile(), encoding='utf-8') as sessionSummaryFile:
sessionSummaryData = json.load(sessionSummaryFile)
except Exception as ex:
sessionSummaryData = SessionSummary()
Expand Down
5 changes: 1 addition & 4 deletions lib/SoftwareHttp.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,6 @@ def requestIt(method, api, payload, jwt):
headers['X-SWDC-Plugin-TZ'] = getTimezone()
headers['X-SWDC-Plugin-Offset'] = int(float(getUtcOffset() / 60))

# print("HEADERS: %s" % headers)

# send the request
connection.request(method, api, payload.encode('utf-8'), headers)

Expand All @@ -111,7 +109,7 @@ def fetchReleaseTag():
return response
except Exception as ex:
print("Code Time: Response Error fetching release tag: %s" % ex)
return getValue("plugin_version", "2.4.1")
return getValue("plugin_version", "2.4.2")

def getVersion():
global version
Expand Down Expand Up @@ -157,7 +155,6 @@ def createAnonymousUser():
try:
responseObj = json.loads(response.read().decode('utf-8'))
jwt = responseObj.get("jwt", None)
print("response obj: %s" % responseObj)
if jwt is not None:
logIt("created anonymous user with jwt %s " % jwt)
setItem("jwt", jwt)
Expand Down
19 changes: 9 additions & 10 deletions lib/SoftwareUserStatus.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,16 @@ def getUserRegistrationState(is_integration=False):
registered = user.get("registered", 0)
user_jwt = user.get("plugin_jwt", None)

if (is_integration is False):
if (user_jwt is not None):
setItem("jwt", user_jwt)
if (registered == 1 and user_jwt is not None):
logIt("Updated user JWT %s " % user_jwt)
setItem("jwt", user_jwt)

if (registered == 1):
setItem("name", user.get("email"))
setItem("name", user.get("email"))

setItem("switching_account", False)
setAuthCallbackState(None)
userState["logged_on"] = True
userState["user"] = user
setItem("switching_account", False)
setAuthCallbackState(None)
userState["logged_on"] = True
userState["user"] = user
else:
userState["logged_on"] = False
userState["user"] = None
Expand Down Expand Up @@ -152,7 +151,7 @@ def getLoginUrl(loginType = "software", switching_account=True):
loginUrl += "?" + qryStr
if (switching_account is True):
loginUrl += "&login=true"

return loginUrl

def launchWebDashboardUrl():
Expand Down
13 changes: 5 additions & 8 deletions lib/SoftwareUtil.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def softwareSessionFileExists():
def getFileDataAsJson(file):
data = None
if os.path.isfile(file):
with open(file) as f:
with open(file, encoding='utf-8') as f:
try:
data = json.load(f)
except Exception as ex:
Expand All @@ -112,7 +112,7 @@ def getFileDataArray(file):
if os.path.isfile(file):
with open(file) as f:
try:
contents = json.load(f)
contents = json.loads(f)
if (isinstance(contents, list)):
payloads = contents
else:
Expand All @@ -126,12 +126,12 @@ def getFileDataPayloadsAsJson(file):
payloads = []
if os.path.isfile(file):
try:
with open(file) as f:
with open(file, encoding='utf-8') as f:
for line in f:
if (line and line.strip()):
line = line.rstrip()
# convert to object
json_obj = json.loads(line)
json_obj = json.load(line)
# convert to json to send
payloads.append(json_obj)
except Exception as ex:
Expand Down Expand Up @@ -196,12 +196,9 @@ def getCommandResultLine(cmd, projectDir):
return resultLine

def getCommandResultList(cmd, projectDir):
# print(cmd)
# print(projectDir)
try:
result = check_output(cmd, cwd=projectDir)
except CalledProcessError as ex:
# print('reusltlisterrorerror: {}'.format(ex.output))
if ex.output != b'': # Suppress trivial error
logIt('Error running {}: {}'.format(cmd, ex.output))
return []
Expand Down Expand Up @@ -370,7 +367,7 @@ def getIcons():
dirname = os.path.dirname(__file__)
icons_file = os.path.join(dirname, '../icons.json')
with open(icons_file, 'r') as f:
icons_dict = json.load(f)
icons_dict = json.loads(f)
return icons_dict
except Exception:
return {}
Expand Down

0 comments on commit 66fb3f7

Please sign in to comment.