Skip to content

Commit

Permalink
more progress
Browse files Browse the repository at this point in the history
  • Loading branch information
gillins committed Jul 24, 2024
1 parent 72f8ada commit 59457ec
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 14 deletions.
3 changes: 2 additions & 1 deletion pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,8 @@ disable=raw-checker-failed,
fixme,
consider-using-get,
trailing-newlines,
consider-using-with
consider-using-with,
too-many-arguments

# Enable the message, report, category or checker with the given id(s). You can
# either give multiple identifier separated by comma (,) or put this option
Expand Down
6 changes: 3 additions & 3 deletions tuiview/geolinkedviewers.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
from . import viewerwindow
from . import pluginmanager
from .querywindow import QueryDockWidget
from .viewerlayers import ViewerQueryPointLayer, ViewerFeatureVectorLayer
from . import viewerwidget


class GeolinkedViewers(QObject):
Expand Down Expand Up @@ -323,7 +325,6 @@ def writeViewersState(self, fileobj):
Gets the state of all the viewers (location, layers etc) as a json encoded
string and write it to fileobj
"""
from .viewerlayers import ViewerQueryPointLayer, ViewerFeatureVectorLayer
viewers = self.getViewerList()

# see if we can get a GeolinkInfo
Expand Down Expand Up @@ -382,7 +383,6 @@ def readViewersState(self, fileobj):
"""
Reads viewer state from the fileobj and restores viewers
"""
from . import viewerwidget
headerDict = json.loads(fileobj.readline())
if 'name' not in headerDict or headerDict['name'] != 'tuiview':
raise ValueError('File not written by tuiview')
Expand All @@ -403,7 +403,7 @@ def readViewersState(self, fileobj):
query_easting_northing = (None, None)
query_viewer = None # so we can grab the last one

for n in range(headerDict['nviewers']):
for _ in range(headerDict['nviewers']):
viewer = self.onNewWindow()
viewerDict = json.loads(fileobj.readline())

Expand Down
26 changes: 16 additions & 10 deletions tuiview/minify_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
'''

import re
import json # requires Python 2.6+ to run tests


def json_minify(json, strip_space=True):
def json_minify(jsons, strip_space=True):
tokenizer = re.compile(r'"|(/\*)|(\*/)|(//)|\n|\r')
in_string = False
in_multiline_comment = False
Expand All @@ -20,18 +21,18 @@ def json_minify(json, strip_space=True):
new_str = []
from_index = 0 # from is a keyword in Python

for match in re.finditer(tokenizer, json):
for match in re.finditer(tokenizer, jsons):

if not in_multiline_comment and not in_singleline_comment:
tmp2 = json[from_index:match.start()]
tmp2 = jsons[from_index:match.start()]
if not in_string and strip_space:
tmp2 = re.sub('[ \t\n\r]*', '', tmp2) # replace only white space defined in standard
new_str.append(tmp2)

from_index = match.end()

if match.group() == '"' and not in_multiline_comment and not in_singleline_comment:
escaped = re.search('(\\\\)*$', json[:match.start()])
escaped = re.search('(\\\\)*$', jsons[:match.start()])
if not in_string or escaped is None or len(escaped.group()) % 2 == 0:
# start of string with ", or unescaped " character found to end string
in_string = not in_string
Expand All @@ -49,15 +50,15 @@ def json_minify(json, strip_space=True):
match.group() not in ['\n', '\r', ' ', '\t'] or not strip_space):
new_str.append(match.group())

new_str.append(json[from_index:])
new_str.append(jsons[from_index:])
return ''.join(new_str)


if __name__ == '__main__':
import json # requires Python 2.6+ to run tests
def test_json(s):
return json.loads(json_minify(s))
def test_json(s):
return json.loads(json_minify(s))


def main():

test1 = '''// this is a JSON file with comments
{
Expand Down Expand Up @@ -112,3 +113,8 @@ def test_json(s):
assert test_json(test4) == json.loads(test4_res), 'Failed test 4'
if __debug__: # Don't print passed message if the asserts didn't run
print('Passed all tests')


if __name__ == '__main__':
main()

0 comments on commit 59457ec

Please sign in to comment.