Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backend code quality #3

Merged
merged 4 commits into from
Oct 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions .github/workflows/backend.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: Backend

on: [push, pull_request]

jobs:
backend:
name: Backend test, lint and format
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Python 3.11
uses: actions/setup-python@v3
with:
python-version: 3.11
- name: Install dependencies
run: |
cd backend
python -m pip install --upgrade pip
pip install -r requirements.txt
- name: End-to-end test
run: |
set -x
cd backend
CORS_ORIGINS='' MEDIA=.. flask --app src/app.py run &
while [[ -z "${DIR_LIST:-}" ]]; do DIR_LIST=$(curl 'http://127.0.0.1:5000/?dir=' || true); done

echo "$DIR_LIST" | grep '{"name":".gitignore","type":"file","url":"/.gitignore"}'
echo "$DIR_LIST" | grep '{"name":"backend","type":"directory","url":"/backend"}'
- name: Lint
run: |
cd backend
pylint $(git ls-files '*.py')
- name: Format
run: |
cd backend
black . --check
2 changes: 1 addition & 1 deletion .github/workflows/frontend.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ permissions:
pull-requests: read

jobs:
test-lint-and-format:
frontend:
name: Frontend test, lint and format
runs-on: ubuntu-latest

Expand Down
14 changes: 14 additions & 0 deletions backend/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
astroid==3.0.0
black==23.9.1
click==8.1.3
dill==0.3.7
Flask==2.2.2
Flask-Cors==3.0.10
gunicorn==20.1.0
iniconfig==2.0.0
isort==5.12.0
itsdangerous==2.1.2
Jinja2==3.1.2
MarkupSafe==2.1.1
mccabe==0.7.0
mypy-extensions==1.0.0
packaging==23.2
pathspec==0.11.2
platformdirs==3.11.0
pluggy==1.3.0
pylint==3.0.1
pytest==7.4.2
six==1.16.0
tomlkit==0.12.1
Werkzeug==2.2.2
56 changes: 36 additions & 20 deletions backend/src/app.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# pylint: disable=missing-module-docstring
import sys
from os import environ, listdir, path
from urllib.parse import quote, unquote
Expand All @@ -7,37 +8,52 @@

app = Flask(__name__)

CORS(app, origins=environ['CORS_ORIGINS'].split())
CORS(app, origins=environ["CORS_ORIGINS"].split())

mediaRoot = environ['MEDIA']
MEDIA_ROOT = environ["MEDIA"]

@app.route('/', methods=['GET'])
def get_films():
dir = unquote(request.args.get('dir'))
dirPath = path.join(mediaRoot, dir)
return jsonify(filmTree(dirPath))

def filmTree(dir):
ls = sorted(listdir(dir))
@app.route("/", methods=["GET"])
def get_files():
"""
Returns the contents of the requested directory in JSON form.
"""
dir_name = unquote(request.args.get("dir"))
dir_path = path.join(MEDIA_ROOT, dir_name)
return jsonify(contents(dir_path))


def contents(directory):
"""
Returns the contents of the requested directory as an array of dictionaries.
"""
sorted_list = sorted(listdir(directory))
result = []
for item in ls:
fullPath = path.join(dir, item)
url = quote(fullPath[len(mediaRoot):])
result.append({'name': item, 'type': 'directory' if path.isdir(fullPath) else 'file', 'url': url})

for item in sorted_list:
full_path = path.join(directory, item)
url = quote(full_path[len(MEDIA_ROOT) :])
result.append(
{
"name": item,
"type": "directory" if path.isdir(full_path) else "file",
"url": url,
}
)

return result


if __name__ == '__main__':
if __name__ == "__main__":
cli_args = sys.argv[1:]

# If the IP and port are supplied as arguments, use them.
if cli_args:
host = cli_args[0]
HOST = cli_args[0]
if len(cli_args) > 1:
port = cli_args[1]
PORT = cli_args[1]
else:
port = 8080
PORT = 8080
else:
host = "127.0.0.1"
HOST = "127.0.0.1"

app.run(host=host, port=port, debug=True)
app.run(host=HOST, port=PORT, debug=True)
Loading