This repository was archived by the owner on Aug 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson_parser.py
More file actions
73 lines (61 loc) · 2.88 KB
/
json_parser.py
File metadata and controls
73 lines (61 loc) · 2.88 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# -*- coding: utf-8 -*-
"""
/***************************************************************************
DSGManagementTools
A QGIS plugin
Plugin to manage DSG's Cartographic production
-------------------
begin : 2015-07-20
git sha : $Format:%H$
copyright : (C) 2015 by Brazilian Army - Geographic Service Bureau
email : suporte.dsgtools@dsg.eb.mil.br
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
"""
import json
class JsonParser():
def __init__(self, filename):
self.filename = filename
try:
file = open(self.filename, 'r')
data = file.read()
self.parsed = json.loads(data)
except:
self.parsed = dict()
def writeCluster(self, clustername, masterconn, masterpid, slaveconn, slavepid):
self.parsed[clustername] = self.getClusterDict(masterconn, masterpid, slaveconn, slavepid)
with open(self.filename, 'w') as outfile:
json.dump(self.parsed, outfile)
def getMasterDict(self, masterconn, masterpid):
master = dict()
master['masterconn'] = masterconn
master['masterpid'] = masterpid
return master
def getSlaveDict(self, slaveconn, slavepid):
slave = dict()
slave['slaveconn'] = slaveconn
slave['slavepid'] = slavepid
return slave
def getClusterDict(self, masterconn, masterpid, slaveconn, slavepid):
cluster = dict()
cluster['master'] = self.getMasterDict(masterconn, masterpid)
cluster['slave'] = self.getSlaveDict(slaveconn, slavepid)
return cluster
def readMasterConn(self, clustername):
return self.parsed[clustername]['master']['masterconn']
def readMasterPid(self, clustername):
return self.parsed[clustername]['master']['masterpid']
def readSlaveConn(self, clustername):
return self.parsed[clustername]['slave']['slaveconn']
def readSlavePid(self, clustername):
return self.parsed[clustername]['slave']['slavepid']
if __name__=='__main__':
parser = JsonParser('/Users/luiz/Downloads/teste.json')
parser.writeCluster('cluster', 'master', '2222', 'slave', '3333')