From e00eeb44e41bf14ec4ddac1824ee5c349a733888 Mon Sep 17 00:00:00 2001 From: Adrien Coulier Date: Fri, 20 Sep 2024 10:22:19 +0200 Subject: [PATCH 1/3] Convert line endings from CRLF to LF --- .../handlers/arteria_runfolder_handlers.py | 322 +++++++++--------- 1 file changed, 161 insertions(+), 161 deletions(-) diff --git a/arteria/handlers/arteria_runfolder_handlers.py b/arteria/handlers/arteria_runfolder_handlers.py index 66c6258..e3455df 100644 --- a/arteria/handlers/arteria_runfolder_handlers.py +++ b/arteria/handlers/arteria_runfolder_handlers.py @@ -1,161 +1,161 @@ -from aiohttp import web -from pathlib import Path -from arteria import __version__ -import logging -from arteria.models.state import State -from arteria.models.config import Config -from arteria.handlers.base import base_routes -from arteria.models.runfolder_utils import Runfolder, list_runfolders - - -routes = base_routes -log = logging.getLogger(__name__) - - -@routes.post("/runfolders/path/{runfolder:.*}") -async def post_runfolders(request): - """ - When this is called with payload {"state": "STARTED"}, - the state of the runfolder is set to STARTED - """ - - data = await request.post() - runfolder_path = Path(request.match_info['runfolder']) - - if not any([ - runfolder_path.parent == Path(monitored_directory) - for monitored_directory in Config()['monitored_directories'] - ]): - raise web.HTTPBadRequest( - reason=f"{runfolder_path} does not belong to a monitored directory") - - try: - runfolder = Runfolder(runfolder_path) - except AssertionError as exc: - log.exception(exc) - raise web.HTTPNotFound(reason=exc) from exc - - state = data["state"] - try: - runfolder.state = State[state] - except KeyError as exc: - raise web.HTTPBadRequest(reason=f"The state '{state}' is not valid") from exc - - return web.Response(status=200) - - -@routes.get("/runfolders/path/{runfolder:.*}") -async def get_runfolders(request): - """ - Returns some information about the runfolder as json - """ - runfolder_path = Path(request.match_info['runfolder']) - - if not any([ - runfolder_path.parent == Path(monitored_directory) - for monitored_directory in Config()['monitored_directories'] - ]): - raise web.HTTPBadRequest( - reason=f"{runfolder_path} does not belong to a monitored directory") - - try: - runfolder = Runfolder(runfolder_path) - except AssertionError as exc: - log.exception(exc) - raise web.HTTPNotFound(reason=exc) from exc - - return web.json_response( - data=serialize_runfolder_path(runfolder, request), - status=200 - ) - - -@routes.get("/runfolders/next") -async def get_next_runfolder(request): - """ - Finds unprocessed runfolder (state=ready) and then - returns some information about this runfolder. - """ - runfolders = list_runfolders( - Config()['monitored_directories'], - filter_key=lambda r: r.state == State.READY - ) - - if len(runfolders) > 0: - runfolder_dict = serialize_runfolder_path(runfolders[0], request) - - return web.json_response( - data=runfolder_dict, - status=200 - ) - else: - raise web.HTTPNoContent( - reason="No ready runfolder found." - ) - - -@routes.get("/runfolders/pickup") -async def get_pickup_runfolder(request): - """ - Used to start processing runfolders and also sets the runfolder to PENDING state. - """ - runfolders = list_runfolders( - Config()['monitored_directories'], - filter_key=lambda r: r.state == State.READY - ) - - if runfolders: - runfolders[0].state = State.PENDING - runfolder_dict = serialize_runfolder_path(runfolders[0], request) - return web.json_response( - data=runfolder_dict, - status=200 - ) - else: - raise web.HTTPNoContent( - reason="No ready runfolders available." - ) - - -@routes.get("/runfolders") -async def get_all_runfolders(request): - """ - Returns information about all the runfolders that - match the state specified (or all runfolders when state - is not specified) - """ - runfolders = list_runfolders( - Config()['monitored_directories'], - filter_key=lambda r: r.state == State.READY - ) - - runfolders = [ - serialize_runfolder_path(runfolder, request) - for runfolder in runfolders - ] - - return web.json_response( - data={"runfolders": runfolders}, - status=200 - ) - - -def get_host_link(request, runfolder_path, ): - host = request.url.raw_host - link = f"{request.scheme}://{host}/api/1.0/runfolders/path{runfolder_path}" - - return host, link - - -def serialize_runfolder_path(runfolder_cls, request): - """ - Get the path uri as web.json_response gives an error when - self.path is of type Path - """ - runfolder_dict = runfolder_cls.to_dict() - runfolder_dict['service_version'] = __version__ - runfolder_path = Path(runfolder_dict['path']) - runfolder_dict['path'] = runfolder_path.as_uri() - runfolder_dict['host'], runfolder_dict['link'] = get_host_link(request, runfolder_path) - - return runfolder_dict +from aiohttp import web +from pathlib import Path +from arteria import __version__ +import logging +from arteria.models.state import State +from arteria.models.config import Config +from arteria.handlers.base import base_routes +from arteria.models.runfolder_utils import Runfolder, list_runfolders + + +routes = base_routes +log = logging.getLogger(__name__) + + +@routes.post("/runfolders/path/{runfolder:.*}") +async def post_runfolders(request): + """ + When this is called with payload {"state": "STARTED"}, + the state of the runfolder is set to STARTED + """ + + data = await request.post() + runfolder_path = Path(request.match_info['runfolder']) + + if not any([ + runfolder_path.parent == Path(monitored_directory) + for monitored_directory in Config()['monitored_directories'] + ]): + raise web.HTTPBadRequest( + reason=f"{runfolder_path} does not belong to a monitored directory") + + try: + runfolder = Runfolder(runfolder_path) + except AssertionError as exc: + log.exception(exc) + raise web.HTTPNotFound(reason=exc) from exc + + state = data["state"] + try: + runfolder.state = State[state] + except KeyError as exc: + raise web.HTTPBadRequest(reason=f"The state '{state}' is not valid") from exc + + return web.Response(status=200) + + +@routes.get("/runfolders/path/{runfolder:.*}") +async def get_runfolders(request): + """ + Returns some information about the runfolder as json + """ + runfolder_path = Path(request.match_info['runfolder']) + + if not any([ + runfolder_path.parent == Path(monitored_directory) + for monitored_directory in Config()['monitored_directories'] + ]): + raise web.HTTPBadRequest( + reason=f"{runfolder_path} does not belong to a monitored directory") + + try: + runfolder = Runfolder(runfolder_path) + except AssertionError as exc: + log.exception(exc) + raise web.HTTPNotFound(reason=exc) from exc + + return web.json_response( + data=serialize_runfolder_path(runfolder, request), + status=200 + ) + + +@routes.get("/runfolders/next") +async def get_next_runfolder(request): + """ + Finds unprocessed runfolder (state=ready) and then + returns some information about this runfolder. + """ + runfolders = list_runfolders( + Config()['monitored_directories'], + filter_key=lambda r: r.state == State.READY + ) + + if len(runfolders) > 0: + runfolder_dict = serialize_runfolder_path(runfolders[0], request) + + return web.json_response( + data=runfolder_dict, + status=200 + ) + else: + raise web.HTTPNoContent( + reason="No ready runfolder found." + ) + + +@routes.get("/runfolders/pickup") +async def get_pickup_runfolder(request): + """ + Used to start processing runfolders and also sets the runfolder to PENDING state. + """ + runfolders = list_runfolders( + Config()['monitored_directories'], + filter_key=lambda r: r.state == State.READY + ) + + if runfolders: + runfolders[0].state = State.PENDING + runfolder_dict = serialize_runfolder_path(runfolders[0], request) + return web.json_response( + data=runfolder_dict, + status=200 + ) + else: + raise web.HTTPNoContent( + reason="No ready runfolders available." + ) + + +@routes.get("/runfolders") +async def get_all_runfolders(request): + """ + Returns information about all the runfolders that + match the state specified (or all runfolders when state + is not specified) + """ + runfolders = list_runfolders( + Config()['monitored_directories'], + filter_key=lambda r: r.state == State.READY + ) + + runfolders = [ + serialize_runfolder_path(runfolder, request) + for runfolder in runfolders + ] + + return web.json_response( + data={"runfolders": runfolders}, + status=200 + ) + + +def get_host_link(request, runfolder_path, ): + host = request.url.raw_host + link = f"{request.scheme}://{host}/api/1.0/runfolders/path{runfolder_path}" + + return host, link + + +def serialize_runfolder_path(runfolder_cls, request): + """ + Get the path uri as web.json_response gives an error when + self.path is of type Path + """ + runfolder_dict = runfolder_cls.to_dict() + runfolder_dict['service_version'] = __version__ + runfolder_path = Path(runfolder_dict['path']) + runfolder_dict['path'] = runfolder_path.as_uri() + runfolder_dict['host'], runfolder_dict['link'] = get_host_link(request, runfolder_path) + + return runfolder_dict From 331357ddf891055075cce3e08b4665efa7d20daf Mon Sep 17 00:00:00 2001 From: Adrien Coulier Date: Fri, 20 Sep 2024 10:34:02 +0200 Subject: [PATCH 2/3] Check for CRLF line endings in CI --- .github/workflows/ci.yml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index d1948dd..95280f6 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -9,6 +9,12 @@ jobs: steps: - uses: actions/checkout@v4 + - name: Check for CRLF line endings + run: > + ! find . -type f -name "*.py" + | grep -v '\.git/' + | xargs file + | grep CRLF - uses: actions/setup-python@v5 with: python-version: "3.12" From ffae8f6b9f6f044b575658f035b3b22942d60011 Mon Sep 17 00:00:00 2001 From: Adrien Coulier Date: Wed, 25 Sep 2024 12:42:18 +0200 Subject: [PATCH 3/3] Ignore case when looking for files --- .github/workflows/ci.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 95280f6..78276b9 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1,4 +1,5 @@ --- + name: Run Unit Tests on: [push, pull_request] @@ -11,7 +12,7 @@ jobs: - uses: actions/checkout@v4 - name: Check for CRLF line endings run: > - ! find . -type f -name "*.py" + ! find . -type f -iname "*.py" | grep -v '\.git/' | xargs file | grep CRLF