Class: Aspera::Agent::Factory

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/aspera/agent/factory.rb

Overview

Factory for Agents: the entries of the extension point agent of bundles.

ALL [Hash=> Hash]: available agents, sorted by name: long capitalized name string, short single character symbol. Computed on access (instance.all): bundles of folders added after this file is loaded are included.

Constant Summary collapse

POINT =

Extension point of the agents of bundles

:agent

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(registry: BundleRegistry.instance) ⇒ Factory

Returns a new instance of Factory.

Parameters:

  • registry (BundleRegistry) (defaults to: BundleRegistry.instance) —

    Registry of the bundles that provide agents (loaded on use: the factory works without the CLI)



35
36
37
38
39
40
41
42
43
# File 'lib/aspera/agent/factory.rb', line 35

def initialize(registry: BundleRegistry.instance)
  @registry = registry
  # Entries of the extension point used to build `@all`
  @entries = nil
  # Available agents, see `all`
  @all = nil
  # Entry of each agent provided by a bundle, by name
  @provided = {}
end

Class Method Details

.const_missing(name) ⇒ Object

ALL: available agents, see #all



28
29
30
31
# File 'lib/aspera/agent/factory.rb', line 28

def const_missing(name)
  return instance.all if name.eql?(:ALL)
  super
end

.instance ⇒ Factory

Returns the singleton instance of Factory

Returns:

  • (Factory) —

    the singleton instance



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
# File 'lib/aspera/agent/factory.rb', line 20

class Factory
  include Singleton

  # Extension point of the agents of bundles
  POINT = :agent

  class << self
    # `ALL`: available agents, see `#all`
    def const_missing(name)
      return instance.all if name.eql?(:ALL)
      super
    end
  end

  # @param registry [BundleRegistry] Registry of the bundles that provide agents (loaded on use: the factory works without the CLI)
  def initialize(registry: BundleRegistry.instance)
    @registry = registry
    # Entries of the extension point used to build `@all`
    @entries = nil
    # Available agents, see `all`
    @all = nil
    # Entry of each agent provided by a bundle, by name
    @provided = {}
  end

  # Available agents: entries of the extension point `agent`.
  # An entry whose name is provided by an earlier entry, or whose letter is taken, is left out with a warning.
  # Recomputed when the entries change.
  # @return [Hash{Symbol => Hash}] By name, sorted: `long` capitalized name string, `short` single character symbol
  def all
    entries = @registry.extensions(POINT)
    unless entries.eql?(@entries)
      @entries = entries
      @provided = provided_agents(entries)
      @all = @provided.sort.to_h { |name, entry| [name, {long: name.to_s.capitalize, short: entry.fields['short'].to_sym}.freeze] }.freeze
    end
    @all
  end

  # Load the class of an agent: the file of the entry of its bundle, with the checks of the registry: security level, availability, gems
  # @param agent [Symbol, String] Name of an agent of `all`
  # @return [Class] The class of the agent
  # @raise [BundleRegistry::Unavailable] If the bundle of the agent is unavailable or a gem is missing
  def agent_class(agent)
    name = agent.to_sym
    reason = unavailable_reason(name)
    raise BundleRegistry::Unavailable, reason unless reason.nil?
    Aspera.assert(@provided.key?(name)) { "unknown agent: #{name}" }
    @registry.load_entry(@provided[name])
  end

  # @param agent [Symbol, String] Name of an agent
  # @return [Boolean] `true` if the agent is provided by an entry of a bundle
  def provided?(agent)
    all
    @provided.key?(agent.to_sym)
  end

  # @param agent [Symbol, String] Name of an agent
  # @return [String, nil] Why the agent is not in `all`: its bundle is unavailable (a bundle that declares it, or, if its manifest is invalid, a bundle of this name); `nil` if it is in `all` or unknown
  def unavailable_reason(agent)
    name = agent.to_sym
    all
    return if @provided.key?(name)
    @registry.names.each do |bundle_name|
      bundle = @registry[bundle_name]
      next unless bundle.bundle?
      reason = @registry.unavailable_reason(bundle_name)
      next if reason.nil?
      return "plugin #{bundle_name} is unavailable: #{reason}" if declares_agent?(bundle, name) || (bundle_name.eql?(name) && !bundle.error.nil?)
    end
    nil
  end

  # Found by the name of the class: no code is loaded (a bundle that cannot be loaded does not hide the others).
  # @param instance [Base] An agent
  # @return [Symbol, nil] Name of the agent of `all` this is an instance of, `nil` if none
  def agent_name(instance)
    class_name = instance.class.name
    all
    @provided.each { |name, entry| return name if @registry.constant_name(entry.bundle, entry.constant).eql?(class_name) }
    nil
  end

  # Create new agent
  # @param agent   [Symbol, String] Name of an agent of `all`
  # @param options [Hash]           Keyword arguments of the constructor of the agent
  # @return [Base] The agent
  def create(agent, options)
    Log.dump(:options, options)
    agent_class(agent).new(**options)
  end

  private

  # @param bundle [BundleRegistry::Plugin] A bundle with a valid manifest
  # @param name   [Symbol]                 Name of an agent
  # @return [Boolean] `true` if the manifest of the bundle declares an agent of this name
  def declares_agent?(bundle, name)
    bundle.extensions.any? { |entry| entry.point.eql?(POINT) && entry.fields['name'].to_s.eql?(name.to_s) }
  end

  # @param entries [Array<BundleRegistry::Extension>] Entries of the extension point, in the order of the registry
  # @return [Hash{Symbol => BundleRegistry::Extension}] Entries kept, by name of agent
  def provided_agents(entries)
    provided = {}
    entries.each do |entry|
      name, short = entry.fields.values_at('name', 'short')
      if !name.is_a?(String) || !short.is_a?(String) || !short.length.eql?(1)
        Log.log.warn { "Agent of bundle #{entry.bundle} is left out: invalid name #{name.inspect} or letter #{short.inspect}" }
        next
      end
      name = name.to_sym
      short = short.to_sym
      if provided.key?(name)
        Log.log.warn { "Agent #{name} of bundle #{entry.bundle} is left out: provided by bundle #{provided[name].bundle}" }
        next
      end
      owner = provided.find { |_, other| other.fields['short'].eql?(short.to_s) }&.first
      unless owner.nil?
        Log.log.warn { "Agent #{name} of bundle #{entry.bundle} is left out: letter #{short} is taken by agent #{owner}" }
        next
      end
      provided[name] = entry
    end
    provided
  end
end

Instance Method Details

#agent_class(agent) ⇒ Class

Load the class of an agent: the file of the entry of its bundle, with the checks of the registry: security level, availability, gems

Parameters:

  • agent (Symbol, String) —

    Name of an agent of all

Returns:

  • (Class) —

    The class of the agent

Raises:



63
64
65
66
67
68
69
# File 'lib/aspera/agent/factory.rb', line 63

def agent_class(agent)
  name = agent.to_sym
  reason = unavailable_reason(name)
  raise BundleRegistry::Unavailable, reason unless reason.nil?
  Aspera.assert(@provided.key?(name)) { "unknown agent: #{name}" }
  @registry.load_entry(@provided[name])
end

#agent_name(instance) ⇒ Symbol?

Found by the name of the class: no code is loaded (a bundle that cannot be loaded does not hide the others).

Parameters:

  • instance (Base) —

    An agent

Returns:

  • (Symbol, nil) —

    Name of the agent of all this is an instance of, nil if none



97
98
99
100
101
102
# File 'lib/aspera/agent/factory.rb', line 97

def agent_name(instance)
  class_name = instance.class.name
  all
  @provided.each { |name, entry| return name if @registry.constant_name(entry.bundle, entry.constant).eql?(class_name) }
  nil
end

#all ⇒ Hash{Symbol => Hash}

Available agents: entries of the extension point agent. An entry whose name is provided by an earlier entry, or whose letter is taken, is left out with a warning. Recomputed when the entries change.

Returns:

  • (Hash{Symbol => Hash}) —

    By name, sorted: long capitalized name string, short single character symbol



49
50
51
52
53
54
55
56
57
# File 'lib/aspera/agent/factory.rb', line 49

def all
  entries = @registry.extensions(POINT)
  unless entries.eql?(@entries)
    @entries = entries
    @provided = provided_agents(entries)
    @all = @provided.sort.to_h { |name, entry| [name, {long: name.to_s.capitalize, short: entry.fields['short'].to_sym}.freeze] }.freeze
  end
  @all
end

#create(agent, options) ⇒ Base

Create new agent

Parameters:

  • agent (Symbol, String) —

    Name of an agent of all

  • options (Hash) —

    Keyword arguments of the constructor of the agent

Returns:

  • (Base) —

    The agent



108
109
110
111
# File 'lib/aspera/agent/factory.rb', line 108

def create(agent, options)
  Log.dump(:options, options)
  agent_class(agent).new(**options)
end

#provided?(agent) ⇒ Boolean

Returns true if the agent is provided by an entry of a bundle.

Parameters:

  • agent (Symbol, String) —

    Name of an agent

Returns:

  • (Boolean) —

    true if the agent is provided by an entry of a bundle



73
74
75
76
# File 'lib/aspera/agent/factory.rb', line 73

def provided?(agent)
  all
  @provided.key?(agent.to_sym)
end

#unavailable_reason(agent) ⇒ String?

Returns Why the agent is not in all: its bundle is unavailable (a bundle that declares it, or, if its manifest is invalid, a bundle of this name); nil if it is in all or unknown.

Parameters:

  • agent (Symbol, String) —

    Name of an agent

Returns:

  • (String, nil) —

    Why the agent is not in all: its bundle is unavailable (a bundle that declares it, or, if its manifest is invalid, a bundle of this name); nil if it is in all or unknown



80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/aspera/agent/factory.rb', line 80

def unavailable_reason(agent)
  name = agent.to_sym
  all
  return if @provided.key?(name)
  @registry.names.each do |bundle_name|
    bundle = @registry[bundle_name]
    next unless bundle.bundle?
    reason = @registry.unavailable_reason(bundle_name)
    next if reason.nil?
    return "plugin #{bundle_name} is unavailable: #{reason}" if declares_agent?(bundle, name) || (bundle_name.eql?(name) && !bundle.error.nil?)
  end
  nil
end