Skip to content

Commit 1400dd4

Browse files
author
CoderOMaster
committed
changes
1 parent 90c9b2a commit 1400dd4

5 files changed

Lines changed: 75 additions & 29 deletions

File tree

openpaygo/metrics_shared.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,8 @@ def load_secret_key_from_hex(cls, secret_key):
150150
secret_key_bytes = bytes(secret_key)
151151
if len(secret_key_bytes) != 16:
152152
raise ValueError(
153-
"The secret key provided is not correctly formatted, it should be 16 bytes. "
153+
"The secret key provided is not correctly formatted, it should be 16 "
154+
"bytes. "
154155
)
155156
return secret_key_bytes
156157

openpaygo/simulators/__init__.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1-
from .device_simulator import DeviceSimulator
2-
from .server_simulator import SingleDeviceServerSimulator
1+
from .device_simulator import DeviceSimulator as DeviceSimulator
2+
from .server_simulator import SingleDeviceServerSimulator as SingleDeviceServerSimulator
3+
4+
__all__ = ["DeviceSimulator", "SingleDeviceServerSimulator"]

openpaygo/simulators/device_simulator.py

Lines changed: 49 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,27 @@
11
from datetime import datetime, timedelta
2-
from openpaygo.token_shared import OpenPAYGOTokenShared
2+
33
from openpaygo.token_decode import OpenPAYGOTokenDecoder, TokenType
4+
from openpaygo.token_shared import OpenPAYGOTokenShared
45

56

67
class DeviceSimulator(object):
78

8-
def __init__(self, starting_code, key, starting_count=1, restricted_digit_set=False, waiting_period_enabled=True, time_divider=1):
9+
def __init__(
10+
self,
11+
starting_code,
12+
key,
13+
starting_count=1,
14+
restricted_digit_set=False,
15+
waiting_period_enabled=True,
16+
time_divider=1,
17+
):
918
self.starting_code = starting_code
1019
self.key = key
1120
self.time_divider = time_divider
1221
self.restricted_digit_set = restricted_digit_set
13-
self.waiting_period_enabled = waiting_period_enabled # Should always be true except for testing
22+
self.waiting_period_enabled = (
23+
waiting_period_enabled # Should always be true except for testing
24+
)
1425

1526
self.payg_enabled = True
1627
self.count = starting_count
@@ -48,18 +59,23 @@ def get_days_remaining(self):
4859
return 'infinite'
4960

5061
def _update_device_status_from_token(self, token, show_result=True):
51-
if self.token_entry_blocked_until > datetime.now() and self.waiting_period_enabled:
62+
if (
63+
self.token_entry_blocked_until > datetime.now()
64+
and self.waiting_period_enabled
65+
):
5266
if show_result:
5367
print('TOKEN_ENTRY_BLOCKED')
5468
return False
5569

56-
token_value, token_type, token_count, updated_counts = OpenPAYGOTokenDecoder.get_activation_value_count_and_type_from_token(
57-
token=token,
58-
starting_code=self.starting_code,
59-
key=self.key,
60-
last_count=self.count,
61-
restricted_digit_set=self.restricted_digit_set,
62-
used_counts=self.used_counts
70+
token_value, token_type, token_count, updated_counts = (
71+
OpenPAYGOTokenDecoder.get_activation_value_count_and_type_from_token(
72+
token=token,
73+
starting_code=self.starting_code,
74+
key=self.key,
75+
last_count=self.count,
76+
restricted_digit_set=self.restricted_digit_set,
77+
used_counts=self.used_counts,
78+
)
6379
)
6480
if token_value is None:
6581
if token_type == TokenType.ALREADY_USED:
@@ -69,7 +85,9 @@ def _update_device_status_from_token(self, token, show_result=True):
6985
if show_result:
7086
print('TOKEN_INVALID')
7187
self.invalid_token_count += 1
72-
self.token_entry_blocked_until = datetime.now() + timedelta(minutes=2**self.invalid_token_count)
88+
self.token_entry_blocked_until = datetime.now() + timedelta(
89+
minutes=2**self.invalid_token_count
90+
)
7391
return -1
7492
elif token_value == -2:
7593
if show_result:
@@ -78,30 +96,40 @@ def _update_device_status_from_token(self, token, show_result=True):
7896
else:
7997
if show_result:
8098
print('TOKEN_VALID', ' | Value:', token_value)
81-
if token_count > self.count or token_value == OpenPAYGOTokenShared.COUNTER_SYNC_VALUE:
99+
if (
100+
token_count > self.count
101+
or token_value == OpenPAYGOTokenShared.COUNTER_SYNC_VALUE
102+
):
82103
self.count = token_count
83104
self.used_counts = updated_counts
84105
self.invalid_token_count = 0
85106
self._update_device_status_from_token_value(token_value, token_type)
86107
return 1
87108

88109
def _update_device_status_from_extended_token(self, token, show_result=True):
89-
if self.token_entry_blocked_until > datetime.now() and self.waiting_period_enabled:
110+
if (
111+
self.token_entry_blocked_until > datetime.now()
112+
and self.waiting_period_enabled
113+
):
90114
if show_result:
91115
print('TOKEN_ENTRY_BLOCKED')
92116

93-
token_value, token_count = OpenPAYGOTokenDecoder.get_activation_value_count_from_extended_token(
94-
token=token,
95-
starting_code=self.starting_code,
96-
key=self.key,
97-
last_count=self.count,
98-
restricted_digit_set=self.restricted_digit_set
117+
token_value, token_count = (
118+
OpenPAYGOTokenDecoder.get_activation_value_count_from_extended_token(
119+
token=token,
120+
starting_code=self.starting_code,
121+
key=self.key,
122+
last_count=self.count,
123+
restricted_digit_set=self.restricted_digit_set,
124+
)
99125
)
100126
if token_value is None:
101127
if show_result:
102128
print('TOKEN_INVALID')
103129
self.invalid_token_count += 1
104-
self.token_entry_blocked_until = datetime.now() + timedelta(minutes=2**self.invalid_token_count)
130+
self.token_entry_blocked_until = datetime.now() + timedelta(
131+
minutes=2**self.invalid_token_count
132+
)
105133
else:
106134
if show_result:
107135
print('Special token entered, value: '+str(token_value))

openpaygo/simulators/server_simulator.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
from datetime import datetime
2+
23
from openpaygo.token_encode import OpenPAYGOTokenEncoder
34
from openpaygo.token_shared import OpenPAYGOTokenShared, TokenType
45

56

67
class SingleDeviceServerSimulator(object):
78

8-
def __init__(self, starting_code, key, starting_count=1, restricted_digit_set=False, time_divider=1):
9+
def __init__(
10+
self,
11+
starting_code,
12+
key,
13+
starting_count=1,
14+
restricted_digit_set=False,
15+
time_divider=1,
16+
):
917
self.starting_code = starting_code
1018
self.key = key
1119
self.count = starting_count
@@ -49,12 +57,16 @@ def generate_token_from_date(self, new_expiration_date, force=False):
4957

5058
if new_expiration_date > furthest_expiration_date:
5159
# If the date is strictly above the furthest date activated, use ADD
52-
value = self._get_value_to_activate(new_expiration_date, self.expiration_date, force)
60+
value = self._get_value_to_activate(
61+
new_expiration_date, self.expiration_date, force
62+
)
5363
self.expiration_date = new_expiration_date
5464
return self._generate_token_from_value(value, mode=TokenType.ADD_TIME)
5565
else:
5666
# If the date is below or equal to the furthest date activated, use SET
57-
value = self._get_value_to_activate(new_expiration_date, datetime.now(), force)
67+
value = self._get_value_to_activate(
68+
new_expiration_date, datetime.now(), force
69+
)
5870
self.expiration_date = new_expiration_date
5971
return self._generate_token_from_value(value, mode=TokenType.SET_TIME)
6072

@@ -82,7 +94,8 @@ def _get_value_to_activate(self, new_time, reference_time, force_maximum=False):
8294
if not force_maximum:
8395
raise Exception('TOO_MANY_DAYS_TO_ACTIVATE')
8496
else:
85-
return OpenPAYGOTokenShared.MAX_ACTIVATION_VALUE # Will need to be activated again after those days
97+
# Will need to be activated again after those days
98+
return OpenPAYGOTokenShared.MAX_ACTIVATION_VALUE
8699
return value
87100

88101
@staticmethod

openpaygo/token_shared.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import codecs
22
import struct
3+
34
import siphash
45

56

@@ -64,7 +65,8 @@ def load_secret_key_from_hex(cls, secret_key):
6465
secret_key_bytes = bytes(secret_key)
6566
if len(secret_key_bytes) != 16:
6667
raise ValueError(
67-
"The secret key provided is not correctly formatted, it should be 16 bytes. "
68+
"The secret key provided is not correctly formatted, it should be 16 "
69+
"bytes. "
6870
)
6971
return secret_key_bytes
7072

0 commit comments

Comments
 (0)