forked from scaleoutsean/storagegrid-dmc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
87 lines (62 loc) · 2.32 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# StorageGRID Data Management Console (DMC)
# Copyright (c) 2018, NetApp, Inc.
# Licensed under the terms of the Modified BSD License (also known as New or Revised or 3-Clause BSD)
from constants import *
import sys
import os
# Define function to import external files when using PyInstaller.
def resource_path(relative_path):
""" Get absolute path to resource, works for dev and for PyInstaller """
try:
# PyInstaller creates a temp folder and stores path in _MEIPASS
base_path = sys._MEIPASS
except Exception:
base_path = os.path.abspath(".")
return os.path.join(base_path, relative_path)
def validate_inputs(inputs):
""" Function used to validate input parameter existence.
:param inputs: dictionary containing input key(s) and value(s)
:return: dictionary containing success status and response or error message
"""
response = {'success': True}
invalid = []
for key, value in inputs.iteritems():
if not str(value):
invalid.append(key)
if len(invalid) > 0:
response.update({
'success': False,
'message': DMC_MISSING_PARAMS.format(', '.join(invalid))
})
return response
def validate_int_inputs(inputs):
""" Function used to validate integer input parameter.
:param inputs: dictionary containing input key(s) and value(s)
:return: dictionary containing success status and response or error message
"""
response = {'success': True}
try:
if not sum(1 for key, curr_input in inputs.iteritems() if int(curr_input) >= 0) == len(inputs.keys()):
response['success'] = False
except (TypeError, ValueError) as e:
response['success'] = False
if not response['success']:
response.update({
'message': DMC_INT_TYPE_CAST_ERROR.format(', '.join(inputs.keys()))
})
return response
def get_version_info():
""" Function used to return version information by reading dmc.version file.
:return: version, build number
"""
version = ''
build = ''
try:
info = ""
with open(resource_path('dmc.version'), 'r') as f:
info = f.read().split('\n')
version = info[0].split('=')[1]
build = info[1].split('=')[1]
except (OSError, IOError) as e:
pass
return version, build