Skip to content

Commit 045aeef

Browse files
Merge pull request #18 from scailable/develop
Merge latest development version (0.1.1) with master.
2 parents b6a5665 + 0c9f82f commit 045aeef

20 files changed

+1571
-683
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,4 +138,5 @@ dmypy.json
138138
.idea/
139139

140140
# User credentials
141-
.creds.json
141+
.creds.json
142+
temp.py

MANIFEST.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
include LICENSE
2+
recursive-include sclblpy *.json

README.md

Lines changed: 150 additions & 242 deletions
Large diffs are not rendered by default.

sclblpy/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
"""
22
The sclblpy package allows users to upload, monitor, and administer models and REST endpoints directly from python.
33
"""
4-
from .main import upload, endpoints, delete_endpoint, remove_credentials
4+
from .main import upload, endpoints, delete_endpoint, remove_credentials, list_models, stop_print, start_print, \
5+
_set_toolchain_URL, _set_admin_URL
6+
from .version import __version__
57

sclblpy/__main__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
# > python sclblpy
33
# from terminal.
44
if __name__ == '__main__':
5-
print("The sclblpy package does not have any command line options.")
5+
print("The sclblpy package currently does not have any command line options.")
66
print("Please import the package into your python project and work from there.")

sclblpy/_bundle.py

Lines changed: 43 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# _bundle.py contains private methods for gzip bundling of object
1+
# _bundle.py contains private methods for gzip bundling of the model object
22
import gzip
33
import pickle
44
import os
@@ -7,49 +7,51 @@
77
import sclblpy._globals as glob
88

99

10-
def _gzip_save(object, filename: str=glob.BUNDLE_NAME, _verbose=False):
10+
def _gzip_save(object, filename: str=glob.GZIP_BUNDLE):
1111
"""Saves a compressed object to disk.
1212
1313
Function is used to pickle and gzip an sclblpy model object as created
1414
by the upload() function. This package is send to the toolchain.
1515
1616
Args:
1717
object: A dictionary containing all the information to be send.
18-
filename: a string stating where to store the .zip file. Default glob.BUNDLE_NAME
19-
_verbose: Bool indicating whether feedback should be printed. Default False.
18+
filename: a string stating where to store the .zip file. Default glob.GZIP_BUNDLE
2019
2120
Returns:
21+
True if the bundle is successfully saved, False if not.
2222
23-
Raises:
23+
Raises (in debug mode):
2424
ModelBundleError if unable to store the model bundle.
2525
2626
"""
2727
try:
28+
os.makedirs(os.path.dirname(filename), exist_ok=True) # create the folder if it does not exists.
2829
fp = gzip.open(filename, 'wb')
29-
pickle.dump(object, fp)
30+
pickle.dump(object, fp, protocol=4) # protocol = 4 is python 3.4 or higher
3031
fp.close()
31-
if _verbose:
32-
print("File successfully stored.")
32+
if glob.DEBUG:
33+
print("Bundle message: File successfully stored.")
34+
return True
3335
except Exception as e:
34-
if _verbose:
35-
print("Exception: " + str(e))
36-
raise ModelBundleError("Unable to pickle and gzip your model.")
36+
if glob.DEBUG:
37+
print("Bundle exception: " + str(e))
38+
raise ModelBundleError("Bundle error: Unable to pickle and gzip your model.")
39+
return False
3740

38-
return
3941

40-
41-
def _gzip_load(filename: str=glob.BUNDLE_NAME, _verbose=True):
42+
def _gzip_load(filename: str=glob.GZIP_BUNDLE):
4243
"""Loads a compressed object from disk.
4344
4445
Currently not used in the sclblpy package but syntax should be used on the toolchain side.
4546
4647
Args:
47-
filename: Name of the gzipped pickle. Default "temp_sclbl_mod.gzip".
48-
_verbose: Bool indicating whether feedback should be printed. Default True.
48+
filename: Name of the gzipped pickle. Default glob.GZIP_BUNDLE.
49+
4950
Returns:
5051
obj: A dictonary containing the unpickled and unzipped contents of the file. Empty dict if unable to open.
52+
False otherwise.
5153
52-
Raises:
54+
Raises (in debug mode):
5355
ModelBundleError if fails.
5456
5557
"""
@@ -59,47 +61,52 @@ def _gzip_load(filename: str=glob.BUNDLE_NAME, _verbose=True):
5961
fp = gzip.open(filename, 'rb')
6062
obj = pickle.load(fp)
6163
fp.close()
62-
if _verbose:
63-
print("Model bundle successfully loaded.")
64+
if glob.DEBUG:
65+
print("Bundle message: Model bundle successfully loaded.")
6466
else:
65-
if _verbose:
66-
print("Model bundle not found.")
67+
if glob.DEBUG:
68+
print("Bundle message: Model bundle not found.")
69+
return False
6770
except Exception as e:
68-
if _verbose:
69-
print("Exception: " + str(e))
70-
raise ModelBundleError("Unable to load model bundle.")
71+
if glob.DEBUG:
72+
print("Bundle exception: " + str(e))
73+
raise ModelBundleError("Bundle error: Unable to load model bundle.")
74+
return False
7175

7276
return obj
7377

7478

75-
def _gzip_delete(filename:str =glob.BUNDLE_NAME, _verbose=False):
79+
def _gzip_delete(filename:str =glob.GZIP_BUNDLE):
7680
"""Deletes a file from user machine.
7781
7882
Args:
79-
filename: Str name of the file to delete, default: temp_sclbl_mod.gzip.
80-
_verbose: Bool indicating whether feedback should be printed. Default False.
83+
filename: Str name of the file to delete. Default: glob.GZIP_BUNDLE.
8184
8285
Returns:
86+
True if all is well, False otherwise.
8387
84-
Raises:
88+
Raises (in debug mode):
8589
ModelBundleError if fails.
8690
8791
"""
8892

8993
try:
9094
if os.path.exists(filename):
9195
os.remove(filename)
92-
if _verbose:
93-
print("Deleted gzipped model bundle.")
96+
if glob.DEBUG:
97+
print("Bundle message: Deleted gzipped model bundle.")
9498
else:
95-
if _verbose:
96-
print("File not found.")
99+
if glob.DEBUG:
100+
print("Bundle message: File not found.")
101+
return False
97102
except Exception as e:
98-
print("Exception: " + str(e))
99-
raise ModelBundleError("Unable to delete file.")
103+
if glob.DEBUG:
104+
print("Bundle message: exception: " + str(e))
105+
raise ModelBundleError("Bundle error: Unable to delete file.")
106+
return False
100107

101-
return
108+
return True
102109

103110

104111
if __name__ == '__main__':
105-
print("No command line options yet for _bundle.py.")
112+
print("No command line options for _bundle.py.")

sclblpy/_globals.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,28 @@
11
# Global variables for the sclblpy package.
22
import os
3+
from sclblpy.appdirs import AppDirs
34

4-
PKG_NAME: str = "sclblpy_v_0.01"
5+
# servers:
6+
USER_MANAGER_URL: str = "https://admin.sclbl.net" # Location of the user manager.
7+
TOOLCHAIN_URL: str = "https://toolchain.sclbl.net" # Location of the toolchain server.
58

6-
USER_MANAGER_URL: str = "http://localhost:8008" # Location of the user manager.
7-
TOOLCHAIN_URL: str = "http://localhost:8001" # Location of the toolchain server.
9+
# control printing:
10+
SILENT: bool = False # Boolean indicating whether user feedback should be suppressed.
11+
DEBUG: bool = False # Boolean indicating whether using the package in debug mode; if so, it will raise exceptions.
812

13+
# Storage locations:
14+
dirs = AppDirs("sclblpy", "sclbl")
15+
USER_CREDENTIALS: str = dirs.user_config_dir + "/.creds.json" # Location of json file to store user credentials
16+
GZIP_BUNDLE: str = dirs.user_data_dir + "/model_bundle.gzip" # Location where a save model is (temporarily) stored
17+
MODELS_JSON: str = os.path.dirname(os.path.realpath(__file__)) + "/supported_models.json" # Location of the json with supported models
18+
19+
# JWT necessities:
920
JWT_TOKEN: str = "" # JWT token.
1021
JWT_USER_ID: str = "" # Scailable user id.
1122
JWT_TIMESTAMP: float = 0.0 # Timestamp in seconds.
1223

13-
USER_CREDENTIALS_FOLDER: str = "" # Location where user credentials are stored.
14-
15-
CURRENT_FOLDER: str = os.path.dirname(os.path.realpath(__file__)) # Folder where the module is running.
24+
# Available models:
1625
SUPPORTED_MODELS: dict = {} # List of supported models.
17-
BUNDLE_NAME: str = "temp_sclbl_mod_bundle.gzip"
1826

1927

2028
if __name__ == '__main__':

0 commit comments

Comments
 (0)