Skip to content

Commit

Permalink
refactor(Rafficer#367): use new fn with 2nd dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
marcel-engelke committed Mar 24, 2024
1 parent 92353fc commit d5b8465
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 14 deletions.
18 changes: 4 additions & 14 deletions protonvpn_cli/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,6 @@ def show_dialog(headline, choices, stop=False):

pull_server_data()

features = {
1: "Secure-Core",
2: "Tor",
4: "P2P",
8: "Streaming",
}
server_tiers = {0: "F", 1: "B", 2: "P"}

servers = get_servers()
Expand Down Expand Up @@ -106,21 +100,17 @@ def show_dialog(headline, choices, stop=False):
get_server_value(servername, "Load", servers)
).rjust(3, " ")

servers_features = []
feat = int(get_server_value(servername, 'Features', servers))
for bit_flag in features:
if (feat & bit_flag) != 0:
servers_features.append(features[bit_flag])
server_features = get_server_features(servername, servers)

if len(servers_features) == 0:
servers_features.append("Normal")
if len(server_features) == 0:
server_features.append("Normal")

tier = server_tiers[
get_server_value(servername, "Tier", servers)
]

choices.append((servername, "Load: {0}% | {1} | {2}".format(
load, tier, ", ".join(servers_features)
load, tier, ", ".join(server_features)
)))

server_result = show_dialog("Choose the server to connect:", choices)
Expand Down
3 changes: 3 additions & 0 deletions protonvpn_cli/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,13 @@ def get_servers():
# Sort server IDs by Tier
return [server for server in servers if server["Tier"] <= user_tier and server["Status"] == 1] # noqa


def get_server_value(servername, key, servers):
"""Return the value of a key for a given server."""
value = [server[key] for server in servers if server['Name'] == servername]
return value[0]


def get_server_features(servername, servers):
"""Decode server feature bit flags and return feature strings in list"""
feat = int(get_server_value(servername, "Features", servers))
Expand All @@ -129,6 +131,7 @@ def get_server_features(servername, servers):
server_features.append(SERVER_FEATURES[bit_flag])
return server_features


def get_config_value(group, key):
"""Return specific value from CONFIG_FILE as string"""
config = configparser.ConfigParser()
Expand Down

0 comments on commit d5b8465

Please sign in to comment.