Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'ruby/file/write'
- class Butler
- >-IsGem = false
- >-# This Class provides services to botcontrol
- >-# For gems, this class uses the Gem module to get information on the where-
- >-# abouts of data. If installed using rake from tarball, it is hard-coded.
- >-class Control
- >->-attr_reader :dialog
- >->-attr_reader :config
- >->-attr_reader :path
- >->-def initialize
- >->->-@path = OpenStruct.new({
- >->->->-:config => "/etc/butler"+'/config.yaml',
- >->->->-:dialogs => "/usr/share/butler/dialogs",
- >->->->-:plugins => "/usr/share/butler/plugins",
- >->->->-:services => ,
- >->->->-:strings => "/usr/share/butler/strings",
- >->->-})
- >->->-File.write(@path.config, File.read(@path.template)) unless File.exist?(@path.config)
- >->->-@config = OpenStruct.new(YAML.load_file(@path.config))
- >->->-@dialog = DialogLine.new(@path.dialogs, @config.language)
- >->-end
- >->-
- >->-def user(name=nil)
- >->->-# use Etc.getlogin?
- >->->-name || ENV['SUDO_USER'] || ENV['USER'] || @dialog.discuss("username", false)[:username]
- >->-end
- >->-def configured?(user=nil)
- >->->-path = @config.users[user(user)]
- >->->-path && File.exist?(path)
- >->-end
- >->-def configure_user(user=nil)
- >->->-raise Errno::EACCES unless File.writable?(@path.config)
- >->->-user ||= user()
- >->->-installer = Installer.new("butler")
- >->->-user_config = @dialog.discuss(:botcontrol, false,
- >->->->-:installer => installer,
- >->->->-:user => user,
- >->->->-:path => path
- >->->-)
- >->->-path = @config.users[user] = user_config.delete(:config)+"/config.yaml"
- >->->-user_config[:language] = "en" # FIXME multilingualize
- >->->-update_config
- >->->-FileUtils.mkdir_p(File.dirname(path))
- >->->-FileUtils.chown(user, nil, File.dirname(path))
- >->->-File.write(path, user_config.to_yaml)
- >->->-FileUtils.chown(user, nil, path)
- >->->-OpenStruct.new(@config.users[user])
- >->-end
- >->-
- >->-def update_config
- >->->-File.write(@path.config, @config.to_hash.to_yaml)
- >->-end
- >->-def user_config(user=nil)
- >->->-path = @config.users[user(user)]
- >->->-user_config = OpenStruct.new(YAML.load_file(path))
- >->->-user_config.plugin_repository = @path.plugins
- >->->-user_config
- >->-end
- >->-def butler_path(user=nil)
- >->->-path = @config.users[user(user)]
- >->->-user_config = OpenStruct.new(YAML.load_file(path))
- >->->-user_config.plugin_repository = @path.plugins
- >->->-user_config.service_repository = @path.services
- >->->-user_config
- >->-end
- >->-
- >->-def discuss(dir, lang=nil, variables={}, &block)
- >->->-@dialog.discuss(dir, lang, variables.merge(:botcontrol => self), &block)
- >->-end>-
- >->-def language
- >->->-"en"
- >->-end
- >-end
- end
Add Comment
Please, Sign In to add comment