forked from Jrohy/multi-v2ray
-
Notifications
You must be signed in to change notification settings - Fork 0
/
selector.py
92 lines (81 loc) · 3.28 KB
/
selector.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from loader import Loader
from utils import color_str, Color
class CommonSelector:
def __init__(self, collection, msg):
from collections.abc import Iterable
if isinstance(collection, Iterable):
self.collection = collection
else:
raise ValueError("{} 对象不可迭代".format(collection))
self.msg = msg
def select(self):
for index, element in enumerate(self.collection):
print("{0}.{1}".format(index + 1, element))
choice = input(self.msg)
if not choice.isnumeric():
raise RuntimeError('输入错误,请检查是否为数字')
choice = int(choice)
if choice < 1 or choice > len(self.collection):
raise RuntimeError('输入错误,请检查是否符合范围中')
else:
return self.collection[choice - 1]
class Selector:
def __init__(self, action):
loader = Loader()
self.profile = loader.profile
self.group_list = loader.profile.group_list
self.action = action
class ClientSelector(Selector):
def __init__(self, action):
super(ClientSelector, self).__init__(action)
self.list_size = self.group_list[-1].node_list[-1].user_number
if "删除" in action and self.list_size == 1:
print(color_str(Color.RED, "仅剩最后一个节点无法删除!!!"))
self.group = None
elif self.list_size > 1:
self.select_client()
else:
self.group = self.group_list[0]
self.client_index = 0
def select_client(self):
print(self.profile)
self.group = None
choice = input("请输入要{}的节点序号数字: ".format(self.action))
if not choice.isnumeric():
print(color_str(Color.RED, '输入错误,请检查是否为数字'))
return
choice = int(choice)
if choice < 1 or choice > self.list_size:
print(color_str(Color.RED, '输入错误,请检查是否符合范围中'))
else:
find = False
for group in self.group_list:
if find:
break
for index, node in enumerate(group.node_list):
if node.user_number == choice:
self.client_index = index
self.group = group
find = True
break
class GroupSelector(Selector):
def __init__(self, action):
super(GroupSelector, self).__init__(action)
if "删除" in action and len(self.group_list) == 1:
print(color_str(Color.RED, "仅剩最后一个端口无法删除!!!"))
self.group = None
elif len(self.group_list) > 1:
self.select_group()
else:
self.group = self.group_list[0]
def select_group(self):
print(self.profile)
choice = input("请输入要{}的节点Group字母: ".format(self.action))
group_list = [x for x in self.group_list if x.tag == choice]
if len(group_list) == 0:
print(color_str(Color.RED, '输入有误,请检查 {} Group是否存在'.format(choice)))
self.group = None
else:
self.group = group_list[0]