-
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
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 2 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,40 @@ class Configuration(object): | |
# Disable client side validation | ||
self.client_side_validation = True | ||
|
||
@classmethod | ||
def copy(cls, source): | ||
if source is None: | ||
return None | ||
ret = copy.copy(source) | ||
ret.api_key = copy.copy(source.api_key) | ||
ret.api_key_prefix = copy.copy(source.api_key_prefix) | ||
return ret | ||
|
||
@classmethod | ||
def set_default(cls, default): | ||
"""Set default instance of configuration. | ||
|
||
It stores default configuration, which can be | ||
returned by new_instance method. | ||
|
||
:param default: object of Configuration | ||
""" | ||
cls._default = Configuration.copy(default) | ||
|
||
@classmethod | ||
def new_instance(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 Configuration.copy(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 changing this to:
__deepcopy__(self, memodict={})
That way you control how everything can be copied. For the logger only you can then make a new logger with the same properties as the old one. All other properties can be deep copied. You can use dir(self) or
self.__dict__
to iterate over all of self's properties.If you do it this way, then in new_instance you can return copy.deepcopy(cls._default)
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.
Yeah, I'd totally forgotten about this possibility. Thanks.