Skip to content
Newer
Older
100644 74 lines (62 sloc) 1.74 KB
8c06b47 readme updated
Anton Pirogov authored
1 #!/usr/bin/env ruby
caeffbf little change in kapinotify, project rename
Anton Pirogov authored
2 #KapiTools configuration loading/saving
8c06b47 readme updated
Anton Pirogov authored
3 #Copyright (C) 2010 Anton Pirogov
4 #Licensed under the GPL version 3 or later
5
8ed6fba Current state
Anton Pirogov authored
6 require 'base64'
7
17f5000 config location is now searched in program dir (not in current user dir)
Anton Pirogov authored
8 #get absolute directory of current script
9 def progdir
10 File.expand_path(File.dirname(__FILE__))
11 end
12
8ed6fba Current state
Anton Pirogov authored
13 class Configuration
14 attr_accessor :nickname,:password,:groups
15
221103b refator, cache func added -> 2-3x speedup (subjectively)
Anton Pirogov authored
16 #save configuration to 2 files - nick+pwd into autokapi.config
17 # & all the groups as marshal to autokapi.groups
8ed6fba Current state
Anton Pirogov authored
18 def save
17f5000 config location is now searched in program dir (not in current user dir)
Anton Pirogov authored
19 f=File.open(progdir+'/autokapi.config','w')
8ed6fba Current state
Anton Pirogov authored
20 f.puts "nickname="+@nickname
21 f.puts "password="+@password
22 f.close
17f5000 config location is now searched in program dir (not in current user dir)
Anton Pirogov authored
23 File.open(progdir+'/autokapi.groups','w') do |f|
8ed6fba Current state
Anton Pirogov authored
24 Marshal.dump(@groups,f)
25 end
26 end
27
28
221103b refator, cache func added -> 2-3x speedup (subjectively)
Anton Pirogov authored
29 #try to load conf files... if fail - init empty stuff
8ed6fba Current state
Anton Pirogov authored
30 def initialize
31 puts 'Loading configuration files...' if $DEBUG
32
17f5000 config location is now searched in program dir (not in current user dir)
Anton Pirogov authored
33 if File.exists?(progdir+'/autokapi.config')
34 f = File.open(progdir+'/autokapi.config','r')
8ed6fba Current state
Anton Pirogov authored
35 strings = f.readlines
36 f.close
37
38 #split each line to array [varname, value]
39 #then assign each corresponding instance variable the according value
40 strings.map!{|s| s=s.split('=') }
41 strings.each{|record|
42 self.instance_variable_set(('@'+record[0]).to_sym, record[1].chomp)
43 }
44 else
45 puts 'No configuration file found!' if $DEBUG
46 end
17f5000 config location is now searched in program dir (not in current user dir)
Anton Pirogov authored
47 if File.exists?(progdir+'/autokapi.groups')
48 f = File.open(progdir+'/autokapi.groups','r') do |f|
8ed6fba Current state
Anton Pirogov authored
49 @groups = Marshal.load(f)
50 end
51 else
221103b refator, cache func added -> 2-3x speedup (subjectively)
Anton Pirogov authored
52 puts 'No groups file found!' if $DEBUG
8ed6fba Current state
Anton Pirogov authored
53 @groups = Hash.new
54 end
55 end
56 end
57
221103b refator, cache func added -> 2-3x speedup (subjectively)
Anton Pirogov authored
58 #object structure to hold information about groups
8ed6fba Current state
Anton Pirogov authored
59 class Group
60 attr_accessor :ids, :type, :name
61 def initialize
62 @ids = Array.new
63 end
64
65 def add(id)
66 @ids.push id
67 @ids.uniq!
68 end
69
70 def remove(id)
71 @ids.delete(id)
72 end
73 end
Something went wrong with that request. Please try again.