aboutsummaryrefslogtreecommitdiff
path: root/resumejson_converter/utils/json.py
blob: 77e1312072c93535b8563c7c3a16ec01e3e2f71e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import sys
import logging
import json

from jsonresume import Resume
from jsonresume.exceptions import InvalidResumeError


class JsonObject(object):
    """
    Simple JSON object.

    All work for this function go to
    https://dev.to/mandrewcito/nested-json-to-python-object--5ajp
    """
    def __init__(self, json):
        jsonDict = dict(json)

        for key, val in jsonDict.items():
            setattr(self, key, self.compute_attr_value(val))

    def compute_attr_value(self, value):
        if type(value) is list:
            return [self.compute_attr_value(x) for x in value]
        elif type(value) is dict:
            return JsonObject(value)
        else:
            return value


def load(json_filepath):
    """
    Return a JsonObject from resumejson_converter.utils.json.
    """
    try:
        logging.info("JSON parsing...")
        with open(json_filepath, 'r') as f:
            resume_json = json.load(f)
    except IOError:
        msg = "Json file could not be loaded. Perhaps file path: \n\
      [{}] is incorrect".format(json_filepath)
        logging.exception(msg)
        sys.exit(1)
    except ValueError:
        logging.exception(
            "Json file could not be loaded. The syntax is not valid.")
        sys.exit(1)
    else:
        resume = Resume(resume_json)
        try:
            resume.validate()
        except InvalidResumeError:
            msg = "The json resume don't respect standard. Check: \n\
      https://jsonresume.org/schema/."
            logging.exception(msg)
            sys.exit(1)
        else:
            logging.info("JSON parsing done.")
            return JsonObject(resume_json)