Mercurial > prosody-hg
comparison core/configmanager.lua @ 6164:ef4024f6bc40
core.configmanager: Move path utility functions into util.paths
| author | Kim Alvefur <zash@zash.se> |
|---|---|
| date | Fri, 09 May 2014 19:34:35 +0200 |
| parents | 5cf1c08805fb |
| children | 5af7fe1014db |
comparison
equal
deleted
inserted
replaced
| 6162:fbc3b195dab8 | 6164:ef4024f6bc40 |
|---|---|
| 13 | 13 |
| 14 local fire_event = prosody and prosody.events.fire_event or function () end; | 14 local fire_event = prosody and prosody.events.fire_event or function () end; |
| 15 | 15 |
| 16 local envload = require"util.envload".envload; | 16 local envload = require"util.envload".envload; |
| 17 local lfs = require "lfs"; | 17 local lfs = require "lfs"; |
| 18 local resolve_relative_path = require"util.paths".resolve_relative_path; | |
| 19 local glob_to_pattern = require"util.paths".glob_to_pattern; | |
| 18 local path_sep = package.config:sub(1,1); | 20 local path_sep = package.config:sub(1,1); |
| 19 | 21 |
| 20 module "configmanager" | 22 module "configmanager" |
| 23 | |
| 24 _M.resolve_relative_path = resolve_relative_path; -- COMPAT | |
| 21 | 25 |
| 22 local parsers = {}; | 26 local parsers = {}; |
| 23 | 27 |
| 24 local config_mt = { __index = function (t, k) return rawget(t, "*"); end}; | 28 local config_mt = { __index = function (t, k) return rawget(t, "*"); end}; |
| 25 local config = setmetatable({ ["*"] = { } }, config_mt); | 29 local config = setmetatable({ ["*"] = { } }, config_mt); |
| 62 function _M.set(host, key, value, _oldvalue) | 66 function _M.set(host, key, value, _oldvalue) |
| 63 if key == "core" then | 67 if key == "core" then |
| 64 key, value = value, _oldvalue; --COMPAT with code that still uses "core" | 68 key, value = value, _oldvalue; --COMPAT with code that still uses "core" |
| 65 end | 69 end |
| 66 return set(config, host, key, value); | 70 return set(config, host, key, value); |
| 67 end | |
| 68 | |
| 69 -- Helper function to resolve relative paths (needed by config) | |
| 70 do | |
| 71 function resolve_relative_path(parent_path, path) | |
| 72 if path then | |
| 73 -- Some normalization | |
| 74 parent_path = parent_path:gsub("%"..path_sep.."+$", ""); | |
| 75 path = path:gsub("^%.%"..path_sep.."+", ""); | |
| 76 | |
| 77 local is_relative; | |
| 78 if path_sep == "/" and path:sub(1,1) ~= "/" then | |
| 79 is_relative = true; | |
| 80 elseif path_sep == "\\" and (path:sub(1,1) ~= "/" and (path:sub(2,3) ~= ":\\" and path:sub(2,3) ~= ":/")) then | |
| 81 is_relative = true; | |
| 82 end | |
| 83 if is_relative then | |
| 84 return parent_path..path_sep..path; | |
| 85 end | |
| 86 end | |
| 87 return path; | |
| 88 end | |
| 89 end | |
| 90 | |
| 91 -- Helper function to convert a glob to a Lua pattern | |
| 92 local function glob_to_pattern(glob) | |
| 93 return "^"..glob:gsub("[%p*?]", function (c) | |
| 94 if c == "*" then | |
| 95 return ".*"; | |
| 96 elseif c == "?" then | |
| 97 return "."; | |
| 98 else | |
| 99 return "%"..c; | |
| 100 end | |
| 101 end).."$"; | |
| 102 end | 71 end |
| 103 | 72 |
| 104 function load(filename, format) | 73 function load(filename, format) |
| 105 format = format or filename:match("%w+$"); | 74 format = format or filename:match("%w+$"); |
| 106 | 75 |
