openmedialibrary_platform/Shared/lib/python3.4/site-packages/ox/jsonc.py

34 lines
892 B
Python
Raw Normal View History

2013-10-11 17:28:32 +00:00
#!/usr/bin/python
# -*- coding: utf-8 -*-
# vi:si:et:sw=4:sts=4:ts=4
2014-09-30 16:15:32 +00:00
from __future__ import with_statement, print_function
2013-10-11 17:28:32 +00:00
2015-11-23 21:13:53 +00:00
import re
2014-09-30 16:15:32 +00:00
from .js import minify
from .utils import json
2013-10-11 17:28:32 +00:00
def load(f):
return loads(f.read())
def loads(source):
try:
minified = minify(source)
return json.loads(minified)
2015-11-23 21:13:53 +00:00
except ValueError as e:
msg = e.message if hasattr(e, 'message') else str(e)
lineno = None
colno = None
try:
m = re.search(r'line (\d+) column (\d+)', msg)
if m:
(lineno, colno) = map(int, m.groups())
except:
pass
if lineno and colno:
s = minified.split('\n')
context = s[lineno-1][max(0, colno-30):colno+30]
msg += ' at:\n\n %s\n %s\033[1m^\033[0m' %(context, ' ' * (colno - max(0, colno-30) - 2))
raise ValueError(msg)