changing default config location

default config is ~/tembo/.config
This commit is contained in:
2021-10-23 21:12:04 +01:00
parent 560991325c
commit 4e6efa326e
3 changed files with 26 additions and 21 deletions

View File

@@ -2,14 +2,14 @@ import os
import panaetius import panaetius
if config_path := os.environ.get("TEMBO_CONFIG") is not None: if (config_path := os.environ.get("TEMBO_CONFIG")) is not None:
CONFIG = panaetius.Config("tembo", config_path) CONFIG = panaetius.Config("tembo", config_path)
else: else:
CONFIG = panaetius.Config("tembo") CONFIG = panaetius.Config("tembo", "~/tembo/.config", skip_header_init=True)
panaetius.set_config(CONFIG, "base_path", "~/tembo") panaetius.set_config(CONFIG, "base_path", "~/tembo")
panaetius.set_config(CONFIG, "template_path", "~/tembo/templates") panaetius.set_config(CONFIG, "template_path", "~/tembo/.templates")
panaetius.set_config(CONFIG, "scopes", {}) panaetius.set_config(CONFIG, "scopes", {})
panaetius.set_config(CONFIG, "logging.level", "DEBUG") panaetius.set_config(CONFIG, "logging.level", "DEBUG")
panaetius.set_config(CONFIG, "logging.path") panaetius.set_config(CONFIG, "logging.path")

View File

@@ -12,8 +12,6 @@ def run():
""" """
Tembo - an organiser for work notes. Tembo - an organiser for work notes.
""" """
print(tembo.CONFIG.base_path)
# print(tembo.CONFIG.scopes)
@click.command(options_metavar="<options>") @click.command(options_metavar="<options>")
@@ -33,7 +31,6 @@ def new(scope, inputs):
Example: tembo new meeting my_presentation Example: tembo new meeting my_presentation
""" """
for user_scope in tembo.CONFIG.scopes: for user_scope in tembo.CONFIG.scopes:
if user_scope["name"] == scope: if user_scope["name"] == scope:
scoped_page = pages.ScopedPageCreator().create_page( scoped_page = pages.ScopedPageCreator().create_page(
@@ -47,6 +44,15 @@ def new(scope, inputs):
) )
scoped_page.save_to_disk() scoped_page.save_to_disk()
tembo.logger.info("Saved %s to disk", scoped_page) tembo.logger.info("Saved %s to disk", scoped_page)
raise SystemExit(0)
tembo.logger.critical(
"No config.yml found in %s - exiting", tembo.CONFIG.config_path
)
raise SystemExit(1)
run.add_command(new) run.add_command(new)
if __name__ == "__main__":
new(["scratchpad"], ())

View File

@@ -19,7 +19,7 @@ class PageCreator:
filename: str, filename: str,
extension: str, extension: str,
name: str, name: str,
user_input: tuple[str, ...], user_input: tuple[str, ...] | None,
template_filename: str | None = None, template_filename: str | None = None,
) -> Page: ) -> Page:
pass pass
@@ -53,7 +53,6 @@ class PageCreator:
else: else:
# default template_path is base_path / templates # default template_path is base_path / templates
template_path = self._convert_to_path(base_path, "templates", "", "") template_path = self._convert_to_path(base_path, "templates", "", "")
print(template_path, template_filename)
# load the template folder # load the template folder
file_loader = jinja2.FileSystemLoader(template_path) file_loader = jinja2.FileSystemLoader(template_path)
env = jinja2.Environment(loader=file_loader, autoescape=True) env = jinja2.Environment(loader=file_loader, autoescape=True)
@@ -96,7 +95,6 @@ class ScopedPageCreator(PageCreator):
template_contents = self._substitute_tokens( template_contents = self._substitute_tokens(
template_contents, user_input, name template_contents, user_input, name
) )
# print(template_contents)
else: else:
template_contents = "" template_contents = ""
return ScopedPage(path, template_contents) return ScopedPage(path, template_contents)
@@ -138,33 +136,33 @@ class ScopedPageCreator(PageCreator):
# if there aren't any tokens in the string, return the string # if there aren't any tokens in the string, return the string
return tokenified_string return tokenified_string
# if there is user input, check the number of tokens match what's passed in # if there is user input, check the number of tokens match what's passed in
if len(input_extraction) != len(user_input) and len(input_extraction) > 0: if len(input_extraction) > 0 and len(input_extraction) != len(user_input):
# if there are input matches and they don't equal the number of input # if there are input matches and they don't equal the number of input
# tokens, raise error # tokens, raise error
logger.critical( logger.critical(
"Your config specifies %s input tokens, you gave %s - exiting", "Your config/template specifies %s input tokens, you gave %s - exiting",
len(input_extraction), len(input_extraction),
len(user_input), len(user_input),
) )
raise SystemExit(1) raise SystemExit(1)
# if the length of user input matches and number of tokens match, or there are # if the length of both the input matches and the number of tokens match then
# no input matches, then substitute each token with the user's input # substitute each token with the user's input
for extracted_input, input_value in zip(input_extraction, user_input): for extracted_input, input_value in zip(input_extraction, user_input):
tokenified_string = tokenified_string.replace(extracted_input, input_value) tokenified_string = tokenified_string.replace(extracted_input, input_value)
return tokenified_string return tokenified_string
@staticmethod @staticmethod
def __substitute_date_tokens(tokenified_string: str) -> str: def __substitute_date_tokens(tokenified_string: str) -> str:
# find any {d:DD-MM-YYYY} tokens # find any {d:%d-%M-%Y} tokens
date_extraction_token = re.findall(r"(\{d\:[^}]*\})", tokenified_string) date_extraction_token = re.findall(r"(\{d\:[^}]*\})", tokenified_string)
for extracted_token in date_extraction_token: for extracted_token in date_extraction_token:
# extract the inner DD-MM-YYYY only # extract the inner %d-%M-%Y only
strftime_value = re.match(r"\{d\:([^\}]*)\}", extracted_token) strftime_value = re.match(r"\{d\:([^\}]*)\}", extracted_token)
if strftime_value: if strftime_value is not None:
strftime_value = strftime_value.group(1) strftime_value = strftime_value.group(1)
# replace {d:DD-MM-YYYY} with todays date formatted as DD-MM-YYYY if isinstance(strftime_value, str):
tokenified_string = tokenified_string.replace( tokenified_string = tokenified_string.replace(
extracted_token, pendulum.now().format(strftime_value) extracted_token, pendulum.now().strftime(strftime_value)
) )
return tokenified_string return tokenified_string
@@ -198,7 +196,8 @@ class ScopedPage(Page):
with scoped_note_file.open("w", encoding="utf-8") as scoped_page: with scoped_note_file.open("w", encoding="utf-8") as scoped_page:
scoped_page.write(self.page_content) scoped_page.write(self.page_content)
else: else:
logger.info("The file %s already exists - skipping.", str(scoped_note_file)) logger.info("%s already exists - skipping.", str(self))
raise SystemExit(0)
if __name__ == "__main__": if __name__ == "__main__":