I am trying to write the python code using the single responsibility principle, and I only wanted to edit the inputs in the config.json. In the json_writer.py the output file name has to be changed if I change the URL in the config.json. So, how can I add the print file name in the existing config.json so that I don't have to change anything in the code?
**#config.json**
{
"url": "https://arcgis/portal/",
"username": "username",
"password":"password",
"query": "",
"item_type": "Feature Layer"
}
**#json_reader.py**
import json
class JsonReader:
def__init__(self, filename):
self.filename = filename
def read_json_file(self):
withopen(self.filename) asfile:
data = json.load(file)
return data
def get_config(self, config):
loaded_json = self.read_json_file()
config = loaded_json[config]
return config
**#portal_connection.py**
from arcgis.gis import GIS
class PortalConnection:
def__init__(self, url, username, password):
self.url = url
self.username = username
self.password = password
def connect(self, query, layer):
# Connection to ArcGIS Enterprise using a built-in account
print("Portal for ArcGIS as a built in user")
gis_portal = GIS(self.url, self.username, self.password)
print("Logged in as: " + gis_portal.properties.user.username)
return gis_portal, query, layer
**#esri_api.py**
class EsriApi:
def__init__(self, portal, item_type, query):
self.item_type = item_type
self.query_ = query
self.portal = portal
def query(self):
api_query_result = self.portal.content.search(query=self.query_, item_type=self.item_type)
return api_query_result
**#json_writer.py**
class JsonWriter:
def __init__(self,api_query_result):
self.api_query_result = api_query_result
def printResults(self):
l = []
for service in self.api_query_result:
if "Hosted" in service.url:
l.append(str((service.url, service.id, service.owner)))
with open('Output_env_Q_Hosted.txt', 'w') as f:
for line in l:
#print(line)
f.write(line)
f.write('\n')
print("saved successfully!!!")
file = open("Output_env_Q_Hosted.txt", "r")
for i in file.readlines():
print(i)
**#main.py**
from json_reader import JsonReader
from portal_connection import PortalConnection
from esri_api import EsriApi
from json_writer import JsonWriter
jreader = JsonReader('config.json')
url = jreader.get_config("url")
username = jreader.get_config("username")
password = jreader.get_config("password")
query = jreader.get_config("query")
layer = jreader.get_config("item_type")
con = PortalConnection(url, username, password)
portal, query, layer = con.connect(query, layer)
esri_api = EsriApi(portal, layer, query)
results = esri_api.query()
jsonWriter = JsonWriter(results)
jsonWriter.printResults ()
You can not run code from a JSON file, so you will have to change the code. JSON is only for storing data, and it is not a programming language.
If you want to set the filename at runtime, you can use something like this:
filename = 'SomeFileName.txt' # set this with whatever code you like
with open(filename, 'w') as f:
for line in l:
f.write(line)
f.write('\n')
print("saved successfully!!!")
file = open(filename, "r")
for i in file.readlines():
print(i)