-
Notifications
You must be signed in to change notification settings - Fork 56
/
common.py
102 lines (84 loc) · 2.57 KB
/
common.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
93
94
95
96
97
98
99
100
101
102
#!/usr/bin/env python
# coding:utf-8
import os
import re
import sys
import codecs
import _winreg
import json
import subprocess
import collections
VERSION = '1.1'
APPNAME = 'PPServ'
AUTHOR = 'zhulin3141@gmail.com'
BASE_DIR = os.getcwd() + '\\'
#运行状态
RUNNING = 'RUNNING'
STOPPED = 'STOPPED'
UNKNOWN = 'UNKNOWN'
def singleton(cls, *args, **kw):
instances = {}
def _singleton():
if cls not in instances:
instances[cls] = cls(*args, **kw)
return instances[cls]
return _singleton
comment_re = re.compile(
'(^)?[^\S\n]*/(?:\*(.*?)\*/[^\S\n]*|/[^\n]*)($)?',
re.DOTALL | re.MULTILINE
)
def load_json(file_path):
'''解析json文件
首先去除注释再用json模块
注释示例:
//...
或
/*
...
*/
'''
with codecs.open(file_path,'r','utf-8') as file:
content = ''.join(file.readlines())
match = comment_re.search(content)
while match:
content = content[:match.start()] + content[match.end():]
match = comment_re.search(content)
return json.loads(content, object_pairs_hook=collections.OrderedDict)
def open_hosts(event):
'''打开hosts文件'''
hostfile = os.environ['SYSTEMROOT'] + '\system32\drivers\etc\hosts'
open_file(hostfile)
def set_autorun(event):
'''设置为开机启动'''
start = sys.argv[0]
run_key_str = r'Software\Microsoft\Windows\CurrentVersion\Run'
try:
run_key = _winreg.OpenKey(HKEY_CURRENT_USER, run_key_str, 0, KEY_ALL_ACCESS)
except:
run_key = _winreg.CreateKey(_winreg.HKEY_CURRENT_USER, run_key_str)
_winreg.SetValueEx(run_key, APPNAME, 0, _winreg.REG_SZ, start)
_winreg.CloseKey(run_key)
def open_main_page(event):
'''用默认浏览器打开设置的主页'''
import webbrowser
port = '80'
webbrowser.open("http://localhost:%s" % port)
def execute(cmd):
'''用命令提示符执行命令
Args:
cmd: 执行的命令
Returns:
执行的结果
'''
import conf
result = ''
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
for line in p.stdout.readlines():
result += line.decode(conf.Conf().get('encoding'))
return result
def open_cmd(path='', cmd=''):
"""在指定的目录下打开cmd"""
os.system('start /D "%s" %s' % (BASE_DIR + path, cmd))
def open_file(file):
import conf
subprocess.Popen([conf.Conf().get('default_editor'), file])