Guest User

Untitled

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