examples: add some examples in Lua using lgi library
authorJiří Klimeš <jklimes@redhat.com>
Thu, 17 Jul 2014 14:13:50 +0000 (16:13 +0200)
committerJiří Klimeš <jklimes@redhat.com>
Fri, 30 Jan 2015 13:41:13 +0000 (14:41 +0100)
[libnm]     https://developer.gnome.org/libnm/1.0/
[lgi]       https://github.com/pavouk/lgi
[lgi-guide] https://github.com/pavouk/lgi/blob/master/docs/guide.md

On most distribution just install lua-lgi.

Note:
There is a bug in lgi. It doesn't handle GPtrArray corectly. It results in
crashing on list-devices.lua and list-connections.lua.
I will send a patch to lgi to fix the issue.

configure.ac
examples/Makefile.am
examples/lua/Makefile.am [new file with mode: 0644]
examples/lua/lgi/Makefile.am [new file with mode: 0644]
examples/lua/lgi/add-connection.lua [new file with mode: 0755]
examples/lua/lgi/list-connections.lua [new file with mode: 0755]
examples/lua/lgi/list-devices.lua [new file with mode: 0755]

index 9fbfe00..cd609d4 100644 (file)
@@ -1024,6 +1024,8 @@ examples/python/Makefile
 examples/python/dbus/Makefile
 examples/python/gi/Makefile
 examples/ruby/Makefile
+examples/lua/Makefile
+examples/lua/lgi/Makefile
 examples/C/Makefile
 examples/C/glib/Makefile
 examples/C/qt/Makefile
index a227f0e..3521b4c 100644 (file)
@@ -2,5 +2,6 @@ SUBDIRS= \
        shell \
        python \
        ruby \
+       lua \
        C \
        dispatcher
diff --git a/examples/lua/Makefile.am b/examples/lua/Makefile.am
new file mode 100644 (file)
index 0000000..bf14f68
--- /dev/null
@@ -0,0 +1,2 @@
+SUBDIRS= lgi
+
diff --git a/examples/lua/lgi/Makefile.am b/examples/lua/lgi/Makefile.am
new file mode 100644 (file)
index 0000000..1b16368
--- /dev/null
@@ -0,0 +1,4 @@
+EXTRA_DIST = \
+       add-connection.lua \
+       list-connections.lua \
+       list-devices.lua
diff --git a/examples/lua/lgi/add-connection.lua b/examples/lua/lgi/add-connection.lua
new file mode 100755 (executable)
index 0000000..3daf5f3
--- /dev/null
@@ -0,0 +1,98 @@
+#!/usr/bin/env lua
+-- -*- Mode: Lua; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
+-- vim: ft=lua ts=2 sts=2 sw=2 et ai
+--
+-- This program is free software; you can redistribute it and/or modify
+-- it under the terms of the GNU General Public License as published by
+-- the Free Software Foundation; either version 2 of the License, or
+-- (at your option) any later version.
+--
+-- This program is distributed in the hope that it will be useful,
+-- but WITHOUT ANY WARRANTY; without even the implied warranty of
+-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+-- GNU General Public License for more details.
+--
+-- You should have received a copy of the GNU General Public License along
+-- with this program; if not, write to the Free Software Foundation, Inc.,
+-- 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+--
+-- Copyright 2015 Red Hat, Inc.
+--
+--
+-- Adding an Ethernet connection to NetworkManager in Lua.
+-- The example uses libnm library using GObject introspection via Lua lgi module.
+-- Most distribution ship the module as lua-lgi package.
+-- libnm guide:   https://developer.gnome.org/libnm/1.0/
+-- Lua-lgi guide: https://github.com/pavouk/lgi/blob/master/docs/guide.md
+--
+
+local lgi = require 'lgi'
+local GLib = lgi.GLib
+local NM = lgi.NM
+
+function uuid()
+  math.randomseed(os.time())
+  local template ='xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'
+  local uuid = string.gsub(template, '[xy]', function (c)
+    local v = (c == 'x') and math.random(0, 0xf) or math.random(8, 0xb)
+    return string.format('%x', v)
+  end)
+  return uuid
+end
+
+-- function creating NMConnection
+function create_profile(name)
+  profile = NM.SimpleConnection.new()
+
+  s_con = NM.SettingConnection.new()
+  s_wired = NM.SettingWired.new()
+  s_con[NM.SETTING_CONNECTION_ID] = name
+  s_con[NM.SETTING_CONNECTION_UUID] = uuid()
+  s_con[NM.SETTING_CONNECTION_TYPE] = "802-3-ethernet"
+
+  profile:add_setting(s_con)
+  profile:add_setting(s_wired)
+
+  -- show the connection
+  -- profile:dump()
+  return profile
+end
+
+-- callback function for add_connection_async()
+function added_cb(client, result, data)
+  local con,err,code = client:add_connection_finish(result)
+  if con then
+    print("The connection profile has been succesfully added to NetworkManager:")
+    print(con:get_id(), con:get_uuid())
+  else
+    print(string.format("Error: (%d) %s", code, err))
+  end
+  main_loop:quit() -- exit now
+end
+
+
+---------------------------
+-- Main code starts here --
+---------------------------
+-- parse command-line arguments
+local name, persist = ...
+if (not name or (persist and not string.find("persistent", persist, 1))) then
+  print(string.format("Usage: %s <connection name> [persistent]", arg[0]:gsub(".*/","")))
+  os.exit(1)
+end
+
+-- create GLib main loop
+main_loop = GLib.MainLoop(nil, false)
+
+-- create Client object
+local client = NM.Client.new()
+
+-- create a connection profile
+local con = create_profile(name)
+
+-- send the connection to NetworkManager
+client:add_connection_async(con, persist, nil, added_cb, nil)
+
+-- run main loop so that the callback could be called
+main_loop:run()
+
diff --git a/examples/lua/lgi/list-connections.lua b/examples/lua/lgi/list-connections.lua
new file mode 100755 (executable)
index 0000000..f2eda44
--- /dev/null
@@ -0,0 +1,63 @@
+#!/usr/bin/env lua
+-- -*- Mode: Lua; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
+-- vim: ft=lua ts=2 sts=2 sw=2 et ai
+--
+-- This program is free software; you can redistribute it and/or modify
+-- it under the terms of the GNU General Public License as published by
+-- the Free Software Foundation; either version 2 of the License, or
+-- (at your option) any later version.
+--
+-- This program is distributed in the hope that it will be useful,
+-- but WITHOUT ANY WARRANTY; without even the implied warranty of
+-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+-- GNU General Public License for more details.
+--
+-- You should have received a copy of the GNU General Public License along
+-- with this program; if not, write to the Free Software Foundation, Inc.,
+-- 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+--
+-- Copyright 2015 Red Hat, Inc.
+--
+--
+-- Getting and printing all NetworkManager connection in Lua.
+-- The example uses libnm library using GObject introspection via Lua lgi module.
+-- Most distribution ship the module as lua-lgi package.
+-- libnm guide:   https://developer.gnome.org/libnm/1.0/
+-- Lua-lgi guide: https://github.com/pavouk/lgi/blob/master/docs/guide.md
+--
+
+local lgi = require 'lgi'
+local GLib = lgi.GLib
+local NM = lgi.NM
+
+function print_values(setting, key, value, flags, data)
+  print(string.format("  %s.%s: %s", setting:get_name(), key, value and tostring(value.value) or "nil"))
+end
+
+
+---------------------------
+-- Main code starts here --
+---------------------------
+-- parse command-line arguments
+local details = ...
+if (details and not string.find("details", details, 1)) then
+  print(string.format("Usage: %s [details]", arg[0]:gsub(".*/","")))
+    os.exit(1)
+end
+
+-- create Client object
+local client = NM.Client.new()
+
+-- get connections
+connections = client:get_connections()
+
+-- print the connections' details
+print(string.format("There are %s connection profiles.", #connections))
+for i, c in pairs(connections) do
+  print(string.format("=== %s | %25s | %s ", c:get_uuid(), c:get_id(), c:get_path()))
+  if details then
+    c:for_each_setting_value(print_values, nil)
+    print("\n")
+  end
+end
+
diff --git a/examples/lua/lgi/list-devices.lua b/examples/lua/lgi/list-devices.lua
new file mode 100755 (executable)
index 0000000..712d353
--- /dev/null
@@ -0,0 +1,51 @@
+#!/usr/bin/env lua
+-- -*- Mode: Lua; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
+-- vim: ft=lua ts=2 sts=2 sw=2 et ai
+--
+-- This program is free software; you can redistribute it and/or modify
+-- it under the terms of the GNU General Public License as published by
+-- the Free Software Foundation; either version 2 of the License, or
+-- (at your option) any later version.
+--
+-- This program is distributed in the hope that it will be useful,
+-- but WITHOUT ANY WARRANTY; without even the implied warranty of
+-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+-- GNU General Public License for more details.
+--
+-- You should have received a copy of the GNU General Public License along
+-- with this program; if not, write to the Free Software Foundation, Inc.,
+-- 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+--
+-- Copyright 2015 Red Hat, Inc.
+--
+--
+-- Getting basic information about network interfaces known to NetworkManager.
+-- The example uses libnm library using GObject introspection via Lua lgi module.
+-- Most distribution ship the module as lua-lgi package.
+-- libnm guide:   https://developer.gnome.org/libnm/1.0/
+-- Lua-lgi guide: https://github.com/pavouk/lgi/blob/master/docs/guide.md
+--
+
+local lgi = require 'lgi'
+local GLib = lgi.GLib
+local NM = lgi.NM
+
+---------------------------
+-- Main code starts here --
+---------------------------
+-- create Client object
+local client = NM.Client.new()
+
+-- get all devices
+devices = client:get_devices()
+
+-- print device details
+for i, d in ipairs(devices) do
+print("============================")
+  print(string.format("Interface: %s", d[NM.DEVICE_INTERFACE]))
+  print(string.format("MAC: %s",       d:get_hw_address()))
+  print(string.format("Type: %s",      d:get_device_type()))
+  print(string.format("Driver: %s",    d:get_driver()))
+  print(string.format("State: %s",     d:get_state()))
+end
+