Skip to content
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
22 changes: 22 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
hooks:
- id: end-of-file-fixer
exclude: "business-facing/layer"
- id: trailing-whitespace
exclude: "business-facing/layer"
- id: check-yaml
exclude: "business-facing/layer"
- id: check-json
exclude: "business-facing/layer"

- repo: https://github.com/astral-sh/ruff-pre-commit
# Ruff version.
rev: v0.4.4
hooks:
# Run the linter, and enable lint fixes
- id: ruff
args: [ --fix ]
# Run the formatter.
- id: ruff-format
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -375,3 +375,20 @@ Example usage:


Since the `EDTFField` and the `_earliest` and `_latest` field values are set automatically, you may want to make them readonly, or not visible in your model admin.

## To develop
### Setup
- Clone the repository: `git clone https://github.com/ixc/python-edtf.git`
- Set up a virtual environment: `python3 -m venv venv`
- Install the dependencies: `pip install -r dev-requirements.txt`
- Install precommit hooks: `pre-commit install`

### Running tests
- From `python-edtf`, run the unit tests: `pytest`
- From `python-edtf/edtf_django_tests`, run the integration tests: `python manage.py test edtf_integration`

### Linting and formatting
- Check linting: `ruff check --output-format=github --config pyproject.toml`
- Check formatting: `ruff format --check --config pyproject.toml`
- Fix formatting: `ruff format --config pyproject.toml`
- Linting and formatting checks and attempted fixes are also run as precommit hooks if you installed them.
5 changes: 5 additions & 0 deletions dev-requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-r requirements.txt # Include all main requirements
django>=4.2,<5.0
pytest
ruff
pre-commit
69 changes: 65 additions & 4 deletions edtf/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,31 @@
from edtf.convert import (
from edtf.natlang import text_to_edtf
from edtf.parser import (
UA,
Consecutives,
Date,
DateAndTime,
EarlierConsecutives,
EDTFObject,
EDTFParseException,
ExponentialYear,
Interval,
LaterConsecutives,
Level1Interval,
Level2Interval,
Level2Season,
LongYear,
MultipleDates,
OneOfASet,
PartialUncertainOrApproximate,
PartialUnspecified,
Season,
UncertainOrApproximate,
Unspecified,
UnspecifiedIntervalSection,
parse_edtf,
)

from .convert import (
dt_to_struct_time,
jd_to_struct_time,
old_specs_to_new_specs_expression,
Expand All @@ -7,6 +34,40 @@
struct_time_to_jd,
trim_struct_time,
)
from edtf.natlang import text_to_edtf
from edtf.parser.grammar import parse_edtf
from edtf.parser.parser_classes import *

# public
__all__ = [
"dt_to_struct_time",
"jd_to_struct_time",
"old_specs_to_new_specs_expression",
"struct_time_to_date",
"struct_time_to_datetime",
"struct_time_to_jd",
"trim_struct_time",
"text_to_edtf",
"parse_edtf",
# parser_exceptions
"EDTFParseException",
# parser_classes
"EDTFObject",
"Date",
"DateAndTime",
"Interval",
"UA",
"UncertainOrApproximate",
"UnspecifiedIntervalSection",
"Unspecified",
"Level1Interval",
"LongYear",
"Season",
"PartialUncertainOrApproximate",
"PartialUnspecified",
"Consecutives",
"EarlierConsecutives",
"LaterConsecutives",
"OneOfASet",
"MultipleDates",
"Level2Interval",
"Level2Season",
"ExponentialYear",
]
2 changes: 1 addition & 1 deletion edtf/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def from_db_value(self, value, expression, connection):

try:
# Try to unpickle if the value was pickled
return pickle.loads(value)
return pickle.loads(value) # noqa S301
except (pickle.PickleError, TypeError):
# If it fails because it's not pickled data, try parsing as EDTF
return parse_edtf(value, fail_silently=True)
Expand Down
2 changes: 2 additions & 0 deletions edtf/natlang/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
from .en import text_to_edtf

__all__ = ["text_to_edtf"]
53 changes: 51 additions & 2 deletions edtf/parser/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,51 @@
from edtf.parser.grammar import parse_edtf
from edtf.parser.parser_classes import *
from .edtf_exceptions import EDTFParseException
from .grammar import parse_edtf
from .parser_classes import (
UA,
Consecutives,
Date,
DateAndTime,
EarlierConsecutives,
EDTFObject,
ExponentialYear,
Interval,
LaterConsecutives,
Level1Interval,
Level2Interval,
Level2Season,
LongYear,
MultipleDates,
OneOfASet,
PartialUncertainOrApproximate,
PartialUnspecified,
Season,
UncertainOrApproximate,
Unspecified,
UnspecifiedIntervalSection,
)

__all__ = [
"parse_edtf",
"EDTFParseException",
"EDTFObject",
"Date",
"DateAndTime",
"Interval",
"UA",
"UncertainOrApproximate",
"Unspecified",
"UnspecifiedIntervalSection",
"Level1Interval",
"LongYear",
"Season",
"PartialUncertainOrApproximate",
"PartialUnspecified",
"Consecutives",
"EarlierConsecutives",
"LaterConsecutives",
"OneOfASet",
"MultipleDates",
"Level2Interval",
"Level2Season",
"ExponentialYear",
]
5 changes: 2 additions & 3 deletions edtf_django_tests/edtf_integration/tests.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from django.test import TestCase

from edtf.convert import struct_time_to_jd
from edtf.parser import EDTFObject
from edtf.parser.grammar import parse_edtf as parse
from edtf import EDTFObject, struct_time_to_jd
from edtf import parse_edtf as parse

from .models import TestEvent

Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ classifiers = [
test = [
"django>=4.2,<5.0",
"pytest",
"ruff"
"ruff",
"pre-commit",
]

[project.urls]
Expand Down Expand Up @@ -117,4 +118,3 @@ ignore = [
# Ignore McCabe complexity (for now).
"C901",
]