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

Replacing os.path with pathlib.Path #94

Closed
wants to merge 1 commit into from
Closed
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
25 changes: 13 additions & 12 deletions evaluation/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""
import os
from datetime import datetime
from pathlib import Path

from django.test import TestCase
from django.utils import timezone
Expand All @@ -16,7 +17,7 @@
from leaderboard.models import TEXT_FILE
from ocelot.settings import BASE_DIR

TESTDATA_DIR = os.path.join(BASE_DIR, 'leaderboard/testdata')
TESTDATA_DIR = str(Path(BASE_DIR, 'leaderboard/testdata'))


class ComparisonTests(TestCase):
Expand All @@ -40,12 +41,12 @@ def setUp(self):
source_language=Language.objects.get(code='en'),
target_language=Language.objects.get(code='de'),
file_format=SGML_FILE,
src_file=os.path.join(
src_file=str(Path(
TESTDATA_DIR, 'newstest2019-ende-src.en.sgm'
),
ref_file=os.path.join(
)),
ref_file=str(Path(
TESTDATA_DIR, 'newstest2019-ende-ref.de.sgm'
),
)),
competition=self.competition,
)

Expand All @@ -61,7 +62,7 @@ def setUp(self):
test_set=testset,
submitted_by=self.team_a,
file_format=TEXT_FILE,
hyp_file=os.path.join(TESTDATA_DIR, file_1),
hyp_file=str(Path(TESTDATA_DIR, file_1)),
)

self.team_b = Team.objects.create(
Expand All @@ -76,7 +77,7 @@ def setUp(self):
test_set=testset,
submitted_by=self.team_b,
file_format=TEXT_FILE,
hyp_file=os.path.join(TESTDATA_DIR, file_2),
hyp_file=str(Path(TESTDATA_DIR, file_2)),
)

session = self.client.session
Expand Down Expand Up @@ -105,12 +106,12 @@ def test_submissions_from_different_test_sets_cannot_be_compared(self):
source_language=Language.objects.get(code='en'),
target_language=Language.objects.get(code='de'),
file_format=SGML_FILE,
src_file=os.path.join(
src_file=str(Path(
TESTDATA_DIR, 'newstest2019-ende-src.en.sgm'
),
ref_file=os.path.join(
)),
ref_file=str(Path(
TESTDATA_DIR, 'newstest2019-ende-ref.de.sgm'
),
)),
competition=self.competition,
)

Expand All @@ -120,7 +121,7 @@ def test_submissions_from_different_test_sets_cannot_be_compared(self):
test_set=testset,
submitted_by=self.team_a,
file_format=TEXT_FILE,
hyp_file=os.path.join(TESTDATA_DIR, _file),
hyp_file=str(Path(TESTDATA_DIR, _file)),
is_public=True,
)

Expand Down
72 changes: 36 additions & 36 deletions leaderboard/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from leaderboard.utils import process_xml_to_text
from ocelot.settings import BASE_DIR

TESTDATA_DIR = os.path.join(BASE_DIR, 'leaderboard/testdata')
TESTDATA_DIR = Path(BASE_DIR, 'leaderboard/testdata')


class UtilsTests(TestCase):
Expand Down Expand Up @@ -128,12 +128,12 @@ def setUp(self):
source_language=Language.objects.get(code='en'),
target_language=Language.objects.get(code='de'),
file_format=SGML_FILE,
src_file=os.path.join(
src_file=str(Path(
TESTDATA_DIR, 'newstest2019-ende-src.en.sgm'
),
ref_file=os.path.join(
)),
ref_file=str(Path(
TESTDATA_DIR, 'newstest2019-ende-ref.de.sgm'
),
)),
competition=self.competition,
)

Expand All @@ -158,7 +158,7 @@ def _make_submission(self, file_name, file_format=TEXT_FILE):
test_set=self.testset,
submitted_by=self.team,
file_format=file_format,
hyp_file=os.path.join(TESTDATA_DIR, file_name),
hyp_file=str(Path(TESTDATA_DIR, file_name)),
)

def test_scores_are_computed_for_submission_in_text_format(self):
Expand Down Expand Up @@ -238,7 +238,7 @@ def test_successfull_submission(self):

_file = 'newstest2019.msft-WMT19-document-level.6808.en-de.txt'
with open(
os.path.join(TESTDATA_DIR, _file), encoding='utf8'
str(Path(TESTDATA_DIR, _file)), encoding='utf8'
) as tst:
data = {
'test_set': '1',
Expand All @@ -257,7 +257,7 @@ def test_submission_cannot_be_made_by_unverified_team(self):

_file = 'newstest2019.msft-WMT19-document-level.6808.en-de.txt'
with open(
os.path.join(TESTDATA_DIR, _file), encoding='utf8'
str(Path(TESTDATA_DIR, _file)), encoding='utf8'
) as tst:
data = {
'test_set': '1',
Expand Down Expand Up @@ -410,8 +410,8 @@ def setUp(self):
source_language=Language.objects.get(code='en'),
target_language=Language.objects.get(code='ha'),
file_format=XML_FILE,
src_file=os.path.join(TESTDATA_DIR, 'xml/sample-src.xml'),
ref_file=os.path.join(TESTDATA_DIR, 'xml/sample-src-ref.xml'),
src_file=str(Path(TESTDATA_DIR, 'xml/sample-src.xml')),
ref_file=str(Path(TESTDATA_DIR, 'xml/sample-src-ref.xml')),
competition=self.competition,
)

Expand All @@ -421,10 +421,10 @@ def setUp(self):
source_language=Language.objects.get(code='en'),
target_language=Language.objects.get(code='ha'),
file_format=XML_FILE,
src_file=os.path.join(TESTDATA_DIR, 'xml/sample-src.xml'),
ref_file=os.path.join(
src_file=str(Path(TESTDATA_DIR, 'xml/sample-src.xml')),
ref_file=str(Path(
TESTDATA_DIR, 'xml/sample-src-multirefs.xml'
),
)),
competition=self.competition,
)

Expand All @@ -439,8 +439,8 @@ def setUp(self):
source_language=Language.objects.get(code='en'),
target_language=Language.objects.get(code='ha'),
file_format=XML_FILE,
src_file=os.path.join(TESTDATA_DIR, 'xml/multi-src-ref.xml'),
ref_file=os.path.join(TESTDATA_DIR, 'xml/multi-ref.xml'),
src_file=str(Path(TESTDATA_DIR, 'xml/multi-src-ref.xml')),
ref_file=str(Path(TESTDATA_DIR, 'xml/multi-ref.xml')),
competition=self.competition,
collection='B',
)
Expand All @@ -466,7 +466,7 @@ def tearDown(self):
def _clean_text_file(self, input_file, add_test_dir=True):
"""Removes a temporary text file."""
_file = (
os.path.join(TESTDATA_DIR, input_file)
str(Path(TESTDATA_DIR, input_file))
if add_test_dir
else input_file
)
Expand All @@ -484,7 +484,7 @@ def _make_submission(
test_set=test_set or self.testset,
submitted_by=self.team,
file_format=file_format,
hyp_file=os.path.join(TESTDATA_DIR, file_name),
hyp_file=str(Path(TESTDATA_DIR, file_name)),
)

def _set_ocelot_team_token(self):
Expand Down Expand Up @@ -546,7 +546,7 @@ def test_submission_in_xml_format_with_invalid_schema(self):

_file = 'xml/sample-hyp.invalid.xml'
with open(
os.path.join(TESTDATA_DIR, _file), encoding='utf8'
str(Path(TESTDATA_DIR, _file)), encoding='utf8'
) as xml:
data = {
'test_set': '1',
Expand Down Expand Up @@ -618,12 +618,12 @@ def test_create_test_set_with_sgml_files(self):
TestSet.objects.create(
name='TestSetA',
file_format=SGML_FILE,
src_file=os.path.join(
src_file=str(Path(
TESTDATA_DIR, 'newstest2019-ende-src.en.sgm'
),
ref_file=os.path.join(
)),
ref_file=str(Path(
TESTDATA_DIR, 'newstest2019-ende-ref.de.sgm'
),
)),
)

tst = TestSet.objects.get(name='TestSetA')
Expand All @@ -636,12 +636,12 @@ def test_create_test_set_with_text_files(self):
TestSet.objects.create(
name='TestSetB',
file_format=SGML_FILE,
src_file=os.path.join(
src_file=str(Path(
TESTDATA_DIR, 'newstest2019-ende-src.en.txt'
),
ref_file=os.path.join(
)),
ref_file=str(Path(
TESTDATA_DIR, 'newstest2019-ende-ref.de.txt'
),
)),
)

tst = TestSet.objects.get(name='TestSetB')
Expand All @@ -655,8 +655,8 @@ def test_create_test_set_with_xml_files(self):
TestSet.objects.create(
name='TestSetC',
file_format=XML_FILE,
src_file=os.path.join(TESTDATA_DIR, 'xml/sample-src.xml'),
ref_file=os.path.join(TESTDATA_DIR, 'xml/sample-src-ref.xml'),
src_file=str(Path(TESTDATA_DIR, 'xml/sample-src.xml')),
ref_file=str(Path(TESTDATA_DIR, 'xml/sample-src-ref.xml')),
)

tst = TestSet.objects.get(name='TestSetC')
Expand All @@ -682,8 +682,8 @@ def test_create_test_set_with_collections(self):
TestSet.objects.create(
name='TestSetD',
file_format=XML_FILE,
src_file=os.path.join(TESTDATA_DIR, 'xml/multi-src-ref.xml'),
ref_file=os.path.join(TESTDATA_DIR, 'xml/multi-src-ref.xml'),
src_file=str(Path(TESTDATA_DIR, 'xml/multi-src-ref.xml')),
ref_file=str(Path(TESTDATA_DIR, 'xml/multi-src-ref.xml')),
collection='B',
)

Expand Down Expand Up @@ -748,12 +748,12 @@ def setUp(self):
source_language=l_en,
target_language=l_de,
file_format=SGML_FILE,
src_file=os.path.join(
src_file=str(Path(
TESTDATA_DIR, 'newstest2019-ende-src.en.sgm'
),
ref_file=os.path.join(
)),
ref_file=str(Path(
TESTDATA_DIR, 'newstest2019-ende-ref.de.sgm'
),
)),
competition=comp_a,
)

Expand All @@ -770,7 +770,7 @@ def setUp(self):
test_set=test_a,
submitted_by=team_a,
file_format=TEXT_FILE,
hyp_file=os.path.join(TESTDATA_DIR, _file1),
hyp_file=str(Path(TESTDATA_DIR, _file1)),
)

team_b = Team.objects.create(
Expand All @@ -786,7 +786,7 @@ def setUp(self):
test_set=test_a,
submitted_by=team_b,
file_format=TEXT_FILE,
hyp_file=os.path.join(TESTDATA_DIR, _file2),
hyp_file=str(Path(TESTDATA_DIR, _file2)),
)

Competition.objects.create(
Expand Down
13 changes: 7 additions & 6 deletions ocelot/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@
"""
import logging
import os
import pathlib
from logging.handlers import ( # pylint: disable=ungrouped-imports
RotatingFileHandler,
)


# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
BASE_DIR = str(pathlib.Path(__file__).parent.parent.resolve())

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/2.2/howto/deployment/checklist/
Expand Down Expand Up @@ -69,15 +70,15 @@
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
'NAME': str(pathlib.Path(BASE_DIR, 'db.sqlite3')),
}
}

FILE_UPLOAD_PERMISSIONS = 0o644

# Logging settings for this Django project.
LOG_LEVEL = logging.DEBUG
LOG_FILENAME = os.path.join(BASE_DIR, 'ocelot.log')
LOG_FILENAME = str(pathlib.Path(BASE_DIR, 'ocelot.log'))
LOG_FORMAT = "[%(asctime)s] %(name)s::%(levelname)s %(message)s"
LOG_DATE = "%m/%d/%Y @ %H:%M:%S"
LOG_FORMATTER = logging.Formatter(LOG_FORMAT, LOG_DATE)
Expand Down Expand Up @@ -172,11 +173,11 @@
# https://docs.djangoproject.com/en/2.2/howto/static-files/

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_ROOT = str(pathlib.Path(BASE_DIR, 'static'))

# Static files that are not tied to a particular app should be put there
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'assets'),
str(pathlib.Path(BASE_DIR, 'assets')),
]

# Allow to specify absolute filesystem path to the directory that will hold user-uploaded files.
Expand All @@ -185,5 +186,5 @@
# Project version
# See point 4 from https://packaging.python.org/guides/single-sourcing-package-version/

with open(os.path.join(BASE_DIR, 'VERSION')) as version_file:
with open(str(pathlib.Path(BASE_DIR, 'VERSION'))) as version_file:
VERSION = version_file.read().strip()