Skip to content

Commit

Permalink
read_json function in util.py
Browse files Browse the repository at this point in the history
  • Loading branch information
AHReccese committed Aug 5, 2024
1 parent 3b7569c commit 041f5ca
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions reserver/util.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
# -*- coding: utf-8 -*-
"""utility module."""
from inspect import signature
import os
import json
import shutil

from inspect import signature
from .reserver_errors import ReserverBaseError
from .reserver_param import INVALID_CONFIG_FILE_NAME_ERROR, PARAM_FILE_DOES_NOT_EXIST_ERROR

def has_named_parameter(func, param_name):
"""
Expand Down Expand Up @@ -32,3 +34,22 @@ def remove_dir(dirpath):
"""
if os.path.exists(dirpath) and os.path.isdir(dirpath):
shutil.rmtree(dirpath)


def read_json(file_name):
"""
Read the json file and return the python obj of it.
:param file_name: name of the .json file
:type file_name: str
:return: obj
"""
if not isinstance(file_name, str):
raise ReserverBaseError(INVALID_CONFIG_FILE_NAME_ERROR)
if ".json" not in file_name:
file_name = file_name + ".json"
if os.path.isfile(file_name):
config_file = open(file_name)
return json.load(config_file)
else:
raise ReserverBaseError(PARAM_FILE_DOES_NOT_EXIST_ERROR)

0 comments on commit 041f5ca

Please sign in to comment.