-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fonctionnalité : ajoute la date aux résultats de la recherche de cont…
…enus (#34)
- Loading branch information
Showing
4 changed files
with
198 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
#! python3 # noqa: E265 | ||
|
||
""" | ||
Extract date from content path, location or name. | ||
Author: Julien Moura (https://github.com/guts) | ||
""" | ||
|
||
# ############################################################################# | ||
# ########## Libraries ############# | ||
# ################################## | ||
|
||
# Standard library | ||
import logging | ||
from datetime import date, datetime | ||
from functools import lru_cache | ||
|
||
# ############################################################################# | ||
# ########## Globals ############### | ||
# ################################## | ||
|
||
# logs | ||
logger = logging.getLogger(__name__) | ||
|
||
|
||
# ############################################################################# | ||
# ########## Functions ############# | ||
# ################################## | ||
|
||
|
||
@lru_cache(maxsize=512) | ||
def get_date_from_content_location(input_content_location: str) -> date: | ||
"""Extract date from content location string. | ||
Args: | ||
input_content_location (str): content location path. | ||
Returns: | ||
date: date object | ||
Example: | ||
.. code-block:: python | ||
> sample_content_location = ( | ||
"articles/2008/2008-08-22_1-introduction-a-l-api-google-maps/" | ||
) | ||
> sample_content_date = get_date_from_content_location(sample_content_location) | ||
> print(type(sample_content_date), sample_content_date) | ||
<class 'datetime.date'> 2008-08-22 | ||
""" | ||
# checks | ||
if not isinstance(input_content_location, str) or "/" not in input_content_location: | ||
logger.error( | ||
ValueError(f"Input location seems to be invalid: {input_content_location}.") | ||
) | ||
return None | ||
|
||
try: | ||
# get the year | ||
parts = input_content_location.split("/") | ||
year = [p for p in parts if p.isdigit()][0] | ||
except Exception as err: | ||
logger.error( | ||
f"Unable to extract year from content location: {input_content_location}" | ||
) | ||
return None | ||
|
||
# get next part | ||
next_part = parts[parts.index(year) + 1] | ||
|
||
# clean next part for rdp | ||
if next_part.startswith("rdp_"): | ||
next_part = next_part[4:] | ||
|
||
# now, the next part should contain the date within the first 10 chars | ||
date_str = next_part[:10] | ||
|
||
try: | ||
return datetime.strptime(date_str, "%Y-%m-%d").date() | ||
except Exception as err: | ||
logger.error(err) | ||
return None | ||
|
||
|
||
# ############################################################################# | ||
# ##### Stand alone program ######## | ||
# ################################## | ||
|
||
if __name__ == "__main__": | ||
"""Standalone execution.""" | ||
# good | ||
sample_content_location = ( | ||
"articles/2008/2008-08-22_1-introduction-a-l-api-google-maps/" | ||
) | ||
sample_content_date = get_date_from_content_location(sample_content_location) | ||
print(type(sample_content_date), sample_content_date) | ||
assert isinstance(sample_content_date, date) | ||
|
||
# good with content folder prefix and md suffix | ||
sample_content_location = ( | ||
"/content/articles/2008/2008-08-22_1-introduction-a-l-api-google-maps.md" | ||
) | ||
sample_content_date = get_date_from_content_location(sample_content_location) | ||
print(type(sample_content_date), sample_content_date) | ||
assert isinstance(sample_content_date, date) | ||
|
||
# good with content folder prefix and md suffix | ||
sample_content_location = "content/rdp/2023/rdp_2023-01-06.md" | ||
sample_content_date = get_date_from_content_location(sample_content_location) | ||
print(type(sample_content_date), sample_content_date) | ||
assert isinstance(sample_content_date, date) | ||
|
||
# bad | ||
sample_content_location = "2008-08-22_1-introduction-a-l-api-google-maps" | ||
sample_content_date = get_date_from_content_location(sample_content_location) | ||
print(type(sample_content_date), sample_content_date) | ||
assert sample_content_date is None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
#! python3 # noqa E265 | ||
|
||
""" | ||
Usage from the repo root folder: | ||
.. code-block:: bash | ||
# for whole tests | ||
python -m unittest tests.test_utils_date_from_content | ||
# for specific test | ||
python -m unittest tests.test_utils_date_from_content.TestUtilsDateFromContent.test_date_from_content_location | ||
""" | ||
|
||
# standard library | ||
import unittest | ||
from datetime import date | ||
|
||
# project | ||
from geotribu_cli.utils.date_from_content import get_date_from_content_location | ||
|
||
# ############################################################################ | ||
# ########## Classes ############# | ||
# ################################ | ||
|
||
|
||
class TestUtilsDateFromContent(unittest.TestCase): | ||
"""Test package utilities.""" | ||
|
||
def test_date_from_content_location(self): | ||
"""Test minimalist slugify function.""" | ||
# good | ||
sample_content_location = ( | ||
"articles/2008/2008-08-22_1-introduction-a-l-api-google-maps/" | ||
) | ||
sample_content_date = get_date_from_content_location(sample_content_location) | ||
print(type(sample_content_date), sample_content_date) | ||
self.assertIsInstance(sample_content_date, date) | ||
|
||
# good with content folder prefix and md suffix | ||
sample_content_location = ( | ||
"/content/articles/2008/2008-08-22_1-introduction-a-l-api-google-maps.md" | ||
) | ||
sample_content_date = get_date_from_content_location(sample_content_location) | ||
print(type(sample_content_date), sample_content_date) | ||
self.assertIsInstance(sample_content_date, date) | ||
|
||
# good with content folder and rdp_ prefixes | ||
sample_content_location = "content/rdp/2023/rdp_2023-01-06" | ||
sample_content_date = get_date_from_content_location(sample_content_location) | ||
print(type(sample_content_date), sample_content_date) | ||
assert isinstance(sample_content_date, date) | ||
|
||
# bad | ||
sample_content_location = "2008-08-22_1-introduction-a-l-api-google-maps" | ||
sample_content_date = get_date_from_content_location(sample_content_location) | ||
print(type(sample_content_date), sample_content_date) | ||
self.assertIsNone(sample_content_date) | ||
|
||
|
||
# ############################################################################ | ||
# ####### Stand-alone run ######## | ||
# ################################ | ||
if __name__ == "__main__": | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters