From 16e4491d44dbc1baa49084cbd70f572b04dba9b4 Mon Sep 17 00:00:00 2001 From: "Soren I. Bjornstad" Date: Fri, 27 Aug 2021 11:36:19 -0500 Subject: [PATCH] set up default config file in 'tzk init' --- tzk/config.py | 25 ----------- tzk/default_config.py | 100 ++++++++++++++++++++++++++++++++++++++++++ tzk/tw.py | 50 ++++++++++----------- 3 files changed, 122 insertions(+), 53 deletions(-) create mode 100644 tzk/default_config.py diff --git a/tzk/config.py b/tzk/config.py index 3acea79..9c582fb 100644 --- a/tzk/config.py +++ b/tzk/config.py @@ -46,31 +46,6 @@ class ConfigurationManager: fail(f"No tzk_config.py found in the current directory. " f"(Try 'tzk init' if you want to create a new one.)") - #TODO: trash this function - def write_attr(self, attr: str, value: str) -> bool: - """ - Try to add a simple attribute = string value config parameter to the - config file, if it doesn't already exist. More complicated data types - are not supported. - - Return: - False if the attribute already has a value. - True if successful. - - Raises: - File access errors if the config file is inaccessible. - """ - - if hasattr(self.conf_mod, attr): - return False - else: - setattr(self.conf_mod, attr, value) - with open(self.config_path / "tzk_config.py", "a") as f: - now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") - f.write(f"\n# Added automatically by tzk at {now}\n") - f.write(f'{attr} = "{value}"\n') - return True - def cm(cache=[]): """ diff --git a/tzk/default_config.py b/tzk/default_config.py new file mode 100644 index 0000000..1d4c0c5 --- /dev/null +++ b/tzk/default_config.py @@ -0,0 +1,100 @@ +# Default tzk config +# <> + +# This config file is live, unrestricted Python. +# You can add your own builders or logic here if necessary. +# tzk's package directory is on the PYTHONPATH when this module is imported, +# so if you want to import a function from, e.g., tzk.util, +# just say 'from tzk.util import whatever'. + +#------------------------------------------------------------------------------# + +# Imports. Don't remove this or you won't be able to configure builders. +from tzk import builders + +# Name of the subfolder containing your wiki data and its tiddlywiki.info file. +wiki_folder = "wiki" + + +### COMMITTING #### +# Default commit message to use with 'tzk commit'. +# You can always use 'tzk commit -m' to use a different message on the fly. +commit_message = "daily checkpoint" + +# Git remote to push changes to when you run 'tzk commit'. +commit_remote = "origin" + +# Uncomment if you want to abort 'tzk commit' if you're not on a specific branch. +#commit_require_branch = "master" + + +### LISTENING ### +# Port to listen on. If you specify 8080 for example, you'll edit your wiki by going to +# http://localhost:8080 in your browser. +listen_port = 8080 + +# Uncomment if you want to require HTTP basic authentication when serving your wiki. +# **WARNING**: this is NOT secure for use over the open Internet or all but the +# simplest local networks, as the password is sent in the clear. For good +# security, you need to add a reverse proxy on top that secures the connection via SSL. +#listen_username = "my_user" +#listen_password = "my_password" + + +### BUILD ### +# Filter for tiddlers that should be included in a public export of the wiki. +_public_export_filt = r""" + [is[system]] + [tag[Public]] + -[[$:/plugins/tiddlywiki/tiddlyweb]] + -[[$:/plugins/tiddlywiki/filesystem]] + -[prefix[$:/temp]] + -[prefix[$:/state]] + -[prefix[$:/sib/StorySaver/saved]] + -[prefix[$:/sib/checkify/]] + -[[$:/config/zettelkasten/Build/KillPhrases]] +""" + +# Each "product" is a different deliverable or target. A product can be built +# by running 'tzk build PRODUCT'. You can include any number of products, +# each of which can use any number of "builders", micro-build steps that +# perform a small part of the build. The configured builders are run in sequence. +# +# See the "Builders" section of the tzk documentation +# for details on the available builders and their parameters. +# +# If you want to do something that's not covered by the existing builders, +# you can write your own, or you can run arbitrary shell commands +# using a 'builders.shell("my shell command here"),' builder. +products = { + # The default configuration contains a single product for building a public wiki. + 'public': [ + builders.new_output_folder(), + builders.export_public_tiddlers(_public_export_filt), + builders.replace_private_people(), + builders.set_tiddler_values({ + '$__config_sib_CurrentEditionPublicity.tid': 'public', + '$__config_sib_IsPublicEdition.tid': 'false', + '$__config_DefaultSidebarTab.tid': '$:/sib/SideBar/Explore', + '$__DefaultTiddlers.tid': 'PublicHomepage', + '$__config_PageControlButtons_Visibility_$__core_ui_Buttons_close-all.tid': 'show', + '$__config_PageControlButtons_Visibility_$__core_ui_Buttons_control-panel.tid': 'hide', + '$__config_PageControlButtons_Visibility_$__core_ui_Buttons_home.tid': 'show', + '$__config_PageControlButtons_Visibility_$__core_ui_Buttons_more-page-actions.tid': 'show', + '$__config_PageControlButtons_Visibility_$__core_ui_Buttons_new-tiddler.tid': 'hide', + '$__config_PageControlButtons_Visibility_$__core_ui_Buttons_permaview.tid': 'show', + '$__config_PageControlButtons_Visibility_$__core_ui_Buttons_tag-manager.tid': 'hide', + '$__config_PageControlButtons_Visibility_$__sib_Buttons_NewSource.tid': 'hide', + '$__config_PageControlButtons_Visibility_$__sib_Buttons_ReadingInbox.tid': 'hide', + '$__config_ViewToolbarButtons_Visibility_$__core_ui_Buttons_close-others.tid': 'show', + '$__config_ViewToolbarButtons_Visibility_$__core_ui_Buttons_edit.tid': 'hide', + '$__config_ViewToolbarButtons_Visibility_$__core_ui_Buttons_info.tid': 'hide', + '$__config_ViewToolbarButtons_Visibility_$__core_ui_Buttons_permalink.tid': 'show', + '$__config_ViewToolbarButtons_Visibility_$__sib_Buttons_CopyPublicZettelkastenLink.tid': 'hide', + '$__config_ViewToolbarButtons_Visibility_DoCopyTitleReference.tid': 'hide', + }), + builders.check_for_kill_phrases(), + builders.save_attachments_externally(), + builders.compile_html_file(externalize_attachments=True), + ], +} diff --git a/tzk/tw.py b/tzk/tw.py index c8e08f8..4b6acde 100644 --- a/tzk/tw.py +++ b/tzk/tw.py @@ -1,3 +1,4 @@ +import datetime import functools import json import os @@ -15,9 +16,11 @@ from tzk.util import pushd def _npm_bin() -> str: return subprocess.check_output(("npm", "bin"), text=True).strip() + def _tw_path() -> str: return _npm_bin() + "/tiddlywiki" + @functools.lru_cache(1) def _whoami() -> str: "Try to guess the user's name." @@ -51,12 +54,26 @@ def exec(args: Sequence[Sequence[str]], base_wiki_folder: str = None) -> int: return subprocess.call(call_args) +def _init_tzk_config() -> None: + print("tzk: Creating new tzk_config.py...") + with open(Path(__file__).parent / "default_config.py") as f: + default_config = f.read() + + now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") + generation_details = f"Created automatically by 'tzk init' at {now}." + default_config = default_config.replace('<>', + generation_details) + + with open("tzk_config.py", "w") as f: + f.write(default_config) + + def _init_npm(wiki_name: str, tw_version_spec: str, author: str) -> None: """ Create a package.json file for this repository, requiring TiddlyWiki at the specified version, and install the npm dependencies. """ - print("tzk: Creating package.json...") + print("tzk: Creating new package.json...") PACKAGE_JSON = dedent(""" { "name": "%(wiki_name)s", @@ -91,26 +108,6 @@ def _init_tw(wiki_name: str) -> None: subprocess.check_call((_tw_path(), "--init")) -def _save_wikifolder_to_config(wiki_name: str) -> bool: - """ - Set the wiki_folder config option to the wiki_name we initialized with, - if it's not already set in the config. - - Return True if the option ended set to wiki_name, False otherwise. - """ - print("tzk: Writing new wiki folder to config file...") - if not config.cm().write_attr("wiki_folder", wiki_name): - if config.cm().wiki_folder == wiki_name: - print("tzk: (Looks like it was already there.)") - else: - print(f"tzk: WARNING: The wiki_folder option in your config appears " - f"to be set to '{config.cm().wiki_folder}', rather than the wiki folder " - f"you're initializing, {wiki_name}. Please check your config file " - "and update this option if necessary.") - return False - return True - - def _add_filesystem_plugins(wiki_name: str) -> None: """ Add the "tiddlywiki/filesystem" and "tiddlywiki/tiddlyweb" plugins @@ -159,20 +156,17 @@ def install(wiki_name: str, tw_version_spec: str, author: Optional[str]): Install TiddlyWiki on Node.js in the current directory and set up a new wiki. """ # assert: caller has checked npm and git are installed - warnings = False if author is None: author = _whoami() + _init_tzk_config() _init_npm(wiki_name, tw_version_spec, author) _init_tw(wiki_name) - #warnings |= not _save_wikifolder_to_config(wiki_name) ## TODO: now write entire config _add_filesystem_plugins(wiki_name) _init_gitignore() _initial_commit() - if warnings: - print("tzk: Initialization completed with warnings. Read the output and " - "make any changes required, then run 'tzk listen' to start the server.") - else: - print("tzk: Initialized successfully. Run 'tzk listen' to start the server.") + print("tzk: Initialized successfully. " + "Review the 'tzk_config.py' in a text editor and make any changes desired, " + "then run 'tzk listen' to start the server.")