Project

General

Profile

« Previous | Next » 

Revision 38e5c5db

Added by Marc Dequènes over 14 years ago

  • ID 38e5c5db43a5c6082ff3f271426f5889810e5b04

[evol] introduce item 'full-handle' and reorganize command arguments, and renamed commands

View differences:

bin/shadowwalker
protected
def params_shift_location(args)
raise SyntaxError, _("no location name given") if args.empty?
location = args.shift.downcase
raise SyntaxError, _("bad location") unless location =~ /^([a-zA-Z]+)\/(.+)$/
loc_obj_hdl = $1
loc_item_hdl = $2
split_args = [loc_obj_hdl, loc_item_hdl]
loc_obj_klass = params_shift_object(split_args)
loc_item = params_shift_item(loc_obj_klass, split_args)
[loc_obj_klass, loc_item]
def params_shift_item_handle_full_handle(args, fetch_item = true, name = "item full-handle")
raise SyntaxError, _("no %s given") % name if args.empty?
full_handle = args.shift.downcase
raise SyntaxError, _("bad %s") % name unless full_handle =~ /^([a-zA-Z]+)\/(.+)$/
obj_hdl = $1
item_hdl = $2
split_args = [obj_hdl, item_hdl]
obj_klass = params_shift_object_handle(split_args)
if fetch_item
item = params_shift_item_handle(obj_klass, split_args)
[obj_klass, item]
else
[obj_klass, item_hdl]
end
end
def params_shift_object(args)
def params_shift_object_handle(args)
raise SyntaxError, _("no object name given") if args.empty?
obj_hdl = args.shift.downcase.singularize
......
obj_klass
end
def params_shift_item(obj_klass, args)
def params_shift_item_handle(obj_klass, args)
raise SyntaxError, _("no item name given") if args.empty?
attr_list = ["*"]
......
puts " - #{obj_name}"
end
else
obj_klass = params_shift_object([obj_hdl])
obj_klass = params_shift_object_handle([obj_hdl])
if args.empty?
obj_human_name = Translator.translate_object_name(obj_hdl)
......
self.short_desc = "show item information"
self.usages_params = [
"<object> <item>",
"<object> <item> <field>#? [<filename>]",
"<object> <item> <field>[#<index>] [<filename>]"
"<object>/<item>",
"<object>/<item> <field>#? [<filename>]",
"<object>/<item> <field>[#<index>] [<filename>]"
]
self.options = CmdParse::OptionParserWrapper.new do |opt|
opt.on( '-f', '--family', 'Display family members.' ) { $program_options[:show_family_members] = true }
......
def execute(args)
super
obj_klass = params_shift_object(args)
item = params_shift_item(obj_klass, args)
obj_klass, item = params_shift_item_handle_full_handle(args)
if args.empty?
$program_options[:skip_binary] = true
......
raise PreProcessingError, _("No such field '%s' in object '%s'") % [field_name, obj_klass.handle]
end
unless item.attribute_present?(field_name)
raise PreProcessingError, _("Field '%s' in item '%s/%s' is not present") %
[field_name, obj_klass.handle, item.name]
raise PreProcessingError, _("Field '%s' in item '%s' is not present") %
[field_name, item.full_handle]
end
field_data = item.send(field_name, true)
......
self.short_desc = "show skeleton items tree"
self.usages_params = [
"[<location>]"
"[<location-item-full-handle>]"
]
self.options = CmdParse::OptionParserWrapper.new do |opt|
opt.on( '-a', '--all', 'Display all objects in the tree.' ) { $program_options[:tree_all_objects] = true }
......
search_base = nil
unless args.empty?
loc_obj_klass, loc_item = params_shift_location(args)
loc_obj_klass, loc_item = params_shift_item_handle_full_handle(args, true, "location full-handle")
search_base = loc_item.dn
end
......
end
cmdparser.add_command(TreeCommand.new)
class ModCommand < Command
class ModifyCommand < Command
def initialize
super('mod', false)
super('modify', false)
self.short_desc = "Modify attributes of an item"
self.usages_params = [
"<object> <item> (<field>|<relation>|:aspects)(=|+=|-=)<value> [(<field>|<relation>|:aspects)(=|+=|-=)<value>] ..."
"<item-full-handle> (<field>|<relation>|:aspects)(=|+=|-=)<value> [(<field>|<relation>|:aspects)(=|+=|-=)<value>] ..."
]
end
def execute(args)
super
obj_klass = params_shift_object(args)
item = params_shift_item(obj_klass, args)
obj_klass, item = params_shift_item_handle_full_handle(args)
modification_done = false
args.each do |mod_info|
......
end
end
end
cmdparser.add_command(ModCommand.new)
cmdparser.add_command(ModifyCommand.new)
class DelCommand < Command
class DeleteCommand < Command
def initialize
super('del', false)
super('delete', false)
self.short_desc = "Delete an item"
self.usages_params = [
"<object> <item>"
"<item-full-handle>"
]
self.options = CmdParse::OptionParserWrapper.new do |opt|
opt.on( '-r', '--recursive', 'Delete object and all children recursively.' ) { $program_options[:recursive_delete] = true }
......
def execute(args)
super
obj_klass = params_shift_object(args)
item = params_shift_item(obj_klass, args)
obj_klass, item = params_shift_item_handle_full_handle(args)
if $program_options[:recursive_delete]
item.delete_recursive
......
puts "Deletion done."
end
end
cmdparser.add_command(DelCommand.new)
cmdparser.add_command(DeleteCommand.new)
class CreateCommand < Command
def initialize
......
self.short_desc = "Create an item"
self.usages_params = [
"<object> <new_item> <location> [<field>=<value>] ..."
"<new-item-full-handle> <parent-item-full-handle> [<field>=<value>] ..."
]
end
def execute(args)
super
obj_klass = params_shift_object(args)
raise SyntaxError, _("no item name given") if args.empty?
item_hdl = args.shift.downcase
loc_obj_klass, loc_item = params_shift_location(args)
obj_klass, item_hdl = params_shift_item_handle_full_handle(args)
loc_obj_klass, loc_item = params_shift_item_handle_full_handle(args, true, "parent full-handle")
item = obj_klass.new(item_hdl)
item.base = loc_item.dn_obj - obj_klass.base_obj
lib/ldap_shadows/display_utils.rb
def self.display_item(item, options = {})
obj_human_name = Translator.translate_object_name(item.class.handle)
name = item.human_name
name += " [#{item.name}]" if options[:handles]
name += " [#{item.full_handle}]" if options[:handles]
puts "=== #{obj_human_name}: #{name} ==="
if options[:debug]
......
puts "=== #{title} (#{item_list.size}) ==="
item_list.each do |item|
str = item.human_name
str += " [#{item.name}]" if options[:handles]
str += " [#{item.full_handle}]" if options[:handles]
str += ": #{item.human_description}" unless item.human_description.empty?
puts str
end
lib/ldap_shadows/elements/object.rb
# default
ldap_mapping :prefix => '', :classes => ['top'], :scope => :sub
def handle
name = self[dn_attribute] || self.attributes[dn_attribute] || self.dn
name = name[0] if name.is_a? Array
name.strip
end
def full_handle
"#{self.class.handle}/#{self.handle}"
end
def self.cast
super
......
instance_variable_set(:@relations_info, object_relations_info)
end
def name
name = self[dn_attribute] || self.attributes[dn_attribute] || self.dn
name = name[0] if name.is_a? Array
name.strip
end
def has_field?(field)
return false if field.downcase == "objectclass"
has_attribute?(field)
......
if obj_hdl
obj_klass = shadow.get_object(obj_hdl)
item = obj_klass.new(raw_item.dn)
{:name => "#{obj_hdl}/#{item.name}", :item => item, :object => obj_klass}
{:name => item.full_handle, :item => item, :object => obj_klass}
else
{:name => "unknown/#{raw_item.dn}"}
end
......
if data.empty?
value = nil
else
value = data.collect{|g| g.name }
value = data.collect{|g| g.handle }
multiple = true
end
else
# the exists? method also ensure the object is loaded
if data.exists?
value = data.name
value = data.handle
else
value = nil
end

Also available in: Unified diff