forked from huhamhire/huhamhire-hosts
-
Notifications
You must be signed in to change notification settings - Fork 1
/
hoststool.py
157 lines (126 loc) · 4.94 KB
/
hoststool.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# hoststool.py : Launch the utility.
#
# Copyleft (C) 2014 - huhamhire hosts team <[email protected]>
# =====================================================================
# Licensed under the GNU General Public License, version 3. You should
# have received a copy of the GNU General Public License along with
# this program. If not, see <http://www.gnu.org/licenses/>.
#
# This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING
# THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE.
# =====================================================================
__author__ = "huhamhire <[email protected]>"
import os
from optparse import OptionParser
import gui
import tui
from __version__ import __version__
import sys
sys.path.append("..")
from util import CommonUtil
class UtilLauncher(object):
"""
HostsUtil class is the entrance to launch `Hosts Setup Utility`. This
class contains methods for user to decide whether to start a session in
Graphical User Interface (GUI) mode or in Text-based User Interface (TUI)
mode.
Usage can be printed to screen via terminal by using ``-h`` or ``--help``
argument. The help message would be like this::
Usage: hoststool.py [-g] [-t] [-h] [--version]
Options:
--version show program's version number and exit
-h, --help show this help message and exit
-g, --graphicui launch in GUI(QT) mode
-t, --textui launch in TUI(Curses) mode
.. seealso:: :ref:`Get Started <intro-get-started>`.
.. note:: All methods from this class are declared as `classmethod`.
"""
@classmethod
def launch(cls):
"""
Launch `Hosts Setup Utility`.
.. note:: This is a `classmethod`.
"""
options, args = cls.set_commands()
if options.tui:
cls.launch_tui()
elif options.gui:
cls.launch_gui()
@classmethod
def set_commands(cls):
"""
Set the command options and arguments to launch `Hosts Setup Utility`.
.. note:: This is a `classmethod`.
:return: :meth:`hoststool.UtilLauncher.set_commands` returns two
values:
(:attr:`options`, :attr:`args`)
* options(`optparse.Values`): An instance of
:class:`optparse.Values` containing values for all of your
options—e.g. if --file takes a single string argument, then
options.file will be the filename supplied by the user, or None
if the user did not supply that option args, the list of
positional arguments leftover after parsing options.
* args(`list`): Positional arguments leftover after parsing
options.
.. seealso:: `OptionParser
<http://docs.python.org/2/library/optparse.html>`_.
:rtype: :class:`optparse.Values`, list
"""
usage = "usage: %prog [-g] [-t] [-h] [--version]"
version = "Hosts Setup Utility v" + __version__
parser = OptionParser(usage=usage, version=version)
parser.add_option("-g", "--graphicui", dest="gui",
default=True, action="store_true",
help="launch in GUI(QT) mode")
parser.add_option("-t", "--textui", dest="tui",
default=False, action="store_true",
help="launch in TUI(Curses) mode ")
return parser.parse_args()
@classmethod
def get_custom_conf_path(cls):
if not CommonUtil.check_platform()[0] == "Windows":
path = os.path.expanduser('~/.custom.hosts')
if not os.path.isfile(path):
path = os.path.expanduser('~/custom.hosts')
if os.path.isfile(path):
return path
return None
@classmethod
def launch_gui(cls):
"""
Start a Graphical User Interface (GUI) session of
`Hosts Setup Utility`.
.. note:: This is a `classmethod`.
"""
main = gui.HostsUtil()
path = UtilLauncher.get_custom_conf_path()
if path is not None:
main.custom = path
main.start()
@classmethod
def launch_tui(cls):
"""
Start a Text-based User Interface (TUI) session of
`Hosts Setup Utility`.
.. note:: This is a `classmethod`.
"""
# Set code page for Windows
system = CommonUtil.check_platform()[0]
cp = "437"
if system == "Windows":
chcp = os.popen("chcp")
cp = chcp.read().split()[-1]
chcp.close()
os.popen("chcp 437")
main = tui.HostsUtil()
main.custom = UtilLauncher.get_custom_conf_path()
main.start()
# Restore the default code page for Windows
if system == "Windows":
os.popen("chcp " + cp)
if __name__ == "__main__":
UtilLauncher.launch()