Skip to content

Commit c7c5444

Browse files
committed
removed missing variable; update module imports; use formatted string literals
1 parent 10ffdca commit c7c5444

File tree

1 file changed

+18
-19
lines changed

1 file changed

+18
-19
lines changed

overwrite_service_to_agol.py

+18-19
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,18 @@
1616
#
1717
# Created: 9/11/2020
1818
#
19-
# Updated: 12/23/2021
19+
# Updated: 9/21/2022
2020
#
2121
# Author: Patrick McKinney
2222
# --------------------------------------------------------------------------------------------------------------------------------------------
2323

2424
# import modules
2525
import arcpy
26-
import os
2726
import sys
28-
import datetime
2927
import time
28+
from datetime import date
29+
from os import path
30+
from os import environ
3031
from arcgis.gis import GIS
3132

3233
# attempt to run code; if an error occurs, error messages will be logged in a text file
@@ -35,15 +36,14 @@
3536
start_time = time.perf_counter()
3637

3738
# Date the script is being run
38-
date_today = datetime.date.today()
39+
date_today = date.today()
3940
# Date formatted as month-day-year (1-1-2020)
4041
formatted_date_today = date_today.strftime("%m-%d-%Y")
4142

4243
# variable to store messages for log file. Messages written in finally statement at end of script
4344
log_message = ''
4445
# Create text file for logging messages of script progress and/or errors
45-
log_file = r'Path\To\Directory\Report File {}.txt'.format(
46-
formatted_date_today)
46+
log_file = path.join(r'Path\To\Directory', f'Report File {formatted_date_today}.txt')
4747

4848
# Set the path to the ArcGIS Pro project
4949
# this project contains the local dataset the feature service is being updated for
@@ -57,10 +57,10 @@
5757
portal = ""
5858
# user name of owner of item (admin users may be able to overwrite)
5959
# create environment variable to store username; pass that variable name into get() method
60-
user = os.environ.get('user_name_environment_variable')
60+
user = environ.get('user_name_environment_variable')
6161
# password of owner of item (admin users may be able to overwrite)
6262
# create environment variable to store pasword; pass that variable name into get() method
63-
password = os.environ.get('password_environment_variable')
63+
password = environ.get('password_environment_variable')
6464

6565
# sign-in to ArcGIS Online or Portal within ArcGIS Pro project
6666
arcpy.SignInToPortal(portal, user, password)
@@ -74,13 +74,13 @@
7474
shrGroups = "" # name of group(s), i.e, 'Public Safety'
7575

7676
# Local paths to create temporary content
77-
relPath = os.path.dirname(projPath)
77+
relPath = path.dirname(projPath)
7878
# service definition draft file
79-
sddraft = os.path.join(relPath, "WebUpdate.sddraft")
79+
sddraft = path.join(relPath, "WebUpdate.sddraft")
8080
# service defition
8181
# this is what is being overwritten to ArcGIS Online
8282
# this is how you push your new data to ArcGIS Online from the local source
83-
sd = os.path.join(relPath, "WebUpdate.sd")
83+
sd = path.join(relPath, "WebUpdate.sd")
8484

8585
# Create a new SDDraft and stage to SD
8686
log_message += "Creating Service Defintion file\n"
@@ -102,7 +102,7 @@
102102
arcpy.StageService_server(sddraft, sd)
103103

104104
# add message
105-
log_message += "\nConnecting to {}\n".format(portal)
105+
log_message += f"\nConnecting to {portal}\n"
106106
# Connect to ArcGIS Online or Portal
107107
# may need to add 'verify_cert=False' argument at end of function call
108108
gis = GIS(portal, user, password)
@@ -114,14 +114,14 @@
114114
sdItem = gis.content.get(sd_id)
115115

116116
# add message
117-
log_message += "\nFound SD: {}, ID: {}\n".format(sdItem.title, sdItem.id)
117+
log_message += f"\nFound SD: {sdItem.title}, ID: {sdItem.id}\n"
118118
# update data for service definition ArcGIS Online/Portal item using the service definition file created in ArcGIS Pro
119119
sdItem.update(data=sd)
120120

121121
# add message
122122
log_message += "\nOverwriting existing feature service\n"
123123
# overwrite feature service so it will now reference updated data pushed from a local source
124-
fs = sdItem.publish(publish_parameters=pub_params, overwrite=True)
124+
fs = sdItem.publish(overwrite=True)
125125

126126
# set sharing options for feature service
127127
if shrOrg or shrEveryone or shrGroups:
@@ -139,19 +139,18 @@
139139
elapsed_time_minutes = round((elapsed_time / 60), 2)
140140

141141
# add message
142-
log_message += "\nOverwrote 'Some Dataset' feature service to ArcGIS Online in {}-minutes on {}\n".format(
143-
elapsed_time_minutes, formatted_date_today)
142+
log_message += f"\nOverwrote 'Some Dataset' feature service to ArcGIS Online in {elapsed_time_minutes}-minutes on {formatted_date_today}\n"
144143
# If an error occurs running geoprocessing tool(s) capture error and write message
145144
except (Exception, EnvironmentError) as e:
146145
tbE = sys.exc_info()[2]
147146
# Write the line number the error occured to the log file
148-
log_message += "\nFailed at Line {}\n".format(tbE.tb_lineno)
147+
log_message += f"\nFailed at Line {tbE.tb_lineno}\n"
149148
# Write the error message to the log file
150-
log_message += "Error: {}".format(str(e))
149+
log_message += f"Error: {str(e)}"
151150
finally:
152151
# write message to log file
153152
try:
154153
with open(log_file, 'w') as f:
155154
f.write(str(log_message))
156155
except:
157-
pass
156+
pass

0 commit comments

Comments
 (0)