Skip to content
This repository has been archived by the owner on Aug 3, 2024. It is now read-only.

Commit

Permalink
Merge branch 'HostsUtl-Desktop-Dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
huhamhire committed Jan 24, 2014
2 parents a25997a + 7e60b41 commit 60e3cda
Show file tree
Hide file tree
Showing 33 changed files with 795 additions and 581 deletions.
24 changes: 17 additions & 7 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ Hosts Setup Utility

Introduction
============
Since the governments of some countries are using the blocking the internet
access to several websites and web service providers which includes some world
famous sites like Google, YouTube, twitter, Facebook, and Wikipedia etc., we
designed this tiny utility in order to help people getting through the
Internet blockade.
Since the governments of some countries are using technical methods blocking
internet access to some websites and web service providers which includes some
world famous sites like Google, YouTube, twitter, Facebook, and Wikipedia
etc., we designed this tiny utility in order to help people getting through
the Internet blockade.

`Hosts Setup Utility` provides basic tools to manage the hosts file on current
operating systems. It also provides both support for Graphical Desktop
Expand Down Expand Up @@ -148,8 +148,18 @@ Text-based User Interface (TUI) Mode
not, a warning message box would show up and then terminate current
session.

If current session is running with `cmd` on a non-english Windows system,
command ``chcp 437`` is required by curses to set the default code page.

User Customized Hosts
=====================

Users are allowed to add customized hosts list as an independent module to
make a hosts file. All you need to do is create a simple text file named
``custom.hosts`` in the working directory, and put your own hosts entries
into this file. Then you would find a `Customized Hosts` option in the
function list.

* Non-ASCII characters are not recommended to be put into the
customized hosts file.


Author/Maintainer
Expand Down
3 changes: 2 additions & 1 deletion __version__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# __version__.py :
# __version__.py : Version info for Hosts Setup Tool.
#
# Copyleft (C) 2014 - huhamhire hosts team <[email protected]>
# =====================================================================
Expand All @@ -15,4 +15,5 @@
# =====================================================================

__version__ = "1.9.8"
__release__ = "beta"
__revision__ = "$Id$"
10 changes: 5 additions & 5 deletions _build.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
]),
(".", [
"LICENSE",
"README.md",
"README.rst",
"network.conf",
]),
]
Expand All @@ -90,7 +90,7 @@
"gui/theme/*.qss",
"*/*.py",
"LICENSE",
"README.md",
"README.rst",
"network.conf",
]
excludes = [
Expand Down Expand Up @@ -194,7 +194,7 @@
author_email=AUTHOR_EMAIL,
license=LICENSE,
url=URL,
zipfile="lib/shared.zip",
zipfile="lib/shared.lib",
data_files=DATA_FILES,
classifiers=CLASSIFIERS,
)
Expand Down Expand Up @@ -235,7 +235,7 @@
from setuptools import setup
# Set working directories
WORK_DIR = SCRIPT_DIR + "work/"
RES_DIR = SCRIPT_DIR + "res/"
RES_DIR = SCRIPT_DIR + "res/mac/"
APP_NAME = "HostsTool.app"
APP_PATH = WORK_DIR + APP_NAME
DIST_DIR = APP_PATH + "/Contents/"
Expand Down Expand Up @@ -296,7 +296,7 @@
# Clean work directory after build
os.remove(DIST_DIR + "Resources/applet.icns")
shutil.copy2(
SCRIPT_DIR + "img/icons/hosts_utl.icns",
SCRIPT_DIR + "res/img/icons/hosts_utl.icns",
DIST_DIR + "Resources/applet.icns")
shutil.copy2(RES_DIR + "Info.plist", DIST_DIR + "Info.plist")
shutil.rmtree(SCRIPT_DIR + "build/")
Expand Down
10 changes: 5 additions & 5 deletions doc/dev/index.rst
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
Hosts Setup Utility Developer Documentation
===========================================

Since the government of some countries are using the blocking the internet
access to several websites and web service providers which includes some world
famous sites like Google, YouTube, twitter, Facebook, and Wikipedia etc., we
designed this tiny utility in order to help people getting through the
Internet blockade.
Since the governments of some countries are using technical methods blocking
internet access to some websites and web service providers which includes some
world famous sites like Google, YouTube, twitter, Facebook, and Wikipedia
etc., we designed this tiny utility in order to help people getting through
the Internet blockade.

Chapter one of this documentation contains a brief introduction of
`Hosts Setup Utility` and basic usage to start this tool. While the others
Expand Down
20 changes: 17 additions & 3 deletions doc/dev/intro.rst
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,23 @@ Text-based User Interface (TUI) Mode

.. seealso:: :class:`~hoststool.UtilLauncher`.

.. warning:: If current session is running with `cmd` on a non-english
Windows system, command ``chcp 437`` is required by curses to set the
default code page.

.. _intro-customize:

User Customized Hosts
---------------------

Users are allowed to add customized hosts list as an independent module to
make a hosts file. All you need to do is create a simple text file named
``custom.hosts`` in the working directory, and put your own hosts entries
into this file. Then you would find a `Customized Hosts` option in the
function list.

.. warning:: Non-ASCII characters are not recommended to be put into the
customized hosts file.

.. versionadded:: 1.9.8


.. note:: Specific user manual is not included in this documentation. For
further information, please visit our
Expand Down
6 changes: 6 additions & 0 deletions gui/__list_trans.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
# __list_trans (list): A list containing names of function list items
# for translator to translate.
__list_trans = [
_translate("Util", "customize", None),
_translate("Util", "google", None),
_translate("Util", "google-apis", None),
_translate("Util", "google(cn)", None),
_translate("Util", "google(hk)", None),
_translate("Util", "google(us)", None),
Expand All @@ -34,6 +37,9 @@
_translate("Util", "wikipedia", None),
_translate("Util", "institutions", None),
_translate("Util", "steam", None),
_translate("Util", "github", None),
_translate("Util", "dropbox", None),
_translate("Util", "wordpress", None),
_translate("Util", "others", None),
_translate("Util", "adblock-hostsx", None),
_translate("Util", "adblock-mvps", None),
Expand Down
132 changes: 16 additions & 116 deletions gui/_make.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,20 @@
import sys
sys.path.append("..")
from util import RetrieveData
from util import MakeHosts


class QSubMakeHosts(QtCore.QThread):
class QSubMakeHosts(QtCore.QThread, MakeHosts):
"""
QSubMakeHosts is a subclass of :class:`PyQt4.QtCore.QThread`. This class
contains methods to make a new hosts file for client.
QSubMakeHosts is a subclass of :class:`PyQt4.QtCore.QThread` and class
:class:`~util.makehosts.MakeHosts`. This class contains methods to make a
new hosts file for client.
.. inheritance-diagram:: gui._make.QSubMakeHosts
:parts: 1
.. note:: The instance of this class should be created in an individual
thread. And an instance of class should be set as :attr:`parent`
thread. And an instance of class should be set as :attr:`parent`
here.
:ivar PyQt4.QtCore.pyqtSignal info_trigger: An instance of
Expand Down Expand Up @@ -72,37 +74,12 @@ class QSubMakeHosts(QtCore.QThread):
.. seealso:: Method :meth:`~gui.qdialog_d.QDialogDaemon.move_hosts`
in :class:`~gui.qdialog_d.QDialogDaemon` class.
:ivar int count: Number of the module being processed currently in the
operation sequence.
:ivar str make_mode: Operation mode for making hosts file. The valid value
could be one of `system`, `ansi`, and `utf-8`.
.. seealso:: :attr:`make_mode` in
:class:`~gui.qdialog_d.QDialogDaemon` class.
:ivar str hostname: File Name of hosts file.
:ivar file hosts_file: The hosts file to write hosts to.
:ivar dict make_cfg: Configuration to make a new hosts file.
:ivar int mod_num: Total number of modules written to hosts file.
:ivar str eol: End-of-Line used by the hosts file created.
:ivar time make_time: Timestamp of making hosts file.
.. seealso:: :class:`tui._make.MakeHosts` class.
.. seealso:: :class:`util.makehosts.MakeHosts` class.
"""
info_trigger = QtCore.pyqtSignal(str, int)
fina_trigger = QtCore.pyqtSignal(str, int)
move_trigger = QtCore.pyqtSignal()

count = 0
make_mode = ""

hostname = ""
hosts_file = None
make_cfg = {}
mod_num = 0
eol = ""
make_time = None

def __init__(self, parent):
"""
Initialize a new instance of this class. Retrieve configuration from
Expand All @@ -114,40 +91,21 @@ class to fetch settings from.
.. warning:: :attr:`parent` MUST NOT be set as `None`.
"""
super(QSubMakeHosts, self).__init__(parent)
self.count = 0
self.make_cfg = parent.make_cfg
self.make_mode = parent.make_mode
make_path = parent.make_path
self.hostname = parent.hostname
if parent.make_mode == "system":
self.eol = parent.sys_eol
self.hosts_file = open("hosts", "wb")
elif parent.make_mode == "ansi":
self.eol = "\r\n"
self.hosts_file = open(unicode(make_path), "wb")
elif parent.make_mode == "utf-8":
self.eol = "\n"
self.hosts_file = open(unicode(make_path), "wb")
QtCore.QThread.__init__(self, parent)
MakeHosts.__init__(self, parent)

def run(self):
"""
Start operations to retrieve data from the data file and generate new
hosts file.
"""
RetrieveData.connect_db()
start_time = time.time()
self.make_time = start_time
self.write_head()
self.write_info()
self.get_hosts(self.make_cfg)
self.hosts_file.close()
self.make()
end_time = time.time()
total_time = "%.4f" % (end_time - start_time)
self.fina_trigger.emit(total_time, self.count)
if self.make_mode == "system":
self.move_trigger.emit()
RetrieveData.disconnect_db()

def get_hosts(self, make_cfg):
"""
Expand All @@ -167,69 +125,11 @@ def get_hosts(self, make_cfg):
mods = RetrieveData.get_ids(mod_cfg)
for mod_id in mods:
self.mod_num += 1
hosts, mod_name = RetrieveData.get_host(part_id, mod_id)
self.info_trigger.emit(mod_name, self.mod_num)
if part_id == 0x02:
self.write_localhost_mod(part_id, mod_id)
self.write_localhost_mod(hosts)
elif part_id == 0x04:
self.write_customized()
else:
self.write_common_mod(part_id, mod_id)

def write_head(self):
"""
Write the head part into the new hosts file.
"""
for head_str in RetrieveData.get_head():
self.hosts_file.write("%s%s" % (head_str[0], self.eol))

def write_info(self):
"""
Write the information part into the new hosts file.
"""
info = RetrieveData.get_info()
info_lines = [
"#",
"# %s: %s" % ("Version", info["Version"]),
"# %s: %s" % ("BuildTime", info["Buildtime"]),
"# %s: %s" % ("ApplyTime", int(self.make_time)),
"#"
]
for line in info_lines:
self.hosts_file.write("%s%s" % (line, self.eol))

def write_common_mod(self, part_id, mod_id):
"""
Write hosts entries in a module specified by `mod_id` from a part of
the data file to the new hosts file specified by `part_id`.
:param part_id: Index number of a part in the data file.
:type part_id: int
:param mod_id: Index number of a module in the data file.
:type mod_id: int
"""
hosts, mod_name = RetrieveData.get_host(part_id, mod_id)
self.info_trigger.emit(mod_name, self.mod_num)
self.hosts_file.write(
"%s# Section Start: %s%s" % (self.eol, mod_name, self.eol))
for host in hosts:
self.hosts_file.write("%s %s%s" % (host[0], host[1], self.eol))
self.count += 1
self.hosts_file.write("# Section End: %s%s" % (mod_name, self.eol))

def write_localhost_mod(self, part_id, mod_id):
"""
Write hosts entries in a localhost module specified by `mod_id` from a
part of the data file to the new hosts file specified by `part_id`.
:param part_id: Index number of a part in the data file.
:type part_id: int
:param mod_id: Index number of a module in the data file.
:type mod_id: int
"""
hosts, mod_name = RetrieveData.get_host(part_id, mod_id)
self.info_trigger.emit(mod_name, self.mod_num)
self.hosts_file.write(
"%s# Section Start: Localhost%s" % (self.eol, self.eol))
for host in hosts:
if "#Replace" in host[1]:
host = (host[0], self.hostname)
self.hosts_file.write("%s %s%s" % (host[0], host[1], self.eol))
self.count += 1
self.hosts_file.write("# Section End: Localhost%s" % (self.eol))
self.write_common_mod(hosts, mod_name)
1 change: 1 addition & 0 deletions gui/hostsutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ def init_main(self):
:class:`~tui.hostsutil.HostsUtil` class.
"""
self.ui.SelectMirror.clear()
self.set_version()
# Set mirrors
self.mirrors = CommonUtil.set_network("network.conf")
self.set_mirrors()
Expand Down
Loading

0 comments on commit 60e3cda

Please sign in to comment.