/* * config - zavai configuration * * Copyright (C) 2009 Enrico Zini * * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ namespace zavai { public class Config { protected Lua.LuaVM lua; protected weak string get_string(string name) { lua.get_global(name); weak string res = lua.to_string(-1); lua.pop(1); return res; } protected void set_string(string name, string? val) { if (val == null) lua.push_nil(); else lua.push_string(val); lua.set_global(name); } protected weak int get_int(string name) { lua.get_global(name); int res = lua.to_integer(-1); lua.pop(1); return res; } protected void set_int(string name, int val) { lua.push_integer(val); lua.set_global(name); } public string version { get { return get_string("version"); } set { set_string("version", value); } } public string homedir { get { return get_string("homedir"); } set { set_string("homedir", value); } } public string icondir { get { return get_string("icondir"); } set { set_string("icondir", value); } } public int min_button_height { get { return get_int("min_button_height"); } set { set_int("min_button_height", value); } } public string gprs_apn { get { return get_string("gprs_apn"); } set { set_string("gprs_apn", value); } } public string gprs_user { get { return get_string("gprs_user"); } set { set_string("gprs_user", value); } } public string gprs_pass { get { return get_string("gprs_pass"); } set { set_string("gprs_pass", value); } } public int backlight_max { get; set; } private string _argv0; public string argv0 { get { return _argv0; } set { _argv0 = value; } } public Config() { lua = new Lua.LuaVM(); lua.open_libs(); stderr.printf("ZA1\n"); // Set defaults version = "0.1"; stderr.printf("ZA2\n"); homedir = GLib.Environment.get_home_dir() + "/.zavai"; stderr.printf("ZA3\n"); icondir = GLib.Environment.get_variable("ZAVAI_ICONDIR"); stderr.printf("ZA4\n"); if (icondir == null) icondir = "/usr/share/zavai/icons"; stderr.printf("ZA5\n"); min_button_height = 80; stderr.printf("ZA6\n"); gprs_apn = "general.t-mobile.uk"; stderr.printf("ZA7\n"); gprs_user = "x"; stderr.printf("ZA8\n"); gprs_pass = "x"; stderr.printf("ZA9\n"); backlight_max = 15; stderr.printf("ZA10\n"); // Read config if (lua.do_file(homedir + "/config")) { zavai.log.error("Failed to parse " + homedir + "/config: " + lua.to_string(-1)); } stderr.printf("ZA11\n"); } /* def _get_homedir(self): res = self.get("global", "home") if res is None: res = os.path.expanduser("~/.zavai") if not os.path.isdir(res): zavai.info("Creating directory", res) os.makedirs(res) return res */ } /* import os import ConfigParser, StringIO def read_config(rootDir = None, defaults = None, nick="octofuss"): """ Read octofuss configuration, returning a ConfigParser object """ if rootDir == None: rootDir = os.environ.get(nick.upper() + "_CONFDIR", "/etc/" + nick) files = [] def trytouse(path): if os.path.exists(path): files.append(path) # Start with the main config file trytouse(os.path.join(rootDir, nick + ".conf")) # Add snippets found in rc.d style directory subdir = os.path.join(rootDir, nick + ".conf.d") if os.path.isdir(subdir): for file in sorted(os.listdir(subdir)): if file.startswith('#'): continue if file.startswith('.'): continue if file.endswith('~'): continue if file.endswith('.bak'): continue trytouse(os.path.join(subdir, file)) config = ConfigParser.ConfigParser() if defaults != None: infile = StringIO.StringIO(defaults) config.readfp(infile, "defaults") config.read(files) return config import os.path import zavai class Config: def __init__(self): self.conf = zavai.read_config(nick="zavai") def get(self, section, name, default=None): if self.conf.has_section(section): if self.conf.has_option(section, name): return self.conf.get(section, name) return None homedir = property(_get_homedir) */ public Config config = null; }