Guest User

Untitled

a guest
Mar 14th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.08 KB | None | 0 0
  1. #--
  2. # Copyright 2007 by Stefan Rusterholz.
  3. # All rights reserved.
  4. # See LICENSE.txt for permissions.
  5. #++
  6.  
  7.  
  8.  
  9. require 'ruby/file/write'
  10.  
  11.  
  12.  
  13. class Butler
  14. IsGem = true # REPLACE: IsGem = false
  15.  
  16. # This Class provides services to botcontrol
  17. # For gems, this class uses the Gem module to get information on the where-
  18. # abouts of data. If installed using rake from tarball, it is hard-coded.
  19. class Control
  20. attr_reader :dialog
  21. attr_reader :config
  22. attr_reader :path
  23.  
  24. def initialize
  25. @path = OpenStruct.new({
  26. :template => Gem.datadir("butler")+'/config_template.yaml', # DELETE
  27. :config => Gem.datadir("butler")+'/config.yaml', # REPLACE: :config => %CONFIG_DIR%+'/config.yaml',
  28. :dialogs => Gem.datadir("butler")+'/dialogs', # REPLACE: :dialogs => %DIALOGS_DIR%,
  29. :plugins => Gem.datadir("butler")+'/plugins', # REPLACE: :plugins => %PLUGINS_DIR%,
  30. :services => Gem.datadir("butler")+'/services', # REPLACE: :services => %SERVICES_DIR%,
  31. :strings => Gem.datadir("butler")+'/strings', # REPLACE: :strings => %STRINGS_DIR%,
  32. :man => Gem.datadir("butler")+'/man1', # DELETE
  33. })
  34. File.write(@path.config, File.read(@path.template)) unless File.exist?(@path.config)
  35. @config = OpenStruct.new(YAML.load_file(@path.config))
  36. @dialog = DialogLine.new(@path.dialogs, @config.language)
  37. end
  38.  
  39. def user(name=nil)
  40. # use Etc.getlogin?
  41. name || ENV['SUDO_USER'] || ENV['USER'] || @dialog.discuss("username", false)[:username]
  42. end
  43.  
  44. def configured?(user=nil)
  45. path = @config.users[user(user)]
  46. path && File.exist?(path)
  47. end
  48.  
  49. def configure_user(user=nil)
  50. raise Errno::EACCES unless File.writable?(@path.config)
  51. user ||= user()
  52. installer = Installer.new("butler")
  53. user_config = @dialog.discuss(:botcontrol, false,
  54. :installer => installer,
  55. :user => user,
  56. :path => path
  57. )
  58. path = @config.users[user] = user_config.delete(:config)+"/config.yaml"
  59. user_config[:language] = "en" # FIXME multilingualize
  60. update_config
  61. FileUtils.mkdir_p(File.dirname(path))
  62. #FileUtils.chown(user, nil, File.dirname(path))
  63. File.write(path, user_config.to_yaml)
  64. #FileUtils.chown(user, nil, path)
  65. OpenStruct.new(@config.users[user])
  66. end
  67.  
  68. def update_config
  69. File.write(@path.config, @config.to_hash.to_yaml)
  70. end
  71.  
  72. def user_config(user=nil)
  73. path = @config.users[user(user)]
  74. user_config = OpenStruct.new(YAML.load_file(path))
  75. user_config.plugin_repository = @path.plugins
  76. user_config.service_repository = @path.services
  77. user_config
  78. end
  79.  
  80. def butler_path(user=nil)
  81. path = @config.users[user(user)]
  82. user_config = OpenStruct.new(YAML.load_file(path))
  83. user_config.plugin_repository = @path.plugins
  84. user_config.service_repository = @path.services
  85. user_config
  86. end
  87.  
  88. def discuss(dir, lang=nil, variables={}, &block)
  89. @dialog.discuss(dir, lang, variables.merge(:botcontrol => self), &block)
  90. end
  91.  
  92. def language
  93. "en"
  94. end
  95. end
  96. end
Add Comment
Please, Sign In to add comment