Skip to content

Commit

Permalink
Merge pull request #673 from TaykYoku/5.0_use_new_WebHandler
Browse files Browse the repository at this point in the history
[5.0] use a new style of handler writing, remove old WebHandler
  • Loading branch information
chrisburr authored Jun 23, 2022
2 parents b366edd + ef38b18 commit 5c5404b
Show file tree
Hide file tree
Showing 28 changed files with 38 additions and 87 deletions.
15 changes: 6 additions & 9 deletions src/WebAppDIRAC/Core/HandlerMgr.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from DIRAC.Core.Utilities.Extensions import extensionsByPriority, getExtensionMetadata

from WebAppDIRAC.Lib import Conf
from WebAppDIRAC.Lib.WebHandler import WebHandler, _WebHandler
from WebAppDIRAC.Lib.WebHandler import WebHandler
from WebAppDIRAC.Core.StaticHandler import StaticHandler


Expand Down Expand Up @@ -59,14 +59,11 @@ def __calculateRoutes(self):
:return: S_OK()/S_ERROR()
"""
ol = ObjectLoader()
handlerList = []
self.log.debug("Add handles from: %s", self.__handlersLocation)
for parentClass in [WebHandler, _WebHandler]:
result = ol.getObjects(self.__handlersLocation, parentClass=parentClass, recurse=True, continueOnError=True)
if not result["OK"]:
return result
handlerList += list(result["Value"].items())
self.__handlers = collections.OrderedDict(handlerList)
self.log.debug("Add handles from:", self.__handlersLocation)
result = ol.getObjects(self.__handlersLocation, parentClass=WebHandler, recurse=True, continueOnError=True)
if not result["OK"]:
return result
self.__handlers = collections.OrderedDict(list(result["Value"].items()))

# ['/opt/dirac/pro/WebAppExt/WebApp/static', ...]
staticPaths = self.getPaths("static")
Expand Down
48 changes: 3 additions & 45 deletions src/WebAppDIRAC/Lib/WebHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import json
import pprint
import datetime
import functools
import traceback
from hashlib import md5
from concurrent.futures import ThreadPoolExecutor
Expand All @@ -13,7 +12,6 @@
import tornado.websocket
from tornado import gen
from tornado.web import HTTPError
from tornado.ioloop import IOLoop

from DIRAC import gLogger, S_OK, S_ERROR
from DIRAC.Core.Utilities.JEncode import DATETIME_DEFAULT_FORMAT
Expand Down Expand Up @@ -125,7 +123,7 @@ def defaultEncoder(data):
raise TypeError("Object of type {} is not JSON serializable".format(data.__class__.__name__))


class _WebHandler(TornadoREST):
class WebHandler(TornadoREST):
DEFAULT_AUTHENTICATION = ["SSL", "SESSION", "VISITOR"]
# Auth requirements DEFAULT_AUTHORIZATION
DEFAULT_AUTHORIZATION = None
Expand Down Expand Up @@ -342,49 +340,9 @@ def finishWithImage(self, data, plotImageFile, disableCaching=False):
self.finish(data)


class WebHandler(_WebHandler):
"""Old WebHandler"""

@classmethod
def _pre_initialize(cls):
# Get tornado URLs
urls = super()._pre_initialize()
# Add a pattern that points to the target method.
# Note that there are handlers with an index method.
# It responds to the request without specifying a method.
# The special characters "*" helps to take into account such a case,
# see https://docs.python.org/3/library/re.html#regular-expression-syntax.
# E.g .: /DIRAC/ -> RootHandler.web_index
cls.PATH_RE = re.compile(f"{cls.BASE_URL}({cls.LOCATION}/[A-z0-9_]*)")
return urls

def get(self, setup, group, *pathArgs):
self.initializeRequest()
return self._getMethod()(*pathArgs)

def post(self, *args, **kwargs):
return self.get(*args, **kwargs)

def threadTask(self, method, *args, **kwargs):
def threadJob(*args, **kwargs):
with self._setupThreadConfig():
return method(*args, **kwargs)

return IOLoop.current().run_in_executor(gThreadPool, functools.partial(threadJob, *args, **kwargs))

def finish(self, data=None, *args, **kwargs):
"""Finishes this response, ending the HTTP request. More details:
https://www.tornadoweb.org/en/stable/_modules/tornado/web.html#RequestHandler.finish
"""
if data and isinstance(data, dict):
self.set_header("Content-Type", "application/json")
data = json.dumps(data, default=defaultEncoder)
return super().finish(data, *args, **kwargs)


class WebSocketHandler(tornado.websocket.WebSocketHandler, _WebHandler):
class WebSocketHandler(tornado.websocket.WebSocketHandler, WebHandler):
def __init__(self, *args, **kwargs):
_WebHandler.__init__(self, *args, **kwargs)
WebHandler.__init__(self, *args, **kwargs)
tornado.websocket.WebSocketHandler.__init__(self, *args, **kwargs)

@classmethod
Expand Down
2 changes: 1 addition & 1 deletion src/WebAppDIRAC/WebApp/handler/AccountingHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from DIRAC.AccountingSystem.Client.ReportsClient import ReportsClient
from DIRAC.Core.Tornado.Client.ClientSelector import TransferClientSelector as TransferClient

from WebAppDIRAC.Lib.WebHandler import _WebHandler as WebHandler, FileResponse
from WebAppDIRAC.Lib.WebHandler import WebHandler, FileResponse


class AccountingHandler(WebHandler):
Expand Down
2 changes: 1 addition & 1 deletion src/WebAppDIRAC/WebApp/handler/ApplicationWizardHandler.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from WebAppDIRAC.Lib.WebHandler import _WebHandler as WebHandler
from WebAppDIRAC.Lib.WebHandler import WebHandler


class ApplicationWizardHandler(WebHandler):
Expand Down
2 changes: 1 addition & 1 deletion src/WebAppDIRAC/WebApp/handler/ComponentHistoryHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import datetime

from DIRAC.FrameworkSystem.Client.ComponentMonitoringClient import ComponentMonitoringClient
from WebAppDIRAC.Lib.WebHandler import _WebHandler as WebHandler, WErr
from WebAppDIRAC.Lib.WebHandler import WebHandler, WErr


class ComponentHistoryHandler(WebHandler):
Expand Down
2 changes: 1 addition & 1 deletion src/WebAppDIRAC/WebApp/handler/DowntimesHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from DIRAC import gLogger
from DIRAC.ResourceStatusSystem.Client.PublisherClient import PublisherClient
from WebAppDIRAC.Lib.WebHandler import _WebHandler as WebHandler, WErr
from WebAppDIRAC.Lib.WebHandler import WebHandler, WErr


class DowntimesHandler(WebHandler):
Expand Down
2 changes: 1 addition & 1 deletion src/WebAppDIRAC/WebApp/handler/ExampleAppHandler.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from WebAppDIRAC.Lib.WebHandler import _WebHandler as WebHandler
from WebAppDIRAC.Lib.WebHandler import WebHandler
import datetime


Expand Down
2 changes: 1 addition & 1 deletion src/WebAppDIRAC/WebApp/handler/FileCatalogHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from DIRAC.DataManagementSystem.Client.DataManager import DataManager
from DIRAC.ConfigurationSystem.Client.Helpers.Registry import getVOForGroup

from WebAppDIRAC.Lib.WebHandler import _WebHandler as WebHandler, FileResponse
from WebAppDIRAC.Lib.WebHandler import WebHandler, FileResponse


class FileCatalogHandler(WebHandler):
Expand Down
2 changes: 1 addition & 1 deletion src/WebAppDIRAC/WebApp/handler/JobLaunchpadHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from DIRAC.FrameworkSystem.Client.ProxyManagerClient import ProxyManagerClient
from DIRAC.WorkloadManagementSystem.Client.WMSClient import WMSClient

from WebAppDIRAC.Lib.WebHandler import _WebHandler as WebHandler
from WebAppDIRAC.Lib.WebHandler import WebHandler


class JobLaunchpadHandler(WebHandler):
Expand Down
2 changes: 1 addition & 1 deletion src/WebAppDIRAC/WebApp/handler/JobMonitorHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from DIRAC.WorkloadManagementSystem.Client.WMSAdministratorClient import WMSAdministratorClient
from DIRAC.WorkloadManagementSystem.Client.SandboxStoreClient import SandboxStoreClient

from WebAppDIRAC.Lib.WebHandler import _WebHandler as WebHandler, WErr
from WebAppDIRAC.Lib.WebHandler import WebHandler, WErr


class JobMonitorHandler(WebHandler):
Expand Down
2 changes: 1 addition & 1 deletion src/WebAppDIRAC/WebApp/handler/JobSummaryHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from DIRAC import gLogger, gConfig
from DIRAC.WorkloadManagementSystem.Client.WMSAdministratorClient import WMSAdministratorClient

from WebAppDIRAC.Lib.WebHandler import _WebHandler as WebHandler
from WebAppDIRAC.Lib.WebHandler import WebHandler

# Dictionary of country code top-level domain (ccTLD) and corresponding country name
COUNTRIES = {
Expand Down
2 changes: 1 addition & 1 deletion src/WebAppDIRAC/WebApp/handler/MonitoringHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from DIRAC.Core.Utilities.Plotting.FileCoding import extractRequestFromFileId, codeRequestInFileId
from DIRAC.MonitoringSystem.Client.MonitoringClient import MonitoringClient

from WebAppDIRAC.Lib.WebHandler import _WebHandler as WebHandler, FileResponse
from WebAppDIRAC.Lib.WebHandler import WebHandler, FileResponse


class MonitoringHandler(WebHandler):
Expand Down
2 changes: 1 addition & 1 deletion src/WebAppDIRAC/WebApp/handler/NotepadHandler.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from WebAppDIRAC.Lib.WebHandler import _WebHandler as WebHandler
from WebAppDIRAC.Lib.WebHandler import WebHandler


class NotepadHandler(WebHandler):
Expand Down
2 changes: 1 addition & 1 deletion src/WebAppDIRAC/WebApp/handler/PilotMonitorHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from DIRAC.Core.Utilities.Graphs.Palette import Palette
from DIRAC.ConfigurationSystem.Client.Helpers.Registry import getUsernameForDN
from DIRAC.WorkloadManagementSystem.Client.PilotManagerClient import PilotManagerClient
from WebAppDIRAC.Lib.WebHandler import _WebHandler as WebHandler
from WebAppDIRAC.Lib.WebHandler import WebHandler


class PilotMonitorHandler(WebHandler):
Expand Down
2 changes: 1 addition & 1 deletion src/WebAppDIRAC/WebApp/handler/PilotSummaryHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from DIRAC import gConfig, gLogger
from DIRAC.WorkloadManagementSystem.Client.PilotManagerClient import PilotManagerClient
from DIRAC.WorkloadManagementSystem.Client.JobMonitoringClient import JobMonitoringClient
from WebAppDIRAC.Lib.WebHandler import _WebHandler as WebHandler
from WebAppDIRAC.Lib.WebHandler import WebHandler


class PilotSummaryHandler(WebHandler):
Expand Down
2 changes: 1 addition & 1 deletion src/WebAppDIRAC/WebApp/handler/ProxyManagerHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from DIRAC.Core.Utilities.List import uniqueElements
from DIRAC.FrameworkSystem.Client.ProxyManagerClient import gProxyManager

from WebAppDIRAC.Lib.WebHandler import _WebHandler as WebHandler, WErr
from WebAppDIRAC.Lib.WebHandler import WebHandler, WErr


class ProxyManagerHandler(WebHandler):
Expand Down
2 changes: 1 addition & 1 deletion src/WebAppDIRAC/WebApp/handler/ProxyUploadHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from DIRAC.FrameworkSystem.Client import ProxyUpload
from DIRAC.ConfigurationSystem.Client.Helpers.Registry import getGroupsForDN

from WebAppDIRAC.Lib.WebHandler import _WebHandler as WebHandler
from WebAppDIRAC.Lib.WebHandler import WebHandler


class ProxyUploadHandler(WebHandler):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from WebAppDIRAC.Lib.WebHandler import _WebHandler as WebHandler
from WebAppDIRAC.Lib.WebHandler import WebHandler


class PublicStateManagerHandler(WebHandler):
Expand Down
2 changes: 1 addition & 1 deletion src/WebAppDIRAC/WebApp/handler/RequestMonitorHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from DIRAC import gLogger
from DIRAC.RequestManagementSystem.Client.ReqClient import ReqClient

from WebAppDIRAC.Lib.WebHandler import _WebHandler as WebHandler
from WebAppDIRAC.Lib.WebHandler import WebHandler


class RequestMonitorHandler(WebHandler):
Expand Down
2 changes: 1 addition & 1 deletion src/WebAppDIRAC/WebApp/handler/ResourceSummaryHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from DIRAC.ResourceStatusSystem.Client.PublisherClient import PublisherClient
from DIRAC.ResourceStatusSystem.PolicySystem.StateMachine import RSSMachine

from WebAppDIRAC.Lib.WebHandler import _WebHandler as WebHandler
from WebAppDIRAC.Lib.WebHandler import WebHandler


class SummaryHandlerMix(WebHandler):
Expand Down
2 changes: 1 addition & 1 deletion src/WebAppDIRAC/WebApp/handler/RootHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from DIRAC.Core.Tornado.Server.private.BaseRequestHandler import TornadoResponse

from WebAppDIRAC.Lib import Conf
from WebAppDIRAC.Lib.WebHandler import _WebHandler as WebHandler, WErr
from WebAppDIRAC.Lib.WebHandler import WebHandler, WErr


class RootHandler(WebHandler):
Expand Down
2 changes: 1 addition & 1 deletion src/WebAppDIRAC/WebApp/handler/SpaceOccupancyHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from DIRAC import gLogger
from DIRAC.ResourceStatusSystem.Client.ResourceManagementClient import ResourceManagementClient
from WebAppDIRAC.Lib.WebHandler import _WebHandler as WebHandler, WErr
from WebAppDIRAC.Lib.WebHandler import WebHandler, WErr


class SpaceOccupancyHandler(WebHandler):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from DIRAC.FrameworkSystem.Client.SystemAdministratorClient import SystemAdministratorClient
from DIRAC.FrameworkSystem.Client.ComponentMonitoringClient import ComponentMonitoringClient

from WebAppDIRAC.Lib.WebHandler import _WebHandler as WebHandler
from WebAppDIRAC.Lib.WebHandler import WebHandler


class SystemAdministrationHandler(WebHandler):
Expand Down
2 changes: 1 addition & 1 deletion src/WebAppDIRAC/WebApp/handler/TokenManagerHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from DIRAC.ConfigurationSystem.Client.Helpers.Registry import getAllUsers
from DIRAC.FrameworkSystem.Client.TokenManagerClient import TokenManagerClient

from WebAppDIRAC.Lib.WebHandler import _WebHandler as WebHandler
from WebAppDIRAC.Lib.WebHandler import WebHandler


class TokenManagerHandler(WebHandler):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from DIRAC.Core.Utilities import TimeUtilities
from DIRAC.TransformationSystem.Client.TransformationClient import TransformationClient

from WebAppDIRAC.Lib.WebHandler import _WebHandler as WebHandler, WErr
from WebAppDIRAC.Lib.WebHandler import WebHandler, WErr


class TransformationMonitorHandler(WebHandler):
Expand Down
2 changes: 1 addition & 1 deletion src/WebAppDIRAC/WebApp/handler/UPHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from DIRAC.FrameworkSystem.Client.UserProfileClient import UserProfileClient
from DIRAC.Core.Tornado.Server.TornadoREST import authorization

from WebAppDIRAC.Lib.WebHandler import _WebHandler as WebHandler, WErr
from WebAppDIRAC.Lib.WebHandler import WebHandler, WErr


class UPHandler(WebHandler):
Expand Down
2 changes: 1 addition & 1 deletion src/WebAppDIRAC/WebApp/handler/VMDiracHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import json

from DIRAC.Core.Utilities import TimeUtilities
from WebAppDIRAC.Lib.WebHandler import _WebHandler as WebHandler
from WebAppDIRAC.Lib.WebHandler import WebHandler
from DIRAC.WorkloadManagementSystem.Client.VMClient import VMClient


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,24 @@
* - We implemented the ExampleAppHandler.py
*
* ExampleHandler.py:
* from WebAppDIRAC.Lib.WebHandler import WebHandler, WErr, asyncGen
* from WebAppDIRAC.Lib.WebHandler import WebHandler, WErr
* from DIRAC import gConfig, S_OK, S_ERROR, gLogger
* import json
* import datetime
*
* class ExampleAppHandler(WebHandler):
*
* AUTH_PROPS = "authenticated"
* DEFAULT_AUTHORIZATION = "authenticated"
*
* @asyncGen
* def web_getJobData(self):
* timestamp = datetime.datetime.utcnow().strftime("%Y-%m-%d %H:%M [UTC]")
* total = 5
* values = []
* values = [{"ExampleId":1,"ExampleValue":"Zoli"},{"ExampleId":2,"ExampleValue":'a'},{"ExampleId":3,"ExampleValue":'aaaa'},{"ExampleId":4,"ExampleValue":'bbbb'},{"ExampleId":5,"ExampleValue":'adsd'}]
* callback = {"success":"true", "result":values, "total":total, "date":timestamp }
* self.finish(callback)
* return {"success":"true", "result":values, "total":total, "date":timestamp }
*
* @asyncGen
* def web_getSelectionData(self):
* callback = {"firstName":["A","C","D"],"lastName":["wwww","dsds","sads"]}
* self.finish(callback)
* return {"firstName":["A","C","D"],"lastName":["wwww","dsds","sads"]}
*
*
* NOTE: You can see the Source by clicking on the page title: DIRAC.ExampleApp.classes.ExampleApp.
Expand Down

0 comments on commit 5c5404b

Please sign in to comment.