From 25ffc62a5b2c9a65e5c1689d5351adcf8cbef7e2 Mon Sep 17 00:00:00 2001 From: Kaarle Ritvanen Date: Fri, 28 Jun 2013 23:31:21 +0300 Subject: eliminate deprecated module style --- acf/error.lua | 23 ++++--- acf/init.lua | 18 ++--- acf/loader.lua | 27 ++++---- acf/model/aaa.lua | 2 - acf/model/combination.lua | 21 +++--- acf/model/field.lua | 91 +++++++++++++------------ acf/model/init.lua | 125 +++++++++++++++++++--------------- acf/model/model.lua | 37 +++++----- acf/model/net.lua | 34 +++++---- acf/model/node.lua | 73 +++++++++----------- acf/model/permission.lua | 14 ++-- acf/model/root.lua | 27 ++++---- acf/model/set.lua | 11 +-- acf/modules/awall.lua | 2 - acf/modules/generic.lua | 2 - acf/modules/net.lua | 2 - acf/object.lua | 27 ++++---- acf/path.lua | 51 +++++++------- acf/persistence/backends/augeas.lua | 7 +- acf/persistence/backends/files.lua | 11 +-- acf/persistence/backends/json.lua | 13 ++-- acf/persistence/backends/null.lua | 6 +- acf/persistence/backends/volatile.lua | 7 +- acf/persistence/init.lua | 15 ++-- acf/persistence/util.lua | 12 ++-- acf/transaction/backend.lua | 21 +++--- acf/transaction/init.lua | 10 ++- acf/util.lua | 20 +++--- server.lua | 14 ++-- 29 files changed, 384 insertions(+), 339 deletions(-) diff --git a/acf/error.lua b/acf/error.lua index 61788e5..7157d37 100644 --- a/acf/error.lua +++ b/acf/error.lua @@ -3,14 +3,14 @@ Copyright (c) 2012-2013 Kaarle Ritvanen See LICENSE file for license details --]] -module(..., package.seeall) +local M = {} local object = require('acf.object') local class = object.class local util = require('acf.util') -require 'json' +local json = require('json') local ErrorTable = class() @@ -24,7 +24,7 @@ function ErrorTable:raise() end -ErrorList = class(ErrorTable) +local ErrorList = class(ErrorTable) function ErrorList:init(label) object.super(self, ErrorList):init() @@ -36,9 +36,9 @@ function ErrorList:insert(msg) end -ErrorDict = class(ErrorTable) +M.ErrorDict = class(ErrorTable) -function ErrorDict:collect(func, ...) +function M.ErrorDict:collect(func, ...) local function pack(success, ...) local arg = {...} return success, success and arg or arg[1] @@ -68,14 +68,14 @@ function ErrorDict:collect(func, ...) end -function raise(label, msg) +function M.raise(label, msg) local err = ErrorList(label) err:insert(msg) err:raise() end -function relabel(label, ...) - local err = ErrorDict() +function M.relabel(label, ...) + local err = M.ErrorDict() local res = {err:collect(...)} if err:success() then return unpack(res) end @@ -86,10 +86,13 @@ function relabel(label, ...) elist:raise() end -function call(...) - local err = ErrorDict() +function M.call(...) + local err = M.ErrorDict() local res = {err:collect(...)} if err:success() then return true, unpack(res) end if err.errors.system then error(err.errors.system[1]) end return false, err.errors end + + +return M diff --git a/acf/init.lua b/acf/init.lua index 341c0f0..62c76e9 100644 --- a/acf/init.lua +++ b/acf/init.lua @@ -3,14 +3,16 @@ Copyright (c) 2012-2013 Kaarle Ritvanen See LICENSE file for license details --]] -module(..., package.seeall) +local M = {} -require 'acf.model' -require 'acf.model.aaa' +M.model = require('acf.model') -require('acf.loader').loadmods('modules') +require('acf.model.aaa') +require('acf.loader')('modules') -call = require('acf.error').call -require 'acf.object' -require 'acf.path' -require 'acf.transaction' +M.call = require('acf.error').call +M.object = require('acf.object') +M.path = require('acf.path') +M.start_txn = require('acf.transaction') + +return M diff --git a/acf/loader.lua b/acf/loader.lua index 95545b7..ff82246 100644 --- a/acf/loader.lua +++ b/acf/loader.lua @@ -6,17 +6,18 @@ See LICENSE file for license details module(..., package.seeall) local pth = require('acf.path') -require 'posix' -require 'stringy' -function loadmods(subdir) - local comps = pth.split('acf/'..subdir) - local res = {} - for _, modfile in ipairs(posix.dir(pth.join(unpack(comps)))) do - if stringy.endswith(modfile, '.lua') then - local name = string.sub(modfile, 1, -5) - res[name] = require(table.concat(comps, '.')..'.'..name) - end - end - return res -end +local posix = require('posix') +local stringy = require('stringy') + +return function(subdir) + local comps = pth.split('acf/'..subdir) + local res = {} + for _, modfile in ipairs(posix.dir(pth.join(unpack(comps)))) do + if stringy.endswith(modfile, '.lua') then + local name = string.sub(modfile, 1, -5) + res[name] = require(table.concat(comps, '.')..'.'..name) + end + end + return res + end diff --git a/acf/model/aaa.lua b/acf/model/aaa.lua index 9e67aa2..d51c10f 100644 --- a/acf/model/aaa.lua +++ b/acf/model/aaa.lua @@ -3,8 +3,6 @@ Copyright (c) 2012-2013 Kaarle Ritvanen See LICENSE file for license details --]] -module(..., package.seeall) - local M = require('acf.model') Role = M.new() diff --git a/acf/model/combination.lua b/acf/model/combination.lua index 7228233..19f84cc 100644 --- a/acf/model/combination.lua +++ b/acf/model/combination.lua @@ -3,7 +3,7 @@ Copyright (c) 2012-2013 Kaarle Ritvanen See LICENSE file for license details --]] -module(..., package.seeall) +local M = {} local err = require('acf.error') local raise = err.raise @@ -18,30 +18,33 @@ local class = object.class local super = object.super -require 'stringy' +local stringy = require('stringy') -Range = class(String) +M.Range = class(String) -function Range:init(params) - super(self, Range):init(params) +function M.Range:init(params) + super(self, M.Range):init(params) if not self.type then self.type = fld.Integer end end -function Range:validate(context, value) +function M.Range:validate(context, value) local comps = stringy.split(value, '-') if #comps > 2 then raise(context.path, 'Invalid range') end for _, v in ipairs(comps) do to_field(self.type):_validate(context, v) end end -Union = class(String) +M.Union = class(String) -function Union:validate(context, value) - super(self, Union):validate(context, value) +function M.Union:validate(context, value) + super(self, M.Union):validate(context, value) for _, tpe in ipairs(self.types) do local field = to_field(tpe) if err.call(field.validate, field, context, value) then return end end raise(context.path, self.error or 'Invalid value') end + + +return M diff --git a/acf/model/field.lua b/acf/model/field.lua index c69b4d7..d4e934c 100644 --- a/acf/model/field.lua +++ b/acf/model/field.lua @@ -3,7 +3,7 @@ Copyright (c) 2012-2013 Kaarle Ritvanen See LICENSE file for license details --]] -module(..., package.seeall) +local M = {} local err = require('acf.error') local raise = err.raise @@ -22,22 +22,22 @@ local function contains(list, value) return false end -Member = class() +M.Member = class() -function Member:init(params) +function M.Member:init(params) for k, v in pairs(params or {}) do if self[k] == nil then self[k] = v end end end -function Member:auto_ui_name(name) +function M.Member:auto_ui_name(name) if not name then return end return string.gsub( string.upper(string.sub(name, 1, 1))..string.sub(name, 2), '-', ' ' ) end -function Member:meta(context) +function M.Member:meta(context) return { name=self.name, description=self.description, @@ -46,10 +46,10 @@ function Member:meta(context) end -Field = class(Member) +M.Field = class(M.Member) -function Field:init(params) - super(self, Field):init(params) +function M.Field:init(params) + super(self, M.Field):init(params) if self.choice and not self['ui-choice'] then self['ui-choice'] = util.map( @@ -63,9 +63,9 @@ function Field:init(params) end end -function Field:meta(context) +function M.Field:meta(context) assert(self.dtype) - local res = super(self, Field):meta(context) + local res = super(self, M.Field):meta(context) res.type = self.dtype res.required = self.required @@ -77,20 +77,20 @@ function Field:meta(context) return res end -function Field:topology(context) +function M.Field:topology(context) return { {path=context.path, addr=context.addr, type=self.dtype} } end -function Field:load(context) +function M.Field:load(context) if not context.txn then return setmetatable({}, context) end local value = context.txn:get(context.addr) if value == nil then return self.default end return value end -function Field:_validate(context, value) +function M.Field:_validate(context, value) if self.required and value == nil then raise(context.path, 'Required value not set') end @@ -101,18 +101,18 @@ function Field:_validate(context, value) return value end -function Field:validate(context, value) end +function M.Field:validate(context, value) end -function Field:save(context, value) +function M.Field:save(context, value) context.txn:set(context.addr, self:_validate(context, value)) end -function Field:validate_saved(context) +function M.Field:validate_saved(context) self:save(context, self:load(context)) end -local Primitive = class(Field) +local Primitive = class(M.Field) function Primitive:validate(context, value) local t = self.dtype @@ -120,76 +120,76 @@ function Primitive:validate(context, value) end -String = class(Primitive) +M.String = class(Primitive) -function String:init(params) - super(self, String):init(params) +function M.String:init(params) + super(self, M.String):init(params) self.dtype = 'string' end -function String:validate(context, value) - super(self, String):validate(context, value) +function M.String:validate(context, value) + super(self, M.String):validate(context, value) if self['max-length'] and string.len(value) > self['max-length'] then raise(context.path, 'Maximum length exceeded') end end -function String:meta(context) - local res = super(self, String):meta(context) +function M.String:meta(context) + local res = super(self, M.String):meta(context) res['max-length'] = self['max-length'] return res end -Number = class(Primitive) +M.Number = class(Primitive) -function Number:init(params) - super(self, Number):init(params) +function M.Number:init(params) + super(self, M.Number):init(params) self.dtype = 'number' end -function Number:_validate(context, value) - return super(self, Number):_validate( +function M.Number:_validate(context, value) + return super(self, M.Number):_validate( context, value and tonumber(value) or value ) end -Integer = class(Number) +M.Integer = class(M.Number) -function Integer:validate(context, value) - super(self, Integer):validate(context, value) +function M.Integer:validate(context, value) + super(self, M.Integer):validate(context, value) if math.floor(value) ~= value then raise(context.path, 'Not an integer') end end -Boolean = class(Primitive) +M.Boolean = class(Primitive) -function Boolean:init(params) - super(self, Boolean):init(params) +function M.Boolean:init(params) + super(self, M.Boolean):init(params) self.dtype = 'boolean' self.widget = self.dtype end -TreeNode = class(Field) +M.TreeNode = class(M.Field) -function TreeNode:topology(context) - local res = super(self, TreeNode):topology(context) +function M.TreeNode:topology(context) + local res = super(self, M.TreeNode):topology(context) res[1].type = 'table' util.extend(res, node.topology(self:load(context, true))) return res end -function TreeNode:load(context, create) +function M.TreeNode:load(context, create) if context.txn and not ( create or self.create or context.txn:get(context.addr) ) then return end return self.itype(context, self.iparams) end -function TreeNode:save(context, value) +function M.TreeNode:save(context, value) local path = context.path if value == path then return end @@ -216,13 +216,13 @@ function TreeNode:save(context, value) end end -function TreeNode.save_member(node, k, v) node[k] = v end +function M.TreeNode.save_member(node, k, v) node[k] = v end -Model = class(TreeNode) +M.Model = class(M.TreeNode) -function Model:init(params) - super(self, Model):init(params) +function M.Model:init(params) + super(self, M.Model):init(params) assert(self.model) self.itype = self.model @@ -230,3 +230,6 @@ function Model:init(params) self.dtype = 'model' self.widget = self.dtype end + + +return M diff --git a/acf/model/init.lua b/acf/model/init.lua index e1a4dd9..9d09204 100644 --- a/acf/model/init.lua +++ b/acf/model/init.lua @@ -3,34 +3,46 @@ Copyright (c) 2012-2013 Kaarle Ritvanen See LICENSE file for license details --]] -module(..., package.seeall) +local M = {} -err = require('acf.error') -local raise = err.raise -local relabel = err.relabel +M.error = require('acf.error') +local raise = M.error.raise +local relabel = M.error.relabel local combination = require('acf.model.combination') -Union = combination.Union -Range = combination.Range +M.Union = combination.Union +M.Range = combination.Range local fld = require('acf.model.field') local Field = fld.Field -Boolean = fld.Boolean -Integer = fld.Integer -Number = fld.Number -String = fld.String +M.Boolean = fld.Boolean +M.Integer = fld.Integer +M.Number = fld.Number +M.String = fld.String local model = require('acf.model.model') -Action = model.Action -new = model.new +M.Action = model.Action +M.new = model.new local to_field = model.to_field -net = require('acf.model.net') - -node = require('acf.model.node') -permission = require('acf.model.permission') -register = require('acf.model.root').register -node.Set = require('acf.model.set').Set +M.net = require('acf.model.net') + +local node = require('acf.model.node') +M.node = {} +for _, m in ipairs{ + 'TreeNode', + 'has_permission', + 'insert', + 'meta', + 'mmeta', + 'path', + 'pairs', + 'ipairs' +} do M.node[m] = node[m] end + +M.permission = require('acf.model.permission') +M.register = require('acf.model.root').register +M.node.Set = require('acf.model.set').Set local object = require('acf.object') local class = object.class @@ -40,30 +52,30 @@ local pth = require('acf.path') local map = require('acf.util').map -require 'stringy' +local stringy = require('stringy') -Reference = class(Field) +M.Reference = class(Field) -function Reference:init(params) +function M.Reference:init(params) if not params.widget then params.widget = 'reference' end - super(self, Reference):init(params) + super(self, M.Reference):init(params) self.dtype = 'reference' if not self.scope then self.scope = '/' end end -function Reference:topology(context) - local res = super(self, Reference):topology(context) +function M.Reference:topology(context) + local res = super(self, M.Reference):topology(context) res[1].scope = self.scope return res end -function Reference:abs_scope(context) +function M.Reference:abs_scope(context) return pth.to_absolute(self.scope, node.path(context.parent)) end -function Reference:meta(context) - local res = super(self, Reference):meta(context) +function M.Reference:meta(context) + local res = super(self, M.Reference):meta(context) res.scope = self:abs_scope(context) local txn = context.txn @@ -76,17 +88,17 @@ function Reference:meta(context) return res end -function Reference:follow(context, value) +function M.Reference:follow(context, value) return context.txn:fetch(pth.rawjoin(self:abs_scope(context), value)) end -function Reference:load(context) - local ref = super(self, Reference):load(context) +function M.Reference:load(context) + local ref = super(self, M.Reference):load(context) return (context.txn and ref) and self:follow(context, ref) or ref end -function Reference:_validate(context, value) - super(self, Reference):_validate(context, value) +function M.Reference:_validate(context, value) + super(self, M.Reference):_validate(context, value) if value == nil then return end @@ -114,7 +126,7 @@ function Reference:_validate(context, value) return value end -function Reference:deleted(context, addr) +function M.Reference:deleted(context, addr) local target = self:load(context) if target and node.addr(target) == addr then -- TODO raise error for the target object @@ -123,14 +135,14 @@ function Reference:deleted(context, addr) end -Model = fld.Model +M.Model = fld.Model -Collection = class(fld.TreeNode) +M.Collection = class(fld.TreeNode) -function Collection:init(params, itype) +function M.Collection:init(params, itype) if params.create == nil then params.create = true end - super(self, Collection):init(params) + super(self, M.Collection):init(params) assert(self.type) self.itype = itype or node.Collection @@ -144,45 +156,48 @@ function Collection:init(params, itype) self.widget = self.dtype end -function Collection:auto_ui_name(name) +function M.Collection:auto_ui_name(name) if not name then return end if string.sub(name, -1, -1) ~= 's' then name = name..'s' end - return super(self, Collection):auto_ui_name(name) + return super(self, M.Collection):auto_ui_name(name) end -function Collection:load(context, create) +function M.Collection:load(context, create) if not self.iparams.field then self.iparams.field = to_field(self.type) end - return super(self, Collection):load(context, create) + return super(self, M.Collection):load(context, create) end -List = class(Collection) -function List:init(params) super(self, List):init(params, node.List) end +M.List = class(M.Collection) +function M.List:init(params) super(self, M.List):init(params, node.List) end -Set = class(Collection) -function Set:init(params) super(self, Set):init(params, node.Set) end -function Set.save_member(tn, k, v) node.insert(tn, v) end +M.Set = class(M.Collection) +function M.Set:init(params) super(self, M.Set):init(params, M.node.Set) end +function M.Set.save_member(tn, k, v) node.insert(tn, v) end -- experimental -Mixed = class(Collection) +M.Mixed = class(M.Collection) -function Mixed:init(params) - params.type = Mixed - super(self, Mixed):init(params, node.Mixed) +function M.Mixed:init(params) + params.type = M.Mixed + super(self, M.Mixed):init(params, node.Mixed) self.pfield = Field() end -function Mixed:topology(context) return {} end +function M.Mixed:topology(context) return {} end -function Mixed:load(context) +function M.Mixed:load(context) local value = self.pfield:load(context) - if type(value) == 'table' then return super(self, Mixed):load(context) end + if type(value) == 'table' then return super(self, M.Mixed):load(context) end return value end -function Mixed:save(context, value) - if type(value) == 'table' then super(self, Mixed):save(context, value) +function M.Mixed:save(context, value) + if type(value) == 'table' then super(self, M.Mixed):save(context, value) else self.pfield:save(context, value) end end + + +return M diff --git a/acf/model/model.lua b/acf/model/model.lua index 8892221..026ada6 100644 --- a/acf/model/model.lua +++ b/acf/model/model.lua @@ -3,7 +3,7 @@ Copyright (c) 2012-2013 Kaarle Ritvanen See LICENSE file for license details --]] -module(..., package.seeall) +local M = {} local raise = require('acf.error').raise @@ -25,7 +25,7 @@ local util = require('acf.util') local function to_member(obj, params) if not params then params = {} end - if object.issubclass(obj, Model) then + if object.issubclass(obj, M.Model) then params.model = obj return fld.Model(params) end @@ -34,23 +34,23 @@ local function to_member(obj, params) return res end -function to_field(obj, params) +function M.to_field(obj, params) local res = to_member(obj, params) assert(isinstance(res, Field)) return res end -Action = class(Member) +M.Action = class(Member) -function Action:init(params) - super(self, Action):init(params) +function M.Action:init(params) + super(self, M.Action):init(params) if not self.func then error('Function not defined for action') end if self.field then assert(type(self.field) == 'table') - self.field = to_field(self.field) + self.field = M.to_field(self.field) self.field.addr = '/null/action' end @@ -61,15 +61,15 @@ function Action:init(params) end end -function Action:meta(context) - local res = super(self, Action):meta(context) +function M.Action:meta(context) + local res = super(self, M.Action):meta(context) if self.field then res.arg = self.field:meta(context) end return res end -function new(base) - if not base then base = Model end +function M.new(base) + if not base then base = M.Model end local res = class(base) res.members = base == node.TreeNode and {} or util.copy(base.members) @@ -94,15 +94,15 @@ function new(base) setmetatable(res, mt) - if isinstance(base, Model) then util.setdefaults(res, base) end + if isinstance(base, M.Model) then util.setdefaults(res, base) end return res end -Model = new(node.TreeNode) +M.Model = M.new(node.TreeNode) -function Model:init(context) - super(self, Model):init(context) +function M.Model:init(context) + super(self, M.Model):init(context) local mt = getmetatable(self) @@ -146,7 +146,7 @@ function Model:init(context) assert(mt.txn) - if isinstance(v, Action) then + if isinstance(v, M.Action) then local f = v.field and BoundMember(self, k, v.field) if create then return f and f:load(true) end @@ -180,7 +180,7 @@ function Model:init(context) mt.meta.type = 'model' mt.meta.fields = tmeta(Field) - mt.meta.actions = tmeta(Action) + mt.meta.actions = tmeta(M.Action) function mt.members() return util.map(function(f) return f.name end, mt.meta.fields) @@ -198,3 +198,6 @@ function Model:init(context) end end end + + +return M diff --git a/acf/model/net.lua b/acf/model/net.lua index dc63e75..e5c1836 100644 --- a/acf/model/net.lua +++ b/acf/model/net.lua @@ -3,7 +3,7 @@ Copyright (c) 2012-2013 Kaarle Ritvanen See LICENSE file for license details --]] -module(..., package.seeall) +local M = {} local raise = require('acf.error').raise local Union = require('acf.model.combination').Union @@ -18,13 +18,13 @@ local super = object.super local update = require('acf.util').update -require 'stringy' +local stringy = require('stringy') -IPv4Address = class(String) +M.IPv4Address = class(String) -function IPv4Address:validate(context, value) - super(self, IPv4Address):validate(context, value) +function M.IPv4Address:validate(context, value) + super(self, M.IPv4Address):validate(context, value) local function test(...) if #{...} ~= 4 then return true end for _, octet in ipairs{...} do @@ -37,10 +37,10 @@ function IPv4Address:validate(context, value) end -IPv6Address = class(String) +M.IPv6Address = class(String) -function IPv6Address:validate(context, value) - super(self, IPv6Address):validate(context, value) +function M.IPv6Address:validate(context, value) + super(self, M.IPv6Address):validate(context, value) local function invalid() raise(context.path, 'Invalid IPv6 address') end @@ -73,20 +73,24 @@ function IPv6Address:validate(context, value) end -IPAddress = class(Union) +M.IPAddress = class(Union) -function IPAddress:init(params) - super(self, IPAddress):init( +function M.IPAddress:init(params) + super(self, M.IPAddress):init( update( - params, {types={IPv4Address, IPv6Address}, error='Invalid IP address'} + params, + {types={M.IPv4Address, M.IPv6Address}, error='Invalid IP address'} ) ) end -Port = class(fld.Integer) +M.Port = class(fld.Integer) -function Port:validate(context, value) - super(self, Port):validate(context, value) +function M.Port:validate(context, value) + super(self, M.Port):validate(context, value) if value < 0 or value > 65535 then raise(context.path, 'Invalid port') end end + + +return M diff --git a/acf/model/node.lua b/acf/model/node.lua index 688336c..b708112 100644 --- a/acf/model/node.lua +++ b/acf/model/node.lua @@ -3,7 +3,7 @@ Copyright (c) 2012-2013 Kaarle Ritvanen See LICENSE file for license details --]] -module(..., package.seeall) +local M = {} local raise = require('acf.error').raise @@ -16,9 +16,9 @@ local pth = require('acf.path') local util = require('acf.util') -BoundMember = class() +M.BoundMember = class() -function BoundMember:init(parent, name, field) +function M.BoundMember:init(parent, name, field) local pmt = getmetatable(parent) local mt = {} @@ -45,9 +45,9 @@ function BoundMember:init(parent, name, field) end -TreeNode = class() +M.TreeNode = class() -function TreeNode:init(context) +function M.TreeNode:init(context) local mt = getmetatable(self) util.update(mt, context) @@ -75,13 +75,13 @@ function TreeNode:init(context) if ({create=true, delete=true})[permission] then permission = 'modify' end - return has_permission(mt.parent, user, permission) + return M.has_permission(mt.parent, user, permission) end mt.txn.validable[mt.path] = mt.addr end -function TreeNode:fetch(path, create) +function M.TreeNode:fetch(path, create) if type(path) == 'string' then path = pth.split(path) end if #path == 0 then return self end @@ -102,10 +102,10 @@ function TreeNode:fetch(path, create) raise(pth.join(mt.path, name), 'Is a primitive value') end - return TreeNode.fetch(next, path, create) + return M.TreeNode.fetch(next, path, create) end -function TreeNode:search_refs(path) +function M.TreeNode:search_refs(path) if type(path) == 'string' then path = pth.split(path) end if #path == 0 then return {} end @@ -121,7 +121,7 @@ function TreeNode:search_refs(path) local member = mt.member(name) if member.deleted then return {member} end - return isinstance(next, TreeNode) and TreeNode.search_refs( + return isinstance(next, M.TreeNode) and M.TreeNode.search_refs( next, path ) or {} end @@ -138,22 +138,22 @@ function TreeNode:search_refs(path) end -Collection = class(TreeNode) +M.Collection = class(M.TreeNode) -function Collection:init(context, params) - super(self, Collection):init(context) +function M.Collection:init(context, params) + super(self, M.Collection):init(context) self.init = nil self.fetch = nil self.search_refs = nil local mt = getmetatable(self) - local field = BoundMember(self, pth.wildcard, params.field) + local field = M.BoundMember(self, pth.wildcard, params.field) function mt.topology() return field:topology() end function mt.member(name) - return BoundMember(self, name, params.field) + return M.BoundMember(self, name, params.field) end function mt.load(k, create) return mt.member(k):load(create) end @@ -189,10 +189,10 @@ function Collection:init(context, params) end -List = class(Collection) +M.List = class(M.Collection) -function List:init(context, params) - super(self, List):init(context, params) +function M.List:init(context, params) + super(self, M.List):init(context, params) local mt = getmetatable(self) mt.meta.type = 'list' @@ -220,10 +220,10 @@ end -- experimental -Mixed = class(Collection) +M.Mixed = class(M.Collection) -function Mixed:init(context, params) - super(self, Mixed):init(context, params) +function M.Mixed:init(context, params) + super(self, M.Mixed):init(context, params) -- TODO dynamic meta: list non-leaf children local mt = getmetatable(self) @@ -242,27 +242,17 @@ local function meta_func(attr) end end -addr = meta_func('addr') -has_permission = meta_func('has_permission') -insert = meta_func('insert') -members = meta_func('members') -meta = meta_func('meta') -mmeta = meta_func('mmeta') -parent = meta_func('parent') -path = meta_func('path') -topology = meta_func('topology') -validate = meta_func('validate') +for _, mf in ipairs{ + 'addr', 'has_permission', 'insert', 'meta', 'mmeta', 'path', 'topology' +} do M[mf] = meta_func(mf) end -local rawpairs = pairs -local rawipairs = ipairs - -function pairs(tbl) - if not isinstance(tbl, TreeNode) then return rawpairs(tbl) end +function M.pairs(tbl) + if not isinstance(tbl, M.TreeNode) then return pairs(tbl) end local mt = getmetatable(tbl) local res = {} - for _, member in rawipairs(mt.members()) do res[member] = mt.load(member) end - return rawpairs(res) + for _, member in ipairs(mt.members()) do res[member] = mt.load(member) end + return pairs(res) end local function _ipairs(mt, i) @@ -271,7 +261,10 @@ local function _ipairs(mt, i) if v == nil then return end return i, v end -function ipairs(tbl) - if not isinstance(tbl, TreeNode) then return rawipairs(tbl) end +function M.ipairs(tbl) + if not isinstance(tbl, M.TreeNode) then return ipairs(tbl) end return _ipairs, getmetatable(tbl), 0 end + + +return M diff --git a/acf/model/permission.lua b/acf/model/permission.lua index 94b3df0..271d478 100644 --- a/acf/model/permission.lua +++ b/acf/model/permission.lua @@ -3,16 +3,20 @@ Copyright (c) 2012-2013 Kaarle Ritvanen See LICENSE file for license details --]] -module(..., package.seeall) +local M = {} local insert = require('acf.model.node').insert -local start = require('acf.transaction').start +local start_txn = require('acf.transaction') -function define(path, ...) - local txn = start() +function M.define(path, ...) + local txn = start_txn() local db = txn:fetch('/auth/permissions') for _, permission in ipairs{...} do insert(db, permission..path) end txn:commit() end -function defaults(path) define(path, 'read', 'create', 'modify', 'delete') end +function M.defaults(path) + M.define(path, 'read', 'create', 'modify', 'delete') +end + +return M diff --git a/acf/model/root.lua b/acf/model/root.lua index a793c75..17c5cfb 100644 --- a/acf/model/root.lua +++ b/acf/model/root.lua @@ -3,7 +3,7 @@ Copyright (c) 2012-2013 Kaarle Ritvanen See LICENSE file for license details --]] -module(..., package.seeall) +local M = {} local model = require('acf.model.model') local node = require('acf.model.node') @@ -14,17 +14,17 @@ local util = require('acf.util') local setdefault = util.setdefault -RootModel = model.new() +M.RootModel = model.new() -function RootModel:init(txn) - object.super(self, RootModel):init{txn=txn, path='/', addr='/null/root'} +function M.RootModel:init(txn) + object.super(self, M.RootModel):init{txn=txn, path='/', addr='/null/root'} end -function RootModel:has_permission(user, permission) +function M.RootModel:has_permission(user, permission) return permission == 'read' end -function RootModel:meta(path) +function M.RootModel:meta(path) local obj = self:fetch(path, true) if object.isinstance(obj, node.TreeNode) then return node.meta(obj) end return node.mmeta(self:fetch(pth.parent(path), true), pth.name(path)) @@ -34,7 +34,7 @@ end local _topology = {} local order = 0 -function topology(addr, create) +function M.topology(addr, create) local top = _topology if type(addr) == 'table' then addr = util.copy(addr) else addr = pth.split(addr) end @@ -56,15 +56,15 @@ function topology(addr, create) return defaults(top) end -function register(name, field, params) +function M.register(name, field, params) if not params then params = {} end params.create = true - RootModel[name] = model.to_field(field, params) + M.RootModel[name] = model.to_field(field, params) - local root = RootModel() + local root = M.RootModel() for _, record in ipairs(node.topology(root:fetch(name))) do - local top = topology(record.addr, true) + local top = M.topology(record.addr, true) setdefault(top, 'order', order) order = order + 1 @@ -82,7 +82,10 @@ function register(name, field, params) root:fetch(pth.to_absolute(record.scope, pth.parent(record.path))) ) set('scope', scope) - table.insert(topology(scope, true).referrers, record.path) + table.insert(M.topology(scope, true).referrers, record.path) end end end + + +return M diff --git a/acf/model/set.lua b/acf/model/set.lua index 47eeaa8..695d7cb 100644 --- a/acf/model/set.lua +++ b/acf/model/set.lua @@ -3,18 +3,18 @@ Copyright (c) 2012-2013 Kaarle Ritvanen See LICENSE file for license details --]] -module(..., package.seeall) +local M = {} local TreeNode = require('acf.model.field').TreeNode local npairs = require('acf.model.node').pairs local object = require('acf.object') -Set = object.class(require('acf.model.node').List) +M.Set = object.class(require('acf.model.node').List) -function Set:init(context, params) +function M.Set:init(context, params) assert(not object.isinstance(params.field, TreeNode)) - object.super(self, Set):init(context, params) + object.super(self, M.Set):init(context, params) local function find(value) for i, member in npairs(self) do @@ -37,3 +37,6 @@ function Set:init(context, params) local insert = mt.insert function mt.insert(v) if not find(v) then insert(v) end end end + + +return M diff --git a/acf/modules/awall.lua b/acf/modules/awall.lua index bb20190..1c7af30 100644 --- a/acf/modules/awall.lua +++ b/acf/modules/awall.lua @@ -3,8 +3,6 @@ Copyright (c) 2012-2013 Kaarle Ritvanen See LICENSE file for license details --]] -module(..., package.seeall) - local M = require('acf.model') local object = require('acf.object') diff --git a/acf/modules/generic.lua b/acf/modules/generic.lua index d12f96b..5477bd0 100644 --- a/acf/modules/generic.lua +++ b/acf/modules/generic.lua @@ -5,8 +5,6 @@ See LICENSE file for license details -- provided as an example, to be removed from production version -module(..., package.seeall) - local M = require('acf.model') M.register('proc', M.Mixed, {addr='/files/proc', ui_name='/proc'}) diff --git a/acf/modules/net.lua b/acf/modules/net.lua index 8e814aa..ad6ba95 100644 --- a/acf/modules/net.lua +++ b/acf/modules/net.lua @@ -3,8 +3,6 @@ Copyright (c) 2012-2013 Kaarle Ritvanen See LICENSE file for license details --]] -module(..., package.seeall) - local M = require('acf.model') local Host = M.new() diff --git a/acf/object.lua b/acf/object.lua index 670a8bb..64fbb9c 100644 --- a/acf/object.lua +++ b/acf/object.lua @@ -1,11 +1,11 @@ --[[ -Copyright (c) 2012 Kaarle Ritvanen +Copyright (c) 2012-2013 Kaarle Ritvanen See LICENSE file for license details --]] -module(..., package.seeall) +local M = {} -function class(base) +function M.class(base) local cls = {} local mt = { @@ -17,7 +17,7 @@ function class(base) end } - if not base and Object then base = Object end + if not base and M.Object then base = M.Object end if base then cls._base = base mt.__index = base @@ -27,13 +27,13 @@ function class(base) end -Object = class() +M.Object = M.class() -function Object:init(...) end +function M.Object:init(...) end -function super(obj, cls) - assert(isinstance(obj, cls)) +function M.super(obj, cls) + assert(M.isinstance(obj, cls)) local mt = {} @@ -51,15 +51,18 @@ function super(obj, cls) end -function issubclass(cls1, cls2) +function M.issubclass(cls1, cls2) if cls1 == cls2 then return true end - if cls1._base then return issubclass(cls1._base, cls2) end + if cls1._base then return M.issubclass(cls1._base, cls2) end return false end -function isinstance(obj, cls) +function M.isinstance(obj, cls) if not obj or type(obj) ~= 'table' then return false end local mt = getmetatable(obj) if not mt then return false end - return type(obj) == 'table' and mt.class and issubclass(mt.class, cls) + return type(obj) == 'table' and mt.class and M.issubclass(mt.class, cls) end + + +return M diff --git a/acf/path.lua b/acf/path.lua index be7387e..4df0350 100644 --- a/acf/path.lua +++ b/acf/path.lua @@ -3,22 +3,20 @@ Copyright (c) 2012-2013 Kaarle Ritvanen See LICENSE file for license details --]] -module(..., package.seeall) +local M = {} local map = require('acf.util').map local up = {} -wildcard = {} -local special = {['..']=up, ['*']=wildcard} +M.wildcard = {} +local special = {['..']=up, ['*']=M.wildcard} -function is_absolute(path) - return string.sub(path, 1, 1) == '/' -end +function M.is_absolute(path) return string.sub(path, 1, 1) == '/' end -function escape(comp) +function M.escape(comp) for symbol, item in pairs(special) do if comp == item then return symbol end end @@ -28,18 +26,18 @@ function escape(comp) end -function rawjoin(p1, p2, ...) +function M.rawjoin(p1, p2, ...) if not p2 then return p1 end - if not is_absolute(p2) then p2 = '/'..p2 end - return rawjoin((p1 == '/' and '' or p1)..p2, ...) + if not M.is_absolute(p2) then p2 = '/'..p2 end + return M.rawjoin((p1 == '/' and '' or p1)..p2, ...) end -function join(parent, ...) - return rawjoin(parent, unpack(map(escape, {...}))) +function M.join(parent, ...) + return M.rawjoin(parent, unpack(map(M.escape, {...}))) end -function split(path) +function M.split(path) local res = {} local comp = '' local escaped @@ -72,19 +70,19 @@ function split(path) end -function is_unique(path) - for _, comp in ipairs(split(path)) do - if comp == wildcard then return false end +function M.is_unique(path) + for _, comp in ipairs(M.split(path)) do + if comp == M.wildcard then return false end end return true end -function to_absolute(path, base) - if not is_absolute(path) then +function M.to_absolute(path, base) + if not M.is_absolute(path) then path = base..(base ~= '/' and '/' or '')..path end - local comps = split(path) + local comps = M.split(path) local i = 1 while i <= #comps do if comps[i] == up then @@ -94,17 +92,20 @@ function to_absolute(path, base) i = i - 1 else i = i + 1 end end - return join('/', unpack(comps)) + return M.join('/', unpack(comps)) end -function parent(path) - local comps = split(path) +function M.parent(path) + local comps = M.split(path) table.remove(comps) - return join('/', unpack(comps)) + return M.join('/', unpack(comps)) end -function name(path) - local comps = split(path) +function M.name(path) + local comps = M.split(path) return comps[#comps] end + + +return M diff --git a/acf/persistence/backends/augeas.lua b/acf/persistence/backends/augeas.lua index d80f806..ec13531 100644 --- a/acf/persistence/backends/augeas.lua +++ b/acf/persistence/backends/augeas.lua @@ -3,8 +3,6 @@ Copyright (c) 2012-2013 Kaarle Ritvanen See LICENSE file for license details --]] -module(..., package.seeall) - local topology = require('acf.model.root').topology local pth = require('acf.path') @@ -21,7 +19,7 @@ end local function ipath(path, index) return path..'['..index..']' end -backend = require('acf.object').class() +local backend = require('acf.object').class() function backend:init() self.aug = require('augeas').init() end @@ -146,3 +144,6 @@ function backend:set(mods) assert(false) end end + + +return backend diff --git a/acf/persistence/backends/files.lua b/acf/persistence/backends/files.lua index 782674d..b14ed10 100644 --- a/acf/persistence/backends/files.lua +++ b/acf/persistence/backends/files.lua @@ -3,15 +3,13 @@ Copyright (c) 2012-2013 Kaarle Ritvanen See LICENSE file for license details --]] -module(..., package.seeall) - local topology = require('acf.model.root').topology local pth = require('acf.path') local util = require('acf.persistence.util') local copy = require('acf.util').copy -require 'posix' -require 'stringy' +local posix = require('posix') +local stringy = require('stringy') local function get_scope(top) @@ -25,7 +23,7 @@ local function get_scope(top) end -backend = require('acf.object').class() +local backend = require('acf.object').class() -- TODO cache expiration function backend:init() self.cache = {} end @@ -104,3 +102,6 @@ function backend:set(mods) end end end + + +return backend diff --git a/acf/persistence/backends/json.lua b/acf/persistence/backends/json.lua index d2e6196..2ac4b5a 100644 --- a/acf/persistence/backends/json.lua +++ b/acf/persistence/backends/json.lua @@ -3,18 +3,16 @@ Copyright (c) 2012-2013 Kaarle Ritvanen See LICENSE file for license details --]] -module(..., package.seeall) - local pth = require('acf.path') -local Cache = require('acf.persistence.backends.volatile').backend +local Cache = require('acf.persistence.backends.volatile') local util = require('acf.persistence.util') local copy = require('acf.util').copy -require 'json' -require 'posix' +local json = require('json') +local posix = require('posix') -backend = require('acf.object').class() +local backend = require('acf.object').class() function backend:init() -- TODO cache expiration @@ -75,3 +73,6 @@ function backend:set(mods) file:close() end end + + +return backend diff --git a/acf/persistence/backends/null.lua b/acf/persistence/backends/null.lua index 0343cbf..7ff58ce 100644 --- a/acf/persistence/backends/null.lua +++ b/acf/persistence/backends/null.lua @@ -3,8 +3,8 @@ Copyright (c) 2012-2013 Kaarle Ritvanen See LICENSE file for license details --]] -module(..., package.seeall) - -backend = require('acf.object').class() +local backend = require('acf.object').class() function backend:get(path, top) if #path == 0 then return {} end end function backend:set(mods) end + +return backend diff --git a/acf/persistence/backends/volatile.lua b/acf/persistence/backends/volatile.lua index 1b1b4cd..a83f7e3 100644 --- a/acf/persistence/backends/volatile.lua +++ b/acf/persistence/backends/volatile.lua @@ -3,12 +3,10 @@ Copyright (c) 2012-2013 Kaarle Ritvanen See LICENSE file for license details --]] -module(..., package.seeall) - local util = require('acf.util') -backend = require('acf.object').class() +local backend = require('acf.object').class() function backend:init(data) self.data = data or {} end @@ -43,3 +41,6 @@ end function backend:set(mods) for _, mod in ipairs(mods) do self:_set(unpack(mod)) end end + + +return backend diff --git a/acf/persistence/init.lua b/acf/persistence/init.lua index 80534c2..044d4b2 100644 --- a/acf/persistence/init.lua +++ b/acf/persistence/init.lua @@ -3,23 +3,25 @@ Copyright (c) 2012-2013 Kaarle Ritvanen See LICENSE file for license details --]] -module(..., package.seeall) +local M = {} -local loadmods = require('acf.loader').loadmods +local loadmods = require('acf.loader') local topology = require('acf.model.root').topology local object = require('acf.object') local pth = require('acf.path') local util = require('acf.util') -require 'stringy' +local stringy = require('stringy') -DataStore = object.class(require('acf.transaction.backend').TransactionBackend) +local DataStore = object.class( + require('acf.transaction.backend').TransactionBackend +) function DataStore:init() object.super(self, DataStore):init() self.backends = util.map( - function(m) return m.backend() end, + function(m) return m() end, loadmods('persistence/backends') ) end @@ -72,3 +74,6 @@ function DataStore:_set_multiple(mods) for backend, bm in pairs(bms) do backend:set(bm) end end + + +return DataStore() diff --git a/acf/persistence/util.lua b/acf/persistence/util.lua index 46de5eb..a657411 100644 --- a/acf/persistence/util.lua +++ b/acf/persistence/util.lua @@ -1,20 +1,22 @@ --[[ -Copyright (c) 2012 Kaarle Ritvanen +Copyright (c) 2012-2013 Kaarle Ritvanen See LICENSE file for license details --]] -module(..., package.seeall) +local M = {} -function open_file(path, mode) +function M.open_file(path, mode) local file = io.open(path, mode) if not file then error('Cannot open file: '..path) end return file end -function read_file(path) - local file = open_file(path) +function M.read_file(path) + local file = M.open_file(path) local data = '' for line in file:lines() do data = data..line end file:close() return data end + +return M diff --git a/acf/transaction/backend.lua b/acf/transaction/backend.lua index 659fca2..4393101 100644 --- a/acf/transaction/backend.lua +++ b/acf/transaction/backend.lua @@ -3,7 +3,7 @@ Copyright (c) 2012-2013 Kaarle Ritvanen See LICENSE file for license details --]] -module(..., package.seeall) +local M = {} local err = require('acf.error') @@ -13,29 +13,29 @@ local err = require('acf.error') local generation = 0 -function gen_number() +function M.gen_number() generation = generation + 1 return generation end -TransactionBackend = require('acf.object').class() +M.TransactionBackend = require('acf.object').class() -function TransactionBackend:init() self.mod_time = {} end +function M.TransactionBackend:init() self.mod_time = {} end -function TransactionBackend:get_if_older(path, timestamp) +function M.TransactionBackend:get_if_older(path, timestamp) local value, ts = self:get(path) if ts > timestamp then err.raise('conflict', path) end return value, ts end -function TransactionBackend:set(path, value) +function M.TransactionBackend:set(path, value) self:set_multiple{{path, value}} end -function TransactionBackend:set_multiple(mods) +function M.TransactionBackend:set_multiple(mods) -- TODO delegate to PM backends? - local timestamp = gen_number() + local timestamp = M.gen_number() local effective = {} local function tostr(s) return s ~= nil and tostring(s) or nil end @@ -56,7 +56,7 @@ function TransactionBackend:set_multiple(mods) end -- TODO should be atomic, mutex with set_multiple -function TransactionBackend:comp_and_setm(accessed, mods) +function M.TransactionBackend:comp_and_setm(accessed, mods) local errors = err.ErrorDict() for path, timestamp in pairs(accessed) do errors:collect(self.get_if_older, self, path, timestamp) @@ -65,3 +65,6 @@ function TransactionBackend:comp_and_setm(accessed, mods) self:set_multiple(mods) end + + +return M diff --git a/acf/transaction/init.lua b/acf/transaction/init.lua index ca6ca6f..2ca63c5 100644 --- a/acf/transaction/init.lua +++ b/acf/transaction/init.lua @@ -3,8 +3,6 @@ Copyright (c) 2012-2013 Kaarle Ritvanen See LICENSE file for license details --]] -module(..., package.seeall) - local ErrorDict = require('acf.error').ErrorDict local root = require('acf.model.root') local object = require('acf.object') @@ -205,8 +203,8 @@ function Transaction:commit() end -local store = require('acf.persistence').DataStore() +local store = require('acf.persistence') -function start(txn, defer_validation) - return Transaction(txn or store, not (txn and defer_validation)) -end +return function(txn, defer_validation) + return Transaction(txn or store, not (txn and defer_validation)) + end diff --git a/acf/util.lua b/acf/util.lua index 63276ec..e66b711 100644 --- a/acf/util.lua +++ b/acf/util.lua @@ -3,39 +3,41 @@ Copyright (c) 2012-2013 Kaarle Ritvanen See LICENSE file for license details --]] -module(..., package.seeall) +local M = {} -function setdefault(t, k, v) +function M.setdefault(t, k, v) if t[k] == nil then t[k] = v end return t[k] end -function update(dst, src, preserve) +function M.update(dst, src, preserve) for k, v in pairs(src) do if not preserve or dst[k] == nil then dst[k] = v end end return dst end -function setdefaults(dst, src) return update(dst, src, true) end +function M.setdefaults(dst, src) return M.update(dst, src, true) end -function copy(var) - return type(var) == 'table' and setdefaults({}, var) or var +function M.copy(var) + return type(var) == 'table' and M.setdefaults({}, var) or var end -function extend(dst, src) +function M.extend(dst, src) for _, v in ipairs(src) do table.insert(dst, v) end return dst end -function keys(tbl) +function M.keys(tbl) local res = {} for k, v in pairs(tbl) do table.insert(res, k) end return res end -function map(func, tbl) +function M.map(func, tbl) local res = {} for k, v in pairs(tbl) do res[k] = func(v) end return res end + +return M diff --git a/server.lua b/server.lua index c3a25a9..727b339 100644 --- a/server.lua +++ b/server.lua @@ -3,12 +3,12 @@ Copyright (c) 2012-2013 Kaarle Ritvanen See LICENSE file for license details --]] -require 'acf' +local acf = require('acf') local mnode = acf.model.node local isinstance = acf.object.isinstance -require 'json' -require 'stringy' +local json = require('json') +local stringy = require('stringy') math.randomseed(os.time()) @@ -74,7 +74,7 @@ return function(env) return wrap(400, nil, 'Invalid transaction ID') end end - local txn = acf.transaction.start(parent_txn, true) + local txn = acf.start_txn(parent_txn, true) local function fetch_user(name) user = name and txn:fetch('/auth/users')[name] @@ -201,9 +201,7 @@ return function(env) parent[name] = nil elseif method == 'PUT' then - if isinstance(parent, acf.model.set.Set) then - return 405 - end + if isinstance(parent, mnode.Set) then return 405 end local permission = 'modify' if obj == nil then obj = parent @@ -233,7 +231,7 @@ return function(env) session.last_txn_id = session.last_txn_id + 1 local txn_id = session.last_txn_id - session.txns[txn_id] = acf.transaction.start(parent_txn) + session.txns[txn_id] = acf.start_txn(parent_txn) return 204, {['X-ACF-Transaction-ID']=txn_id} end -- cgit v1.2.3