Skip to content

Commit ca0efa1

Browse files
committed
Changes for Brian C's pr review
1 parent d5b7932 commit ca0efa1

File tree

10 files changed

+50
-17
lines changed

10 files changed

+50
-17
lines changed

arxiv/base/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,10 @@ def register_blueprint(self: Flask, blueprint: Blueprint,
126126
filters.register_filters(app)
127127
context_processors.register_context_processors(app)
128128

129+
130+
# This piece of code is crucial to making sure sqlalchemy sessions work in flask
131+
# It is the same as the flask_sqlalchemy implementation
132+
# See: https://github.com/pallets-eco/flask-sqlalchemy/blob/42a36a3cb604fd39d81d00b54ab3988bbd0ad184/src/flask_sqlalchemy/session.py#L109
129133
@app.teardown_appcontext
130134
def remove_scoped_session (response_or_exc):
131135
session.remove()

arxiv/config/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class Settings(BaseSettings):
2424
curl http://127.0.0.1:5000/ -sv -H "Host: subdomain.arxiv.org"
2525
"""
2626

27-
SECRET_KEY: str = "qwert2345"
27+
SECRET_KEY: SecretStr = SecretStr("qwert2345")
2828

2929
ARXIV_ACCESSIBILITY_URL: str = "mailto:[email protected]"
3030

@@ -137,6 +137,8 @@ class Settings(BaseSettings):
137137
Timezone of the arxiv business offices.
138138
"""
139139

140+
FS_TZ: str = "US/Eastern"
141+
140142
BASE_VERSION: str = importlib.metadata.version('arxiv-base')
141143
"""The version of the arxiv-base package."""
142144

arxiv/db/__init__.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,30 @@
11
"""
22
This is the primary SQLAlchemy implementation of the main arXiv database
3+
4+
To use this in a simple non-flask non-fastapi script do:
5+
6+
from arxiv.db import get_db
7+
8+
with get_db() as session:
9+
session.execute(
10+
select(...)
11+
)
12+
13+
To use this in flask just do:
14+
15+
from arxiv.db import session
16+
17+
session.execute(
18+
select(...)
19+
)
20+
21+
To use this with fastapi do:
22+
23+
with get_db() as session:
24+
session.execute(
25+
select(...)
26+
)
27+
328
"""
429
from typing import Generator
530
import logging

arxiv/db/types.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
"""
2+
This file contains common database types. We have lots of integer
3+
primary keys in our db, so we can include a shorthand like intpk
4+
to make annotating the column type in .models easier
5+
"""
6+
17
from typing import Annotated
28
from sqlalchemy.orm import mapped_column
39

arxiv/document/parse_abs.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from zoneinfo import ZoneInfo
99
from dateutil import parser
1010

11-
from flask import current_app
11+
from ..config import settings
1212

1313
from ..taxonomy import definitions
1414
from .metadata import Archive, AuthorList, Category, \
@@ -75,10 +75,7 @@ def parse_abs_file(filename: str) -> DocMetadata:
7575
try:
7676
with absfile.open(mode='r', encoding='latin-1') as absf:
7777
raw = absf.read()
78-
if current_app:
79-
modified = datetime.fromtimestamp(absfile.stat().st_mtime, tz=_get_tz())
80-
else:
81-
modified = datetime.fromtimestamp(absfile.stat().st_mtime)
78+
modified = datetime.fromtimestamp(absfile.stat().st_mtime, tz=_get_tz())
8279
modified = modified.astimezone(ZoneInfo("UTC"))
8380
return parse_abs(raw, modified)
8481

@@ -301,9 +298,9 @@ def alt_component_split(components: List[str]) -> List[str]:
301298

302299

303300
def _get_tz() -> ZoneInfo:
304-
"""Gets the timezone from the flask current_app."""
301+
"""Gets the timezone from the environment"""
305302
global _fs_tz
306303
if _fs_tz is None:
307-
_fs_tz = ZoneInfo(current_app.config["FS_TZ"])
304+
_fs_tz = ZoneInfo(settings.FS_TZ)
308305

309306
return _fs_tz

arxiv/ops/fastly_log_ingest/app.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ def __init__(self, message: Message, arrival_time: float):
6363
self.arrival_time = arrival_time
6464
try:
6565
self.data: dict = json.loads(message.data.decode('utf-8').strip())
66-
except:
67-
logger.warning(f'Failed to log the following message: {message.data.decode("utf-8")}')
66+
except Exception as e:
67+
logger.warning(f'Failed to log the following message: {message.data.decode("utf-8", errors='replace')} with {str(e)}')
6868

6969
def to_payload(self) -> dict:
7070
"""Convert the message into a `payload` for the log entry."""

arxiv/urls/__init__.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
from ..identifier import Identifier
2+
from ..config import settings
23

34
def pdf_canonical_url (identifier: Identifier) -> str:
45
# This won't include the version unless identifier
56
# was explicitly initialized with a version
6-
return f'https://arxiv.org/pdf/{identifier.idv}'
7+
return f'https://{settings.CANONICAL_SERVER}/pdf/{identifier.idv}'
78

89
def html_canonical_url (identifier: Identifier) -> str:
9-
return f'https://arxiv.org/html/{identifier.idv}'
10+
return f'https://{settings.CANONICAL_SERVER}/html/{identifier.idv}'
1011

1112
def abs_canonical_url (identifier: Identifier) -> str:
12-
return f'https://arxiv.org/abs/{identifier.idv}'
13+
return f'https://{settings.CANONICAL_SERVER}/abs/{identifier.idv}'

arxiv/util/tests/test_authors.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""Tests for author and affiliation parsing."""
22
from unittest import TestCase
33

4-
from arxiv.authors import parse_author_affil, split_authors
4+
from ...authors import parse_author_affil, split_authors
55

66

77
class TestAuthorAffiliationParsing(TestCase):

mypy.ini

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
[mypy]
2-
# mypy_path = $MYPYPATH:./sqlalchemy-stubs
3-
42
#
53
# Covered by --strict, with some turned off:
64
#

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ include = [
1616
[tool.poetry.dependencies]
1717
python = "^3.11"
1818
pydantic = "==1.10.*"
19-
flask = "3.0.2"
19+
flask = "3.0.*"
2020
pytz = "*"
2121
boto3 = "==1.*"
2222
wtforms = "*"

0 commit comments

Comments
 (0)