| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | local lfs = require 'lfs' |
| |
|
| | local private = {} |
| | local format, gsub, sub, match, len, pairs, ipairs = |
| | string.format, string.gsub, string.sub, string.match, string.len, pairs, ipairs |
| | local yield, insert, sort, remove, type = |
| | coroutine.yield, table.insert, table.sort, table.remove, type |
| |
|
| | local TAIL_CALL = rawget(_G, 'setfenv') and '%(tail call%)' or '%(%.%.%.tail calls%.%.%.%)' |
| |
|
| | local CALL_TO_NEW = {__call = function(lib, ...) return lib.new(...) end} |
| |
|
| | local lib = {} |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | |
| | |
| |
|
| | |
| | lib.VERSION = '1.1.0' |
| |
|
| | lib.DEPENDS = { |
| | |
| | "lua >= 5.1, < 5.4", |
| | |
| | "luafilesystem >= 1.4.0", |
| | } |
| |
|
| | |
| | lib.DESCRIPTION = { |
| | summary = "Lubyk base module.", |
| | detailed = [[ |
| | lub: helper code, class declaration. |
| | |
| | lub.Autoload: autoloading classes in modules. |
| | |
| | lub.Dir: a simple directory traversal class. |
| | |
| | lub.Template: a simple templating class that uses {{moustache}} like syntax. |
| | |
| | lub.Param: script parameter save/restore. |
| | ]], |
| | homepage = "http://doc.lubyk.org/lub.html", |
| | author = "Gaspard Bucher", |
| | license = "MIT", |
| | } |
| |
|
| | |
| | lib.BUILD = { |
| | github = "lubyk", |
| | pure_lua = true, |
| | } |
| |
|
| | local SYSTINFO |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | function lib.plat() |
| | if not SYSTINFO then |
| | local cfg = require 'luarocks.cfg' |
| | local ok, system = pcall(function() |
| | return io.popen("uname -s"):read("*l") |
| | end) |
| | if not ok then |
| | error('Cannot get platform information without "io.popen"') |
| | end |
| | |
| | SYSTINFO = {SYSTEM = system} |
| |
|
| | if system == 'Darwin' then |
| | SYSTINFO.PLAT = 'macosx' |
| | elseif system:match('^CYGWIN') then |
| | SYSTINFO.PLAT = 'win32' |
| | SYSTINFO.PLAT_DETAIL = 'Cygwin' |
| | elseif system and system:match("^Windows") then |
| | SYSTINFO.PLAT = 'win32' |
| | SYSTINFO.PLAT_DETAIL = 'Windows' |
| | elseif system and system:match("^MINGW") then |
| | SYSTINFO.PLAT = 'win32' |
| | SYSTINFO.PLAT_DETAIL = 'MinGW' |
| | else |
| | SYSTINFO.PLAT = 'unix' |
| | end |
| | end |
| | return SYSTINFO.PLAT, SYSTINFO.PLAT_DETAIL, SYSTINFO.SYSTEM |
| | end |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | function lib.class(class_name, tbl) |
| | local lib = tbl or {} |
| | lib.type = class_name |
| | lib.__index = lib |
| |
|
| | return setmetatable(lib, CALL_TO_NEW) |
| | end |
| |
|
| | |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | |
| | local function itdeepSearch(data, func, depth, max_depth) |
| | local end_reached = true |
| | local result |
| | for _, child in ipairs(data) do |
| | if type(child) == 'table' then |
| | if depth == max_depth then |
| | local r = func(child) |
| | if r then |
| | return r |
| | elseif child[1] then |
| | |
| | end_reached = false |
| | end |
| | elseif child[1] then |
| | |
| | local r, e = itdeepSearch(child, func, depth + 1, max_depth) |
| | if r then |
| | return r |
| | else |
| | end_reached = end_reached and e |
| | end |
| | end |
| | end |
| | end |
| |
|
| | return nil, end_reached |
| | end |
| |
|
| | |
| | function lib.IDDFS(data, func, max_depth) |
| | local depth = 1 |
| | max_depth = max_depth or 3000 |
| | key = key or 'xml' |
| | local r = func(data) |
| | if r then return r end |
| | while true do |
| | local result, end_reached = itdeepSearch(data, func, 1, depth) |
| | if result then |
| | return result |
| | elseif end_reached then |
| | return nil |
| | elseif depth < max_depth then |
| | depth = depth + 1 |
| | else |
| | error(format('Could not finish search: maximal depth of %i reached.', max_depth)) |
| | end |
| | end |
| | end |
| |
|
| | |
| | function lib.BFS(data, func, max_depth) |
| | local max_depth = max_depth or 3000 |
| | local queue = {} |
| | local depth = {} |
| | local head = 1 |
| | local tail = 1 |
| | local function push(e, d) |
| | queue[tail] = e |
| | depth[tail] = d |
| | tail = tail + 1 |
| | end |
| |
|
| | local function pop() |
| | if head == tail then return nil end |
| | local e, d = queue[head], depth[head] |
| | head = head + 1 |
| | return e, d |
| | end |
| |
|
| | local elem = data |
| | local d = 1 |
| | while elem and d <= max_depth do |
| | local r = func(elem) |
| | if r then return elem end |
| | for _, child in ipairs(elem) do |
| | if type(child) == 'table' then |
| | push(child, d + 1) |
| | end |
| | end |
| | elem, d = pop() |
| | end |
| |
|
| | if d and d > max_depth then |
| | error(format('Could not finish search: maximal depth of %i reached.', max_depth)) |
| | else |
| | return nil |
| | end |
| | end |
| | |
| | |
| | lib.search = lib.BFS |
| |
|
| | |
| |
|
| | |
| | |
| | local function scriptSource(level) |
| | local level = level or 0 |
| | return debug.getinfo(2 - level).source |
| | end |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | local function scriptPath(level) |
| | local level = level or 0 |
| | return match(scriptSource(level - 1), '^@(.*)$') |
| | end |
| |
|
| | |
| | |
| | |
| | local function scriptDir(level) |
| | local level = level or 0 |
| | local file = scriptPath(level - 1) |
| | assert(file, "Cannot use scriptDir here because of a tail call optimization.\n"..debug.traceback()) |
| | if match(file, '/') then |
| | local p = gsub(file, '/[^/]+$', '') |
| | return p |
| | else |
| | return '.' |
| | end |
| | end |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | function lib.path(path, level) |
| | level = level or 2 |
| | local chr = sub(path, 1, 1) |
| | if chr == '|' or chr == '&' then |
| | local src = debug.getinfo(level).source |
| | if src:match(TAIL_CALL) then |
| | src = debug.getinfo(level + 1).source |
| | end |
| | local s = match(src, '^@(.*)$') |
| | if chr == '&' then |
| | return s |
| | end |
| | local b = lib.dir(s) |
| | if len(path) == 1 then return b end |
| | local r = sub(path, 2, -1) |
| | return lib.absolutizePath(b..'/'..r) |
| | else |
| | return lib.absolutizePath(path) |
| | end |
| | end |
| |
|
| | |
| | |
| | |
| | function lib.fileType(path) |
| | if not path then return nil end |
| | local attrs = lfs.attributes(path) |
| | return attrs and attrs.mode |
| | end |
| |
|
| | |
| | |
| |
|
| | |
| | lib.exist = lib.fileType |
| |
|
| | |
| | |
| | |
| | function lib.content(basepath, path) |
| | if path then |
| | path = format('%s/%s', basepath, path) |
| | else |
| | path = basepath |
| | end |
| | local f = assert(io.open(path, 'rb')) |
| | local s = f:read('*all') |
| | f:close() |
| | return s |
| | end |
| |
|
| | |
| | |
| | |
| | function lib.writeall(filepath, data, check_diff) |
| | |
| | lib.makePath(lib.dir(filepath)) |
| | if check_diff and lib.exist(filepath) then |
| | if data == lib.content(filepath) then |
| | return true |
| | end |
| | end |
| | local f = assert(io.open(filepath, 'wb')) |
| | local s = f:write(data) |
| | f:close() |
| | return s |
| | end |
| |
|
| | |
| | function lib.makePath(path) |
| | local fullpath = lib.absolutizePath(path) |
| | private.makePathPart(fullpath, fullpath) |
| | end |
| |
|
| | |
| | |
| | function lib.move(path, new_path) |
| | lib.makePath(lib.dir(new_path)) |
| | return os.rename(path, new_path) |
| | end |
| |
|
| | |
| | |
| | function lib.copy(path, new_path) |
| | lib.makePath(lib.dir(new_path)) |
| | return os.execute(format('cp %s %s', lib.shellQuote(path), lib.shellQuote(new_path))) |
| | end |
| |
|
| | |
| | |
| | function lib.rmFile(path) |
| | local typ = lib.fileType(path) |
| | if not typ then return end |
| | assert(typ == 'file', format("Cannot remove '%s': it is a %s.", path, typ)) |
| | os.remove(path) |
| | end |
| |
|
| | |
| | |
| | |
| | function lib.rmTree(path, recursive) |
| | local fullpath = lib.absolutizePath(path) |
| | if not lib.exist(fullpath) then |
| | return true |
| | end |
| | if fullpath ~= '/' and fullpath ~= '' then |
| | |
| | if not recursive then |
| | return lfs.rmdir(fullpath) |
| | else |
| | local code = format('rm -r %s', lib.shellQuote(fullpath)) |
| | if os.execute(code) == 0 then |
| | return true |
| | else |
| | return false |
| | end |
| | end |
| | end |
| | end |
| |
|
| | |
| | |
| | |
| | |
| | |
| | function lib.dir(filepath) |
| | local base, file = match(filepath, '(.*)/(.*)$') |
| | if not base then |
| | return '.', filepath |
| | elseif base == '' then |
| | |
| | return '/', file |
| | else |
| | return base, file |
| | end |
| | end |
| |
|
| | |
| | function lib.pathDir(...) |
| | return lib.deprecation('lub', 'pathDir', 'dir', ...) |
| | end |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | function lib.absolutizePath(path, basepath) |
| | if not match(path, '^/') then |
| | path = format('%s/%s', basepath or lfs.currentdir(), path) |
| | end |
| | |
| | local parts = lib.split(path, '/') |
| | local path = {} |
| | for i, part in ipairs(parts) do |
| | if part == '.' then |
| | |
| | elseif part == '..' then |
| | |
| | |
| | if i > 2 then |
| | remove(path, #path) |
| | end |
| | else |
| | insert(path, part) |
| | end |
| | end |
| | return lib.join(path, '/') |
| | end |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | function lib.absToRel(abs_string, base) |
| | local l = string.len(base) |
| | local s = string.sub(abs_string, 1, l) |
| | if s == base then |
| | if base == abs_string then |
| | return abs_string |
| | else |
| | return string.sub(abs_string, l+2, -1) |
| | end |
| | else |
| | return abs_string |
| | end |
| | end |
| |
|
| | |
| |
|
| | |
| | function lib.strip(str) |
| | return match(str, '^[ \t\n\r]*(.-)[ \t\n\r]*$') |
| | end |
| |
|
| | |
| | |
| | |
| | |
| | |
| | function lib.split(str, pat) |
| | local t = {} |
| | if not pat then |
| | local i = 1 |
| | while true do |
| | local s = string.sub(str, i, i) |
| | if s == '' then |
| | break |
| | else |
| | insert(t, s) |
| | end |
| | i = i + 1 |
| | end |
| | else |
| | local fpat = '(.-)' .. pat |
| | local last_end = 1 |
| | local s, e, cap = string.find(str,fpat, 1) |
| | while s do |
| | insert(t,cap) |
| | last_end = e+1 |
| | s, e, cap = str:find(fpat, last_end) |
| | end |
| | if last_end <= #str then |
| | cap = str:sub(last_end) |
| | insert(t, cap) |
| | end |
| | end |
| | return t |
| | end |
| |
|
| | |
| | |
| | |
| | |
| | function lib.join(list, sep) |
| | local res = nil |
| | for _, part in ipairs(list) do |
| | if not res then |
| | res = part |
| | else |
| | res = res .. sep .. part |
| | end |
| | end |
| | return res or '' |
| | end |
| |
|
| | |
| | |
| | function lib.keys(dict, no_order) |
| | local res, n = {}, 0 |
| | for k, v in pairs(dict) do |
| | if type(k) == 'string' then |
| | n = n + 1 |
| | res[n] = k |
| | end |
| | end |
| | if not no_order then |
| | sort(res) |
| | end |
| | return res |
| | end |
| |
|
| | |
| | |
| | function lib.insertSorted(list, elem, key) |
| | local pos = -1 |
| | for i, n in ipairs(list) do |
| | local a, b = n, elem |
| | if key then |
| | a = a[key] |
| | b = b[key] |
| | end |
| | if a > b then |
| | pos = i |
| | break |
| | end |
| | end |
| | if pos == -1 then |
| | insert(list, elem) |
| | else |
| | insert(list, pos, elem) |
| | end |
| | end |
| |
|
| | |
| | |
| | function lib.merge(source, table) |
| | for k, v in pairs(table) do |
| | source[k] = v |
| | end |
| | end |
| |
|
| | local function deepMerge(base, key, value) |
| | local base_v = base[key] |
| | if type(value) == 'table' then |
| | if not base_v then |
| | base[key] = value |
| | return true |
| | else |
| | |
| | local changed = false |
| | for k, v in pairs(value) do |
| | changed = deepMerge(base_v, k, v) or changed |
| | end |
| | return changed |
| | end |
| | elseif base_v == value then |
| | |
| | return false |
| | else |
| | base[key] = value |
| | return true |
| | end |
| | end |
| | |
| | lib.deepMerge = deepMerge |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | local orig_req = require |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | function lib.traceRequire(enable) |
| | if enable then |
| | require = function(path) |
| | print("require '"..path.."'") |
| | return orig_req(path) |
| | end |
| | else |
| | require = orig_req |
| | end |
| | end |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | function lib.log(...) |
| | local trace = lib.split(debug.traceback(), '\n\t') |
| | local part = trace[3] |
| | if part:match(TAIL_CALL) then |
| | part = trace[4] |
| | end |
| | local file, line = match(part, '^([^:]+):([^:]+):') |
| | if file and line then |
| | print(format('%s:%i:', file, line), ...) |
| | else |
| | print(part, ...) |
| | end |
| | end |
| |
|
| |
|
| | |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | function lib.deprecation(lib_name, old, new, ...) |
| | local trace = lib.split(debug.traceback(), '\n\t')[4] |
| | local arg = ... |
| | if arg then |
| | print(format("[DEPRECATION] %s\n\t'%s.%s' is deprecated. Please use '%s.%s' instead.", trace, lib_name, old, lib_name, new)) |
| | return package.loaded[lib_name][new](...) |
| | else |
| | print(format("[DEPRECATION] %s\n\t'%s.%s' is deprecated and will be removed. Please use '%s' instead.", trace, lib_name, old, new)) |
| | end |
| | end |
| |
|
| | |
| | |
| | |
| | function lib.shellQuote(str) |
| | return '"' .. gsub(str, '[\\%$"]', function(x) |
| | return '\\' .. x |
| | end) .. '"' |
| | end |
| |
|
| | function private.makePathPart(path, fullpath) |
| | local file_type = lib.fileType(path) |
| | if file_type == 'file' then |
| | error(format("Could not build path '%s' ('%s' is a file).", fullpath, path)) |
| | elseif file_type == 'directory' then |
| | return |
| | else |
| | local base = lib.dir(path) |
| | private.makePathPart(base, fullpath) |
| | |
| | lfs.mkdir(path) |
| | |
| | end |
| | end |
| |
|
| | |
| | |
| | package.loaded.lub = lib |
| |
|
| | |
| | lib.Autoload = require 'lub.Autoload' |
| |
|
| | lib.Autoload('lub', lib) |
| |
|
| |
|
| | |
| |
|
| | return lib |
| |
|