Skip to content

Commit 4f4df10

Browse files
authored
Merge pull request #15 from platformsh/cleanup
Cleanup
2 parents 1cb5eb4 + 5461cf1 commit 4f4df10

3 files changed

Lines changed: 82 additions & 61 deletions

File tree

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,4 +98,7 @@ ENV/
9898
/site
9999

100100
# mypy
101-
.mypy_cache/
101+
.mypy_cache/
102+
103+
# Pytest
104+
.pytest_cache

platformshconfig/config.py

Lines changed: 77 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -21,28 +21,6 @@ class Config:
2121
existence with hasattr(config, variableName). Attempting to access a nonexistent variable will throw an exception.
2222
2323
Attributes:
24-
directVariables (dict):
25-
Local index of the variables that can be accessed as direct properties (build and
26-
runtime). The key is the property that will be read. The value is the environment variables, minus prefix,
27-
that contains the value to look up.
28-
directVariablesRuntime (dict):
29-
Local index of the variables that can be accessed as direct properties
30-
(runtime only). The key is the property that will be read. The value is the environment variables, minus
31-
prefix, that contains the value to look up.
32-
environmentVariables (dict):
33-
A local copy of all environment variables as of when the object was initialized.
34-
envPrefix (string):
35-
The vendor prefix for all environment variables we care about.
36-
routesDef (dict):
37-
The routes definition array. Only available at runtime.
38-
relationshipsDef (dict):
39-
The relationships definition array. Only available at runtime.
40-
variablesDef (dict):
41-
The variables definition array. Available in both build and runtime, although possibly with different
42-
values.
43-
applicationDef (dict):
44-
The application definition array. This is, approximately, the .platform.app.yaml file in nested array form.
45-
4624
(The following properties are available at build time and run time.)
4725
4826
project (string):
@@ -78,34 +56,74 @@ class Config:
7856
7957
"""
8058

81-
directVariables = {
59+
"""
60+
Local index of the variables that can be accessed as direct properties (build and
61+
runtime). The key is the property that will be read. The value is the environment variables, minus prefix,
62+
that contains the value to look up.
63+
"""
64+
_directVariables = {
8265
"project": "PROJECT",
8366
"appDir": "APP_DIR",
8467
"applicationName": 'APPLICATION_NAME',
8568
"treeID": "TREE_ID",
8669
"projectEntropy": "PROJECT_ENTROPY"
8770
}
8871

89-
directVariablesRuntime = {
72+
"""
73+
Local index of the variables that can be accessed as direct properties
74+
(runtime only). The key is the property that will be read. The value is the environment variables, minus
75+
prefix, that contains the value to look up.
76+
"""
77+
_directVariablesRuntime = {
9078
"branch": "BRANCH",
9179
"environment": "ENVIRONMENT",
9280
"documentRoot": "DOCUMENT_ROOT",
9381
"smtpHost": "SMTP_HOST"
9482
}
9583

96-
unPrefixedVariablesRuntime = {
84+
"""
85+
Local index of variables available at runtime that have no prefix.
86+
"""
87+
_unPrefixedVariablesRuntime = {
9788
"port": "PORT",
9889
"socket": "SOCKET"
9990
}
10091

101-
environmentVariables = []
102-
envPrefix = ''
92+
"""
93+
A local copy of all environment variables as of when the object was initialized.
94+
"""
95+
_environmentVariables = []
96+
97+
"""
98+
The vendor prefix for all environment variables we care about.
99+
"""
100+
_envPrefix = ''
101+
102+
"""
103+
The routes definition array. Only available at runtime.
104+
"""
105+
_routesDef = []
103106

104-
routesDef = []
105-
relationshipsDef = []
106-
variablesDef = []
107-
applicationDef = []
108-
credentialFormatters = {}
107+
"""
108+
The relationships definition array. Only available at runtime.
109+
"""
110+
_relationshipsDef = []
111+
112+
"""
113+
The variables definition array. Available in both build and runtime, although possibly with different
114+
values.
115+
"""
116+
_variablesDef = []
117+
118+
"""
119+
The application definition array. This is, approximately, the .platform.app.yaml file in nested dictionary form.
120+
"""
121+
_applicationDef = []
122+
123+
"""
124+
A map of the registered credential formatters. The key is the name, the value is a function.
125+
"""
126+
_credentialFormatters = {}
109127

110128
def __init__(self, environment_variables=None, env_prefix='PLATFORM_'):
111129
"""Constructs a ConfigReader object.
@@ -118,27 +136,27 @@ def __init__(self, environment_variables=None, env_prefix='PLATFORM_'):
118136
119137
"""
120138

121-
self.environmentVariables = os.environ if environment_variables is None else environment_variables
122-
self.envPrefix = env_prefix
139+
self._environmentVariables = os.environ if environment_variables is None else environment_variables
140+
self._envPrefix = env_prefix
123141

124142
if self.is_valid_platform():
125143
if self.in_runtime():
126144
if self['ROUTES']:
127145
routes = self['ROUTES']
128-
self.routesDef = self.decode(routes)
146+
self._routesDef = self.decode(routes)
129147
if self['RELATIONSHIPS']:
130148
relationships = self['RELATIONSHIPS']
131-
self.relationshipsDef = self.decode(relationships)
149+
self._relationshipsDef = self.decode(relationships)
132150

133151
self.register_formatter('pymongo', pymongo_formatter)
134152
self.register_formatter('pysolr', pysolr_formatter)
135153

136154
if self['VARIABLES']:
137155
variables = self['VARIABLES']
138-
self.variablesDef = self.decode(variables)
156+
self._variablesDef = self.decode(variables)
139157
if self['APPLICATION']:
140158
application = self['APPLICATION']
141-
self.applicationDef = self.decode(application)
159+
self._applicationDef = self.decode(application)
142160

143161
def is_valid_platform(self):
144162
"""Checks whether the code is running on a platform with valid environment variables.
@@ -198,15 +216,15 @@ def credentials(self, relationship, index=0):
198216
raise BuildTimeVariableAccessException(
199217
'Relationships are not available during the build phase.'
200218
)
201-
if relationship not in self.relationshipsDef:
219+
if relationship not in self._relationshipsDef:
202220
raise KeyError(
203221
'No relationship defined: {}. Check your .platform.app.yaml file.'
204222
.format(relationship))
205-
if index >= len(self.relationshipsDef):
223+
if index >= len(self._relationshipsDef):
206224
raise KeyError('No index {} defined for relationship: {}. '
207225
'Check your .platform.app.yaml file.'.format(
208226
index, relationship))
209-
return self.relationshipsDef[relationship][index]
227+
return self._relationshipsDef[relationship][index]
210228

211229
def variable(self, name, default=None):
212230
"""Returns a variable from the VARIABLES array.
@@ -229,7 +247,7 @@ def variable(self, name, default=None):
229247

230248
if not self.is_valid_platform():
231249
return default
232-
return self.variablesDef.get(name, default)
250+
return self._variablesDef.get(name, default)
233251

234252
def variables(self):
235253
"""Returns the full variables array.
@@ -246,7 +264,7 @@ def variables(self):
246264
raise NotValidPlatformException(
247265
'You are not running on Platform.sh, so the variables array is not available.'
248266
)
249-
return self.variablesDef
267+
return self._variablesDef
250268

251269
def routes(self):
252270
"""Return the routes definition.
@@ -267,7 +285,7 @@ def routes(self):
267285
raise BuildTimeVariableAccessException(
268286
'Routes are not available during the build phase.'
269287
)
270-
return self.routesDef
288+
return self._routesDef
271289

272290
def get_route(self, route_id):
273291
"""Get route definition by route ID.
@@ -306,7 +324,7 @@ def application(self):
306324
raise NotValidPlatformException(
307325
'You are not running on Platform.sh, so the application definitions are not available.'
308326
)
309-
return self.applicationDef
327+
return self._applicationDef
310328

311329
def on_enterprise(self):
312330
"""Determines if the current environment is a Platform.sh Enterprise environment.
@@ -357,7 +375,7 @@ def register_formatter(self, name, formatter):
357375
358376
"""
359377

360-
self.credentialFormatters[name] = formatter
378+
self._credentialFormatters[name] = formatter
361379
return self
362380

363381
def formatted_credentials(self, relationship, formatter):
@@ -374,12 +392,12 @@ def formatted_credentials(self, relationship, formatter):
374392
NoCredentialFormatterFoundException
375393
376394
"""
377-
if formatter not in self.credentialFormatters:
395+
if formatter not in self._credentialFormatters:
378396
raise NoCredentialFormatterFoundException(
379397
'There is no credential formatter named {0} registered. Did you remember to call register_formatter()?'
380398
.format(formatter)
381399
)
382-
return self.credentialFormatters[formatter](self.credentials(relationship))
400+
return self._credentialFormatters[formatter](self.credentials(relationship))
383401

384402
def __getitem__(self, item):
385403
"""Reads an environment variable, taking the prefix into account.
@@ -390,9 +408,9 @@ def __getitem__(self, item):
390408
391409
"""
392410

393-
check_name = self.envPrefix + item.upper()
411+
check_name = self._envPrefix + item.upper()
394412

395-
return self.environmentVariables.get(check_name)
413+
return self._environmentVariables.get(check_name)
396414

397415
@staticmethod
398416
def decode(variable):
@@ -457,23 +475,23 @@ def __getattr__(self, config_property):
457475
raise NotValidPlatformException(
458476
'You are not running on Platform.sh, so the {0} variable is not available.'.format(config_property)
459477
)
460-
is_build_var = config_property in self.directVariables.keys()
461-
is_runtime_var = config_property in self.directVariablesRuntime.keys()
478+
is_build_var = config_property in self._directVariables.keys()
479+
is_runtime_var = config_property in self._directVariablesRuntime.keys()
462480

463481
# For now, all unprefixed variables are also runtime variables. If that ever changes this logic will change
464482
# with it.
465-
is_unprefixed_var = config_property in self.unPrefixedVariablesRuntime.keys()
483+
is_unprefixed_var = config_property in self._unPrefixedVariablesRuntime.keys()
466484

467485
if self.in_build() and is_runtime_var:
468486
raise BuildTimeVariableAccessException(
469487
'The {0} variable is not available during build time.'.format(config_property)
470488
)
471489
if is_build_var:
472-
return self[self.directVariables[config_property]]
490+
return self[self._directVariables[config_property]]
473491
if is_runtime_var:
474-
return self[self.directVariablesRuntime[config_property]]
492+
return self[self._directVariablesRuntime[config_property]]
475493
if is_unprefixed_var:
476-
return self.environmentVariables.get(self.unPrefixedVariablesRuntime[config_property])
494+
return self._environmentVariables.get(self._unPrefixedVariablesRuntime[config_property])
477495
raise AttributeError('No such variable defined: '.format(config_property))
478496

479497
def isset(self, config_property):
@@ -492,12 +510,12 @@ def isset(self, config_property):
492510
if not self.is_valid_platform():
493511
return False
494512

495-
is_build_var = config_property in self.directVariables.keys()
496-
is_runtime_var = config_property in self.directVariablesRuntime.keys()
513+
is_build_var = config_property in self._directVariables.keys()
514+
is_runtime_var = config_property in self._directVariablesRuntime.keys()
497515

498516
# For now, all unprefixed variables are also runtime variables. If that ever changes this logic will change
499517
# with it.
500-
is_unprefixed_var = config_property in self.unPrefixedVariablesRuntime.keys()
518+
is_unprefixed_var = config_property in self._unPrefixedVariablesRuntime.keys()
501519

502520
if self.in_build():
503521
return is_build_var and config_property is not None

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def run(self):
3636
version=VERSION,
3737
name='platformshconfig',
3838
description='Small helper to access Platform.sh environment variables.',
39-
url='https://github.com/platformsh/platformsh-config-reader-python3',
39+
url='https://github.com/platformsh/config-reader-python3',
4040
author='Platform.sh',
4141
author_email='sayhello@platform.sh',
4242
license='MIT',

0 commit comments

Comments
 (0)