Skip to content

Commit 6a871aa

Browse files
John-Braggrobert-closekennydanielJohn Braggzeryx
authored
Dev 315 (#94)
* INSIGHTS-12 Initial structure of insight functionality * INSIGHTS-12 Added todo statements * INSIGHTS-12 Moved Insights out of client * INSIGHTS-12 Adjusted insight methods to reside in the client class. Removed the ability to collect insights before sending, now the every time the user invokes the collectInsights method, it will also send. This prevents any State issues with the algorithm. * INSIGHTS-12 Added a todo. Tests fail for unknown reasons at this time * INSIGHTS-12 Fixed method call. Added a todo to get url from config if necessary. * INSIGHTS-12 Fixed method call. * INSIGHTS-12 added json serialization. might not be needed * INSIGHTS-12 commented test temporarily * INSIGHTS-12 comment updates and json .encode change * INSIGHTS-12 comment update * INSIGHTS-12 changed method signatures to match documentation https://insights1.enthalpy.click/developers/clients/python#publishing-algorithmia-insights * INSIGHTS-12 Added system property for queue reader url * INSIGHTS-12 Fixed URL to not be https * INSIGHTS-12 minor version update * INSIGHTS-12 revert change * INSIGHTS-12 removed todo * INSIGHTS-12 uncommented test. May start failing again in the pipeline. * INSIGHTS-12 commented test. * INSIGHTS-12 changed version. Removed unused import. * INSIGHTS-12 changed url to include /v1/ * Allow listing of non-data:// files on cli * Allow catting non-data:// files on cli * Fix tests * adding get an environment template * CICD fix * CICD fix * changing envid to correct cluster * test syntax change Co-authored-by: robert-close <[email protected]> Co-authored-by: Kenny Daniel <[email protected]> Co-authored-by: Kenny Daniel <[email protected]> Co-authored-by: John Bragg <[email protected]> Co-authored-by: James Sutton <[email protected]>
1 parent 71ad98d commit 6a871aa

File tree

5 files changed

+79
-0
lines changed

5 files changed

+79
-0
lines changed

Algorithmia/CLI.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,10 @@ def getconfigfile(self):
338338

339339
return key
340340

341+
def get_template(self,envid,dest,client):
342+
response = client.get_template(envid,dest)
343+
return response
344+
341345
def getAPIkey(self,profile):
342346
key = self.getconfigfile()
343347
config_dict = toml.load(key)

Algorithmia/__main__.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,16 @@ def main():
107107
parser_cat.add_argument('path', nargs = '*', help = 'file(s) to concatenate and print')
108108
parser_cat.add_argument('--profile', action = 'store', type = str, default = 'default')
109109

110+
#sub parser for getting environment template
111+
parser_template = subparsers.add_parser('template',help='template <envid> <dest> downloads an environment template to the destination')
112+
parser_template.add_argument('envid',help='environment specification id')
113+
parser_template.add_argument('dest',help='destination for template download')
114+
110115
#sub parser for getting environment by language name
111116
parser_env = subparsers.add_parser('environment', help = 'environment <language> gets environment info by language')
112117
parser_env.add_argument('language', help='supported language name')
113118

119+
114120
#sub parser for listing languages
115121
subparsers.add_parser('languages', help = 'lists supported languages')
116122

@@ -200,6 +206,9 @@ def main():
200206
for lang in response:
201207
print("{:<25} {:<35}".format(lang['name'],lang['display_name']))
202208

209+
elif args.cmd == 'template':
210+
CLI().get_template(args.envid,args.dest,client)
211+
203212
elif args.cmd == 'environment':
204213
response = CLI().get_environment_by_language(args.language,client)
205214
print(response)

Algorithmia/client.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from tempfile import mkstemp
1111
import atexit
1212
import json, re, requests, six, certifi
13+
import tarfile
1314
import os
1415

1516

@@ -114,6 +115,38 @@ def invite_to_org(self,orgname,username):
114115
response = self.putHelper(url,data={})
115116
return response
116117

118+
119+
def get_template(self,envid,dest,save_tar=False):
120+
url = "/v1/algorithm-environments/edge/environment-specifications/"+envid+"/template"
121+
filename="template.tar.gz"
122+
123+
if not os.path.exists(dest):
124+
os.makedirs(dest)
125+
126+
filepath = os.path.join(dest, filename)
127+
response = self.getStreamHelper(url)
128+
129+
if response.ok:
130+
with open(filepath, 'wb') as f:
131+
for chunk in response.iter_content(chunk_size=1024 * 8):
132+
if chunk:
133+
f.write(chunk)
134+
f.flush()
135+
os.fsync(f.fileno())
136+
137+
tar = tarfile.open(filepath, "r:gz")
138+
tar.extractall(dest)
139+
tar.close()
140+
141+
if not save_tar:
142+
try:
143+
os.remove(filepath)
144+
except OSError as e:
145+
print(e)
146+
return response
147+
else:
148+
return json.loads(response.content.decode("utf-8"))
149+
117150
def get_environment(self,language):
118151
url = "/v1/algorithm-environments/edge/languages/"+language+"/environments"
119152
response = self.getHelper(url)
@@ -125,6 +158,7 @@ def get_supported_languages(self):
125158
return response.json()
126159

127160

161+
128162
# Used to send insight data to Algorithm Queue Reader in cluster
129163
def report_insights(self, insights):
130164
return Insights(insights)
@@ -163,6 +197,12 @@ def getHelper(self, url, **query_parameters):
163197
headers['Authorization'] = self.apiKey
164198
return self.requestSession.get(self.apiAddress + url, headers=headers, params=query_parameters)
165199

200+
def getStreamHelper(self, url, **query_parameters):
201+
headers = {}
202+
if self.apiKey is not None:
203+
headers['Authorization'] = self.apiKey
204+
return self.requestSession.get(self.apiAddress + url, headers=headers, params=query_parameters, stream=True)
205+
166206
def patchHelper(self, url, params):
167207
headers = {'content-type': 'application/json'}
168208
if self.apiKey is not None:

Test/CLI_test.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import Algorithmia
1010
from Algorithmia.CLI import CLI
1111
import argparse
12+
import shutil
1213

1314
class CLITest(unittest.TestCase):
1415
def setUp(self):
@@ -204,6 +205,18 @@ def test_rm(self):
204205

205206
self.assertTrue("testRM.txt" in result1 and "testRM.txt" not in result2)
206207

208+
def test_get_template(self):
209+
filename = "./temptest"
210+
envid = "36fd467e-fbfe-4ea6-aa66-df3f403b7132"
211+
response = CLI().get_template(envid,filename,self.client)
212+
print(response)
213+
self.assertTrue(response.ok)
214+
try:
215+
shutil.rmtree(filename)
216+
except OSError as e:
217+
print(e)
218+
219+
207220

208221
if __name__ == '__main__':
209222
unittest.main()

Test/client_test.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from datetime import datetime, time
2+
import shutil
23
import sys
34
import os
45
from random import seed
@@ -74,6 +75,17 @@ def test_edit_org(self):
7475
response = self.c.edit_org(orgname,obj)
7576
self.assertEqual(204,response.status_code)
7677

78+
def test_get_template(self):
79+
filename = "./temptest"
80+
client =Algorithmia.client(api_key=os.environ.get('ALGORITHMIA_API_KEY'))
81+
response = client.get_template("36fd467e-fbfe-4ea6-aa66-df3f403b7132",filename)
82+
print(response)
83+
self.assertTrue(response.ok)
84+
try:
85+
shutil.rmtree(filename)
86+
except OSError as e:
87+
print(e)
88+
7789
def test_get_supported_languages(self):
7890
client = Algorithmia.client(api_key=os.environ.get('ALGORITHMIA_API_KEY'))
7991
response = client.get_supported_languages()
@@ -82,6 +94,7 @@ def test_get_supported_languages(self):
8294
print(response)
8395
self.assertTrue(response is not None and language_found)
8496

97+
8598
def test_invite_to_org(self):
8699
response = self.c.invite_to_org("a_myOrg38","a_Mrtest4")
87100
self.assertEqual(200,response.status_code)

0 commit comments

Comments
 (0)