mirror of
https://github.com/jrnl-org/jrnl.git
synced 2025-05-10 16:48:31 +02:00
Filter with glob first for max performance
This commit is contained in:
parent
034aaed346
commit
128cd16744
1 changed files with 13 additions and 8 deletions
|
@ -13,14 +13,19 @@ from .Journal import Journal
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from jrnl.journals import Entry
|
from jrnl.journals import Entry
|
||||||
|
|
||||||
|
# Search patterns
|
||||||
|
DIGIT_PATTERN = "[0123456789]"
|
||||||
|
YEAR_PATTERN = DIGIT_PATTERN * 4
|
||||||
|
MONTH_PATTERN = DIGIT_PATTERN * 2
|
||||||
|
DAY_PATTERN = (DIGIT_PATTERN * 2) + ".txt"
|
||||||
|
|
||||||
|
# Entire range of possible folder/file names for months and days
|
||||||
|
MONTH_FOLDERS = [str(m).zfill(2) for m in range(1, 13)]
|
||||||
|
DAY_FILE_STEMS = [str(d).zfill(2) for d in range(1, 32)]
|
||||||
|
|
||||||
class Folder(Journal):
|
class Folder(Journal):
|
||||||
"""A Journal handling multiple files in a folder"""
|
"""A Journal handling multiple files in a folder"""
|
||||||
|
|
||||||
# Entire range of possible folder/file names for months and days
|
|
||||||
MONTH_FOLDERS = [str(m).zfill(2) for m in range(1, 13)]
|
|
||||||
DAY_FILE_STEMS = [str(d).zfill(2) for d in range(1, 32)]
|
|
||||||
|
|
||||||
def __init__(self, name: str = "default", **kwargs):
|
def __init__(self, name: str = "default", **kwargs):
|
||||||
self.entries = []
|
self.entries = []
|
||||||
|
@ -128,23 +133,23 @@ class Folder(Journal):
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _get_year_folders(path: pathlib.Path) -> list[pathlib.Path]:
|
def _get_year_folders(path: pathlib.Path) -> list[pathlib.Path]:
|
||||||
for child in path.iterdir():
|
for child in path.glob(YEAR_PATTERN):
|
||||||
if child.name.isdigit() and len(child.name) == 4 and child.is_dir():
|
if child.name.isdigit() and len(child.name) == 4 and child.is_dir():
|
||||||
yield child
|
yield child
|
||||||
return
|
return
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _get_month_folders(path: pathlib.Path) -> list[pathlib.Path]:
|
def _get_month_folders(path: pathlib.Path) -> list[pathlib.Path]:
|
||||||
for child in path.iterdir():
|
for child in path.glob(MONTH_PATTERN):
|
||||||
if child.name in Folder.MONTH_FOLDERS and path.is_dir():
|
if child.name in MONTH_FOLDERS and path.is_dir():
|
||||||
yield child
|
yield child
|
||||||
return
|
return
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _get_day_files(path: pathlib.Path) -> list[str]:
|
def _get_day_files(path: pathlib.Path) -> list[str]:
|
||||||
for child in path.iterdir():
|
for child in path.glob(DAY_PATTERN):
|
||||||
if (
|
if (
|
||||||
child.stem in Folder.DAY_FILE_STEMS
|
child.stem in DAY_FILE_STEMS
|
||||||
and child.match("*.txt")
|
and child.match("*.txt")
|
||||||
and child.is_file()
|
and child.is_file()
|
||||||
):
|
):
|
||||||
|
|
Loading…
Add table
Reference in a new issue