Permalink
Browse files

Add basic ini parser, SABnzbd.local to attempt to login using the loc…

…al settings
  • Loading branch information...
1 parent d368214 commit cea7d86dc1368d6edadf1577daf14445453dbfb5 @Sutto committed Jan 8, 2009
Showing with 59 additions and 0 deletions.
  1. +59 −0 lib/sabnzbd.rb
View
@@ -121,4 +121,63 @@ def jobs
Status.jobs_from(status)
end
+ class << self
+
+ @@settings_path = File.expand_path("~/.sabnzbd/sabnzbd.ini")
+
+ def settings_path
+ @@settings_path
+ end
+
+ def settings_path=(value)
+ @@settings_path = value
+ end
+
+ def settings
+ self.parse_ini(File.read(self.settings_path))
+ end
+
+ def local
+ s = self.settings
+ self.new(s["misc"]["username"], s["misc"]["password"])
+ end
+
+ protected
+
+ def parse_ini(text)
+ options = {}
+ keys = []
+ text.each_line do |line|
+ if line =~ /^(\[+([^\[\]]+)\]+)$/
+ normal, name = $1, $2
+ start = line.match(/^\[+/)[0]
+ depth = start.length
+ if depth == 1
+ keys = []
+ elsif depth <= keys.size
+ width = (depth - 2)
+ width = 0 if width < 0
+ keys = keys[0..width]
+ end
+ keys << name
+ set_value options, keys, {}
+ elsif line =~ /^(.*)=(.*)$/
+ key, value = $1.strip, $2.strip
+ set_value options, (keys + [key]), value
+ end
+ end
+ return options
+ end
+
+ def set_value(options, keys, value)
+ r = options
+ keys[0..-2].each do |k|
+ r = (r[k] ||= {})
+ end
+ r[keys.last] = value
+ return options
+ end
+
+ end
+
end

0 comments on commit cea7d86

Please sign in to comment.