11import sys
22import random
3+ import os
4+ import platform
35
6+ from collections import Counter
7+
8+ target = []
9+ output = False
10+ output_path = ''
411
512logo = """
613 _ _ _ _____ _____ _____ _____ _____
1623-h Print this help message
1724-t Convert the text,must be include by ''
1825-f Convert the text from files
26+ -o Output the result into a file
1927""" .strip ()
2028
21- def func (text ):
22- """
23- 核心类(core)
24- -------------------------------------
25- if嵌套 判断字母__true____lower__add
26- | (random)|
27- | |__upper__add
28- |
29- |____false__add
30- (代码写的臭 if嵌套用时爽,事后一直火葬场)
31- """
32- res = ""
33- for i in text :
34- if i .isalpha ():
35- # print(f"i是{i}, 是字母")
36- if random .choice ([True , False ]):
37- # print(1)
38- letter = i .upper ()
39- res = "" .join ([res , letter ])
40- else :
41- # print(0)
42- letter = i .lower ()
43- res = "" .join ([res , letter ])
44- else :
45- # print(f"i是{i}, 不是是字母")
46- res = "" .join ([res , i ])
47- return res
48-
49- def OpenFiles (path ):
50- """
51- OpenFiles: 打开文件
52- ------------------------------------
53- ToDO: 完善对读取文件中的异常处理
54- """
55- with open (path , 'r' ) as f :
56- code = f .read ()
57- return code
58-
59-
60- def P_text (word = '' ):
61- """
62- P_text: 打印一个带logo的提示语
63- ------------------------------------
64- 其实都是混子代码(指join)
65- """
66- if word :
67- print_list = []
68- print_list .append (logo )
69- print_list .append (word )
70- w = ''
71- print (w .join (print_list ))
72- else :
73- print (logo )
29+ class Error (ValueError ):
30+ pass
31+
32+ def OpenFiles (path ,output ):
33+ """
34+ OpenFiles: 打开文件
35+ ------------------------------------
36+ ToDO: 完善对读取文件中的异常处理
37+ """
38+ with open (path , 'r' ) as f :
39+ code = f .read ()
40+ func (code ,output )
41+
42+ def func (cont ,output ):
43+ """
44+ 核心类(core)
45+ -------------------------------------
46+ if嵌套 判断字母__true____lower__add
47+ | (random)|
48+ | |__upper__add
49+ |
50+ |____false__add
51+ (代码写的臭 if嵌套用时爽,事后一直火葬场)
52+ """
53+ res = ""
54+ for i in cont :
55+ if i .isalpha ():
56+ # print(f"i是{i}, 是字母")
57+ if random .choice ([True , False ]):
58+ # print(1)
59+ letter = i .upper ()
60+ res = "" .join ([res , letter ])
61+ else :
62+ # print(0)
63+ letter = i .lower ()
64+ res = "" .join ([res , letter ])
65+ else :
66+ # print(f"i是{i}, 不是是字母")
67+ res = "" .join ([res , i ])
68+ P_text (f"return:{ res } " )
69+ # print(output)
70+ # print(output_path)
71+ if output :
72+ OutPut (res )
73+ else :
74+ sys .exit (0 )
75+
76+ def OutPut (cont ):
77+ """
78+ Output: 输出文件
79+ ------------------------------------
80+ 他甚至在文件检验是否存在
81+ """
82+ # print(output_path)
83+ if os .path .exists (output_path ):
84+ while True :
85+ inp = input ("The file already exists. Do you want to overwrite it(yes/no)?" )
86+ if inp == 'yes' or inp == 'y' :
87+ print ("Alright!They have been overwrite.That depends on your brilliant choice.Just YOLO!" )
88+ break
89+ elif inp == 'no' or inp == 'n' :
90+ print ("Your file isn't be change, they are very safe! \n Thank for your use" )
91+ sys .exit (0 )
92+ with open (output_path , 'w' ) as f :
93+ f .write (cont )
94+ sys .exit (0 )
95+
96+ def P_text (word = "" ):
97+ """
98+ P_text: 打印一个带logo的提示语
99+ ------------------------------------
100+ 其实都是混子代码(指join)
101+ """
102+ print_list = []
103+ print_list .append (logo )
104+ print_list .append (word )
105+ print (("-" * 60 + '\n ' ).join (print_list ))
106+
107+ def CheckOS ():
108+ if platform .system () != "Darwin" :
109+ print ("WARNING: It seems like you're using a non-Apple device, which is not cool!" )
110+
111+
112+ def get_help ():
113+ P_text (PRINT_HELP )
114+ sys .exit (0 )
115+
116+ key_list = {
117+ "-t" : func ,
118+ "-f" : OpenFiles ,
119+ }
74120
75121def Target (text ):
76- """
77- Target: 判断传入的参数
78- -----------------------------------
79- if嵌套 验空==>判断参数
80- (if嵌套不是好方法,小孩不要学 逃)
81- """
82- # print(text)
83- if text :
84- if text [0 ] == '-h' :
85- P_text (PRINT_HELP )
86-
87- elif text [0 ] == '-t' :
88- # print("-t")
89- res = func (text [1 ])
90- P_text ()
91- print (f"return: { res } " )
92-
93-
94- elif text [0 ] == '-f' :
95- # print("-f")
96- P_text ("Reading files...." )
97- files = OpenFiles (text [1 ])
98- res = func (files )
99- P_text ()
100- print (f"return: { res } " )
101-
102-
103- else :
104- print (f'Invalid keyword: { text } , Maybe you can try "-h" to get help.' )
105-
106- else :
107- # print("他是空的")
108- P_text (PRINT_HELP )
109- return 0
122+ key = []
123+ key_i = []
124+
125+ CheckOS ()
126+ # 收集参数
127+ for i ,t in enumerate (text ):
128+ if t .startswith ('-' ):
129+ key .append (t )
130+ key_i .append (i )
131+ # print(f"key:{key},key_i{key_i}")
132+
133+ # 判断是否输出
134+ if "-o" in key :
135+ global output
136+ output = True
137+ i = text .index ("-o" )
138+ global output_path
139+ output_path = text [i + 1 ]
140+
141+ # 参数查重
142+ c_repeat = dict (Counter (key ))
143+ if [key for key ,value in c_repeat .items ()if value > 1 ] or ("-t" in key and "-f" in key ) or ("-h" in key and ("-t" in key or "-f" in key )):
144+ raise Error ("There are some parm is repetitive, please work out them at first." )
145+
146+ # 解析参数
147+ for i ,k in enumerate (key ):
148+ if k not in key_list .keys ():
149+ raise Error (f'Invalid keyword: { k } , Maybe you can try "-h" to get help.' )
150+
151+ if k == '-h' :
152+ get_help ()
153+
154+ if k in key_list .keys ():
155+ # print(k,i)
156+ keyword = key_list [k ]
157+ keyword_i = text [key_i [i ] + 1 ]
158+ # print(keyword,keyword_i)
159+ keyword (keyword_i ,output )
110160
111161
112162def main ():
113- """
114- 这个不是main函数 ;)
115- This isn't a main funication
116- """
117- target = sys .argv [1 :] #传入参数
118- # print(f"main:{target}")
119- Target (target )
120-
163+ CheckOS ()
164+ target = sys .argv [1 :]
165+ Target (target )
121166
122167
123168if __name__ == '__main__' :
124- main ()
169+ main ()
0 commit comments