Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When JSON parsing fails, give a rough indication of why #3137

Merged
merged 2 commits into from
Aug 8, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions pokecli.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@
from pokemongo_bot.health_record import BotEvent
from pokemongo_bot.plugin_loader import PluginLoader

try:
from demjson import jsonlint
except ImportError:
# Run `pip install -r requirements.txt` to fix this
jsonlint = None

if sys.version_info >= (2, 7, 9):
ssl._create_default_https_context = ssl._create_unverified_context

Expand Down Expand Up @@ -162,16 +168,28 @@ def init_config():
# If config file exists, load variables from json
load = {}

def _json_loader(filename):
try:
with open(filename, 'rb') as data:
load.update(json.load(data))
except ValueError:
if jsonlint:
with open(filename, 'rb') as data:
lint = jsonlint()
rc = lint.main(['-v', filename])

logger.critical('Error with configuration file')
sys.exit(-1)

# Select a config file code
parser.add_argument("-cf", "--config", help="Config File to use")
config_arg = parser.parse_known_args() and parser.parse_known_args()[0].config or None

if config_arg and os.path.isfile(config_arg):
with open(config_arg) as data:
load.update(json.load(data))
_json_loader(config_arg)
elif os.path.isfile(config_file):
logger.info('No config argument specified, checking for /configs/config.json')
with open(config_file) as data:
load.update(json.load(data))
_json_loader(config_file)
else:
logger.info('Error: No /configs/config.json or specified config')

Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ gpxpy==1.1.1
mock==2.0.0
timeout-decorator==0.3.2
raven==5.23.0
-e git+https://github.com/dmeranda/demjson.git@5bc65974e7141746acc88c581f5d2dfb8ea14064#egg=demjson
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use official package instead of grab from git?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the feedback, I shall update this momentarily.