mirror of
https://github.com/mikf/gallery-dl.git
synced 2024-11-21 18:22:30 +01:00
move 'pprint()' into its own module
to reuse its code in create_test_data.py later rename to 'pyprint' since 'pprint' is already used by stdlib module
This commit is contained in:
parent
8a11b72253
commit
2501adeda0
@ -14,6 +14,7 @@ import itertools
|
|||||||
import collections
|
import collections
|
||||||
|
|
||||||
import util
|
import util
|
||||||
|
from pyprint import pyprint
|
||||||
from gallery_dl import extractor
|
from gallery_dl import extractor
|
||||||
|
|
||||||
|
|
||||||
@ -33,97 +34,6 @@ __tests__ = (
|
|||||||
'''
|
'''
|
||||||
|
|
||||||
|
|
||||||
def pprint(obj, indent=0, lmin=9, lmax=16):
|
|
||||||
|
|
||||||
if isinstance(obj, str):
|
|
||||||
if obj.startswith("lit:"):
|
|
||||||
return f'''{obj[4:]}'''
|
|
||||||
|
|
||||||
if "\\" in obj or obj.startswith("re:"):
|
|
||||||
prefix = "r"
|
|
||||||
else:
|
|
||||||
prefix = ""
|
|
||||||
|
|
||||||
if "\n" in obj:
|
|
||||||
quote = '"""'
|
|
||||||
elif '"' in obj:
|
|
||||||
obj = re.sub(r'(?<!\\)"', '\\"', obj)
|
|
||||||
quote = '"'
|
|
||||||
else:
|
|
||||||
quote = '"'
|
|
||||||
|
|
||||||
return f'''{prefix}{quote}{obj}{quote}'''
|
|
||||||
|
|
||||||
if isinstance(obj, bytes):
|
|
||||||
return f'''b"{str(obj)[2:-1]}"'''
|
|
||||||
|
|
||||||
if isinstance(obj, type):
|
|
||||||
if obj.__module__ == "builtins":
|
|
||||||
return f'''{obj.__name__}'''
|
|
||||||
|
|
||||||
name = obj.__module__.rpartition(".")[2]
|
|
||||||
if name[0].isdecimal():
|
|
||||||
name = f"_{name}"
|
|
||||||
return f'''{name}.{obj.__name__}'''
|
|
||||||
|
|
||||||
if isinstance(obj, dict):
|
|
||||||
if not obj:
|
|
||||||
return "{}"
|
|
||||||
|
|
||||||
if len(obj) >= 2 or not indent:
|
|
||||||
ws = " " * indent
|
|
||||||
|
|
||||||
lkey = max(map(len, obj))
|
|
||||||
if not indent:
|
|
||||||
if lkey < lmin:
|
|
||||||
lkey = lmin
|
|
||||||
elif lkey > lmax:
|
|
||||||
lkey = lmax
|
|
||||||
|
|
||||||
lines = []
|
|
||||||
lines.append("{")
|
|
||||||
for key, value in obj.items():
|
|
||||||
if key.startswith("#blank-"):
|
|
||||||
lines.append("")
|
|
||||||
else:
|
|
||||||
lines.append(
|
|
||||||
f'''{ws} "{key}"'''
|
|
||||||
f'''{' '*(lkey - len(key))}: '''
|
|
||||||
f'''{pprint(value, indent+4)},'''
|
|
||||||
)
|
|
||||||
lines.append(f'''{ws}}}''')
|
|
||||||
return "\n".join(lines)
|
|
||||||
else:
|
|
||||||
key, value = obj.popitem()
|
|
||||||
return f'''{{"{key}": {pprint(value)}}}'''
|
|
||||||
|
|
||||||
if isinstance(obj, list):
|
|
||||||
if not obj:
|
|
||||||
return "[]"
|
|
||||||
|
|
||||||
if len(obj) >= 2:
|
|
||||||
ws = " " * indent
|
|
||||||
|
|
||||||
lines = []
|
|
||||||
lines.append("[")
|
|
||||||
lines.extend(
|
|
||||||
f'''{ws} {pprint(value, indent+4)},'''
|
|
||||||
for value in obj
|
|
||||||
)
|
|
||||||
lines.append(f'''{ws}]''')
|
|
||||||
return "\n".join(lines)
|
|
||||||
else:
|
|
||||||
return f'''[{pprint(obj[0])}]'''
|
|
||||||
|
|
||||||
if isinstance(obj, tuple):
|
|
||||||
if len(obj) == 1:
|
|
||||||
return f'''({pprint(obj[0], indent+4)},)'''
|
|
||||||
return f'''({", ".join(pprint(v, indent+4) for v in obj)})'''
|
|
||||||
|
|
||||||
else:
|
|
||||||
return f'''{obj}'''
|
|
||||||
|
|
||||||
|
|
||||||
def extract_tests_from_source(lines):
|
def extract_tests_from_source(lines):
|
||||||
tests = {}
|
tests = {}
|
||||||
|
|
||||||
@ -201,7 +111,7 @@ def build_test(extr, data):
|
|||||||
if (pattern := data.pop("pattern", None)):
|
if (pattern := data.pop("pattern", None)):
|
||||||
if pattern in PATTERNS:
|
if pattern in PATTERNS:
|
||||||
cls = PATTERNS[pattern]
|
cls = PATTERNS[pattern]
|
||||||
pattern = f"lit:{pprint(cls)}.pattern"
|
pattern = f"lit:{pyprint(cls)}.pattern"
|
||||||
instr["#pattern"] = pattern
|
instr["#pattern"] = pattern
|
||||||
if (exception := data.pop("exception", None)):
|
if (exception := data.pop("exception", None)):
|
||||||
instr["#exception"] = exception
|
instr["#exception"] = exception
|
||||||
@ -280,15 +190,15 @@ _{name} = getattr({module}, "{name}")'''
|
|||||||
stmt = f"import {name}"
|
stmt = f"import {name}"
|
||||||
imports[v.__module__] = stmt
|
imports[v.__module__] = stmt
|
||||||
|
|
||||||
test = pprint(head)
|
test = pyprint(head)
|
||||||
if instr:
|
if instr:
|
||||||
test = f"{test[:-2]}{pprint(instr)[1:]}"
|
test = f"{test[:-2]}{pyprint(instr)[1:]}"
|
||||||
if metadata:
|
if metadata:
|
||||||
for k, v in metadata.items():
|
for k, v in metadata.items():
|
||||||
if v == "type:datetime":
|
if v == "type:datetime":
|
||||||
imports["datetime"] = "import datetime"
|
imports["datetime"] = "import datetime"
|
||||||
metadata[k] = "lit:datetime.datetime"
|
metadata[k] = "lit:datetime.datetime"
|
||||||
test = f"{test[:-1]}{pprint(metadata, lmin=0)[1:]}"
|
test = f"{test[:-1]}{pyprint(metadata, lmin=0)[1:]}"
|
||||||
|
|
||||||
tests.append(f"{test},\n\n")
|
tests.append(f"{test},\n\n")
|
||||||
|
|
||||||
|
101
scripts/pyprint.py
Normal file
101
scripts/pyprint.py
Normal file
@ -0,0 +1,101 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
# Copyright 2024 Mike Fährmann
|
||||||
|
#
|
||||||
|
# This program is free software; you can redistribute it and/or modify
|
||||||
|
# it under the terms of the GNU General Public License version 2 as
|
||||||
|
# published by the Free Software Foundation.
|
||||||
|
|
||||||
|
import re
|
||||||
|
|
||||||
|
|
||||||
|
def pyprint(obj, indent=0, lmin=9, lmax=16):
|
||||||
|
|
||||||
|
if isinstance(obj, str):
|
||||||
|
if obj.startswith("lit:"):
|
||||||
|
return f'''{obj[4:]}'''
|
||||||
|
|
||||||
|
if "\\" in obj or obj.startswith("re:"):
|
||||||
|
prefix = "r"
|
||||||
|
else:
|
||||||
|
prefix = ""
|
||||||
|
|
||||||
|
if "\n" in obj:
|
||||||
|
quote = '"""'
|
||||||
|
elif '"' in obj:
|
||||||
|
obj = re.sub(r'(?<!\\)"', '\\"', obj)
|
||||||
|
quote = '"'
|
||||||
|
else:
|
||||||
|
quote = '"'
|
||||||
|
|
||||||
|
return f'''{prefix}{quote}{obj}{quote}'''
|
||||||
|
|
||||||
|
if isinstance(obj, bytes):
|
||||||
|
return f'''b"{str(obj)[2:-1]}"'''
|
||||||
|
|
||||||
|
if isinstance(obj, type):
|
||||||
|
if obj.__module__ == "builtins":
|
||||||
|
return f'''{obj.__name__}'''
|
||||||
|
|
||||||
|
name = obj.__module__.rpartition(".")[2]
|
||||||
|
if name[0].isdecimal():
|
||||||
|
name = f"_{name}"
|
||||||
|
return f'''{name}.{obj.__name__}'''
|
||||||
|
|
||||||
|
if isinstance(obj, dict):
|
||||||
|
if not obj:
|
||||||
|
return "{}"
|
||||||
|
|
||||||
|
if len(obj) >= 2 or not indent:
|
||||||
|
ws = " " * indent
|
||||||
|
|
||||||
|
lkey = max(map(len, obj))
|
||||||
|
if not indent:
|
||||||
|
if lkey < lmin:
|
||||||
|
lkey = lmin
|
||||||
|
elif lkey > lmax:
|
||||||
|
lkey = lmax
|
||||||
|
|
||||||
|
lines = []
|
||||||
|
lines.append("{")
|
||||||
|
for key, value in obj.items():
|
||||||
|
if key.startswith("#blank-"):
|
||||||
|
lines.append("")
|
||||||
|
else:
|
||||||
|
lines.append(
|
||||||
|
f'''{ws} "{key}"'''
|
||||||
|
f'''{' '*(lkey - len(key))}: '''
|
||||||
|
f'''{pyprint(value, indent+4)},'''
|
||||||
|
)
|
||||||
|
lines.append(f'''{ws}}}''')
|
||||||
|
return "\n".join(lines)
|
||||||
|
else:
|
||||||
|
key, value = obj.popitem()
|
||||||
|
return f'''{{"{key}": {pyprint(value)}}}'''
|
||||||
|
|
||||||
|
if isinstance(obj, list):
|
||||||
|
if not obj:
|
||||||
|
return "[]"
|
||||||
|
|
||||||
|
if len(obj) >= 2:
|
||||||
|
ws = " " * indent
|
||||||
|
|
||||||
|
lines = []
|
||||||
|
lines.append("[")
|
||||||
|
lines.extend(
|
||||||
|
f'''{ws} {pyprint(value, indent+4)},'''
|
||||||
|
for value in obj
|
||||||
|
)
|
||||||
|
lines.append(f'''{ws}]''')
|
||||||
|
return "\n".join(lines)
|
||||||
|
else:
|
||||||
|
return f'''[{pyprint(obj[0])}]'''
|
||||||
|
|
||||||
|
if isinstance(obj, tuple):
|
||||||
|
if len(obj) == 1:
|
||||||
|
return f'''({pyprint(obj[0], indent+4)},)'''
|
||||||
|
return f'''({", ".join(pyprint(v, indent+4) for v in obj)})'''
|
||||||
|
|
||||||
|
else:
|
||||||
|
return f'''{obj}'''
|
Loading…
Reference in New Issue
Block a user