Skip to content

Commit 591e999

Browse files
committed
formatted document
1 parent 464f3b4 commit 591e999

File tree

1 file changed

+56
-47
lines changed

1 file changed

+56
-47
lines changed

update_items.py

Lines changed: 56 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,14 @@
3838
{'notebookRuntimeName': 'ArcGIS Notebook Python 3 Advanced',
3939
'notebookRuntimeVersion': '5.0'}
4040
NB_ITEM_PROPERTIES_RUNTIME_STAMP_ADVANCED_GPU = \
41-
{'notebookRuntimeName': 'ArcGIS Notebook Python 3 Advanced with GPU support',
42-
'notebookRuntimeVersion': '5.0'}
41+
{'notebookRuntimeName': 'ArcGIS Notebook Python 3 Advanced with GPU support',
42+
'notebookRuntimeVersion': '5.0'}
4343
NB_ITEM_PROPERTIES_RUNTIME_STAMP_STANDARD = \
4444
{'notebookRuntimeName': 'ArcGIS Notebook Python 3 Standard',
4545
'notebookRuntimeVersion': '5.0'}
4646
NB_ITEM_FOLDER = "Notebook Samples"
4747

48+
4849
def _main():
4950
"""Parses arguments, connects to GIS, reads YAML, uploads NBs"""
5051
args = _parse_cmd_line_args()
@@ -59,95 +60,101 @@ def _main():
5960
if s.failed_uploads:
6061
raise Exception(f"Some uploads failed: {s.failed_uploads}")
6162

63+
6264
def _parse_cmd_line_args():
6365
"""Parse CMD args, returns an object instance of all user passed in args"""
64-
parser = argparse.ArgumentParser(description = "Takes all notebooks "\
65-
"this in `gallery` directory, and will upload it to the specified "\
66-
"portal/org in the right group with the right categories. "\
67-
"(default is geosaurus.maps.arcgis.com, 'Esri Sample Notebooks' group)",
68-
formatter_class=argparse.RawTextHelpFormatter)
66+
parser = argparse.ArgumentParser(description="Takes all notebooks "
67+
"this in `gallery` directory, and will upload it to the specified "
68+
"portal/org in the right group with the right categories. "
69+
"(default is geosaurus.maps.arcgis.com, 'Esri Sample Notebooks' group)",
70+
formatter_class=argparse.RawTextHelpFormatter)
6971
parser.add_argument("--username", "-u", type=str,
70-
help="Required username for the portal/org")
72+
help="Required username for the portal/org")
7173
parser.add_argument("--password", "-p", type=str,
72-
help="Required password for the portal/org")
74+
help="Required password for the portal/org")
7375
parser.add_argument("--portal-url", "-r", type=str,
74-
help="The portal to connect to (Default:geosaurus.maps.arcgis.com)",
75-
default="https://geosaurus.maps.arcgis.com/")
76+
help="The portal to connect to (Default:geosaurus.maps.arcgis.com)",
77+
default="https://geosaurus.maps.arcgis.com/")
7678
parser.add_argument("--verbose", "-v", action="store_true",
77-
help="Print all DEBUG log messages instead of just INFO")
79+
help="Print all DEBUG log messages instead of just INFO")
7880
parser.add_argument("--replace-profiles", "-c", action="store_true",
79-
help="Replace all profiles in notebooks with their appropriate username "\
80-
"and passwords. Does this by running misc/tools/replace_profiles.py")
81-
args = parser.parse_args(sys.argv[1:]) #don't use filename as 1st arg
81+
help="Replace all profiles in notebooks with their appropriate username "
82+
"and passwords. Does this by running misc/tools/replace_profiles.py")
83+
args = parser.parse_args(sys.argv[1:]) # don't use filename as 1st arg
8284
return args
8385

86+
8487
def _setup_logging(args):
8588
"""Sets up the logging based on args"""
8689
if args.verbose:
8790
log.setLevel(logging.DEBUG)
8891
else:
89-
log.setLevel(logging.INFO)
92+
log.setLevel(logging.INFO)
9093
stdout_handler = logging.StreamHandler(stream=sys.stdout)
9194
stdout_handler.setLevel(logging.DEBUG)
9295
stdout_handler.setFormatter(logging.Formatter(
93-
'----- %(levelname)s | '\
94-
'%(asctime)s | '\
95-
'%(filename)s line %(lineno)d'\
96-
' -----\n'\
96+
'----- %(levelname)s | '
97+
'%(asctime)s | '
98+
'%(filename)s line %(lineno)d'
99+
' -----\n'
97100
'"%(message)s"'))
98101
log.addHandler(stdout_handler)
99102
log.info("Logging at level {}.".format(logging.getLevelName(log.level)))
100103
log.debug("args passed in => {}".format(args))
101104

105+
102106
def _read_items_metadata_yaml():
103107
"""Returns the items_metadata.yaml file as a dict"""
104108
with open(ITEMS_METADATA_YAML_PATH) as f:
105109
return yaml.safe_load(f)
106110

111+
107112
def _replace_profiles():
108113
"""Runs misc/tools/replace_profiles.py to go through each notebook in the
109114
repo and replace profiles with usernames/passwords
110115
"""
111116
cmd = f"{sys.executable} {REPLACE_PROFILES_SCRIPT}"
112117
os.system(cmd)
113118

119+
114120
class ItemsUploader:
115121
def __init__(self, gis, items_metadata_yaml):
116122
self._gis = gis
117123
self._items_metadata_yaml = items_metadata_yaml
118124
self.failed_uploads = []
119125

120-
def upload_items(self, share_after_upload = True):
126+
def upload_items(self, share_after_upload=True):
121127
for entry in self._items_metadata_yaml["samples"] + \
122-
self._items_metadata_yaml["guides"] + \
123-
self._items_metadata_yaml["labs"]:
128+
self._items_metadata_yaml["guides"] + \
129+
self._items_metadata_yaml["labs"]:
124130
self._stage_and_upload_item(entry, share_after_upload)
125131

126-
def _stage_and_upload_item(self, entry, share_after_upload = True):
132+
def _stage_and_upload_item(self, entry, share_after_upload=True):
127133
log.info(f"Uploading {entry['title']}")
128134
log.debug(f" sample: {entry}")
129135
try:
130136
nb_path = entry["path"]
131137
self._preupload_check(entry['title'], nb_path)
132-
runtime_stamp = self._infer_runtime_stamp(entry.get("runtime", "standard"))
138+
runtime_stamp = self._infer_runtime_stamp(
139+
entry.get("runtime", "standard"))
133140
categories = entry.get("categories", None)
134141
self._stamp_file_with_runtime(nb_path, runtime_stamp)
135142
item_id = self._infer_item_id(entry["url"])
136143
item = self.update_item(
137-
item_id = item_id,
138-
item_type = NB_PORTAL_TYPE,
139-
item_type_keywords = NB_PORTAL_TYPE_KEYWORDS,
140-
title = entry['title'],
141-
categories = categories,
142-
snippet = entry['snippet'],
143-
description = entry['description'],
144-
license_info = entry['licenseInfo'],
145-
tags = entry['tags'],
146-
nb_path = nb_path,
147-
runtime_stamp = runtime_stamp,
148-
thumbnail = entry['thumbnail'])
144+
item_id=item_id,
145+
item_type=NB_PORTAL_TYPE,
146+
item_type_keywords=NB_PORTAL_TYPE_KEYWORDS,
147+
title=entry['title'],
148+
categories=categories,
149+
snippet=entry['snippet'],
150+
description=entry['description'],
151+
license_info=entry['licenseInfo'],
152+
tags=entry['tags'],
153+
nb_path=nb_path,
154+
runtime_stamp=runtime_stamp,
155+
thumbnail=entry['thumbnail'])
149156
if share_after_upload:
150-
item.share(everyone = True)
157+
item.share(everyone=True)
151158
item.protect()
152159
if categories:
153160
self._assign_categories_to_item(item, categories)
@@ -171,11 +178,11 @@ def update_item(self, item_id, item_type, item_type_keywords, title, categories,
171178
snippet, description, license_info, tags, nb_path,
172179
runtime_stamp, thumbnail):
173180
"""Actually uploads the notebook item to the portal"""
174-
item_properties = {"title" : title,
175-
"snippet" : snippet,
176-
"description" : description,
177-
"licenseInfo" : license_info,
178-
"tags" : tags,
181+
item_properties = {"title": title,
182+
"snippet": snippet,
183+
"description": description,
184+
"licenseInfo": license_info,
185+
"tags": tags,
179186
"properties": runtime_stamp}
180187
if categories:
181188
item_properties["categories"] = categories
@@ -189,11 +196,12 @@ def update_item(self, item_id, item_type, item_type_keywords, title, categories,
189196
log.debug(f'item {existing_item.homepage} exists, updating...')
190197
item_properties["url"] = existing_item.homepage
191198
existing_item.update(item_properties,
192-
data = nb_path,
193-
thumbnail = thumbnail)
199+
data=nb_path,
200+
thumbnail=thumbnail)
194201
resp = existing_item
195202
else:
196-
raise Exception(f"Could not find item {item_id} to update. Failing!")
203+
raise Exception(
204+
f"Could not find item {item_id} to update. Failing!")
197205
return resp
198206

199207
def _assign_categories_to_item(self, item, categories):
@@ -209,7 +217,7 @@ def _apply_html_preview_to_item(self, item, nb_path):
209217
json_file_name = "notebook_preview.json"
210218
json_file_path = os.path.join(".", json_file_name)
211219
with open(json_file_path, 'w') as f:
212-
json.dump({"html" : html_str}, f)
220+
json.dump({"html": html_str}, f)
213221

214222
if item.resources.list():
215223
item.resources.remove()
@@ -240,6 +248,7 @@ def _stamp_file_with_runtime(self, notebook_file_path, runtime_stamp):
240248
nb['metadata']['esriNotebookRuntime'] = runtime_stamp
241249
nbformat.write(nb, notebook_file_path, nbformat.NO_CONVERT)
242250

251+
243252
if __name__ == "__main__":
244253
try:
245254
_main()

0 commit comments

Comments
 (0)