]> icculus.org git repositories - duncan/yast2-ruby-bindings.git/blob - src/ruby/yast.rb
fix the method call, and learned to use the * operator in the way.
[duncan/yast2-ruby-bindings.git] / src / ruby / yast.rb
1 # -----------------------------------------------------------------------\
2 # |                                                                      |
3 # |                      __   __    ____ _____ ____                      |
4 # |                      \ \ / /_ _/ ___|_   _|___ \                     |
5 # |                       \ V / _` \___ \ | |   __) |                    |
6 # |                        | | (_| |___) || |  / __/                     |
7 # |                        |_|\__,_|____/ |_| |_____|                    |
8 # |                                                                      |
9 # |                                                                      |
10 # | ruby language support                              (C) Novell Inc.   |
11 # \----------------------------------------------------------------------/
12 #
13 # Author: Duncan Mac-Vicar <dmacvicar@suse.de>
14 #
15 # This program is free software; you can redistribute it and/or
16 # modify it under the terms of the GNU General Public License
17 # as published by the Free Software Foundation; either version
18 # 2 of the License, or (at your option) any later version.
19 #
20
21 ENV['LD_LIBRARY_PATH'] = "/usr/lib/YaST2/plugin"
22
23 # Load the native part (.so)
24 require 'yastx'
25
26 module YCP
27   def self.method_missing(id, *args)
28     puts "stop"
29   end
30
31   def self.add_ycp_module(mname)
32     puts "import #{mname}"
33     YCP::import(mname)
34     m = Module.new
35     YCP::each_symbol(mname) do |sname,stype|
36       if (stype == :function) and !sname.empty?
37         m.module_eval <<-"END"
38           def self.#{sname}(*args)
39             return YCP::forward_call("#{mname}", :#{sname}, *args)
40           end
41         END
42       end # if function
43     end
44     YCP.const_set(mname, m)
45   end
46 end
47
48 module Kernel
49   alias require_ require 
50   def require(name)
51     if name =~ /^ycp\/(.+)$/
52       ycpns = $1
53       YCP::add_ycp_module(ycpns.capitalize)
54       return true
55     end
56     return require_(name)
57   end
58 end
59
60
61 module YCP
62   module Ui
63     #my @e_logging = qw(y2debug y2milestone y2warning y2error y2security y2internal);
64     
65   # Define symbols for the UI
66   ui_terms = [ :BarGraph, :Bottom, :CheckBox, :ColoredLabel, :ComboBox, :Date,
67     :DownloadProgress, :DumbTab, :DummySpecialWidget, :Empty, :Frame, :HBox, :HBoxvHCenter,
68     :HMultiProgressMeter, :HSpacing, :HSquash, :HStretch, :HVCenter, :HVSquash,
69     :HVStretch, :HWeight, :Heading, :IconButton, :Image, :IntField, :Label, :Left, :LogView,
70     :MarginBox, :MenuButton, :MinHeight, :MinSize, :MinWidth, :MultiLineEdit,
71     :MultiSelectionBox, :PackageSelector, :PatternSelector, :PartitionSplitter,
72     :Password, :PkgSpecial, :ProgressBar, :PushButton, :RadioButton,
73     :RadioButtonGroup, :ReplacePoint, :RichText, :Right, :SelectionBox, :Slider, :Table,
74     :TextEntry, :Time, :Top, :Tree, :VBox, :VCenter, :VMultiProgressMeter, :VSpacing,
75     :VSquash, :VStretch, :VWeight, :Wizard,
76     :id, :opt ]
77        
78 #     buffer = String.new
79 #     buffer << "["
80 #     ui_terms.each do |t|
81 #       buffer << " :" << t.to_s.downcase << ","
82 #     end
83 #     buffer <<  " ]"
84 #     puts buffer
85     # If the method name contains underscores, convert to camel case
86 #     while method =~ /([^_]*)_(.)(.*)/ 
87 #            method = $1 + $2.upcase + $3
88 #        end
89          
90     # for each symbol define a util function that will create a term
91     ui_terms.each do | term_name |
92       define_method(term_name) do | *args |
93         t = YaST::Term.new(term_name.to_s)
94         args.each do |arg|
95           t.add(arg)
96         end
97         return t
98       end
99       alias_method term_name.to_s.downcase, term_name
100     end
101
102   end # end Ui module
103 end
104
105 module YaST
106
107   class TermBuilder
108     # blank slate
109     instance_methods.each { |m| undef_method m unless (m =~ /^__|instance_eval$/)}
110     
111     def initialize(&block)
112         @term = nil
113         @term = instance_eval(&block)
114     end
115     
116     def method_missing(name, *args, &block )
117   #    puts "hi #{name.to_s} | #{args}"
118       term = nil
119       elements = block ? nil : args
120       @__to_s = nil # invalidate to_s cache
121       term = YaST::Term.new(name.to_s)
122       if not elements.nil?
123         elements.each do | e |
124           term.add(e)
125         end
126         return term
127       else
128         r = instance_eval(&block)
129         puts term.class
130         term.add(r) if not r.nil?
131       end
132       return term
133     end
134     
135     def to_s
136       return @term.to_s
137     end
138     
139     def term
140       return @term
141     end
142     
143   end
144
145   def y2_logger_helper(*args)
146     level = args.shift
147     
148     caller[0] =~ /(.+):(\d+):in `([^']+)'/
149     y2_logger(level,"Ruby",$1,$2.to_i,"",args[0])
150   end
151   
152   def y2debug(*args)
153     y2_logger_helper(0, args)
154   end
155   
156   def y2milestone(*args)
157     y2_logger_helper(1, args)
158   end
159   
160   def y2warning(*args)
161     y2_logger_helper(2, args)
162   end
163   
164   def y2error(*args)
165     y2_logger_helper(3, args)
166   end
167   
168   def y2security(*args)
169     y2_logger_helper(4, args)
170   end
171   
172   def y2internal(*args)
173     y2_logger_helper(5, args)
174   end
175   
176 end # module YaST