Move advanced json formatter to helper.py

This commit is contained in:
shortcutme 2019-08-02 14:05:14 +02:00
parent 06406fa46c
commit 5e90cd9714
No known key found for this signature in database
GPG key ID: 5B63BAE6CB9613AE
2 changed files with 29 additions and 26 deletions

View file

@ -326,35 +326,10 @@ class SiteStorage(object):
with self.open(inner_path, "r", encoding="utf8") as file:
return json.load(file)
def formatJson(self, data):
content = json.dumps(data, indent=1, sort_keys=True)
# Make it a little more compact by removing unnecessary white space
def compact_dict(match):
if "\n" in match.group(0):
return match.group(0).replace(match.group(1), match.group(1).strip())
else:
return match.group(0)
content = re.sub("\{(\n[^,\[\{]{10,100}?)\}[, ]{0,2}\n", compact_dict, content, flags=re.DOTALL)
def compact_list(match):
if "\n" in match.group(0):
stripped_lines = re.sub("\n[ ]*", "", match.group(1))
return match.group(0).replace(match.group(1), stripped_lines)
else:
return match.group(0)
content = re.sub("\[([^\[\{]{2,300}?)\][, ]{0,2}\n", compact_list, content, flags=re.DOTALL)
# Remove end of line whitespace
content = re.sub("(?m)[ ]+$", "", content)
return content
# Write formatted json file
def writeJson(self, inner_path, data):
# Write to disk
self.write(inner_path, self.formatJson(data).encode("utf8"))
self.write(inner_path, helper.jsonDumps(data).encode("utf8"))
# Get file size
def getSize(self, inner_path):

View file

@ -7,6 +7,8 @@ import collections
import time
import logging
import base64
import json
import gevent
from Config import config
@ -37,6 +39,32 @@ def atomicWrite(dest, content, mode="wb"):
return False
def jsonDumps(data):
content = json.dumps(data, indent=1, sort_keys=True)
# Make it a little more compact by removing unnecessary white space
def compact_dict(match):
if "\n" in match.group(0):
return match.group(0).replace(match.group(1), match.group(1).strip())
else:
return match.group(0)
content = re.sub(r"\{(\n[^,\[\{]{10,100}?)\}[, ]{0,2}\n", compact_dict, content, flags=re.DOTALL)
def compact_list(match):
if "\n" in match.group(0):
stripped_lines = re.sub("\n[ ]*", "", match.group(1))
return match.group(0).replace(match.group(1), stripped_lines)
else:
return match.group(0)
content = re.sub(r"\[([^\[\{]{2,300}?)\][, ]{0,2}\n", compact_list, content, flags=re.DOTALL)
# Remove end of line whitespace
content = re.sub(r"(?m)[ ]+$", "", content)
return content
def openLocked(path, mode="wb"):
try:
if os.name == "posix":