Added prompt for bug report name to info plugin

This commit is contained in:
squidfunk
2023-08-10 15:32:46 +02:00
parent 50f455ec09
commit 554976f90f
4 changed files with 48 additions and 36 deletions

View File

@@ -32,5 +32,4 @@ class InfoConfig(Config):
# Options for archive # Options for archive
archive = Type(bool, default = True) archive = Type(bool, default = True)
archive_name = Type(str, default = "example")
archive_stop_on_violation = Type(bool, default = True) archive_stop_on_violation = Type(bool, default = True)

View File

@@ -28,6 +28,7 @@ import sys
from colorama import Fore, Style from colorama import Fore, Style
from importlib.metadata import distributions, version from importlib.metadata import distributions, version
from io import BytesIO from io import BytesIO
from markdown.extensions.toc import slugify
from mkdocs import utils from mkdocs import utils
from mkdocs.plugins import BasePlugin, event_priority from mkdocs.plugins import BasePlugin, event_priority
from mkdocs.structure.files import get_files from mkdocs.structure.files import get_files
@@ -46,14 +47,17 @@ class InfoPlugin(BasePlugin[InfoConfig]):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
# Initialize variables for incremental builds # Initialize incremental builds
self.is_serve = False self.is_serve = False
# Determine whether we're serving # Determine whether we're serving the site
def on_startup(self, *, command, dirty): def on_startup(self, *, command, dirty):
self.is_serve = (command == "serve") self.is_serve = command == "serve"
# Initialize plugin (run earliest) # Create a self-contained example (run earliest) - determine all files that
# are visible to MkDocs and are used to build the site, create an archive
# that contains all of them, and print a summary of the archive contents.
# The user must attach this archive to the bug report.
@event_priority(100) @event_priority(100)
def on_config(self, config): def on_config(self, config):
if not self.config.enabled: if not self.config.enabled:
@@ -96,36 +100,39 @@ class InfoPlugin(BasePlugin[InfoConfig]):
log.error("Please remove 'hooks' setting.") log.error("Please remove 'hooks' setting.")
self._help_on_customizations_and_exit() self._help_on_customizations_and_exit()
# Create in-memory archive # Create in-memory archive and prompt user to enter a short descriptive
# name for the archive, which is also used as the directory name. Note
# that the name is slugified for better readability and stripped of any
# file extension that the user might have entered.
archive = BytesIO() archive = BytesIO()
archive_name = self.config.archive_name example = input("\nPlease name your bug report (2-4 words): ")
example, _ = os.path.splitext(example)
example = slugify(example, "-")
# Create self-contained example from project # Create self-contained example from project
files = [] files: list[str] = []
with ZipFile(archive, "a", ZIP_DEFLATED, False) as f: with ZipFile(archive, "a", ZIP_DEFLATED, False) as f:
for path in ["mkdocs.yml", "requirements.txt"]: for path in ["mkdocs.yml", "requirements.txt"]:
if os.path.isfile(path): if os.path.isfile(path):
f.write(path, os.path.join(archive_name, path)) f.write(path, os.path.join(example, path))
# Append all files visible to MkDocs # Append all files visible to MkDocs
for file in get_files(config): for file in get_files(config):
path = os.path.relpath(file.abs_src_path, os.path.curdir) path = os.path.relpath(file.abs_src_path, os.path.curdir)
f.write(path, os.path.join(archive_name, path)) f.write(path, os.path.join(example, path))
# Add information on installed packages # Add information on installed packages
f.writestr( f.writestr(
os.path.join(archive_name, "requirements.lock.txt"), os.path.join(example, "requirements.lock.txt"),
"\n".join(sorted([ "\n".join(sorted([
"==".join([ "==".join([package.name, package.version])
package.name, for package in distributions()
package.version
]) for package in distributions()
])) ]))
) )
# Add information in platform # Add information in platform
f.writestr( f.writestr(
os.path.join(archive_name, "platform.json"), os.path.join(example, "platform.json"),
json.dumps( json.dumps(
{ {
"system": platform.platform(), "system": platform.platform(),
@@ -145,7 +152,7 @@ class InfoPlugin(BasePlugin[InfoConfig]):
# Finally, write archive to disk # Finally, write archive to disk
buffer = archive.getbuffer() buffer = archive.getbuffer()
with open(f"{archive_name}.zip", "wb") as f: with open(f"{example}.zip", "wb") as f:
f.write(archive.getvalue()) f.write(archive.getvalue())
# Print summary # Print summary
@@ -169,7 +176,7 @@ class InfoPlugin(BasePlugin[InfoConfig]):
if buffer.nbytes > 1000000: if buffer.nbytes > 1000000:
log.warning("Archive exceeds recommended maximum size of 1 MB") log.warning("Archive exceeds recommended maximum size of 1 MB")
# Aaaaaand done. # Aaaaaand done
sys.exit(1) sys.exit(1)
# ------------------------------------------------------------------------- # -------------------------------------------------------------------------

View File

@@ -32,5 +32,4 @@ class InfoConfig(Config):
# Options for archive # Options for archive
archive = Type(bool, default = True) archive = Type(bool, default = True)
archive_name = Type(str, default = "example")
archive_stop_on_violation = Type(bool, default = True) archive_stop_on_violation = Type(bool, default = True)

View File

@@ -28,6 +28,7 @@ import sys
from colorama import Fore, Style from colorama import Fore, Style
from importlib.metadata import distributions, version from importlib.metadata import distributions, version
from io import BytesIO from io import BytesIO
from markdown.extensions.toc import slugify
from mkdocs import utils from mkdocs import utils
from mkdocs.plugins import BasePlugin, event_priority from mkdocs.plugins import BasePlugin, event_priority
from mkdocs.structure.files import get_files from mkdocs.structure.files import get_files
@@ -46,14 +47,17 @@ class InfoPlugin(BasePlugin[InfoConfig]):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
# Initialize variables for incremental builds # Initialize incremental builds
self.is_serve = False self.is_serve = False
# Determine whether we're serving # Determine whether we're serving the site
def on_startup(self, *, command, dirty): def on_startup(self, *, command, dirty):
self.is_serve = (command == "serve") self.is_serve = command == "serve"
# Initialize plugin (run earliest) # Create a self-contained example (run earliest) - determine all files that
# are visible to MkDocs and are used to build the site, create an archive
# that contains all of them, and print a summary of the archive contents.
# The user must attach this archive to the bug report.
@event_priority(100) @event_priority(100)
def on_config(self, config): def on_config(self, config):
if not self.config.enabled: if not self.config.enabled:
@@ -96,36 +100,39 @@ class InfoPlugin(BasePlugin[InfoConfig]):
log.error("Please remove 'hooks' setting.") log.error("Please remove 'hooks' setting.")
self._help_on_customizations_and_exit() self._help_on_customizations_and_exit()
# Create in-memory archive # Create in-memory archive and prompt user to enter a short descriptive
# name for the archive, which is also used as the directory name. Note
# that the name is slugified for better readability and stripped of any
# file extension that the user might have entered.
archive = BytesIO() archive = BytesIO()
archive_name = self.config.archive_name example = input("\nPlease name your bug report (2-4 words): ")
example, _ = os.path.splitext(example)
example = slugify(example, "-")
# Create self-contained example from project # Create self-contained example from project
files = [] files: list[str] = []
with ZipFile(archive, "a", ZIP_DEFLATED, False) as f: with ZipFile(archive, "a", ZIP_DEFLATED, False) as f:
for path in ["mkdocs.yml", "requirements.txt"]: for path in ["mkdocs.yml", "requirements.txt"]:
if os.path.isfile(path): if os.path.isfile(path):
f.write(path, os.path.join(archive_name, path)) f.write(path, os.path.join(example, path))
# Append all files visible to MkDocs # Append all files visible to MkDocs
for file in get_files(config): for file in get_files(config):
path = os.path.relpath(file.abs_src_path, os.path.curdir) path = os.path.relpath(file.abs_src_path, os.path.curdir)
f.write(path, os.path.join(archive_name, path)) f.write(path, os.path.join(example, path))
# Add information on installed packages # Add information on installed packages
f.writestr( f.writestr(
os.path.join(archive_name, "requirements.lock.txt"), os.path.join(example, "requirements.lock.txt"),
"\n".join(sorted([ "\n".join(sorted([
"==".join([ "==".join([package.name, package.version])
package.name, for package in distributions()
package.version
]) for package in distributions()
])) ]))
) )
# Add information in platform # Add information in platform
f.writestr( f.writestr(
os.path.join(archive_name, "platform.json"), os.path.join(example, "platform.json"),
json.dumps( json.dumps(
{ {
"system": platform.platform(), "system": platform.platform(),
@@ -145,7 +152,7 @@ class InfoPlugin(BasePlugin[InfoConfig]):
# Finally, write archive to disk # Finally, write archive to disk
buffer = archive.getbuffer() buffer = archive.getbuffer()
with open(f"{archive_name}.zip", "wb") as f: with open(f"{example}.zip", "wb") as f:
f.write(archive.getvalue()) f.write(archive.getvalue())
# Print summary # Print summary
@@ -169,7 +176,7 @@ class InfoPlugin(BasePlugin[InfoConfig]):
if buffer.nbytes > 1000000: if buffer.nbytes > 1000000:
log.warning("Archive exceeds recommended maximum size of 1 MB") log.warning("Archive exceeds recommended maximum size of 1 MB")
# Aaaaaand done. # Aaaaaand done
sys.exit(1) sys.exit(1)
# ------------------------------------------------------------------------- # -------------------------------------------------------------------------