comparison share/lua/5.2/luarocks/persist.lua @ 1132:d137f631bad5

<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
author HackBot
date Fri, 14 Dec 2012 22:24:27 +0000
parents
children
comparison
equal deleted inserted replaced
1131:ff65dfb63263 1132:d137f631bad5
1
2 --- Utility module for loading files into tables and
3 -- saving tables into files.
4 -- Implemented separately to avoid interdependencies,
5 -- as it is used in the bootstrapping stage of the cfg module.
6 module("luarocks.persist", package.seeall)
7
8 local util = require("luarocks.util")
9
10 --- Load a Lua file containing assignments, storing them in a table.
11 -- The global environment is not propagated to the loaded file.
12 -- @param filename string: the name of the file.
13 -- @param tbl table or nil: if given, this table is used to store
14 -- loaded values.
15 -- @return table or (nil, string): a table with the file's assignments
16 -- as fields, or nil and a message in case of errors.
17 function load_into_table(filename, tbl)
18 assert(type(filename) == "string")
19 assert(type(tbl) == "table" or not tbl)
20
21 local result, chunk, ran, err
22 local result = tbl or {}
23 if setfenv then -- Lua 5.1
24 chunk, err = loadfile(filename)
25 if chunk then
26 setfenv(chunk, result)
27 ran, err = pcall(chunk)
28 end
29 else -- Lua 5.2
30 chunk, err = loadfile(filename, "t", result)
31 if chunk then
32 ran, err = pcall(chunk)
33 end
34 end
35
36 if not chunk then
37 if err:sub(1,5) ~= filename:sub(1,5) then
38 return false, err
39 end
40 return nil, "Error loading file: "..err
41 end
42 if not ran then
43 return nil, "Error running file: "..err
44 end
45 return result
46 end
47
48 local write_table
49
50 --- Write a value as Lua code, invoking write_table.
51 -- This function handles only numbers, strings and tables
52 -- are keys (tables are handled recursively).
53 -- @param out userdata: a file object, open for writing.
54 -- @param v: the value to be written.
55 -- @param level number: the indentation level
56 -- @param sub_order table: optional prioritization table
57 -- @see write_table
58 local function write_value(out, v, level, sub_order)
59 if type(v) == "table" then
60 write_table(out, v, level + 1, sub_order)
61 elseif type(v) == "string" then
62 if v:match("\n") then
63 local open, close = "[[", "]]"
64 local equals = 0
65 while v:find(open,1,true) or v:find(close,1,true) do
66 equals = equals + 1
67 local eqs = ("="):rep(equals)
68 open, close = "["..eqs.."[", "]"..eqs.."]"
69 end
70 out:write(open.."\n"..v..close)
71 else
72 out:write("\""..v:gsub("\"", "\\\"").."\"")
73 end
74 else
75 out:write(tostring(v))
76 end
77 end
78
79 --- Write a table as Lua code representing a table to disk
80 -- (that is, in curly brackets notation).
81 -- This function handles only numbers, strings and tables
82 -- are keys (tables are handled recursively).
83 -- @param out userdata: a file object, open for writing.
84 -- @param tbl table: the table to be written.
85 -- @param level number: the indentation level
86 -- @param field_order table: optional prioritization table
87 write_table = function(out, tbl, level, field_order)
88 out:write("{")
89 local sep = "\n"
90 local indentation = " "
91 local indent = true
92 local i = 1
93 for k, v, sub_order in util.sortedpairs(tbl, field_order) do
94 out:write(sep)
95 if indent then
96 for n = 1,level do out:write(indentation) end
97 end
98 sep = ",\n"
99 indent = true
100 if type(k) == "number" then
101 if k ~= i then
102 out:write("["..tostring(k).."]=")
103 else
104 i = i + 1
105 end
106 indent = false
107 sep = ", "
108 elseif type(k) == "table" then
109 out:write("[")
110 write_table(out, k, level + 1)
111 out:write("] = ")
112 else
113 if k:match("^[a-zA-Z_][a-zA-Z0-9_]*$") then
114 out:write(k.." = ")
115 else
116 out:write("['"..k:gsub("'", "\\'").."'] = ")
117 end
118 end
119 write_value(out, v, level, sub_order)
120 end
121 if sep ~= "\n" then
122 out:write("\n")
123 for n = 1,level-1 do out:write(indentation) end
124 end
125 out:write("}")
126 end
127
128 --- Writes a table to an io-like object.
129 -- @param out userdata: a file object, open for writing.
130 -- @param tbl table: the table to be written.
131 -- @param field_order table: optional prioritization table
132 -- @return userdata The file object originally passed in as the `out` parameter.
133 local function write_table(out, tbl, field_order)
134 for k, v, sub_order in util.sortedpairs(tbl, field_order) do
135 out:write(k.." = ")
136 write_value(out, v, 0, sub_order)
137 out:write("\n")
138 end
139 return out
140 end
141
142 --- Save the contents of a table to a string.
143 -- Each element of the table is saved as a global assignment.
144 -- Only numbers, strings and tables (containing numbers, strings
145 -- or other recursively processed tables) are supported.
146 -- @param tbl table: the table containing the data to be written
147 -- @param field_order table: an optional array indicating the order of top-level fields.
148 -- @return string
149 function save_from_table_to_string(tbl, field_order)
150 local out = {buffer = {}}
151 function out:write(data) table.insert(self.buffer, data) end
152 write_table(out, tbl, field_order)
153 return table.concat(out.buffer)
154 end
155
156 --- Save the contents of a table in a file.
157 -- Each element of the table is saved as a global assignment.
158 -- Only numbers, strings and tables (containing numbers, strings
159 -- or other recursively processed tables) are supported.
160 -- @param filename string: the output filename
161 -- @param tbl table: the table containing the data to be written
162 -- @param field_order table: an optional array indicating the order of top-level fields.
163 -- @return boolean or (nil, string): true if successful, or nil and a
164 -- message in case of errors.
165 function save_from_table(filename, tbl, field_order)
166 local out = io.open(filename, "w")
167 if not out then
168 return nil, "Cannot create file at "..filename
169 end
170 write_table(out, tbl, field_order)
171 out:close()
172 return true
173 end