Skip to content

Commit

Permalink
this was awful
Browse files Browse the repository at this point in the history
  • Loading branch information
wren committed May 2, 2021
1 parent 7665def commit e77a548
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 68 deletions.
13 changes: 10 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,16 @@ lint: ## Check style with various tools
poetry run pyflakes jrnl tests
poetry run black --check --diff .

test: lint ## Run unit tests and behave tests
poetry run pytest
poetry run behave --no-skipped --format progress2
unit: # unit tests
poetry run pytest tests/*.py

e2e: # end-to-end tests
pytest tests/step_defs/test_*.py --gherkin-terminal-reporter --tb=native --diff-type=unified

e2e-debug: # end-to-end tests
pytest tests/step_defs/test_*.py --gherkin-terminal-reporter --tb=native --diff-type=unified -x -vv

test: lint unit e2e ## Run unit tests and behave tests

build:
poetry build
Expand Down
4 changes: 2 additions & 2 deletions jrnl/DayOneJournal.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env python

from datetime import datetime
import datetime
import fnmatch
import os
from pathlib import Path
Expand Down Expand Up @@ -118,7 +118,7 @@ def write(self):
"""Writes only the entries that have been modified into plist files."""
for entry in self.entries:
if entry.modified:
utc_time = datetime.utcfromtimestamp(
utc_time = datetime.datetime.utcfromtimestamp(
time.mktime(entry.date.timetuple())
)

Expand Down
4 changes: 2 additions & 2 deletions jrnl/Entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# License: https://www.gnu.org/licenses/gpl-3.0.html


from datetime import datetime
import datetime
import re

import ansiwrap
Expand All @@ -15,7 +15,7 @@
class Entry:
def __init__(self, journal, date=None, text="", starred=False):
self.journal = journal # Reference to journal mainly to access its config
self.date = date or datetime.now()
self.date = date or datetime.datetime.now()
self.text = text
self._title = None
self._body = None
Expand Down
8 changes: 4 additions & 4 deletions jrnl/Journal.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# License: https://www.gnu.org/licenses/gpl-3.0.html


from datetime import datetime
import datetime
import logging
import os
import re
Expand Down Expand Up @@ -135,7 +135,7 @@ def _parse(self, journal_txt):
for match in date_blob_re.finditer(journal_txt):
date_blob = match.groups()[0]
try:
new_date = datetime.strptime(date_blob, self.config["timeformat"])
new_date = datetime.datetime.strptime(date_blob, self.config["timeformat"])
except ValueError:
# Passing in a date that had brackets around it
new_date = time.parse(date_blob, bracketed=True)
Expand Down Expand Up @@ -348,7 +348,7 @@ def _parse(self, journal_txt):
"""Parses a journal that's stored in a string and returns a list of entries"""
# Entries start with a line that looks like 'date title' - let's figure out how
# long the date will be by constructing one
date_length = len(datetime.today().strftime(self.config["timeformat"]))
date_length = len(datetime.datetime.today().strftime(self.config["timeformat"]))

# Initialise our current entry
entries = []
Expand All @@ -358,7 +358,7 @@ def _parse(self, journal_txt):
line = line.rstrip()
try:
# try to parse line as date => new entry begins
new_date = datetime.strptime(
new_date = datetime.datetime.strptime(
line[:date_length], self.config["timeformat"]
)

Expand Down
20 changes: 10 additions & 10 deletions jrnl/time.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# Copyright (C) 2012-2021 jrnl contributors
# License: https://www.gnu.org/licenses/gpl-3.0.html

from datetime import datetime
import datetime

FAKE_YEAR = 9999
DEFAULT_FUTURE = datetime(FAKE_YEAR, 12, 31, 23, 59, 59)
DEFAULT_PAST = datetime(FAKE_YEAR, 1, 1, 0, 0)
DEFAULT_FUTURE = datetime.datetime(FAKE_YEAR, 12, 31, 23, 59, 59)
DEFAULT_PAST = datetime.datetime(FAKE_YEAR, 1, 1, 0, 0)


def __get_pdt_calendar():
Expand All @@ -27,7 +27,7 @@ def parse(
"""Parses a string containing a fuzzy date and returns a datetime.datetime object"""
if not date_str:
return None
elif isinstance(date_str, datetime):
elif isinstance(date_str, datetime.datetime):
return date_str

# Don't try to parse anything with 6 or less characters and was parsed from the existing journal.
Expand All @@ -44,42 +44,42 @@ def parse(

date = dateparse(date_str, default=default_date)
if date.year == FAKE_YEAR:
date = datetime(datetime.now().year, date.timetuple()[1:6])
date = datetime.datetime(datetime.datetime.now().year, date.timetuple()[1:6])
else:
year_present = True
flag = 1 if date.hour == date.minute == 0 else 2
date = date.timetuple()
except Exception as e:
if e.args[0] == "day is out of range for month":
y, m, d, H, M, S = default_date.timetuple()[:6]
default_date = datetime(y, m, d - 1, H, M, S)
default_date = datetime.datetime(y, m, d - 1, H, M, S)
else:
calendar = __get_pdt_calendar()
date, flag = calendar.parse(date_str)

if not flag: # Oops, unparsable.
try: # Try and parse this as a single year
year = int(date_str)
return datetime(year, 1, 1)
return datetime.datetime(year, 1, 1)
except ValueError:
return None
except TypeError:
return None

if flag == 1: # Date found, but no time. Use the default time.
date = datetime(
date = datetime.datetime(
*date[:3],
hour=23 if inclusive else default_hour or 0,
minute=59 if inclusive else default_minute or 0,
second=59 if inclusive else 0
)
else:
date = datetime(*date[:6])
date = datetime.datetime(*date[:6])

# Ugly heuristic: if the date is more than 4 weeks in the future, we got the year wrong.
# Rather then this, we would like to see parsedatetime patched so we can tell it to prefer
# past dates
dt = datetime.now() - date
dt = datetime.datetime.now() - date
if dt.days < -28 and not year_present:
date = date.replace(date.year - 1)
return date
22 changes: 12 additions & 10 deletions tests/features/datetime.feature
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ Feature: Reading and writing to journal with custom date formats
When we run "jrnl <command>"
Then we should see the message "Entry added"
When we run "jrnl -n 1"
Then the output should contain "<output>"
Then the output should contain "<expected_output>"
Examples: Day-first Dates
| config_file | command | output |
| config_file | command | expected_output |
| little_endian_dates.yaml | 2020-09-19: My first entry. | 19.09.2020 09:00 My first entry. |
| little_endian_dates.yaml | 2020-08-09: My second entry. | 09.08.2020 09:00 My second entry. |
| little_endian_dates.yaml | 2020-02-29: Test. | 29.02.2020 09:00 Test. |
Expand All @@ -53,10 +53,10 @@ Feature: Reading and writing to journal with custom date formats
Scenario Outline: Searching for dates with custom date
Given we use the config "<config_file>"
When we run "jrnl <command>"
Then the output should be "<output>"
Then the output should be "<expected_output>"

Examples: Day-first Dates
| config_file | command | output |
| config_file | command | expected_output |
| little_endian_dates.yaml | -on '2013-07-10' --short | 10.07.2013 15:40 Life is good. |
| little_endian_dates.yaml | -on 'june 9 2013' --short | 09.06.2013 15:39 My first entry. |
| little_endian_dates.yaml | -on 'july 10 2013' --short | 10.07.2013 15:40 Life is good. |
Expand Down Expand Up @@ -84,16 +84,17 @@ Feature: Reading and writing to journal with custom date formats
And the output should not contain "But I'm better."


Scenario Outline: Create entry using day of the week as entry date.
Scenario Outline: Create entry using day of the week as entry date one.
Given we use the config "simple.yaml"
And now is "2019-03-12 1:30:32 PM"
When we run "jrnl <command>"
Then we should see the message "Entry added"
When we run "jrnl -1"
Then the output should contain "<output>"
Then the output should contain "<expected_output>"
Then the output should contain the date "<date>"

Examples: Days of the week
| command | output | date |
| command | expected_output | date |
| Monday: entry on a monday | entry on a monday | monday at 9am |
| Tuesday: entry on a tuesday | entry on a tuesday | tuesday at 9am |
| Wednesday: entry on a wednesday | entry on a wednesday | wednesday at 9am |
Expand All @@ -105,16 +106,17 @@ Feature: Reading and writing to journal with custom date formats
| sUndAy: entry on a sunday | entry on a sunday | sunday at 9am |


Scenario Outline: Create entry using day of the week as entry date.
Scenario Outline: Create entry using day of the week as entry date two.
Given we use the config "simple.yaml"
And now is "2012-03-12 1:30:32 PM"
When we run "jrnl <command>"
Then we should see the message "Entry added"
When we run "jrnl -1"
Then the output should contain "<output>"
Then the output should contain "<expected_output>"
Then the output should contain the date "<date>"

Examples: Days of the week
| command | output | date |
| command | expected_output | date |
| Mon: entry on a monday | entry on a monday | monday at 9am |
| Tue: entry on a tuesday | entry on a tuesday | tuesday at 9am |
| Wed: entry on a wednesday | entry on a wednesday | wednesday at 9am |
Expand Down
Loading

0 comments on commit e77a548

Please sign in to comment.