Skip to content

Commit

Permalink
refactor: Run pre-commit autoupdate and add pyupgrade+flynt
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisburr committed Feb 14, 2023
1 parent 8da280d commit 97a4c47
Show file tree
Hide file tree
Showing 36 changed files with 93 additions and 134 deletions.
19 changes: 15 additions & 4 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,20 @@ exclude: |
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.1.0
rev: v4.4.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-yaml
- id: check-added-large-files

- repo: https://github.com/psf/black
rev: 22.3.0
rev: 23.1.0
hooks:
- id: black

- repo: https://github.com/pre-commit/mirrors-prettier
rev: v2.4.1
rev: v3.0.0-alpha.4
hooks:
- id: prettier
exclude: |
Expand All @@ -33,9 +33,20 @@ repos:
)$
- repo: https://github.com/pre-commit/mirrors-eslint
rev: v8.0.1
rev: v8.34.0
hooks:
- id: eslint
additional_dependencies:
- eslint@8.0.1
- http://diracproject.web.cern.ch/diracproject/externalLibraries/@sencha_eslint-plugin-extjs.7.0.0.tgz

- repo: https://github.com/ikamensh/flynt/
rev: "0.77"
hooks:
- id: flynt

- repo: https://github.com/asottile/pyupgrade
rev: v3.3.1
hooks:
- id: pyupgrade
args: ["--py39-plus"]
3 changes: 2 additions & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"useTabs": false,
"printWidth": 150,
"singleQuote": false
"singleQuote": false,
"trailingComma": "es5"
}
10 changes: 5 additions & 5 deletions src/WebAppDIRAC/Core/App.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def bootstrap(self):
# Configure tornado app
self.__app = tornado.web.Application(routes, **kw)
port = Conf.HTTPPort()
self.log.notice("Configuring HTTP on port %s" % port)
self.log.notice(f"Configuring HTTP on port {port}")
# Create the web servers
srv = tornado.httpserver.HTTPServer(self.__app, xheaders=True)
srv.listen(port)
Expand All @@ -127,7 +127,7 @@ def bootstrap(self):
Conf.generateRevokedCertsFile() # it is used by nginx....

if Conf.HTTPS():
self.log.notice("Configuring HTTPS on port %s" % Conf.HTTPSPort())
self.log.notice(f"Configuring HTTPS on port {Conf.HTTPSPort()}")
sslops = dict(
certfile=Conf.HTTPSCert(),
keyfile=Conf.HTTPSKey(),
Expand All @@ -142,8 +142,8 @@ def bootstrap(self):
if sslprotocol in aviableProtocols:
sslops["ssl_version"] = getattr(ssl, sslprotocol)
else:
message = "%s protocol is not provided." % sslprotocol
message += "The following protocols are provided: %s" % str(aviableProtocols)
message = f"{sslprotocol} protocol is not provided."
message += f"The following protocols are provided: {str(aviableProtocols)}"
gLogger.warn(message)

self.log.debug(" - %s" % "\n - ".join([f"{k} = {sslops[k]}" for k in sslops]))
Expand All @@ -165,6 +165,6 @@ def run(self):
urls = []
for proto, port in self.__servers:
urls.append(f"{proto}://0.0.0.0:{port}/{bu}/")
self.log.always("Listening on %s" % " and ".join(urls))
self.log.always(f"Listening on {' and '.join(urls)}")
tornado.autoreload.add_reload_hook(self.__reloadAppCB)
tornado.ioloop.IOLoop.instance().start()
2 changes: 1 addition & 1 deletion src/WebAppDIRAC/Core/CoreHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def get(self, setup, group, route):
dest = "/"
rootURL = Conf.rootURL()
if rootURL:
dest += "%s/" % rootURL.strip("/")
dest += f"{rootURL.strip('/')}/"
if setup and group:
dest += f"s:{setup}/g:{group}/"
self.redirect(dest)
4 changes: 2 additions & 2 deletions src/WebAppDIRAC/Core/HandlerMgr.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def __calculateRoutes(self):

# Add some standard paths for static files
statDirectories = Conf.getStaticDirs()
self.log.info("The following static directories are used: %s" % str(statDirectories))
self.log.info(f"The following static directories are used: {str(statDirectories)}")
for staticDirectory in statDirectories:
# Get real static files directory
realStaticDirectory = f"{rootPath}/webRoot/www/{staticDirectory}"
Expand All @@ -95,7 +95,7 @@ def __calculateRoutes(self):
handler_base_url = self.__baseURL + self.__setupGroupRE

for hn in self.__handlers:
self.log.info("Found handler %s" % hn)
self.log.info(f"Found handler {hn}")
handler = self.__handlers[hn]

# Set the base URL of the request
Expand Down
2 changes: 1 addition & 1 deletion src/WebAppDIRAC/Core/TemplateLoader.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ def _create_template(self, name):
template = Template(f.read(), name=name, loader=self)
f.close()
return template
raise RuntimeError("Can't find template %s" % name)
raise RuntimeError(f"Can't find template {name}")
7 changes: 3 additions & 4 deletions src/WebAppDIRAC/Lib/SessionData.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@


class SessionData:

__handlers = {}
__groupMenu = {}
__extensions = []
Expand Down Expand Up @@ -153,7 +152,7 @@ def __getGroupMenu(self):
:return: list
"""
menuSection = "%s/Schema" % (Conf.BASECS)
menuSection = f"{Conf.BASECS}/Schema"
# Somebody coming from HTTPS and not with a valid group
group = self.__credDict.get("group", "")
# Cache time!
Expand Down Expand Up @@ -219,6 +218,6 @@ def getData(self):
# if result['OK']:
# data['groupsStatuses'] = result['Value']
# Calculate baseURL
baseURL = [Conf.rootURL().strip("/"), "s:%s" % data["setup"], "g:%s" % self.__credDict.get("group", "")]
data["baseURL"] = "/%s" % "/".join(baseURL)
baseURL = [Conf.rootURL().strip("/"), f"s:{data['setup']}", f"g:{self.__credDict.get('group', '')}"]
data["baseURL"] = f"/{'/'.join(baseURL)}"
return data
6 changes: 2 additions & 4 deletions src/WebAppDIRAC/Lib/WebHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ def _authzSESSION(self):
try:
return self._authzJWT(token["access_token"])
except Exception as e:
sLog.debug("Cannot check access token %s, try to fetch.." % repr(e))
sLog.debug(f"Cannot check access token {repr(e)}, try to fetch..")
# Try to refresh access_token and refresh_token
result = self._idps.getIdProvider("DIRACWeb")
if not result["OK"]:
Expand Down Expand Up @@ -320,9 +320,7 @@ def write_error(self, status_code, **kwargs):
def finishWithImage(self, data, plotImageFile, disableCaching=False):
# Set headers
self.set_header("Content-Type", "image/png")
self.set_header(
"Content-Disposition", 'attachment; filename="%s.png"' % md5(plotImageFile.encode()).hexdigest()
)
self.set_header("Content-Disposition", f'attachment; filename="{md5(plotImageFile.encode()).hexdigest()}.png"')
self.set_header("Content-Length", len(data))
self.set_header("Content-Transfer-Encoding", "Binary")
if disableCaching:
Expand Down
11 changes: 5 additions & 6 deletions src/WebAppDIRAC/WebApp/handler/AccountingHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,13 @@ def web_getSelectionData(self, type, **kwargs):
callback["selectionValues"] = records

# Cache for plotsList?
data = AccountingHandler.__keysCache.get("reportsList:%s" % typeName)
data = AccountingHandler.__keysCache.get(f"reportsList:{typeName}")
if not data:
retVal = self.repClient.listReports(typeName)
if not retVal["OK"]:
return {"success": "false", "result": "", "error": retVal["Message"]}
data = retVal["Value"]
AccountingHandler.__keysCache.add("reportsList:%s" % typeName, 300, data)
AccountingHandler.__keysCache.add(f"reportsList:{typeName}", 300, data)
callback["plotsList"] = data
return {"success": "true", "result": callback}

Expand Down Expand Up @@ -144,7 +144,6 @@ def __parseFormParams(self, timeSelector, typeName=None, **pD):
if not pinDates:
extraParams["lastSeconds"] = pD["timeSelector"]
else:

end = False
if "endTime" in pD:
end = TimeUtilities.fromString(pD["endTime"])
Expand Down Expand Up @@ -295,14 +294,14 @@ def web_getCsvPlotData(self, typeName, plotName, timeSelector: int, grouping, **
data = rawData["data"]
tS = int(TimeUtilities.toEpoch(start))
timeStart = tS - tS % granularity
strData = "epoch,%s\n" % ",".join(groupKeys)
strData = f"epoch,{','.join(groupKeys)}\n"
for timeSlot in range(timeStart, int(TimeUtilities.toEpoch(end)), granularity):
lineData = [str(timeSlot)]
for key in groupKeys:
lineData.append(str(data[key][timeSlot]) if timeSlot in data[key] else "")
strData += "%s\n" % ",".join(lineData)
strData += f"{','.join(lineData)}\n"
else:
strData = "%s\n" % ",".join(groupKeys)
strData = f"{','.join(groupKeys)}\n"
strData += ",".join([str(rawData["data"][k]) for k in groupKeys])

return FileResponse(strData, str(params), "csv", cache=False)
Expand Down
1 change: 0 additions & 1 deletion src/WebAppDIRAC/WebApp/handler/ApplicationWizardHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@


class ApplicationWizardHandler(WebHandler):

DEFAULT_AUTHORIZATION = "authenticated"
5 changes: 2 additions & 3 deletions src/WebAppDIRAC/WebApp/handler/ComponentHistoryHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@


class ComponentHistoryHandler(WebHandler):

DEFAULT_AUTHORIZATION = "authenticated"

def web_getInstallationData(self):
Expand Down Expand Up @@ -118,15 +117,15 @@ def __request(self):
time = self.get_argument("startTime")
else:
time = "00:00"
date = datetime.datetime.strptime("{}-{}".format(self.get_argument("startDate"), time), "%Y-%m-%d-%H:%M")
date = datetime.datetime.strptime(f"{self.get_argument('startDate')}-{time}", "%Y-%m-%d-%H:%M")
req["installation"]["InstallationTime.bigger"] = date

if "endDate" in self.request.arguments and len(self.get_argument("endDate")) > 0:
if len(self.get_argument("endTime")) > 0:
time = self.get_argument("endTime")
else:
time = "00:00"
date = datetime.datetime.strptime("{}-{}".format(self.get_argument("endDate"), time), "%Y-%m-%d-%H:%M")
date = datetime.datetime.strptime(f"{self.get_argument('endDate')}-{time}", "%Y-%m-%d-%H:%M")
req["installation"]["UnInstallationTime.smaller"] = date

return req
20 changes: 8 additions & 12 deletions src/WebAppDIRAC/WebApp/handler/ConfigurationManagerHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@


class ConfigurationManagerHandler(WebSocketHandler):

AUTH_PROPS = "authenticated"

def on_open(self):
Expand Down Expand Up @@ -91,7 +90,6 @@ def __getRemoteConfiguration(self, funcName):
return {"success": 1, "op": funcName, "version": version, "name": configName}

def __getSubnodes(self, parentNodeId, sectionPath):

self.log.info("Expanding section", sectionPath)

retData = []
Expand All @@ -101,7 +99,6 @@ def __getSubnodes(self, parentNodeId, sectionPath):
return {"success": 1, "op": "getSubnodes", "nodes": retData, "parentNodeId": parentNodeId}

def __getSubnodesForPath(self, sectionPath, retData):

try:
sectionCfg = self.__configData["cfgData"].getCFG()
for section in [section for section in sectionPath.split("/") if not section.strip() == ""]:
Expand Down Expand Up @@ -146,7 +143,7 @@ def __htmlComment(self, rawComment):
continue
commentLines.append(line)
if commentLines or commiter:
return "{}<small><strong>{}</strong></small>".format("<br/>".join(commentLines), commiter)
return f"{'<br/>'.join(commentLines)}<small><strong>{commiter}</strong></small>"
return False

def __showConfigurationAsText(self):
Expand Down Expand Up @@ -176,7 +173,7 @@ def __setOptionValue(self, params):
if self.__configData["cfgData"].getValue(optionPath) == optionValue:
self.log.info("Set option value", f"{optionPath} = {optionValue}")
return {"success": 1, "op": "setOptionValue", "parentNodeId": params["parentNodeId"], "value": optionValue}
return {"success": 0, "op": "setOptionValue", "message": "Can't update %s" % optionPath}
return {"success": 0, "op": "setOptionValue", "message": f"Can't update {optionPath}"}

def __setComment(self, params):
try:
Expand Down Expand Up @@ -373,7 +370,7 @@ def __moveNode(self, params):
"oldIndex": params["oldIndex"],
}
# Calculate the old parent path
oldParentPath = "/%s" % "/".join(List.fromChar(nodePath, "/")[:-1])
oldParentPath = f"/{'/'.join(List.fromChar(nodePath, '/')[:-1])}"
if not oldParentPath == destinationParentPath and newParentDict["value"].existsKey(nodeDict["key"]):
return {
"success": 0,
Expand Down Expand Up @@ -437,7 +434,7 @@ def __copyKey(self, params):
if not newParentDict:
return {"success": 0, "op": "copyKey", "message": "Destination does not exist"}
# Calculate the old parent path
oldParentPath = "/%s" % "/".join(List.fromChar(nodePath, "/")[:-1])
oldParentPath = f"/{'/'.join(List.fromChar(nodePath, '/')[:-1])}"
if not oldParentPath == destinationParentPath and newParentDict["value"].existsKey(newNodeName):
return {"success": 0, "op": "copyKey", "message": "Another entry with the same name already exists"}

Expand Down Expand Up @@ -547,7 +544,7 @@ def __showDiff(self, params):
fromDate = str(params["fromVersion"])
toDate = str(params["toVersion"])
except Exception as e:
raise WErr(500, "Can't decode params: %s" % e)
raise WErr(500, f"Can't decode params: {e}")
rpcClient = ConfigurationClient(
url=gConfig.getValue("/DIRAC/Configuration/MasterServer", "Configuration/Server")
)
Expand All @@ -561,7 +558,7 @@ def __showDiff(self, params):
"totalLines": processedData["totalLines"],
"html": self.render_string(
"ConfigurationManager/diffConfig.tpl",
titles=("From version %s" % fromDate, "To version %s" % toDate),
titles=(f"From version {fromDate}", f"To version {toDate}"),
diffList=processedData["diff"],
).decode("utf-8"),
}
Expand All @@ -573,7 +570,7 @@ def __rollback(self, params):
try:
rollbackVersion = str(params["rollbackToVersion"])
except Exception as e:
raise WErr(500, "Can't decode params: %s" % e)
raise WErr(500, f"Can't decode params: {e}")
rpcClient = ConfigurationClient(
url=gConfig.getValue("/DIRAC/Configuration/MasterServer", "Configuration/Server")
)
Expand Down Expand Up @@ -605,10 +602,9 @@ def __history(self):
return {"success": 1, "op": "showshowHistory", "result": cDict}

def __download(self):

version = str(self.__configData["cfgData"].getCFG()["DIRAC"]["Configuration"]["Version"])
configName = str(self.__configData["cfgData"].getCFG()["DIRAC"]["Configuration"]["Name"])
fileName = "cs.{}.{}".format(configName, version.replace(":", "").replace("-", "").replace(" ", ""))
fileName = f"cs.{configName}.{version.replace(':', '').replace('-', '').replace(' ', '')}"

return {"success": 1, "op": "download", "result": self.__configData["strCfgData"], "fileName": fileName}

Expand Down
1 change: 0 additions & 1 deletion src/WebAppDIRAC/WebApp/handler/DowntimesHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@


class DowntimesHandler(WebHandler):

DEFAULT_AUTHORIZATION = "authenticated"

# Publisher client
Expand Down
1 change: 0 additions & 1 deletion src/WebAppDIRAC/WebApp/handler/ExampleAppHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@


class ExampleAppHandler(WebHandler):

DEFAULT_AUTHORIZATION = "all"

def web_getJobData(self):
Expand Down
Loading

0 comments on commit 97a4c47

Please sign in to comment.