Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #--
- # Copyright 2007 by Stefan Rusterholz.
- # All rights reserved.
- # See LICENSE.txt for permissions.
- #++
- require 'ruby/file/write'
- class Butler
- IsGem = true # REPLACE: 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({
- :template => Gem.datadir("butler")+'/config_template.yaml', # DELETE
- :config => Gem.datadir("butler")+'/config.yaml', # REPLACE: :config => %CONFIG_DIR%+'/config.yaml',
- :dialogs => Gem.datadir("butler")+'/dialogs', # REPLACE: :dialogs => %DIALOGS_DIR%,
- :plugins => Gem.datadir("butler")+'/plugins', # REPLACE: :plugins => %PLUGINS_DIR%,
- :services => Gem.datadir("butler")+'/services', # REPLACE: :services => %SERVICES_DIR%,
- :strings => Gem.datadir("butler")+'/strings', # REPLACE: :strings => %STRINGS_DIR%,
- :man => Gem.datadir("butler")+'/man1', # DELETE
- })
- 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.service_repository = @path.services
- 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