-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.lua
More file actions
89 lines (77 loc) · 2.3 KB
/
Copy pathinit.lua
File metadata and controls
89 lines (77 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
---@diagnostic disable: lowercase-global
---@diagnostic disable-next-line: unused-local
local mname, murl -- Standard lua `require` arguments
, margs = ... -- Reserved for user custom extension
---@class M.INIT_OPTIONS
local options = {}
--==================================== INIT OPTIONS ====================================--
options.GLOBAL_IMPORT = true;
options.INTERFACE_INCLUDED = true;
options.DEFAULT_I_FEATURE = "general";
--==================================== INIT OPTIONS ====================================--
-- Merge import options
if type(margs) == "table" then
for k, v in next, margs do
if v ~= nil then
options[k] = v
end
end
end
local M = require "simpleclass.m" ---@class M
require "simpleclass.object"
require "simpleclass.class"
require "simpleclass.super"
---Whether to register classes as global variables automatically.
---Default true when import globally, false otherwise. Could be overridden in runtime.
---@type boolean
M.AUTO_GLOBAL = false
---Interface feature to use, meaningless if interface module not included.
---**Warning**: switch in "general" & "nocheck" is safe, BUT "lexical" skipping definition is irreversible.
---@type simpleclass.I_FEATURE
M.I_FEATURE = options.DEFAULT_I_FEATURE
---Import fields from simpleclass module to environment.
---eg:
---```
---simpleclass.env_import({
--- "class",
--- "super",
--- ["type"] = "cls_type"
---}, _ENV)
---```
---which means:
---```python
---from simpleclass import
--- class,
--- super,
--- type as cls_type
---```
---@param fields {[simpleclass.FIELD]?: string, [integer]?: simpleclass.FIELD}
---@param env? table Default to `_G` if missing.
function M.env_import(fields, env)
env = env or _G
for k, v in next, fields do
if M[v] then -- eg. { "class" }
env[v] = M[v]
elseif M[k] then -- eg. { "cls_type" = "type" }
env[v] = M[k]
end
end
end
---Include interface module
if options.INTERFACE_INCLUDED then
require "simpleclass.interface"
end
---Global import
if options.GLOBAL_IMPORT then
M.AUTO_GLOBAL = true
M.env_import {
"class",
"super",
"interface",
"object",
"property",
"isinstance",
"issubclass",
}
end
return M