Move advanced json formatter to helper.py
This commit is contained in:
parent
06406fa46c
commit
5e90cd9714
2 changed files with 29 additions and 26 deletions
|
@ -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):
|
||||
|
|
|
@ -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":
|
||||
|
|
Loading…
Reference in a new issue