Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Speed up jrnl by 10%, improve slow imports #959

Merged
merged 5 commits into from
May 27, 2020
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
3 changes: 2 additions & 1 deletion jrnl/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import jrnl

from . import install, plugins, util
from .EncryptedJournal import EncryptedJournal
from .Journal import PlainJournal, open_journal
from .util import ERROR_COLOR, RESET_COLOR, UserAbort

Expand Down Expand Up @@ -242,6 +241,8 @@ def guess_mode(args, config):

def encrypt(journal, filename=None):
""" Encrypt into new file. If filename is not set, we encrypt the journal file itself. """
from .EncryptedJournal import EncryptedJournal

journal.config["encrypt"] = True

new_journal = EncryptedJournal.from_journal(journal)
Expand Down
33 changes: 18 additions & 15 deletions jrnl/install.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import xdg.BaseDirectory
import yaml

from . import __version__, upgrade, util
from . import __version__, util
from .util import UserAbort, verify_config

if "win32" not in sys.platform:
Expand Down Expand Up @@ -96,20 +96,23 @@ def load_or_install_jrnl():
log.debug("Reading configuration from file %s", config_path)
config = util.load_config(config_path)

try:
upgrade.upgrade_jrnl_if_necessary(config_path)
except upgrade.UpgradeValidationException:
print("Aborting upgrade.", file=sys.stderr)
print(
"Please tell us about this problem at the following URL:",
file=sys.stderr,
)
print(
"https://github.com/jrnl-org/jrnl/issues/new?title=UpgradeValidationException",
file=sys.stderr,
)
print("Exiting.", file=sys.stderr)
sys.exit(1)
if util.is_old_version(config_path):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that this is good to break out into a separate function, but without also updating upgrade_jrnl_if_necessary we now perform this check twice.

from . import upgrade

try:
upgrade.upgrade_jrnl(config_path)
except upgrade.UpgradeValidationException:
print("Aborting upgrade.", file=sys.stderr)
print(
"Please tell us about this problem at the following URL:",
file=sys.stderr,
)
print(
"https://github.com/jrnl-org/jrnl/issues/new?title=UpgradeValidationException",
file=sys.stderr,
)
print("Exiting.", file=sys.stderr)
sys.exit(1)

upgrade_config(config)
verify_config(config)
Expand Down
26 changes: 16 additions & 10 deletions jrnl/time.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
from datetime import datetime

from dateutil.parser import parse as dateparse

try:
import parsedatetime.parsedatetime_consts as pdt
except ImportError:
import parsedatetime as pdt

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

consts = pdt.Constants(usePyICU=False)
consts.DOWParseStyle = -1 # "Monday" will be either today or the last Monday
CALENDAR = pdt.Calendar(consts)

def __get_pdt_calendar():
try:
import parsedatetime.parsedatetime_consts as pdt
except ImportError:
import parsedatetime as pdt

consts = pdt.Constants(usePyICU=False)
consts.DOWParseStyle = -1 # "Monday" will be either today or the last Monday
calendar = pdt.Calendar(consts)

return calendar


def parse(
Expand All @@ -35,6 +38,8 @@ def parse(
year_present = False
while not date:
try:
from dateutil.parser import parse as dateparse

date = dateparse(date_str, default=default_date)
if date.year == FAKE_YEAR:
date = datetime(datetime.now().year, date.timetuple()[1:6])
Expand All @@ -47,7 +52,8 @@ def parse(
y, m, d, H, M, S = default_date.timetuple()[:6]
default_date = datetime(y, m, d - 1, H, M, S)
else:
date, flag = CALENDAR.parse(date_str)
calendar = __get_pdt_calendar()
date, flag = calendar.parse(date_str)

if not flag: # Oops, unparsable.
try: # Try and parse this as a single year
Expand Down
7 changes: 1 addition & 6 deletions jrnl/upgrade.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,7 @@ def check_exists(path):
return os.path.exists(path)


def upgrade_jrnl_if_necessary(config_path):
with open(config_path, "r", encoding="utf-8") as f:
config_file = f.read()
if not config_file.strip().startswith("{"):
return

def upgrade_jrnl(config_path):
config = util.load_config(config_path)

print(
Expand Down
10 changes: 10 additions & 0 deletions jrnl/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,16 @@ def load_config(config_path):
return yaml.load(f, Loader=yaml.FullLoader)


def is_config_json(config_path):
with open(config_path, "r", encoding="utf-8") as f:
config_file = f.read()
return config_file.strip().startswith("{")


def is_old_version(config_path):
return is_config_json(config_path)


def scope_config(config, journal_name):
if journal_name not in config["journals"]:
return config
Expand Down