webmcp

annotate framework/env/__init.lua @ 498:e360b1933c78

Improve efficiency of table.insert in case of Lua 5.3 (do not use compatibility wrapper)
author jbe
date Sun Jul 23 03:43:49 2017 +0200 (2017-07-23)
parents 7eec5b604b00
children
rev   line source
jbe@324 1 --[[--
jbe@324 2 translated_string = -- translated string
jbe@324 3 _(
jbe@324 4 string_to_translate, -- string to translate
jbe@324 5 {
jbe@324 6 placeholder_name1 = text1, -- replace all occurrences of "#{placeholder_name1}" with the string text1
jbe@324 7 placeholder_name2 = text2, -- replace all occurrences of "#{placeholder_name2}" with the string text2
jbe@324 8 ...
jbe@324 9 }
jbe@324 10 )
jbe@324 11
jbe@325 12 Translation function for localization. The "_" function translates a given string to the currently selected language (see locale.set{...}). If the translated string contains placeholders in the form #{name}, then those placeholders may be automatically replaced with a corresponding substring which is taken from the table passed as optional second argument.
jbe@325 13
jbe@325 14 Hint: Lua's syntax rules allow to write _"text" as a shortcut for _("text"), or _'text' instead of _('text') respectivly.
jbe@324 15
jbe@324 16 --]]--
jbe@324 17
jbe@324 18 function _G._(text, replacements)
jbe/bsw@0 19 local text = locale._get_translation_table()[text] or text
jbe/bsw@0 20 if replacements then
jbe/bsw@0 21 return (
jbe/bsw@0 22 string.gsub(
jbe/bsw@0 23 text,
jbe/bsw@0 24 "#{(.-)}",
jbe/bsw@0 25 function (placeholder)
jbe/bsw@0 26 return replacements[placeholder]
jbe/bsw@0 27 end
jbe/bsw@0 28 )
jbe/bsw@0 29 )
jbe/bsw@0 30 else
jbe/bsw@0 31 return text
jbe/bsw@0 32 end
jbe/bsw@0 33 end
jbe@324 34 --//--
jbe@203 35
jbe@203 36 --[[--
jbe@203 37 cloned_table = -- newly generated table
jbe@203 38 table.new(
jbe@203 39 table_or_nil -- keys of a given table will be copied to the new table
jbe@203 40 )
jbe@203 41
jbe@203 42 If a table is given, then a cloned table is returned.
jbe@203 43 If nil is given, then a new empty table is returned.
jbe@203 44
jbe@203 45 --]]--
jbe@203 46 function table.new(tbl)
jbe@240 47 local new_tbl = {}
jbe@203 48 if tbl then
jbe@203 49 for key, value in pairs(tbl) do
jbe@203 50 new_tbl[key] = value
jbe@203 51 end
jbe@203 52 end
jbe@203 53 return new_tbl
jbe@203 54 end
jbe@203 55 --//--
jbe@203 56
jbe@495 57 --[[--
jbe@495 58 table.insert(
jbe@495 59 t, -- table
jbe@495 60 index, -- optional index
jbe@495 61 value -- value
jbe@495 62 )
jbe@495 63
jbe@495 64 Custom implementation of Lua's table.insert(...) where table.insert(table, value) also respects metamethods in Lua 5.2 (this behavior is already supported by Lua 5.3).
jbe@495 65
jbe@495 66 --]]--
jbe@498 67 if _VERSION == "Lua 5.2" then
jbe@495 68 local old_insert = table.insert
jbe@495 69 function table.insert(...)
jbe@498 70 if select("#", ...) == 2 then
jbe@495 71 local t, value = ...
jbe@495 72 t[#t+1] = value
jbe@495 73 return
jbe@495 74 end
jbe@495 75 return old_insert(...)
jbe@495 76 end
jbe@495 77 end
jbe@495 78 --//--
jbe@495 79
jbe@352 80 -- load libraries
jbe@352 81 -- (except "extos", which has to be loaded earlier, and "multirand", which must be loaded after forking)
jbe@324 82 _G.nihil = require 'nihil'
jbe@324 83 _G.mondelefant = require 'mondelefant'
jbe@324 84 _G.atom = require 'atom'
jbe@324 85 _G.json = require 'json'
jbe@203 86 require 'mondelefant_atom_connector'
jbe@286 87 -- NOTE: "multirand" library is loaded in mcp.lua after forking
jbe@203 88
jbe@295 89 -- setup mondelefant
jbe@295 90 mondelefant.connection_prototype.error_objects = true
jbe@295 91 function mondelefant.connection_prototype:sql_tracer(command)
jbe@295 92 if trace.is_disabled() then
jbe@295 93 return
jbe@295 94 end
jbe@295 95 local start_time = extos.monotonic_hires_time()
jbe@295 96 return function(error_info)
jbe@295 97 trace.sql{
jbe@295 98 command = command,
jbe@295 99 execution_time = extos.monotonic_hires_time() - start_time,
jbe@295 100 error_position = error_info and error_info.position or nil
jbe@295 101 }
jbe@295 102 end
jbe@295 103 end
jbe@295 104
jbe@206 105 --[[--
jbe@206 106 config -- table to store application configuration
jbe@206 107
jbe@206 108 'config' is a global table, which can be modified by a config file of an application to modify the behaviour of that application.
jbe@206 109 --]]--
jbe@324 110 _G.config = {}
jbe@206 111 --//--
jbe@206 112

Impressum / About Us