Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1# -*- coding: utf-8 -*- 

2# 

3# Copyright (c) 2017, the cclib development team 

4# 

5# This file is part of cclib (http://cclib.github.io) and is distributed under 

6# the terms of the BSD 3-Clause License. 

7 

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

9 

10import json 

11 

12from cclib.io import filereader 

13from cclib.parser.data import ccData 

14 

15 

16class CJSON(filereader.Reader): 

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

18 

19 def __init__(self, source, *args, **kwargs): 

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

21 

22 self.representation = dict() 

23 

24 def parse(self): 

25 super(CJSON, self).parse() 

26 

27 json_data = json.loads(self.filecontents) 

28 

29 self.generate_repr(json_data) 

30 

31 return self.representation 

32 

33 def generate_repr(self, json_data): 

34 for k, v in ccData._attributes.items(): 

35 json_key = v.json_key 

36 attribute_path = v.attribute_path.split(":") 

37 

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

39 continue 

40 

41 levels = len(attribute_path) 

42 if attribute_path[0] in json_data: 

43 l1_data_object = json_data[attribute_path[0]] 

44 if levels == 1: 

45 if json_key in l1_data_object: 

46 self.representation[k] = l1_data_object[json_key] 

47 

48 elif levels >= 2: 

49 if attribute_path[1] in l1_data_object: 

50 l2_data_object = l1_data_object[attribute_path[1]] 

51 if json_key in l2_data_object: 

52 self.representation[k] = l2_data_object[json_key] 

53 

54 if levels == 3 and attribute_path[2] in l2_data_object: 

55 l3_data_object = l2_data_object[attribute_path[2]] 

56 if json_key in l3_data_object: 

57 self.representation[k] = l3_data_object[json_key]