File: parser.rb

package info (click to toggle)
ruby-activeldap 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 1,588 kB
  • sloc: ruby: 18,143; sh: 12; makefile: 5
file content (161 lines) | stat: -rw-r--r-- 5,030 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
require 'active_ldap'
require 'gettext/parser/ruby'

module ActiveLdap
  module GetText
    class Parser
      include GetText

      def initialize(configuration=nil)
        configuration = ensure_configuration(configuration)
        configuration = default_configuration.merge(configuration)

        configuration = extract_options(configuration)
        ActiveLdap::Base.setup_connection(configuration)
      end

      def parse(file, targets=[])
        targets = RubyParser.parse(file, targets) if RubyParser.target?(file)
        extract(targets) do
          load_constants(file).each do |name|
            klass = name.constantize
            next unless klass.is_a?(Class)
            next unless klass < ActiveLdap::Base
            register(klass.name.singularize.underscore.gsub(/_/, " "), file)
            next unless @extract_schema
            klass.classes.each do |object_class|
              register_object_class(object_class, file)
            end
          end
        end
      end

      def target?(file)
        @classes_re.match(File.read(file))
      end

      def extract_all_in_schema(targets=[])
        extract(targets) do
          schema = ActiveLdap::Base.schema
          schema.object_classes.each do |object_class|
            register_object_class(object_class, "-")
          end
          schema.attributes.each do |attribute|
            register_attribute(attribute, "-")
          end
          schema.ldap_syntaxes.each do |syntax|
            register_syntax(syntax, "-")
          end
        end
      end

      private
      def extract_options(configuration)
        configuration = configuration.dup
        classes = configuration.delete(:classes) || ["ActiveLdap::Base"]
        @classes_re = /class.*#{Regexp.union(*classes)}/ #
        @extract_schema = configuration.delete(:extract_schema)
        configuration
      end

      def default_configuration
        {
          :host => "127.0.0.1",
          :allow_anonymous => true,
          :extract_schema => false,
        }
      end

      def ensure_configuration(configuration)
        configuration ||= ENV["RAILS_ENV"] || {}
        if configuration.is_a?(String)
          if File.exists?(configuration)
            require 'erb'
            require 'yaml'
            erb = ERB.new(File.read(configuration))
            erb.filename = configuration
            configuration = YAML.load(erb.result)
          else
            ENV["RAILS_ENV"] = configuration
            require 'config/environment'
            configuration = ActiveLdap::Base.configurations[configuration]
          end
        end
        if defined?(Rails)
          rails_configuration = ActiveLdap::Base.configurations[Rails.env]
          configuration = rails_configuration.merge(configuration)
        end
        configuration = configuration.symbolize_keys
      end

      def load_constants(file)
        old_constants = Object.constants
        begin
          eval(File.read(file), TOPLEVEL_BINDING, file)
        rescue
          format = _("Ignored '%{file}'. Solve dependencies first.")
          $stderr.puts(format % {:file => file})
          $stderr.puts($!)
        end
        Object.constants - old_constants
      end

      def extract(targets)
        @targets = {}
        targets.each do |id, *file_infos|
          @targets[id] = file_infos
        end
        yield
        @targets.collect do |id, file_infos|
          [id, *file_infos.uniq]
        end.sort_by do |id,|
          id
        end
      end

      def register(id, file)
        file_info = "#{file}:-"
        @targets[id] ||= []
        @targets[id] << file_info
      end

      def register_object_class(object_class, file)
        [object_class.name, *object_class.aliases].each do |name|
          register(ActiveLdap::Base.human_object_class_name_msgid(name), file)
        end
        if object_class.description
          msgid =
            ActiveLdap::Base.human_object_class_description_msgid(object_class)
          register(msgid, file)
        end
        (object_class.must(false) + object_class.may(false)).each do |attribute|
          register_attribute(attribute, file)
        end
        object_class.super_classes.each do |super_class|
          register_object_class(super_class, file)
        end
      end

      def register_attribute(attribute, file)
        [attribute.name, *attribute.aliases].each do |name|
          msgid = ActiveLdap::Base.human_attribute_name_msgid(name)
          register(msgid, file) if msgid
        end
        if attribute.description
          msgid = ActiveLdap::Base.human_attribute_description_msgid(attribute)
          register(msgid, file)
        end
      end

      def register_syntax(syntax, file)
        msgid = ActiveLdap::Base.human_syntax_name_msgid(syntax)
        register(msgid, file)

        if syntax.description
          msgid = ActiveLdap::Base.human_syntax_description_msgid(syntax)
          register(msgid, file)
        end
      end
    end
  end
end