Simplify Lua modules.

This commit is contained in:
Nodir Temirkhodjaev 2015-01-26 09:55:17 +05:00
parent 5b400100fc
commit 8c849420f4
4 changed files with 153 additions and 164 deletions

View File

@ -8,7 +8,9 @@ local util_fs = require"wipf.util.fs"
local util_ip = require"wipf.util.ip"
local APP_GROUP_MAX = 10
local util_conf = {
APP_GROUP_MAX = 10
}
local function parse_app(line)
@ -53,8 +55,8 @@ local function app_groups_to_plain(app_groups)
local groups, groups_count = {}, app_groups.n
local apps_map = {}
if groups_count > APP_GROUP_MAX then
return nil, i18n.tr_fmt('err_conf_app_group_max', APP_GROUP_MAX)
if groups_count > util_conf.APP_GROUP_MAX then
return nil, i18n.tr_fmt('err_conf_app_group_max', util_conf.APP_GROUP_MAX)
end
for i = 1, groups_count do
@ -97,71 +99,73 @@ end
-- Configuration objects meta-table
local conf_meta = {
local conf_meta = {}
set_ip_include_all = function (self, bool)
conf_meta.__index = conf_meta
function conf_meta:set_ip_include_all(bool)
self.ip_include_all = bool
end,
get_ip_include_all = function (self)
end
function conf_meta:get_ip_include_all()
return self.ip_include_all
end,
end
set_ip_exclude_all = function (self, bool)
function conf_meta:set_ip_exclude_all(bool)
self.ip_exclude_all = bool
end,
get_ip_exclude_all = function (self)
end
function conf_meta:get_ip_exclude_all()
return self.ip_exclude_all
end,
end
set_app_log_blocked = function (self, bool)
function conf_meta:set_app_log_blocked(bool)
self.app_log_blocked = bool
end,
get_app_log_blocked = function (self)
end
function conf_meta:get_app_log_blocked()
return self.app_log_blocked
end,
end
set_app_block_all = function (self, bool)
function conf_meta:set_app_block_all(bool)
self.app_block_all = bool
end,
get_app_block_all = function (self)
end
function conf_meta:get_app_block_all()
return self.app_block_all
end,
end
set_app_allow_all = function (self, bool)
function conf_meta:set_app_allow_all(bool)
self.app_allow_all = bool
end,
get_app_allow_all = function (self)
end
function conf_meta:get_app_allow_all()
return self.app_allow_all
end,
end
set_ip_include = function (self, str)
function conf_meta:set_ip_include(str)
self.ip_include = str
end,
get_ip_include = function (self)
end
function conf_meta:get_ip_include()
return self.ip_include
end,
end
set_ip_exclude = function (self, str)
function conf_meta:set_ip_exclude(str)
self.ip_exclude = str
end,
get_ip_exclude = function (self)
end
function conf_meta:get_ip_exclude()
return self.ip_exclude
end,
end
add_app_group = function (self, app_group)
function conf_meta:add_app_group(app_group)
local app_groups = self.app_groups
table.insert(app_groups, app_group)
app_groups.n = app_groups.n + 1
end,
end
remove_app_group = function (self, index)
function conf_meta:remove_app_group(index)
local app_groups = self.app_groups
table.remove(app_groups, index)
app_groups.n = app_groups.n - 1
end,
end
-- Write conf. object to buffer
write = function (self, buf)
function conf_meta:write(buf)
local iprange_from_inc, iprange_to_inc =
util_ip.ip4range_to_numbers(self.ip_include)
@ -182,13 +186,10 @@ local conf_meta = {
end
return true
end,
}
conf_meta.__index = conf_meta
end
-- New conf. object
local function new_conf()
function util_conf.new_conf()
return setmetatable({
ip_include_all = false,
ip_exclude_all = false,
@ -206,41 +207,40 @@ end
-- Application group's meta-table
local app_group_meta = {
set_name = function (self, str)
self.name = str
end,
get_name = function (self)
return self.name
end,
set_enabled = function (self, bool)
self.enabled = bool
end,
get_enabled = function (self)
return self.enabled
end,
set_block = function (self, str)
self.block = str
end,
get_block = function (self)
return self.block
end,
set_allow = function (self, str)
self.allow = str
end,
get_allow = function (self)
return self.allow
end,
}
local app_group_meta = {}
app_group_meta.__index = app_group_meta
function app_group_meta:set_name(str)
self.name = str
end
function app_group_meta:get_name()
return self.name
end
function app_group_meta:set_enabled(bool)
self.enabled = bool
end
function app_group_meta:get_enabled()
return self.enabled
end
function app_group_meta:set_block(str)
self.block = str
end
function app_group_meta:get_block()
return self.block
end
function app_group_meta:set_allow(str)
self.allow = str
end
function app_group_meta:get_allow()
return self.allow
end
-- New app. group object
local function new_app_group()
function util_conf.new_app_group()
return setmetatable({
name = "",
enabled = true,
@ -250,8 +250,4 @@ local function new_app_group()
end
return {
APPGROUP_MAX = APPGROUP_MAX,
new_conf = new_conf,
new_app_group = new_app_group,
}
return util_conf

View File

@ -5,20 +5,18 @@ local sys = require"sys"
local win32 = sys.win32
local util_fs = {}
-- Get Process's Native path by identifier
local function pid_dospath(id)
function util_fs.pid_dospath(id)
local pid = sys.pid(id, true)
local path = (pid and pid:path()) or ""
pid:close()
return path
end
-- Convert DOS device name to drive letter (A: .. Z:)
local dosname_to_drive
-- Convert drive letter (A: .. Z:) to DOS device name
local drive_to_dosname
-- DOS device name <-> drive letter (A: .. Z:)
do
local drives
@ -39,35 +37,37 @@ do
return drives[key] or ""
end
dosname_to_drive = function (dos_name)
-- Convert DOS device name to drive letter (A: .. Z:)
function util_fs.dosname_to_drive(dos_name)
return get_value(dos_name:lower())
end
drive_to_dosname = function (drive)
-- Convert drive letter (A: .. Z:) to DOS device name
function util_fs.drive_to_dosname(drive)
return get_value(drive:upper())
end
end
-- Convert Native path to Win32 path
local function dospath_to_path(dos_path)
function util_fs.dospath_to_path(dos_path)
local dos_name, sub_path = string.match(dos_path, [[(\[^\]+\[^\]+)(\.+)]])
if not dos_name then
return dos_path
end
return dosname_to_drive(dos_name) .. sub_path
return util_fs.dosname_to_drive(dos_name) .. sub_path
end
-- Convert Win32 path to Native path
local function path_to_dospath(path)
function util_fs.path_to_dospath(path)
local drive, sub_path = string.match(path, [[(%a:)(\.+)]])
if not drive then
return path
end
return drive_to_dosname(drive) .. sub_path
return util_fs.drive_to_dosname(drive) .. sub_path
end
-- Load file, run it in sandbox and return it's globals in a table
function sandbox(path)
function util_fs.sandbox(path)
local chunk, err_msg = loadfile(path)
if not chunk then
return nil, err_msg
@ -79,9 +79,4 @@ function sandbox(path)
end
return {
pid_dospath = pid_dospath,
dospath_to_path = dospath_to_path,
path_to_dospath = path_to_dospath,
sandbox = sandbox,
}
return util_fs

View File

@ -3,25 +3,27 @@
local util_fs = require"wipf.util.fs"
local i18n = {}
local current_lang, lang_strings
local function set_current_lang(lang)
function i18n.set_current_lang(lang)
current_lang = lang
lang_strings = assert(util_fs.sandbox("lua/wipf/lang/" .. lang .. ".lua"))
end
local function get_current_lang()
function i18n.get_current_lang()
return current_lang
end
-- Get translation text for l10n identifier
local function tr(id)
function i18n.tr(id)
return lang_strings[id] or id
end
-- Get formatted translation text for l10n identifier
local function tr_fmt(id, ...)
function i18n.tr_fmt(id, ...)
local s = lang_strings[id]
if not s then
return id
@ -32,12 +34,7 @@ end
-- Set default language English
set_current_lang("en")
i18n.set_current_lang("en")
return {
set_current_lang = set_current_lang,
get_current_lang = get_current_lang,
tr = tr,
tr_fmt = tr_fmt,
}
return i18n

View File

@ -6,8 +6,10 @@ local sys = require"sys"
local sock = require"sys.sock"
-- Convert IPv4 ranges in text to 'from_ip4' & 'to_ip4' arrays with numbers
local ip4range_to_numbers
local util_ip = {}
-- Convert IPv4 ranges
do
-- sort and try to merge ranges
local function iprange_map_merge(map)
@ -82,7 +84,8 @@ do
return from_ip, to_ip
end
ip4range_to_numbers = function (text)
-- Convert IPv4 ranges in text to 'from_ip4' & 'to_ip4' arrays with numbers
function util_ip.ip4range_to_numbers(text)
local iprange_map = {}
local line_no = 0
@ -103,6 +106,4 @@ do
end
return {
ip4range_to_numbers = ip4range_to_numbers,
}
return util_ip