Skip to content

Commit c6b2311

Browse files
stonebigclaude
andcommitted
-md --json: emit the package index as a JSON build manifest
generate_package_index_markdown is now a rendering of get_package_index_data(), which returns {distribution, tools, packages, wheelhouse}; markdown output is unchanged. Also stop crashing on consoles that cannot encode emoji found in package summaries (pre-existing UnicodeEncodeError in wppm -md -ws): stdout now replaces unencodable characters, and JSON stays ASCII-escaped. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
1 parent 32174fb commit c6b2311

1 file changed

Lines changed: 36 additions & 9 deletions

File tree

‎wppm/wppm.py‎

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,10 @@ def render_markdown_for_list(self, title, items):
7979
md += "\n"
8080
return md
8181

82-
def generate_package_index_markdown(self, python_executable_directory: str|None = None, winpyver2: str|None = None,
82+
def get_package_index_data(self, python_executable_directory: str|None = None, winpyver2: str|None = None,
8383
flavor: str|None = None, architecture_bits: int|None = None
84-
, release_level: str|None = None, wheeldir: str|None = None) -> str:
85-
"""Generates a Markdown formatted package index page."""
84+
, release_level: str|None = None, wheeldir: str|None = None) -> dict:
85+
"""Collects the package index data: distribution identity, tools, packages, wheelhouse."""
8686
my_ver , my_arch = utils.get_python_infos(python_executable_directory or self.target)
8787
my_winpyver2 = winpyver2 or os.getenv("WINPYVER2","")
8888
my_winpyver2 = my_winpyver2 if my_winpyver2 != "" else my_ver
@@ -97,14 +97,35 @@ def generate_package_index_markdown(self, python_executable_directory: str|None
9797
wheelhouse_list = [(name, f"https://pypi.org/project/{name}", version, utils.sum_up(summary))
9898
for name, version, summary in wh.list_packages_with_metadata(str(my_wheeldir)) ]
9999

100-
return f"""## WinPython {my_winpyver2 + my_flavor}
100+
as_records = lambda items: [{"name": n, "url": u, "version": v, "summary": s} for n, u, v, s in items]
101+
return {
102+
"distribution": {
103+
"winpython": my_winpyver2 + my_flavor,
104+
"python_version": my_ver,
105+
"architecture_bits": my_arch,
106+
"release_level": my_release_level,
107+
},
108+
"tools": as_records(tools_list),
109+
"packages": as_records(package_list),
110+
"wheelhouse": as_records(wheelhouse_list),
111+
}
112+
113+
def generate_package_index_markdown(self, python_executable_directory: str|None = None, winpyver2: str|None = None,
114+
flavor: str|None = None, architecture_bits: int|None = None
115+
, release_level: str|None = None, wheeldir: str|None = None) -> str:
116+
"""Generates a Markdown formatted package index page."""
117+
data = self.get_package_index_data(python_executable_directory, winpyver2, flavor, architecture_bits, release_level, wheeldir)
118+
d = data["distribution"]
119+
as_tuples = lambda items: [(i["name"], i["url"], i["version"], i["summary"]) for i in items]
120+
121+
return f"""## WinPython {d["winpython"]}
101122
102-
The following packages are included in WinPython-{my_arch}bit v{my_winpyver2 + my_flavor} {my_release_level}.
123+
The following packages are included in WinPython-{d["architecture_bits"]}bit v{d["winpython"]} {d["release_level"]}.
103124
104125
105-
{self.render_markdown_for_list("Tools", tools_list)}
106-
{self.render_markdown_for_list("Python packages", package_list)}
107-
{self.render_markdown_for_list("WheelHouse packages", wheelhouse_list)}
126+
{self.render_markdown_for_list("Tools", as_tuples(data["tools"]))}
127+
{self.render_markdown_for_list("Python packages", as_tuples(data["packages"]))}
128+
{self.render_markdown_for_list("WheelHouse packages", as_tuples(data["wheelhouse"]))}
108129
"""
109130

110131
def find_package(self, name: str) -> Package | None:
@@ -257,6 +278,9 @@ def install_bdist_direct(self, package, install_options=None):
257278
self._print_done()
258279

259280
def main(test=False):
281+
# package summaries may contain characters the console codepage can't encode (emoji): don't crash
282+
if sys.stdout and hasattr(sys.stdout, "reconfigure"):
283+
sys.stdout.reconfigure(errors="replace")
260284

261285
registerWinPythonHelp = f"Register WinPython: associate file extensions, icons and context menu with this WinPython"
262286
unregisterWinPythonHelp = f"Unregister WinPython: de-associate file extensions, icons and context menu from this WinPython"
@@ -278,7 +302,7 @@ def main(test=False):
278302
parser.add_argument("-p",dest="pipdown",action="store_true",help="show Package (!= missing) dependencies of the given package[option], [.]=all: wppm -p pandas[.]")
279303
parser.add_argument("-r", dest="pipup", action="store_true", help=f"show Reverse (!= constraining) dependancies of the given package[option]: wppm -r pytest![test]")
280304
parser.add_argument("-l", dest="levels", type=int, default=-1, help="show 'LEVELS' levels of dependencies (with -p, -r): wppm -p pandas -l1")
281-
parser.add_argument("--json", dest="json", action="store_true", help="machine-readable JSON output (with -p, -r, -ls): wppm -p pandas[.] --json")
305+
parser.add_argument("--json", dest="json", action="store_true", help="machine-readable JSON output (with -p, -r, -ls, -md): wppm -p pandas[.] --json")
282306
parser.add_argument("-t", dest="target", default=sys.prefix, help=f'path to target Python distribution (default: "{sys.prefix}")')
283307
parser.add_argument("-i", "--install", action="store_true", help="install a given package wheel or pylock file (use pip for more features)")
284308
parser.add_argument("-u", "--uninstall", action="store_true", help="uninstall package (use pip for more features)")
@@ -372,6 +396,9 @@ def main(test=False):
372396
p = subprocess.Popen(["start", "cmd", "/k",dist.python_exe, "-c" , cmd_mov], shell = True, cwd=dist.target)
373397
sys.exit()
374398
if args.markdown:
399+
if args.json:
400+
print(json.dumps(dist.get_package_index_data(wheeldir=args.wheelsource), indent=4))
401+
sys.exit()
375402
default = dist.generate_package_index_markdown()
376403
if args.wheelsource:
377404
compare = dist.generate_package_index_markdown(wheeldir = args.wheelsource)

0 commit comments

Comments
 (0)