-
-
Notifications
You must be signed in to change notification settings - Fork 6.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[python] add method to set/get default configuration #5315
Changes from 3 commits
70a5854
2a4c726
be04fb9
f99b59a
f6bd708
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
|
||
from __future__ import absolute_import | ||
|
||
import copy | ||
import logging | ||
{{^asyncio}} | ||
import multiprocessing | ||
|
@@ -117,6 +118,8 @@ class Configuration(object): | |
{{/hasAuthMethods}} | ||
""" | ||
|
||
_default = None | ||
|
||
def __init__(self, host="{{{basePath}}}", | ||
api_key=None, api_key_prefix=None, | ||
username=None, password=None, | ||
|
@@ -241,6 +244,45 @@ class Configuration(object): | |
# Disable client side validation | ||
self.client_side_validation = True | ||
|
||
def __deepcopy__(self, memo): | ||
cls = self.__class__ | ||
result = cls.__new__(cls) | ||
memo[id(self)] = result | ||
for k, v in self.__dict__.items(): | ||
if k not in ('logger', 'logger_file_handler'): | ||
setattr(result, k, copy.deepcopy(v, memo)) | ||
# shallow copy of loggers | ||
result.logger = copy.copy(self.logger) | ||
# use setters to configure loggers | ||
result.logger_file = self.logger_file | ||
result.debug = self.debug | ||
return result | ||
|
||
@classmethod | ||
def set_default(cls, default): | ||
"""Set default instance of configuration. | ||
|
||
It stores default configuration, which can be | ||
returned by get_default_copy method. | ||
|
||
:param default: object of Configuration | ||
""" | ||
cls._default = copy.deepcopy(default) | ||
|
||
@classmethod | ||
def get_default_copy(cls): | ||
"""Return new instance of configuration. | ||
|
||
This method returns newly created, based on default constructor, | ||
object of Configuration class or returns a copy of default | ||
configuration passed by the set_default method. | ||
|
||
:return: The configuration object. | ||
""" | ||
if cls._default is not None: | ||
return copy.deepcopy(cls._default) | ||
return Configuration() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about returning None if the default copy is not present? Or if we want to always return an instance how about naming it There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think |
||
|
||
@property | ||
def logger_file(self): | ||
"""The logger file. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about?
default_copy = Configuration.get_default_copy()
configuration = Configuration() if default_copy is None else default_copy