File: cjsonreader.py

package info (click to toggle)
cclib-data 1.6.2-2
  • links: PTS, VCS
  • area: non-free
  • in suites: bookworm, bullseye, sid
  • size: 87,912 kB
  • sloc: python: 16,440; sh: 131; makefile: 79; cpp: 31
file content (57 lines) | stat: -rw-r--r-- 1,912 bytes parent folder | download | duplicates (2)
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
# -*- coding: utf-8 -*-
#
# Copyright (c) 2017, the cclib development team
#
# This file is part of cclib (http://cclib.github.io) and is distributed under
# the terms of the BSD 3-Clause License.

"""A reader for chemical JSON (CJSON) files."""

import json

from cclib.io import filereader
from cclib.parser.data import ccData


class CJSON(filereader.Reader):
    """A reader for chemical JSON (CJSON) log files."""

    def __init__(self, source, *args, **kwargs):
        super(CJSON, self).__init__(source, *args, **kwargs)

        self.representation = dict()

    def parse(self):
        super(CJSON, self).parse()

        json_data = json.loads(self.filecontents)

        self.generate_repr(json_data)

        return self.representation

    def generate_repr(self, json_data):
        for k, v in ccData._attributes.items():
            json_key = v.json_key
            attribute_path = v.attribute_path.split(":")

            if attribute_path[0] == 'N/A':
                continue

            levels = len(attribute_path)
            if attribute_path[0] in json_data:
                l1_data_object = json_data[attribute_path[0]]
                if levels == 1:
                    if json_key in l1_data_object:
                        self.representation[k] = l1_data_object[json_key]

                elif levels >= 2:
                    if attribute_path[1] in l1_data_object:
                        l2_data_object = l1_data_object[attribute_path[1]]
                        if json_key in l2_data_object:
                            self.representation[k] = l2_data_object[json_key]

                        if levels == 3 and attribute_path[2] in l2_data_object:
                            l3_data_object = l2_data_object[attribute_path[2]]
                            if json_key in l3_data_object:
                                self.representation[k] = l3_data_object[json_key]