turn into enum instead of strings

This commit is contained in:
Jonathan Wren 2022-09-25 18:39:06 -07:00
parent 97f3290e02
commit ab70c56cf6

View file

@ -1,22 +1,31 @@
# Copyright © 2012-2022 jrnl contributors # Copyright © 2012-2022 jrnl contributors
# License: https://www.gnu.org/licenses/gpl-3.0.html # License: https://www.gnu.org/licenses/gpl-3.0.html
from enum import Enum
from importlib import import_module from importlib import import_module
from .BaseEncryption import BaseEncryption
class EncryptionMethods(str, Enum):
NONE = "NoEncryption"
JRNLV1 = "Jrnlv1Encryption"
JRNLV2 = "Jrnlv2Encryption"
ENCRYPTION_METHODS = { ENCRYPTION_METHODS = {
# config: classname (as string) # config: classname (as string)
"default": "NoEncryption", True: EncryptionMethods.JRNLV2, # the default
False: "NoEncryption", False: EncryptionMethods.NONE,
True: "Jrnlv2Encryption", "jrnlv1": EncryptionMethods.JRNLV1,
"jrnlv1": "Jrnlv1Encryption", "jrnlv2": EncryptionMethods.JRNLV2,
"jrnlv2": "Jrnlv2Encryption",
} }
def determine_encryption_method(config): def determine_encryption_method(config: str | bool) -> BaseEncryption:
key = config key = config
if isinstance(config, str): if isinstance(config, str):
key = config.lower() key = config.lower()
my_class = ENCRYPTION_METHODS.get(key, "default") my_class = ENCRYPTION_METHODS[key]
return getattr(import_module(f"jrnl.encryption.{my_class}"), my_class) return getattr(import_module(f"jrnl.encryption.{my_class}"), my_class)