Improve profile retrieval-related logic

This commit is contained in:
codeking 2024-10-13 08:14:33 +02:00
parent b34a6f8935
commit 669cddc2b4
5 changed files with 38 additions and 17 deletions

View file

@ -96,25 +96,28 @@ class BaseProfile:
@staticmethod
def exists(id: int):
return os.path.isdir(BaseProfile.__get_config_path(id)) and re.match(r'^\d+$', str(id))
return re.match(r'^\d{1,2}$', str(id)) and os.path.isfile(f'{BaseProfile.__get_config_path(id)}/config.json')
@staticmethod
def all():
profiles = {}
for id in map(int, sorted(os.listdir(Constants.SP_PROFILE_CONFIG_HOME))):
for directory_entry in os.listdir(Constants.SP_PROFILE_CONFIG_HOME):
try:
id = int(directory_entry)
except ValueError:
continue
if BaseProfile.exists(id):
if os.path.exists(f'{BaseProfile.__get_config_path(id)}/config.json'):
profile = BaseProfile.find_by_id(id)
profile = BaseProfile.find_by_id(id)
if profile is not None:
profiles[id] = profile
if profile is not None:
profiles[id] = profile
return profiles
return dict(sorted(profiles.items()))
@staticmethod
def save(profile):

View file

@ -21,6 +21,9 @@ class Subscription:
)
)
def get_sanitized_billing_code(self):
return '-'.join(['****'] * 3 + [self.billing_code[-4:]])
def has_been_activated(self):
return self.expires_at is not None

View file

@ -60,7 +60,7 @@ class SessionProfile(BaseProfile):
return proxy_configuration
def has_proxy_configuration(self):
return os.path.exists(f'{self.get_config_path()}/proxy.json')
return os.path.isfile(f'{self.get_config_path()}/proxy.json')
def has_wireguard_configuration(self):
return os.path.exists(f'{self.get_config_path()}/wg.conf')
return os.path.isfile(f'{self.get_config_path()}/wg.conf')

View file

@ -45,7 +45,7 @@ class SystemProfile(BaseProfile):
return f'{self.get_system_config_path()}/wg.conf'
def has_wireguard_configuration(self):
return os.path.exists(f'{self.get_system_config_path()}/wg.conf')
return os.path.isfile(f'{self.get_system_config_path()}/wg.conf')
def delete(self):

27
main.py
View file

@ -20,7 +20,7 @@ from core.observers.ConnectionObserver import ConnectionObserver
from core.observers.InvoiceObserver import InvoiceObserver
from core.observers.ProfileObserver import ProfileObserver
from pathlib import Path
from typing import Optional
from typing import Optional, Union
import argparse
import pprint
import re
@ -44,10 +44,10 @@ if __name__ == '__main__':
invoice_observer.subscribe('processing', lambda event: print('A payment has been detected and is being verified...\n'))
invoice_observer.subscribe('settled', lambda event: print('The payment has been successfully verified.\n'))
profile_observer.subscribe('created', lambda event: pprint.pp((event.subject, 'Created')))
profile_observer.subscribe('destroyed', lambda event: pprint.pp((event.subject, 'Destroyed')))
profile_observer.subscribe('disabled', lambda event: pprint.pp((event.subject, 'Disabled')) if event.meta.get('explicitly') else None)
profile_observer.subscribe('enabled', lambda event: pprint.pp((event.subject, 'Enabled')))
profile_observer.subscribe('created', lambda event: pprint.pp((__sanitize_profile(event.subject), 'Created')))
profile_observer.subscribe('destroyed', lambda event: pprint.pp((__sanitize_profile(event.subject), 'Destroyed')))
profile_observer.subscribe('disabled', lambda event: pprint.pp((__sanitize_profile(event.subject), 'Disabled')) if event.meta.get('explicitly') else None)
profile_observer.subscribe('enabled', lambda event: pprint.pp((__sanitize_profile(event.subject), 'Enabled')))
def __parse_application_string(application_string: Optional[str] = None):
@ -61,6 +61,15 @@ if __name__ == '__main__':
else:
return parsed_application_string.groupdict()
def __sanitize_profile(candidate: Optional[Union[SessionProfile, SystemProfile]]):
if candidate is not None and candidate.has_subscription():
sanitized_billing_code = candidate.subscription.get_sanitized_billing_code()
candidate.subscription.billing_code = sanitized_billing_code
return candidate
pristine_parser = argparse.ArgumentParser(add_help=False)
pristine_parser.add_argument('--pristine', '-p', action='store_true')
@ -144,7 +153,13 @@ if __name__ == '__main__':
profile_parser.print_help()
elif arguments.subcommand == 'list':
pprint.pp(ProfileController.get_all())
profiles = ProfileController.get_all()
for key, value in profiles.items():
profiles[key] = __sanitize_profile(value)
pprint.pp(profiles)
elif arguments.subcommand == 'show':
pprint.pp(ProfileController.get(arguments.id))