From c7fc967d527ef6303a47591d04b2d3a8c4991ddc Mon Sep 17 00:00:00 2001 From: Sergey Fedorov Date: Tue, 27 Aug 2024 09:29:18 +0800 Subject: [PATCH] Unrustify glances See: https://trac.macports.org/ticket/70625 diff --git README.rst README.rst index 7fbc4319..d5be3b45 100644 --- README.rst +++ README.rst @@ -86,7 +86,7 @@ Requirements - ``psutil`` (better with latest version) - ``defusedxml`` (in order to monkey patch xmlrpc) - ``packaging`` (for the version comparison) -- ``orjson`` (an optimized alternative to the standard json module) +- ``ujson`` (an optimized alternative to the standard json module) *Note for Python 2 users* diff --git doc-requirements.txt doc-requirements.txt index 28ee5654..c13c583a 100644 --- doc-requirements.txt +++ doc-requirements.txt @@ -1,7 +1,7 @@ psutil defusedxml -orjson reuse setuptools>=65.5.1 # not directly required, pinned by Snyk to avoid a vulnerability sphinx sphinx_rtd_theme +ujson diff --git glances/client.py glances/client.py index 308f2061..a73fedb0 100644 --- glances/client.py +++ glances/client.py @@ -11,7 +11,7 @@ import sys import time -import orjson +import ujson from glances import __version__ from glances.globals import Fault, ProtocolError, ServerProxy, Transport @@ -118,7 +118,7 @@ class GlancesClient: if __version__.split('.')[0] == client_version.split('.')[0]: # Init stats self.stats = GlancesStatsClient(config=self.config, args=self.args) - self.stats.set_plugins(orjson.loads(self.client.getAllPlugins())) + self.stats.set_plugins(ujson.loads(self.client.getAllPlugins())) logger.debug(f"Client version: {__version__} / Server version: {client_version}") else: self.log_and_exit( @@ -195,7 +195,7 @@ class GlancesClient: """ # Update the stats try: - server_stats = orjson.loads(self.client.getAll()) + server_stats = ujson.loads(self.client.getAll()) except OSError: # Client cannot get server stats return "Disconnected" diff --git glances/client_browser.py glances/client_browser.py index 12a52680..233eb0bb 100644 --- glances/client_browser.py +++ glances/client_browser.py @@ -10,7 +10,7 @@ import threading -import orjson +import ujson from glances.autodiscover import GlancesAutoDiscoverServer from glances.client import GlancesClient, GlancesClientTransport @@ -97,13 +97,13 @@ class GlancesClientBrowser: # CPU% # logger.info(f"CPU stats {s.getPlugin('cpu')}") # logger.info(f"CPU views {s.getPluginView('cpu')}") - server['cpu_percent'] = orjson.loads(s.getPlugin('cpu'))['total'] - server['cpu_percent_decoration'] = orjson.loads(s.getPluginView('cpu'))['total']['decoration'] + server['cpu_percent'] = ujson.loads(s.getPlugin('cpu'))['total'] + server['cpu_percent_decoration'] = ujson.loads(s.getPluginView('cpu'))['total']['decoration'] # MEM% - server['mem_percent'] = orjson.loads(s.getPlugin('mem'))['percent'] - server['mem_percent_decoration'] = orjson.loads(s.getPluginView('mem'))['percent']['decoration'] + server['mem_percent'] = ujson.loads(s.getPlugin('mem'))['percent'] + server['mem_percent_decoration'] = ujson.loads(s.getPluginView('mem'))['percent']['decoration'] # OS (Human Readable name) - server['hr_name'] = orjson.loads(s.getPlugin('system'))['hr_name'] + server['hr_name'] = ujson.loads(s.getPlugin('system'))['hr_name'] server['hr_name_decoration'] = 'DEFAULT' except (OSError, Fault, KeyError) as e: logger.debug(f"Error while grabbing stats form server ({e})") @@ -124,8 +124,8 @@ class GlancesClientBrowser: # Optional stats (load is not available on Windows OS) try: # LOAD - server['load_min5'] = round(orjson.loads(s.getPlugin('load'))['min5'], 1) - server['load_min5_decoration'] = orjson.loads(s.getPluginView('load'))['min5']['decoration'] + server['load_min5'] = round(ujson.loads(s.getPlugin('load'))['min5'], 1) + server['load_min5_decoration'] = ujson.loads(s.getPluginView('load'))['min5']['decoration'] except Exception as e: logger.warning(f"Error while grabbing stats form server ({e})") diff --git glances/globals.py glances/globals.py index 0060e3d1..f53ee1d4 100644 --- glances/globals.py +++ glances/globals.py @@ -33,7 +33,7 @@ from urllib.request import Request, urlopen from xmlrpc.client import Fault, ProtocolError, Server, ServerProxy, Transport from xmlrpc.server import SimpleXMLRPCRequestHandler, SimpleXMLRPCServer -import orjson +import ujson # Correct issue #1025 by monkey path the xmlrpc lib from defusedxml.xmlrpc import monkey_patch @@ -309,9 +309,9 @@ def json_dumps(data): Manage the issue #815 for Windows OS with UnicodeDecodeError catching. """ try: - return orjson.dumps(data) + return ujson.dumps(data) except UnicodeDecodeError: - return orjson.dumps(data, ensure_ascii=False) + return ujson.dumps(data, ensure_ascii=False) def dictlist(data, item): diff --git glances/outputs/glances_restful_api.py glances/outputs/glances_restful_api.py index a4549abf..334a19b1 100644 --- glances/outputs/glances_restful_api.py +++ glances/outputs/glances_restful_api.py @@ -31,7 +31,7 @@ try: from fastapi import APIRouter, Depends, FastAPI, HTTPException, Request, status from fastapi.middleware.cors import CORSMiddleware from fastapi.middleware.gzip import GZipMiddleware - from fastapi.responses import HTMLResponse, ORJSONResponse + from fastapi.responses import HTMLResponse, UJSONResponse from fastapi.security import HTTPBasic, HTTPBasicCredentials from fastapi.staticfiles import StaticFiles from fastapi.templating import Jinja2Templates @@ -188,85 +188,85 @@ class GlancesRestfulApi: f'/api/{self.API_VERSION}/status', status_code=status.HTTP_200_OK, methods=['HEAD', 'GET'], - response_class=ORJSONResponse, + response_class=UJSONResponse, endpoint=self._api_status, ) router.add_api_route( - f'/api/{self.API_VERSION}/config', response_class=ORJSONResponse, endpoint=self._api_config + f'/api/{self.API_VERSION}/config', response_class=UJSONResponse, endpoint=self._api_config ) router.add_api_route( f'/api/{self.API_VERSION}/config/{{section}}', - response_class=ORJSONResponse, + response_class=UJSONResponse, endpoint=self._api_config_section, ) router.add_api_route( f'/api/{self.API_VERSION}/config/{{section}}/{{item}}', - response_class=ORJSONResponse, + response_class=UJSONResponse, endpoint=self._api_config_section_item, ) - router.add_api_route(f'/api/{self.API_VERSION}/args', response_class=ORJSONResponse, endpoint=self._api_args) + router.add_api_route(f'/api/{self.API_VERSION}/args', response_class=UJSONResponse, endpoint=self._api_args) router.add_api_route( - f'/api/{self.API_VERSION}/args/{{item}}', response_class=ORJSONResponse, endpoint=self._api_args_item + f'/api/{self.API_VERSION}/args/{{item}}', response_class=UJSONResponse, endpoint=self._api_args_item ) router.add_api_route( - f'/api/{self.API_VERSION}/pluginslist', response_class=ORJSONResponse, endpoint=self._api_plugins + f'/api/{self.API_VERSION}/pluginslist', response_class=UJSONResponse, endpoint=self._api_plugins ) - router.add_api_route(f'/api/{self.API_VERSION}/all', response_class=ORJSONResponse, endpoint=self._api_all) + router.add_api_route(f'/api/{self.API_VERSION}/all', response_class=UJSONResponse, endpoint=self._api_all) router.add_api_route( - f'/api/{self.API_VERSION}/all/limits', response_class=ORJSONResponse, endpoint=self._api_all_limits + f'/api/{self.API_VERSION}/all/limits', response_class=UJSONResponse, endpoint=self._api_all_limits ) router.add_api_route( - f'/api/{self.API_VERSION}/all/views', response_class=ORJSONResponse, endpoint=self._api_all_views + f'/api/{self.API_VERSION}/all/views', response_class=UJSONResponse, endpoint=self._api_all_views ) - router.add_api_route(f'/api/{self.API_VERSION}/help', response_class=ORJSONResponse, endpoint=self._api_help) - router.add_api_route(f'/api/{self.API_VERSION}/{{plugin}}', response_class=ORJSONResponse, endpoint=self._api) + router.add_api_route(f'/api/{self.API_VERSION}/help', response_class=UJSONResponse, endpoint=self._api_help) + router.add_api_route(f'/api/{self.API_VERSION}/{{plugin}}', response_class=UJSONResponse, endpoint=self._api) router.add_api_route( - f'/api/{self.API_VERSION}/{{plugin}}/history', response_class=ORJSONResponse, endpoint=self._api_history + f'/api/{self.API_VERSION}/{{plugin}}/history', response_class=UJSONResponse, endpoint=self._api_history ) router.add_api_route( f'/api/{self.API_VERSION}/{{plugin}}/history/{{nb}}', - response_class=ORJSONResponse, + response_class=UJSONResponse, endpoint=self._api_history, ) router.add_api_route( - f'/api/{self.API_VERSION}/{{plugin}}/top/{{nb}}', response_class=ORJSONResponse, endpoint=self._api_top + f'/api/{self.API_VERSION}/{{plugin}}/top/{{nb}}', response_class=UJSONResponse, endpoint=self._api_top ) router.add_api_route( - f'/api/{self.API_VERSION}/{{plugin}}/limits', response_class=ORJSONResponse, endpoint=self._api_limits + f'/api/{self.API_VERSION}/{{plugin}}/limits', response_class=UJSONResponse, endpoint=self._api_limits ) router.add_api_route( - f'/api/{self.API_VERSION}/{{plugin}}/views', response_class=ORJSONResponse, endpoint=self._api_views + f'/api/{self.API_VERSION}/{{plugin}}/views', response_class=UJSONResponse, endpoint=self._api_views ) router.add_api_route( - f'/api/{self.API_VERSION}/{{plugin}}/{{item}}', response_class=ORJSONResponse, endpoint=self._api_item + f'/api/{self.API_VERSION}/{{plugin}}/{{item}}', response_class=UJSONResponse, endpoint=self._api_item ) router.add_api_route( f'/api/{self.API_VERSION}/{{plugin}}/{{item}}/history', - response_class=ORJSONResponse, + response_class=UJSONResponse, endpoint=self._api_item_history, ) router.add_api_route( f'/api/{self.API_VERSION}/{{plugin}}/{{item}}/history/{{nb}}', - response_class=ORJSONResponse, + response_class=UJSONResponse, endpoint=self._api_item_history, ) router.add_api_route( f'/api/{self.API_VERSION}/{{plugin}}/{{item}}/description', - response_class=ORJSONResponse, + response_class=UJSONResponse, endpoint=self._api_item_description, ) router.add_api_route( f'/api/{self.API_VERSION}/{{plugin}}/{{item}}/unit', - response_class=ORJSONResponse, + response_class=UJSONResponse, endpoint=self._api_item_unit, ) router.add_api_route( f'/api/{self.API_VERSION}/{{plugin}}/{{item}}/{{value:path}}', - response_class=ORJSONResponse, + response_class=UJSONResponse, endpoint=self._api_value, ) @@ -361,7 +361,7 @@ class GlancesRestfulApi: See related issue: Web server health check endpoint #1988 """ - return ORJSONResponse({'version': __version__}) + return UJSONResponse({'version': __version__}) def _api_help(self): """Glances API RESTful implementation. @@ -373,7 +373,7 @@ class GlancesRestfulApi: except Exception as e: raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"Cannot get help view data ({str(e)})") - return ORJSONResponse(plist) + return UJSONResponse(plist) def _api_plugins(self): """Glances API RESTFul implementation. @@ -409,7 +409,7 @@ class GlancesRestfulApi: except Exception as e: raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"Cannot get plugin list ({str(e)})") - return ORJSONResponse(plist) + return UJSONResponse(plist) def _api_all(self): """Glances API RESTful implementation. @@ -436,7 +436,7 @@ class GlancesRestfulApi: except Exception as e: raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"Cannot get stats ({str(e)})") - return ORJSONResponse(statval) + return UJSONResponse(statval) def _api_all_limits(self): """Glances API RESTful implementation. @@ -452,7 +452,7 @@ class GlancesRestfulApi: except Exception as e: raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"Cannot get limits ({str(e)})") - return ORJSONResponse(limits) + return UJSONResponse(limits) def _api_all_views(self): """Glances API RESTful implementation. @@ -468,7 +468,7 @@ class GlancesRestfulApi: except Exception as e: raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"Cannot get views ({str(e)})") - return ORJSONResponse(limits) + return UJSONResponse(limits) def _api(self, plugin): """Glances API RESTful implementation. @@ -493,7 +493,7 @@ class GlancesRestfulApi: except Exception as e: raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"Cannot get plugin {plugin} ({str(e)})") - return ORJSONResponse(statval) + return UJSONResponse(statval) def _api_top(self, plugin, nb: int = 0): """Glances API RESTful implementation. @@ -525,7 +525,7 @@ class GlancesRestfulApi: if isinstance(statval, list): statval = statval[:nb] - return ORJSONResponse(statval) + return UJSONResponse(statval) def _api_history(self, plugin, nb: int = 0): """Glances API RESTful implementation. @@ -577,7 +577,7 @@ class GlancesRestfulApi: status_code=status.HTTP_404_NOT_FOUND, detail=f"Cannot get limits for plugin {plugin} ({str(e)})" ) - return ORJSONResponse(ret) + return UJSONResponse(ret) def _api_views(self, plugin): """Glances API RESTful implementation. @@ -601,7 +601,7 @@ class GlancesRestfulApi: status_code=status.HTTP_404_NOT_FOUND, detail=f"Cannot get views for plugin {plugin} ({str(e)})" ) - return ORJSONResponse(ret) + return UJSONResponse(ret) def _api_item(self, plugin, item): """Glances API RESTful implementation. @@ -629,7 +629,7 @@ class GlancesRestfulApi: detail=f"Cannot get item {item} in plugin {plugin} ({str(e)})", ) - return ORJSONResponse(ret) + return UJSONResponse(ret) def _api_item_history(self, plugin, item, nb: int = 0): """Glances API RESTful implementation. @@ -657,7 +657,7 @@ class GlancesRestfulApi: status_code=status.HTTP_404_NOT_FOUND, detail=f"Cannot get history for plugin {plugin} ({str(e)})" ) else: - return ORJSONResponse(ret) + return UJSONResponse(ret) def _api_item_description(self, plugin, item): """Glances API RESTful implementation. @@ -682,7 +682,7 @@ class GlancesRestfulApi: detail=f"Cannot get {item} description for plugin {plugin} ({str(e)})", ) else: - return ORJSONResponse(ret) + return UJSONResponse(ret) def _api_item_unit(self, plugin, item): """Glances API RESTful implementation. @@ -707,7 +707,7 @@ class GlancesRestfulApi: detail=f"Cannot get {item} unit for plugin {plugin} ({str(e)})", ) else: - return ORJSONResponse(ret) + return UJSONResponse(ret) def _api_value(self, plugin, item, value): """Glances API RESTful implementation. @@ -735,7 +735,7 @@ class GlancesRestfulApi: detail=f"Cannot get {item} = {value} for plugin {plugin} ({str(e)})", ) else: - return ORJSONResponse(ret) + return UJSONResponse(ret) def _api_config(self): """Glances API RESTful implementation. @@ -750,7 +750,7 @@ class GlancesRestfulApi: except Exception as e: raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"Cannot get config ({str(e)})") else: - return ORJSONResponse(args_json) + return UJSONResponse(args_json) def _api_config_section(self, section): """Glances API RESTful implementation. @@ -772,7 +772,7 @@ class GlancesRestfulApi: status_code=status.HTTP_404_NOT_FOUND, detail=f"Cannot get config section {section} ({str(e)})" ) - return ORJSONResponse(ret_section) + return UJSONResponse(ret_section) def _api_config_section_item(self, section, item): """Glances API RESTful implementation. @@ -803,7 +803,7 @@ class GlancesRestfulApi: detail=f"Cannot get item {item} in config section {section} ({str(e)})", ) - return ORJSONResponse(ret_item) + return UJSONResponse(ret_item) def _api_args(self): """Glances API RESTful implementation. @@ -820,7 +820,7 @@ class GlancesRestfulApi: except Exception as e: raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"Cannot get args ({str(e)})") - return ORJSONResponse(args_json) + return UJSONResponse(args_json) def _api_args_item(self, item): """Glances API RESTful implementation. @@ -841,4 +841,4 @@ class GlancesRestfulApi: except Exception as e: raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"Cannot get args item ({str(e)})") - return ORJSONResponse(args_json) + return UJSONResponse(args_json) diff --git glances/plugins/ip/__init__.py glances/plugins/ip/__init__.py index 62a95658..6d5f55f2 100644 --- glances/plugins/ip/__init__.py +++ glances/plugins/ip/__init__.py @@ -10,7 +10,7 @@ import threading -from orjson import loads +from ujson import loads from glances.globals import queue, urlopen_auth from glances.logger import logger diff --git requirements.txt requirements.txt index 474a1f93..1b01d13b 100644 --- requirements.txt +++ requirements.txt @@ -1,4 +1,4 @@ defusedxml -orjson packaging psutil>=5.6.7 +ujson>=5.4.0 diff --git tox.ini tox.ini index aedea68a..e61d733e 100644 --- tox.ini +++ tox.ini @@ -18,7 +18,7 @@ deps = psutil defusedxml packaging - orjson + ujson fastapi uvicorn jinja2