Skip to content

Commit

Permalink
Added copy method on PNConfiguration instance
Browse files Browse the repository at this point in the history
  • Loading branch information
seba-aln committed Aug 6, 2024
1 parent 64fce87 commit e97e0f6
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
6 changes: 6 additions & 0 deletions pubnub/pnconfiguration.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import warnings
from typing import Any
from copy import deepcopy
from Cryptodome.Cipher import AES
from pubnub.enums import PNHeartbeatNotificationOptions, PNReconnectionPolicy
from pubnub.exceptions import PubNubException
Expand Down Expand Up @@ -179,6 +180,11 @@ def user_id(self, user_id):
def lock(self):
self.__dict__['_locked'] = False if self.disable_config_locking else True

def copy(self):
config_copy = deepcopy(self)
config_copy.__dict__['_locked'] = False
return config_copy

def __setattr__(self, name: str, value: Any) -> None:
if self._locked:
warnings.warn(UserWarning('Configuration is locked. Any changes made won\'t have any effect'))
Expand Down
12 changes: 12 additions & 0 deletions tests/unit/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,15 @@ async def test_asyncio_config_mutability_lock_disabled(self):

config.user_id = 'test'
assert pubnub.config.user_id == 'test'

def test_config_copy(self):
config = PNConfiguration()
config.disable_config_locking = False
config.publish_key = 'demo'
config.subscribe_key = 'demo'
config.user_id = 'demo'
config.lock()
config_copy = config.copy()
assert id(config) != id(config_copy)
assert config._locked is True
assert config_copy._locked is False

0 comments on commit e97e0f6

Please sign in to comment.