mirror of
https://github.com/jrnl-org/jrnl.git
synced 2025-05-10 08:38:32 +02:00
Fix #1289 by adding extension
config.
This commit is contained in:
parent
628edd0399
commit
c70f938c97
1 changed files with 13 additions and 8 deletions
|
@ -18,7 +18,6 @@ if TYPE_CHECKING:
|
|||
DIGIT_PATTERN = "[0123456789]"
|
||||
YEAR_PATTERN = DIGIT_PATTERN * 4
|
||||
MONTH_PATTERN = "[01]" + DIGIT_PATTERN
|
||||
DAY_PATTERN = "[0-3][0-9].(txt|md)"
|
||||
|
||||
|
||||
class Folder(Journal):
|
||||
|
@ -35,7 +34,7 @@ class Folder(Journal):
|
|||
self.entries = []
|
||||
|
||||
if os.path.exists(self.config["journal"]):
|
||||
filenames = Folder._get_files(self.config["journal"])
|
||||
filenames = Folder._get_files(self, self.config["journal"])
|
||||
for filename in filenames:
|
||||
with codecs.open(filename, "r", "utf-8") as f:
|
||||
journal = f.read()
|
||||
|
@ -46,6 +45,8 @@ class Folder(Journal):
|
|||
|
||||
def write(self) -> None:
|
||||
"""Writes only the entries that have been modified into proper files."""
|
||||
if self.config["extension"] is not None:
|
||||
EXTENSION = self.config["extension"]
|
||||
# Create a list of dates of modified entries. Start with diff_entry_dates
|
||||
modified_dates = self._diff_entry_dates
|
||||
seen_dates = set(self._diff_entry_dates)
|
||||
|
@ -64,7 +65,7 @@ class Folder(Journal):
|
|||
self.config["journal"],
|
||||
d.strftime("%Y"),
|
||||
d.strftime("%m"),
|
||||
d.strftime("%d") + ".txt",
|
||||
d.strftime("%d") + "." + EXTENSION,
|
||||
)
|
||||
dirname = os.path.dirname(filename)
|
||||
# create directory if it doesn't exist
|
||||
|
@ -82,7 +83,7 @@ class Folder(Journal):
|
|||
journal_file.write(journal)
|
||||
# look for and delete empty files
|
||||
filenames = []
|
||||
filenames = Folder._get_files(self.config["journal"])
|
||||
filenames = Folder._get_files(self, self.config["journal"])
|
||||
for filename in filenames:
|
||||
if os.stat(filename).st_size <= 0:
|
||||
os.remove(filename)
|
||||
|
@ -122,12 +123,14 @@ class Folder(Journal):
|
|||
self.entries = mod_entries
|
||||
|
||||
@staticmethod
|
||||
def _get_files(journal_path: str) -> list[str]:
|
||||
def _get_files(self, journal_path: str) -> list[str]:
|
||||
"""Searches through sub directories starting with journal_path and find all text
|
||||
files that look like entries"""
|
||||
if self.config["extension"] is not None:
|
||||
EXTENSION = self.config["extension"]
|
||||
for year_folder in Folder._get_year_folders(pathlib.Path(journal_path)):
|
||||
for month_folder in Folder._get_month_folders(year_folder):
|
||||
yield from Folder._get_day_files(month_folder)
|
||||
yield from Folder._get_day_files(self, EXTENSION, month_folder)
|
||||
|
||||
@staticmethod
|
||||
def _get_year_folders(path: pathlib.Path) -> list[pathlib.Path]:
|
||||
|
@ -144,9 +147,11 @@ class Folder(Journal):
|
|||
return
|
||||
|
||||
@staticmethod
|
||||
def _get_day_files(path: pathlib.Path) -> list[str]:
|
||||
def _get_day_files(self, extension, path: pathlib.Path) -> list[str]:
|
||||
EXTENSION = extension
|
||||
DAY_PATTERN = "[0-3][0-9]." + EXTENSION
|
||||
for child in path.iterdir():
|
||||
match = re.search(DAY_PATTERN, str(child))
|
||||
match = re.fullmatch(DAY_PATTERN, str(child.name))
|
||||
if (
|
||||
match is not None
|
||||
and int(child.stem) > 0
|
||||
|
|
Loading…
Add table
Reference in a new issue