]> icculus.org git repositories - duncan/yast2-ruby-bindings.git/blob - src/ruby/yast.rb
Allows for a different mporting
[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             args.insert(0, "#{mname}")
40             args.insert(0, :#{sname})
41             puts "to forward call #{sname.to_s} in #{mname}"
42             return YCP::forward_call(args)
43           end
44         END
45       end
46     end
47     YCP.const_set(mname, m)
48   end
49 end
50
51 module Kernel
52   alias require_ require 
53   def require(name)
54     if name =~ /^ycp\/(.+)$/
55       ycpns = $1
56       YCP::add_ycp_module(ycpns.capitalize)
57       return true
58     end
59     return require_(name)
60   end
61 end
62
63
64 module YaST
65   module Ui
66     #my @e_logging = qw(y2debug y2milestone y2warning y2error y2security y2internal);
67     
68   # Define symbols for the UI
69   ui_terms = [ :BarGraph, :Bottom, :CheckBox, :ColoredLabel, :ComboBox, :Date,
70     :DownloadProgress, :DumbTab, :DummySpecialWidget, :Empty, :Frame, :HBox, :HBoxvHCenter,
71     :HMultiProgressMeter, :HSpacing, :HSquash, :HStretch, :HVCenter, :HVSquash,
72     :HVStretch, :HWeight, :Heading, :IconButton, :Image, :IntField, :Label, :Left, :LogView,
73     :MarginBox, :MenuButton, :MinHeight, :MinSize, :MinWidth, :MultiLineEdit,
74     :MultiSelectionBox, :PackageSelector, :PatternSelector, :PartitionSplitter,
75     :Password, :PkgSpecial, :ProgressBar, :PushButton, :RadioButton,
76     :RadioButtonGroup, :ReplacePoint, :RichText, :Right, :SelectionBox, :Slider, :Table,
77     :TextEntry, :Time, :Top, :Tree, :VBox, :VCenter, :VMultiProgressMeter, :VSpacing,
78     :VSquash, :VStretch, :VWeight, :Wizard,
79     :id, :opt ]
80        
81 #     buffer = String.new
82 #     buffer << "["
83 #     ui_terms.each do |t|
84 #       buffer << " :" << t.to_s.downcase << ","
85 #     end
86 #     buffer <<  " ]"
87 #     puts buffer
88     # If the method name contains underscores, convert to camel case
89 #     while method =~ /([^_]*)_(.)(.*)/ 
90 #            method = $1 + $2.upcase + $3
91 #        end
92          
93     # for each symbol define a util function that will create a term
94     ui_terms.each do | term_name |
95       define_method(term_name) do | *args |
96         t = YaST::Term.new(term_name.to_s)
97         args.each do |arg|
98           t.add(arg)
99         end
100         return t
101       end
102       alias_method term_name.to_s.downcase, term_name
103     end
104
105   end # end Ui module
106 end
107
108 module YaST
109
110   class TermBuilder
111     # blank slate
112     instance_methods.each { |m| undef_method m unless (m =~ /^__|instance_eval$/)}
113     
114     def initialize(&block)
115         @term = nil
116         @term = instance_eval(&block)
117     end
118     
119     def method_missing(name, *args, &block )
120   #    puts "hi #{name.to_s} | #{args}"
121       term = nil
122       elements = block ? nil : args
123       @__to_s = nil # invalidate to_s cache
124       term = YaST::Term.new(name.to_s)
125       if not elements.nil?
126         elements.each do | e |
127           term.add(e)
128         end
129         return term
130       else
131         r = instance_eval(&block)
132         puts term.class
133         term.add(r) if not r.nil?
134       end
135       return term
136     end
137     
138     def to_s
139       return @term.to_s
140     end
141     
142     def term
143       return @term
144     end
145     
146   end
147
148   def y2_logger_helper(*args)
149     level = args.shift
150     
151     caller[0] =~ /(.+):(\d+):in `([^']+)'/
152     y2_logger(level,"Ruby",$1,$2.to_i,"",args[0])
153   end
154   
155   def y2debug(*args)
156     y2_logger_helper(0, args)
157   end
158   
159   def y2milestone(*args)
160     y2_logger_helper(1, args)
161   end
162   
163   def y2warning(*args)
164     y2_logger_helper(2, args)
165   end
166   
167   def y2error(*args)
168     y2_logger_helper(3, args)
169   end
170   
171   def y2security(*args)
172     y2_logger_helper(4, args)
173   end
174   
175   def y2internal(*args)
176     y2_logger_helper(5, args)
177   end
178   
179 end # module YaST