Skip to content

Commit c7ca4d0

Browse files
committed
Adding Enum serialization and docstrings
1 parent b41fb3d commit c7ca4d0

File tree

1 file changed

+44
-7
lines changed

1 file changed

+44
-7
lines changed

gp/modules.py

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
Compatible with Python 2.7 and Python 3.4+
77
"""
88

9+
import json
910
import os
1011
import string
1112
import zipfile
@@ -17,25 +18,32 @@
1718
__status__ = 'Beta'
1819

1920

20-
class Privacy(Enum):
21+
class StringEnum(str, Enum):
22+
"""
23+
Enum where members are also (and must be) strings
24+
Necessary for JSON serialization of the Enums declared here
25+
"""
26+
27+
28+
class Privacy(StringEnum):
2129
PRIVATE = "private"
2230
PUBLIC = "public"
2331

2432

25-
class Quality(Enum):
33+
class Quality(StringEnum):
2634
DEVELOPMENT = "development"
2735
PREPRODUCTION = "preproduction"
2836
PRODUCTION = "production"
2937

3038

31-
class OS(Enum):
39+
class OS(StringEnum):
3240
ANY = "any"
3341
LINUX = "linux"
3442
MAC = "mac"
3543
WINDOWS = "windows"
3644

3745

38-
class CPU(Enum):
46+
class CPU(StringEnum):
3947
ANY = "any"
4048
ALPHA = "alpha"
4149
INTEL = "intel"
@@ -82,6 +90,12 @@ def __init__(self, name=None, description="", version_comment="", author="", ins
8290
self.parameters = parameters
8391

8492
def validate(self):
93+
"""
94+
Perform some basic checks to help ensure that the specification is valid.
95+
Throws an exception if an invalid value is found.
96+
Returns true if all checks were passed.
97+
:return: boolean
98+
"""
8599
# Check all values for None
86100
for attr in self.__dict__:
87101
if self.__dict__[attr] is None:
@@ -119,6 +133,11 @@ def validate(self):
119133
return True
120134

121135
def create_zip(self, clean=True):
136+
"""
137+
Creates a GenePattern module zip file for upload and installation on a GenePattern server
138+
:param clean: boolean
139+
:return:
140+
"""
122141
# First validate the attributes
123142
self.validate()
124143

@@ -137,6 +156,10 @@ def create_zip(self, clean=True):
137156
os.remove(MANIFEST_FILE_NAME)
138157

139158
def _zip_files(self):
159+
"""
160+
Adds the manifest and all support files to the zip file
161+
:return:
162+
"""
140163
# Create the zip file
141164
zip = zipfile.ZipFile(self.name + '.zip', 'w', zipfile.ZIP_DEFLATED)
142165

@@ -238,17 +261,31 @@ def manifest_escape(string):
238261

239262
@staticmethod
240263
def all_strings(arr):
264+
"""
265+
Ensures that the argument is a list that either is empty or contains only strings
266+
:param arr: list
267+
:return:
268+
"""
241269
if not isinstance([], list):
242270
raise TypeError("non-list value found where list is expected")
243271
return all(isinstance(x, str) for x in arr)
244272

245273
@staticmethod
246274
def _all_params(arr):
275+
"""
276+
Ensures that the argument is a list that either is empty or contains only GPParamSpec's
277+
:param arr: list
278+
:return:
279+
"""
247280
if not isinstance([], list):
248281
raise TypeError("non-list value found for parameters")
249282
return all(isinstance(x, GPParamSpec) for x in arr)
250283

251284
def _valid_lsid(self):
285+
"""
286+
Performs some basic (non-comprehensive) LSID validation
287+
:return:
288+
"""
252289
if not isinstance(self.lsid, str):
253290
raise TypeError("lsid is not a string, string expected: " + str(self.lsid))
254291

@@ -267,7 +304,7 @@ def invalid_chars():
267304
return set(string.punctuation.replace("_", "").replace(".", "") + string.whitespace)
268305

269306

270-
class Type(Enum):
307+
class Type(StringEnum):
271308
FILE = "FILE"
272309
TEXT = "TEXT"
273310
INTEGER = "Integer"
@@ -276,7 +313,7 @@ class Type(Enum):
276313
PASSWORD = "PASSWORD"
277314

278315

279-
class JavaType(Enum):
316+
class JavaType(StringEnum):
280317
FILE = "java.io.File"
281318
TEXT = "java.lang.String"
282319
INTEGER = "java.lang.Integer"
@@ -285,7 +322,7 @@ class JavaType(Enum):
285322
PASSWORD = "PASSWORD"
286323

287324

288-
class Optional(Enum):
325+
class Optional(StringEnum):
289326
REQUIRED = ""
290327
OPTIONAL = "on"
291328

0 commit comments

Comments
 (0)