From 5280ad2f5ef36a71b0067b74a01b2886aa0a4b1c Mon Sep 17 00:00:00 2001 From: gauthiier Date: Wed, 8 Dec 2021 12:09:34 +0100 Subject: [PATCH] python3 all the way --- pppadump/commands/index.py | 37 +++++++++++++++++++++++++++---------- 1 file changed, 27 insertions(+), 10 deletions(-) diff --git a/pppadump/commands/index.py b/pppadump/commands/index.py index 72ff970..75ed04d 100644 --- a/pppadump/commands/index.py +++ b/pppadump/commands/index.py @@ -4,20 +4,16 @@ import sys, json, re, os, time from datetime import datetime import dateutil.parser -try: - # python2 - from urllib2 import urlopen, URLError, HTTPError - from urllib import urlencode - from urlparse import urlparse, urlunparse -except ImportError: - # python3 - from urllib.parse import urlparse, urlunparse, urlencode, quote - from urllib.request import urlopen, URLError, HTTPError +# python3 +from urllib.parse import urlparse, urlunparse, urlencode, quote +from urllib.request import urlopen, URLError, HTTPError from jinja2 import FileSystemLoader, Environment from pppadump.commands.common import * from time import sleep import dateutil.parser +from pathlib import Path +import os """ index: @@ -283,17 +279,38 @@ def main (args): p["text"] = f.read() except FileNotFoundError: p['text'] = '' + # ADD IN LINK TO PAD AS "link" for v in linkversions: if v in versions_by_type: vdata = versions_by_type[v] try: - if v == "pad" or os.path.exists(vdata["path"]): + if v == "pad": p["link"] = absurl(vdata["url"], linkbase) break except KeyError as e: pass + # Not sure this goes here but fixing relpaths with output is quite nice... + if args.output: + outpath = Path(args.output).parent + for v in p["versions"]: + if v["type"] != "pad": + vpath = Path(v["path"]) + try: + # 2 types (pathlib or os.path) + # v["path"] = vpath.parent.relative_to(outpath).joinpath(vpath.name) + v["path"] = os.path.join(os.path.relpath(vpath.parent, outpath), vpath.name) + + # path or url??? if it's a file based publishing ethos, it should be path non? + # puttting this here since templates are using url... + v["url"] = v["path"] + except Exception as e: + pass + + + + if args.output: with open(args.output, "w") as f: print (template.render(vars(args)), file=f)