Skip to content

Commit

Permalink
cleanup python
Browse files Browse the repository at this point in the history
  • Loading branch information
jarno-knaw committed Jul 23, 2024
1 parent 9a6bebe commit 77bb7cd
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 16 deletions.
1 change: 1 addition & 0 deletions .github/workflows/pylint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ jobs:
run: |
python -m pip install --upgrade pip
pip install pylint
pip install -r app/requirements.txt
- name: Analysing the code with pylint
run: |
pylint $(git ls-files '*.py')
19 changes: 12 additions & 7 deletions src/import/article.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
"""
article.py
Contains Article class used for importing Usenet News articles.
"""

import email
import json
from dateutil import parser
import email


class Article(object):
class Article:
"""
Represents a single article.
"""

def __init__(self, id, folder, path, from_raw, newsgroups, subject, message_id, date, x_gateway, lines, xref,
def __init__(self, articleId, folder, path, from_raw, newsgroups, subject, message_id, date, x_gateway, lines, xref,
body, references):
self.from_name = None
self.from_email = None
self.date = None
self.id = id
self.articleId = articleId
self.location = folder
self.path = path
self.newsgroups = newsgroups.split(',')
Expand Down Expand Up @@ -73,7 +78,7 @@ def to_dict(self):
:return:
"""
return {
'id': self.id,
'id': self.articleId,
'path': self.path,
'folder': self.location,
'from_name': self.from_name,
Expand Down Expand Up @@ -120,9 +125,9 @@ def from_file(path):

pathstr = str(path)
path_parts = pathstr.split('/')
id = '-'.join(path_parts[-3:])
article_id = '-'.join(path_parts[-3:])
return Article(
id,
article_id,
'/'.join(path_parts[-3:-1]),
msg['Path'],
msg['From'],
Expand Down
2 changes: 0 additions & 2 deletions src/import/import.py

This file was deleted.

10 changes: 8 additions & 2 deletions src/import/test_import.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
#!/usr/bin/env python3

"""
This is a test import script which imports data from zipped
Usenet news files.
"""

from article import Article
import os
import sys
from pathlib import Path
from elasticsearch import Elasticsearch, helpers
import tempfile
Expand All @@ -20,12 +26,12 @@ def create_mapping(name):
if client.indices.exists(name):
client.indices.delete(index=name)

with open("../service/fields.yaml") as stream:
with open("../service/fields.yaml", encoding="utf-8") as stream:
try:
data = yaml.safe_load(stream)
except yaml.YAMLError as e:
print(e)
exit(1)
sys.exit(1)

properties = {}

Expand Down
34 changes: 29 additions & 5 deletions src/service/app.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from flask import Flask, request, jsonify
from flask_cors import CORS
import os
from elastic_index import Index
from flask import Flask, request, jsonify
from flask_cors import CORS
import yaml

app = Flask(__name__, static_folder="browser", static_url_path="")
Expand All @@ -28,16 +28,28 @@ def after_request(response):
@app.route("/", methods=["GET", "POST"])
@app.route("/search")
def catch_all():
"""
Return the front-end, pages are handled by React
:return:
"""
return app.send_static_file("index.html")


@app.route("/api/detail/<id>")
@app.route("/detail/<id>")
def detail(id):
"""
Return the front-end, pages are handled by React
:return:
"""
return app.send_static_file("index.html")


@app.route("/api/facets", methods=["GET"])
def get_facets():
"""
Get all used facets, and their configuration.
:return:
"""
try:
data = index.get_facets()
except yaml.YAMLError as e:
Expand All @@ -49,19 +61,31 @@ def get_facets():

@app.route("/api/facet", methods=["POST", "GET"])
def get_facet():
"""
Get facet information.
:return:
"""
struc = request.get_json()
ret_struc = index.get_facet(struc["name"], struc["amount"], struc["filter"], struc["searchvalues"])
return jsonify(ret_struc)


@app.route("/api/browse", methods=["POST", "GET"])
def browse():
"""
Search for articles using elasticsearch.
:return:
"""
struc = request.get_json()
ret_struc = index.browse(struc["page"], struc["page_length"], struc["searchvalues"])
return jsonify(ret_struc)


@app.route("/api/article", methods=["GET"])
def get_article():
id = request.args.get("rec")
return jsonify(index.by_id(id))
"""
Get details of a single article.
:return:
"""
article_id = request.args.get("rec")
return jsonify(index.by_id(article_id))

0 comments on commit 77bb7cd

Please sign in to comment.