root/lib/ldap_shadows/display_utils.rb @ 428fdd46
a9b44a39 | Marc Dequènes (Duck) | #--
|
|
# LdapShadows, a Medium-level LDAP Access Library and Tool.
|
|||
# Copyright (c) 2009 Marc Dequènes (Duck) <Duck@DuckCorp.org>
|
|||
#
|
|||
# This program is free software: you can redistribute it and/or modify
|
|||
# it under the terms of the GNU General Public License as published by
|
|||
# the Free Software Foundation, either version 3 of the License, or
|
|||
# (at your option) any later version.
|
|||
#
|
|||
# This program is distributed in the hope that it will be useful,
|
|||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|||
# GNU General Public License for more details.
|
|||
#
|
|||
# You should have received a copy of the GNU General Public License
|
|||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|||
#++
|
|||
require 'active_support'
|
|||
bbb82941 | Marc Dequènes (Duck) | ||
module LdapShadows
|
|||
a9b44a39 | Marc Dequènes (Duck) | module Translator
|
|
I18n.default_locale = :en
|
|||
def self.translate_object_name(obj_hdl)
|
|||
I18n.t(obj_hdl, :scope => 'objects', :default => "Object '#{obj_hdl}'")
|
|||
end
|
|||
def self.translate_field_name(field_name)
|
|||
if field_name.index(":")
|
|||
type, key = field_name.split(":")
|
|||
case type
|
|||
when 'rel'
|
|||
I18n.t(key, :scope => 'relations', :default => field_name)
|
|||
else
|
|||
raise "Cannot translate unknown data key type"
|
|||
end
|
|||
else
|
|||
att = ActiveLdap::Base.schema.attribute(field_name)
|
|||
I18n.t(att.human_attribute_name, :scope => 'attribute_types', :default => att.human_attribute_description)
|
|||
end
|
|||
end
|
|||
def self.translate_aspect_name(aspect_name)
|
|||
I18n.t(aspect_name, :scope => 'aspects', :default => "Aspect '#{aspect_name}'")
|
|||
end
|
|||
end
|
|||
bbb82941 | Marc Dequènes (Duck) | module Display
|
|
def self.display_fields(attr_data, options = {})
|
|||
attr_data.each_pair do |key, val|
|
|||
af19cdc5 | Marc Dequènes (Duck) | next if val[:expert] and not options[:expert]
|
|
b52f0f7d | Marc Dequènes (Duck) | next if val[:admin] and not options[:admin]
|
|
f7217dcd | Marc Dequènes (Duck) | ||
a9b44a39 | Marc Dequènes (Duck) | field_name = Translator.translate_field_name(key)
|
|
f7217dcd | Marc Dequènes (Duck) | ||
str = field_name
|
|||
str += " [#{key}]" if options[:handles]
|
|||
if val[:binary] and options[:skip_binary]
|
|||
str += " -> #{val[:value].size} file(s) available"
|
|||
else
|
|||
str += ": " + (val[:multiple] ? val[:value].sort.collect{|v| v.to_s }.join(", ") : val[:value].to_s)
|
|||
end
|
|||
puts str
|
|||
bbb82941 | Marc Dequènes (Duck) | end
|
|
end
|
|||
def self.display_item(item, options = {})
|
|||
c331fe49 | Marc Dequènes (Duck) | obj_human_name = Translator.translate_object_name(item.class.handle)
|
|
bbb82941 | Marc Dequènes (Duck) | name = item.human_name
|
|
38e5c5db | Marc Dequènes (Duck) | name += " [#{item.full_handle}]" if options[:handles]
|
|
bbb82941 | Marc Dequènes (Duck) | puts "=== #{obj_human_name}: #{name} ==="
|
|
if options[:debug]
|
|||
puts item.to_s
|
|||
puts "--- Detected Info ---"
|
|||
puts "aspects: " + item.aspects.sort.join(", ")
|
|||
puts "--- Relations ---"
|
|||
item.relations.each do |rel|
|
|||
3d58e226 | Marc Dequènes (Duck) | rel_data = item.send(rel)
|
|
if rel_data.is_a? Enumerable
|
|||
next if rel_data.empty?
|
|||
rel_value = rel_data.collect{|g| g.name }.join(", ")
|
|||
else
|
|||
a987d14b | Marc Dequènes (Duck) | # the exists? method also ensure the object is loaded
|
|
next unless rel_data.exists?
|
|||
3d58e226 | Marc Dequènes (Duck) | rel_value = rel_data.name
|
|
end
|
|||
puts "#{rel}: " + rel_value
|
|||
bbb82941 | Marc Dequènes (Duck) | end
|
|
c30d4613 | Marc Dequènes (Duck) | ||
66ccfd46 | Marc Dequènes (Duck) | if options[:show_family_members]
|
|
puts "--- Family ---"
|
|||
puts "parent: " + item.family_parent_dn.to_s
|
|||
puts "siblings: " + item.family_siblings_dn.join(", ")
|
|||
puts "children: " + item.family_children_dn.join(", ")
|
|||
end
|
|||
bbb82941 | Marc Dequènes (Duck) | else
|
|
f7217dcd | Marc Dequènes (Duck) | obj_info, obj_aspects = item.organized_data
|
|
bbb82941 | Marc Dequènes (Duck) | ||
display_fields(obj_info, options)
|
|||
obj_aspects.each_pair do |aspect_name, aspect_data|
|
|||
7b2c50a8 | Marc Dequènes (Duck) | name = Translator.translate_aspect_name(aspect_name)
|
|
name += " [#{aspect_name}]" if options[:handles]
|
|||
puts "--- #{name} ---"
|
|||
bbb82941 | Marc Dequènes (Duck) | display_fields(aspect_data, options)
|
|
end
|
|||
c30d4613 | Marc Dequènes (Duck) | ||
66ccfd46 | Marc Dequènes (Duck) | if options[:show_family_members]
|
|
puts "--- Family ---"
|
|||
cf271e9b | Marc Dequènes (Duck) | puts "parent: " + item.class.raw_item_info(item.class.shadow, item.family_parent)[:name]
|
|
puts "siblings: " + item.family_siblings.collect{|raw_item| item.class.raw_item_info(item.class.shadow, raw_item)[:name] }.join(", ")
|
|||
puts "children: " + item.family_children.collect{|raw_item| item.class.raw_item_info(item.class.shadow, raw_item)[:name] }.join(", ")
|
|||
66ccfd46 | Marc Dequènes (Duck) | end
|
|
bbb82941 | Marc Dequènes (Duck) | end
|
|
end
|
|||
af19cdc5 | Marc Dequènes (Duck) | def self.display_item_list(title, item_list, options)
|
|
a24bc5b1 | Marc Dequènes (Duck) | puts "=== #{title} (#{item_list.size}) ==="
|
|
item_list.each do |item|
|
|||
str = item.human_name
|
|||
38e5c5db | Marc Dequènes (Duck) | str += " [#{item.full_handle}]" if options[:handles]
|
|
378df927 | Marc Dequènes (Duck) | str += ": #{item.human_description}" unless item.human_description.empty?
|
|
a9b44a39 | Marc Dequènes (Duck) | puts str
|
|
end
|
|||
end
|
|||
bbb82941 | Marc Dequènes (Duck) | def self.display_hash_tree(tree, level)
|
|
tree.keys.sort.each do |key|
|
|||
str = ""
|
|||
str += " " + "| " * (level -1) + "+-- " if level > 0
|
|||
str += "<#{key}>"
|
|||
puts str
|
|||
display_hash_tree(tree[key], level + 1) if tree[key]
|
|||
end
|
|||
end
|
|||
end
|
|||
end
|