Skip to content

Commit 54226d5

Browse files
committed
minor fixes.
1 parent fb554ed commit 54226d5

8 files changed

Lines changed: 135 additions & 86 deletions

File tree

src/python_activator/api.py

Lines changed: 47 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,17 @@
1111
import typer
1212
from python_activator.manifest import *
1313

14+
1415
class knowledge_object:
15-
def __init__(self, name, status, function ,id):
16-
self.id= id
16+
def __init__(self, name, status, function, id):
17+
self.id = id
1718
self.name = name
1819
self.status = status
1920
self.function = function
2021

21-
class CustomEncoder(json.JSONEncoder):
22-
def default(self, obj):
23-
if isinstance(obj, knowledge_object):
24-
return {'name': obj.name, 'status': obj.status, 'id': obj.id}
25-
return super().default(obj)
2622

2723
app = FastAPI()
28-
Knowledge_Objects = (
29-
{}
30-
)
24+
Knowledge_Objects = {}
3125

3226
# this dictionary used to keep loaded objects (key:is and value:function)
3327
object_directory = "" # used for location of knowledge objects
@@ -39,9 +33,8 @@ def hello():
3933

4034

4135
@app.get("/endpoints")
42-
def hello():
43-
return json.loads(json.dumps(Knowledge_Objects,cls=CustomEncoder, indent=4))
44-
36+
def endpoints():
37+
return Knowledge_Objects # json.loads(json.dumps(Knowledge_Objects,cls=CustomEncoder, indent=4))
4538

4639

4740
# end point to expose all packages
@@ -52,7 +45,7 @@ async def execute_endpoint(
5245
# get the method
5346
if endpoint_key in Knowledge_Objects:
5447
function = Knowledge_Objects[endpoint_key].function
55-
if function and Knowledge_Objects[endpoint_key].status=="Activated":
48+
if function and Knowledge_Objects[endpoint_key].status == "Activated":
5649
# run the imported method and pass the request json
5750
# if content_type == 'application/json':
5851
data = await request.json()
@@ -62,7 +55,7 @@ async def execute_endpoint(
6255
result = function(data)
6356

6457
return {"result": result}
65-
elif Knowledge_Objects[endpoint_key].status!="Activated":
58+
elif Knowledge_Objects[endpoint_key].status != "Activated":
6659
return {"result": Knowledge_Objects[endpoint_key].status}
6760
else:
6861
return {"result": "Knowledge object not found!"}
@@ -86,41 +79,43 @@ def install_requirements(modulepath):
8679

8780
# look into the main directory that has all the packages and have the python ones installed
8881
def install_packages_from_directory(directory, manifest: dict):
89-
for ko in manifest:
82+
for ko in manifest:
9083
install_module(directory, manifest[ko])
9184
list_installed_packages()
9285

9386

94-
def install_module(
95-
directory, ko
96-
): # TO DO: test how it works for windows installation
97-
Knowledge_Objects[ko.name] = knowledge_object(ko.name,ko.status,None,"")
87+
def install_module(directory, ko): # TO DO: test how it works for windows installation
88+
Knowledge_Objects[ko.name] = knowledge_object(ko.name, ko.status, None, "")
9889

9990
try:
10091
modulepath = directory + ko.name + "/"
10192

10293
#########delete me: temporarily ignoring execute package
10394
if ko.name == "python-executive-v1.0":
10495
return
105-
106-
if ko.status!="Ready for install":
107-
return
10896

97+
if ko.status != "Ready for install":
98+
return
10999

110100
Knowledge_Objects[ko.name].status = "Activating"
111-
101+
112102
# get metadata and deployment files
113103
with open(modulepath + "deployment.yaml", "r") as file:
114104
deployment_data = yaml.safe_load(file)
115105
with open(modulepath + "metadata.json", "r") as file:
116106
metadata = json.load(file)
117107
first_key = next(iter(deployment_data))
118108
second_key = next(iter(deployment_data[first_key]))
119-
109+
120110
# do not install non python packages
121111
if deployment_data[first_key][second_key]["engine"] != "python":
122112
del Knowledge_Objects[ko.name]
123-
Knowledge_Objects[metadata["@id"]] = knowledge_object(ko.name,"Knowledge object is not activated. It is not a python object.",None,metadata["@id"])
113+
Knowledge_Objects[metadata["@id"]] = knowledge_object(
114+
ko.name,
115+
"Knowledge object is not activated. It is not a python object.",
116+
None,
117+
metadata["@id"],
118+
)
124119
return
125120

126121
# install requirements
@@ -140,17 +135,28 @@ def install_module(
140135
)
141136
mymethod = getattr(module, deployment_data[first_key][second_key]["function"])
142137
del Knowledge_Objects[ko.name]
143-
Knowledge_Objects[metadata["@id"]] = knowledge_object(ko.name,"Activated",mymethod,metadata["@id"])
138+
Knowledge_Objects[metadata["@id"]] = knowledge_object(
139+
ko.name, "Activated", mymethod, metadata["@id"]
140+
)
144141
except Exception as e:
145-
Knowledge_Objects[ko.name].status="Faield activating with error: "+ repr(e)
142+
Knowledge_Objects[ko.name].status = "Faield activating with error: " + repr(e)
143+
146144

147145
def list_installed_packages():
148146
print("-------------------\nPackages installed:")
149-
#keys_list = list(KnowledgeObjects.keys())
150-
print("{:<4}. {:<30} {:<30} {:<30}".format("","ID","NAME","STATUS"))
151-
for i,item in enumerate(Knowledge_Objects):
152-
print("{:<4}. {:<30} {:<30} {:<30}".format(str(i),Knowledge_Objects[item].id,Knowledge_Objects[item].name[:30], Knowledge_Objects[item].status[:50]))
153-
147+
# keys_list = list(KnowledgeObjects.keys())
148+
#print(Knowledge_Objects)
149+
print("{:<4}. {:<30} {:<30} {:<30}".format("", "ID", "NAME", "STATUS"))
150+
for i, item in enumerate(Knowledge_Objects):
151+
print(
152+
"{:<4}. {:<30} {:<30} {:<30}".format(
153+
str(i),
154+
Knowledge_Objects[item].id,
155+
Knowledge_Objects[item].name[:30],
156+
Knowledge_Objects[item].status[:50],
157+
)
158+
)
159+
154160
print("-------------------")
155161

156162

@@ -161,17 +167,20 @@ async def startup_event():
161167
object_directory = os.environ["COLLECTION_PATH"]
162168
manifest = process_manifest(object_directory)
163169
install_packages_from_directory(object_directory, manifest)
164-
del os.environ["COLLECTION_PATH"]
165-
del os.environ["MANIFEST_PATH"]
170+
try:
171+
del os.environ["COLLECTION_PATH"]
172+
del os.environ["MANIFEST_PATH"]
173+
except Exception as e:
174+
print("error deleting env variables")
166175

167176

168177
# run virtual server when running this .py file directly for debugging. It will look for objects at {code folder}/pyshelf
169178
if __name__ == "__main__":
170179
print(">>>>>running with debug<<<<<")
171-
os.environ["MANIFEST_PATH"]="/home/faridsei/dev/test/package/manifest.json"
172-
#os.environ[
180+
# os.environ["MANIFEST_PATH"]="/home/faridsei/dev/test/package/manifest.json"
181+
# os.environ[
173182
# "MANIFEST_PATH"
174-
#] = "https://github.com/kgrid-objects/example-collection/releases/download/4.2.1/manifest.json"
183+
# ] = "https://github.com/kgrid-objects/example-collection/releases/download/4.2.1/manifest.json"
175184
os.environ["COLLECTION_PATH"] = "/home/faridsei/dev/test/pyshelf/"
176185

177186
uvicorn.run(app, host="127.0.0.1", port=8000)

src/python_activator/cli.py

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,11 @@
88
@cli.command()
99
def run(collection_path: str = "", manifest_path: str = ""):
1010
object_directory = set_object_directory(collection_path)
11-
#manifest_path=set_manifest_path(manifest_path)
11+
1212
if manifest_path:
1313
os.environ["MANIFEST_PATH"]=manifest_path
1414
os.environ["COLLECTION_PATH"]=object_directory
1515

16-
#manifest = process_manifest(object_directory)
17-
18-
#install_packages_from_directory(object_directory, manifest)
19-
2016
uvicorn.run(app, host="127.0.0.1", port=8000)
2117

2218

@@ -42,14 +38,7 @@ def set_object_directory(collection_path: str) -> str:
4238
object_directory += "/"
4339
return object_directory
4440

45-
def set_manifest_path(manifest_path: str) -> str:
46-
if manifest_path:
47-
return manifest_path
48-
elif os.environ.get("MANIFEST_PATH"):
49-
return os.environ.get("MANIFEST_PATH")
50-
else:
51-
return manifest_path
52-
41+
5342

5443
@cli.command()
5544
def version():

src/python_activator/manifest.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def process_manifest(object_directory: str)->dict:
2020
# 0. if no manifest provided consider list of existing knowledge object folders as manifest
2121
if not manifest_path:
2222
for item in scanned_directories:
23-
output_manifest[ko_name]=str.replace(item, ".zip", "")
23+
output_manifest[str.replace(item, ".zip", "")]=ko_object(str.replace(item, ".zip", ""),"Ready for install")
2424
return output_manifest
2525

2626
input_manifest, scanned_files = init_process(manifest_path, object_directory)
@@ -41,6 +41,7 @@ def process_manifest(object_directory: str)->dict:
4141
continue
4242

4343
# 2. if zip file does not exists in the folder download it from local path or http (uses uri)
44+
4445
if not ko_filename in scanned_files:
4546
try:
4647
urllib.request.urlretrieve(
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
from fastapi import FastAPI, HTTPException,Request
2+
from fastapi.responses import JSONResponse
3+
import uvicorn
4+
5+
class UnicornException(Exception):
6+
def __init__(self, name: str):
7+
self.name = name
8+
9+
app = FastAPI()
10+
11+
items = {"foo": "The Foo Wrestlers"}
12+
13+
@app.exception_handler(UnicornException)
14+
async def unicorn_exception_handler(request: Request, exc: UnicornException):
15+
return JSONResponse(
16+
status_code=418,
17+
content={"message": f"Oops! {exc.name} did something. There goes a rainbow..."},
18+
)
19+
20+
@app.get("/unicorns/{name}")
21+
async def read_unicorn(name: str):
22+
if name == "yolo":
23+
raise UnicornException(name=name)
24+
return {"unicorn_name": name}
25+
26+
@app.get("/items/{item_id}")
27+
async def read_item(item_id: str):
28+
if item_id not in items:
29+
raise HTTPException(status_code=404, detail="Item not found")
30+
return {"item": items[item_id]}
31+
32+
if __name__ == "__main__":
33+
uvicorn.run(app, host="127.0.0.1", port=8000)
34+
35+

tests/test_api.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import pytest
22
from src.python_activator.api import hello,execute_endpoint
3-
3+
#from fastapi.testclient import TestClient
4+
from src.test.test_fastapi_exceptions import app # Assuming your FastAPI app instance is named `app`
45
def test_root():
56

6-
expected = {"Hello": "World"}
7+
expected = {"Hello": "World9"}
78

89
# Act
910
output = hello()
@@ -21,6 +22,11 @@ def test_hard_coded_root_nessage_doesnt_know_Farid():
2122
# Assert
2223
assert output != expected
2324

24-
25+
def test_how_exceptions_are_handled_in_apis():
26+
#client = TestClient(app)
27+
#response = client.get("/item/foo")
28+
#assert response.status_code == 200
29+
#assert response.json() == {"item": "The Foo Wrestlers"}
30+
assert 1!=1
2531

2632

tests/test_cli.py

Lines changed: 0 additions & 25 deletions
This file was deleted.

tests/test_dot_dirs.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from src.python_activator.api import install_module
2+
from src.python_activator.manifest import ko_object
23
import os
34
import sys
45
import importlib
@@ -9,13 +10,13 @@ def test_basics():
910
assert os.path.abspath("etc/pyshelf").endswith("etc/pyshelf")
1011

1112
directory="/home/faridsei/dev/code/python-activator/etc/pyshelf/"
12-
sys.path.append( directory )
13+
#sys.path.append( directory )
1314

14-
install_module(directory, "python-multiartifact-v1-0")
15-
del sys.modules["ko_folder"]
16-
sys.path.remove(
17-
directory
18-
)
15+
install_module(directory, ko_object("python-multiartifact-v1-0","Ready to install"))
16+
#del sys.modules["ko_folder"]
17+
#sys.path.remove(
18+
# directory
19+
#)
1920

2021
def test_direct_import_of_module():
2122

tests/test_manifest.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import pytest
2+
from src.python_activator.api import *
3+
4+
def test_process_local_manifest():
5+
os.environ["MANIFEST_PATH"]="/home/faridsei/dev/test/package/manifest.json"
6+
process_manifest("/home/faridsei/dev/test/pyshelf/")
7+
del os.environ["MANIFEST_PATH"]
8+
9+
10+
# Assert
11+
assert 1==1
12+
13+
def test_process_internet_manifest():
14+
os.environ["MANIFEST_PATH"]="https://github.com/kgrid-objects/example-collection/releases/download/4.2.1/manifest.json"
15+
process_manifest("/home/faridsei/dev/test/pyshelf/")
16+
del os.environ["MANIFEST_PATH"]
17+
18+
# Assert
19+
assert 1==1
20+
21+
def test_process_no_manifest():
22+
23+
process_manifest("/home/faridsei/dev/test/pyshelf/")
24+
25+
# Assert
26+
assert 1==1
27+
28+
29+
def test_using_uri_to_download_zip():
30+
assert 1!=1
31+
32+
def test_formatting_dictionary_of_object_to_json():
33+
assert 1!=1

0 commit comments

Comments
 (0)