eco-helpers 0.8.3 → 0.8.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (53) hide show
  1. checksums.yaml +4 -4
  2. data/eco-helpers.gemspec +0 -2
  3. data/lib/eco-helpers.rb +0 -1
  4. data/lib/eco/api/common/people/person_entry.rb +67 -30
  5. data/lib/eco/api/common/version_patches.rb +2 -1
  6. data/lib/eco/api/common/version_patches/base_model.rb +27 -0
  7. data/lib/eco/api/organization/people.rb +6 -0
  8. data/lib/eco/api/organization/tag_tree.rb +6 -0
  9. data/lib/eco/api/policies/policy.rb +2 -2
  10. data/lib/eco/api/session.rb +23 -30
  11. data/lib/eco/api/session/batch.rb +3 -2
  12. data/lib/eco/api/session/batch_job.rb +18 -10
  13. data/lib/eco/api/session/batch_status.rb +27 -11
  14. data/lib/eco/api/session/config.rb +20 -7
  15. data/lib/eco/api/usecases/default_cases/change_email_case.rb +2 -6
  16. data/lib/eco/api/usecases/default_cases/refresh_presets_case.rb +3 -2
  17. data/lib/eco/api/usecases/use_case.rb +6 -5
  18. data/lib/eco/api/usecases/use_case_chain.rb +3 -3
  19. data/lib/eco/api/usecases/use_case_io.rb +3 -3
  20. data/lib/eco/assets.rb +3 -1
  21. data/lib/eco/{common → assets}/language.rb +2 -2
  22. data/lib/eco/cli.rb +3 -4
  23. data/lib/eco/cli/config.rb +10 -0
  24. data/lib/eco/cli/config/options.rb +11 -0
  25. data/lib/eco/cli/scripting.rb +23 -0
  26. data/lib/eco/cli/scripting/args_helpers.rb +55 -0
  27. data/lib/eco/cli/scripting/argument.rb +31 -0
  28. data/lib/eco/cli/scripting/arguments.rb +70 -0
  29. data/lib/eco/common.rb +1 -3
  30. data/lib/eco/data/crypto/encryption.rb +2 -2
  31. data/lib/eco/language/models/collection.rb +1 -1
  32. data/lib/eco/version.rb +1 -1
  33. metadata +9 -62
  34. data/lib/eco/cli/api.rb +0 -14
  35. data/lib/eco/cli/root.rb +0 -9
  36. data/lib/eco/cli/session.rb +0 -9
  37. data/lib/eco/cli/session/batch.rb +0 -9
  38. data/lib/eco/common/base_cli.rb +0 -23
  39. data/lib/eco/common/base_cli_backup.rb +0 -120
  40. data/lib/eco/common/meta_thor.rb +0 -111
  41. data/lib/eco/common/meta_thor/command_group.rb +0 -36
  42. data/lib/eco/common/meta_thor/command_unit.rb +0 -48
  43. data/lib/eco/common/meta_thor/input_backup.rb +0 -111
  44. data/lib/eco/common/meta_thor/input_multi_backup.rb +0 -139
  45. data/lib/eco/common/meta_thor/pipe.rb +0 -85
  46. data/lib/eco/common/meta_thor/thor.rb +0 -1
  47. data/lib/eco/common/meta_thor/thor/command.rb +0 -36
  48. data/lib/eco/common/meta_thor/value.rb +0 -54
  49. data/lib/eco/scripting.rb +0 -32
  50. data/lib/eco/scripting/README.md +0 -11
  51. data/lib/eco/scripting/args_helpers.rb +0 -53
  52. data/lib/eco/scripting/argument.rb +0 -29
  53. data/lib/eco/scripting/arguments.rb +0 -68
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9d1df7add146810d2ed8fa9bb2a2854d54880fa531d095cdd0e39f4ca44b181f
4
- data.tar.gz: e3777d5c30e4eaff58043940428fff9cce05da58039bb17a620bbcffcb51c7af
3
+ metadata.gz: 05fa297f660326cad5045f7760c4352b961492c7be71a18eda2093cd7032b011
4
+ data.tar.gz: 13690bdea42b4960af20e835c5171df3424cfc7213f25123f154c0a42205bc58
5
5
  SHA512:
6
- metadata.gz: 6d047a6b824c60f778d0c0151a0177e9edcc21a80c9fbb1473e347d909b4b02ec4cb6fe0b1ce4922d0a2dc43b366d0215f15b68bb6caf9fe117bab41d3db0444
7
- data.tar.gz: 6517140b0277436045dbb162bf75c49f6fe1a76dc02b82b826b4bdc997941ba88316f3cd67ed29dc91b5e4155e1b48bdf641acbf4fc808c023c431f90905d078
6
+ metadata.gz: f795b7015969ddeaa59c76a1625d6d336ace2d35145651c15fdaecbbc0c32345c7023666febd94b1abb8e1de7ed1d3410aaf61bb19d5799edec6078bb0a02390
7
+ data.tar.gz: 33e176df8b2209373683ef79eba539f8e05e61fb1bbee63ebf4c8823c0f2ffa215a2c1cc62aa1e55d6c6e75c799f6f57599d8f67f7fd924bc371657018617777
data/eco-helpers.gemspec CHANGED
@@ -29,8 +29,6 @@ Gem::Specification.new do |s|
29
29
  s.add_development_dependency "redcarpet", "~> 3.4", ">= 3.4.0"
30
30
 
31
31
  s.add_dependency 'ecoportal-api', '~> 0.4', '>= 0.4.3'
32
- s.add_dependency 'thor', '~> 0', '>= 0.20'
33
- s.add_dependency 'nokogiri', '~> 1.6', '>= 1.6.8'
34
32
  s.add_dependency 'aws-sdk-s3', '~> 1.30', '>= 1.30.1'
35
33
  s.add_dependency 'aws-sdk-ses', '~> 1.14', '>= 1.14.0'
36
34
  s.add_dependency 'dotenv', '~> 2.6', '>= 2.6.0'
data/lib/eco-helpers.rb CHANGED
@@ -8,7 +8,6 @@ end
8
8
 
9
9
  require_relative 'eco/language'
10
10
  require_relative 'eco/common'
11
- require_relative 'eco/scripting'
12
11
  require_relative 'eco/data'
13
12
  require_relative 'eco/api'
14
13
  require_relative 'eco/tester'
@@ -3,6 +3,7 @@ module Eco
3
3
  module Common
4
4
  module People
5
5
  class PersonEntry
6
+
6
7
  # This class is meant to provide a common interface to access entries of source data that come in different formats.
7
8
  # @note
8
9
  # - if `data` is a `Person` object, its behaviour is `serialise`.
@@ -17,21 +18,21 @@ module Eco
17
18
  raise "Constructor needs a PersonParser. Given: #{parser}" if !person_parser.is_a?(Eco::API::Common::People::PersonParser)
18
19
  raise "Expecting Mapper object. Given: #{attr_map}" if attr_map && !attr_map.is_a?(Eco::Data::Mapper)
19
20
 
20
- @source = data
21
+ @source = data
21
22
  @person_parser = person_parser
22
- @deps = dependencies
23
- @logger = logger
24
- @attr_map = attr_map
23
+ @deps = dependencies
24
+ @logger = logger
25
+ @attr_map = attr_map
25
26
  @emap = PersonEntryAttributeMapper.new(@source, person_parser: @person_parser, attr_map: @attr_map, logger: @logger)
26
27
 
27
28
  if parsing?
28
29
  @external_entry = data
29
- @serialized_entry = mapped_entry(@external_entry)
30
- @internal_entry = internal_entry(@serialized_entry)
30
+ @serialized_entry = _mapped_entry(@external_entry)
31
+ @internal_entry = _internal_entry(@serialized_entry)
31
32
  else # SERIALIZING
32
33
  @person = data
33
- @internal_entry = internal_entry(@person)
34
- @serialized_entry = mapped_entry(@internal_entry)
34
+ @internal_entry = _internal_entry(@person)
35
+ @serialized_entry = _mapped_entry(@internal_entry)
35
36
  #@external_entry = external_entry
36
37
  end
37
38
  end
@@ -99,7 +100,7 @@ module Eco
99
100
  def set_core(person, exclude: nil)
100
101
  scoped_attrs = @emap.core_attrs - into_a(exclude)
101
102
  @internal_entry.slice(*scoped_attrs).each do |attr, value|
102
- set_to_core(person, attr, value)
103
+ _set_to_core(person, attr, value)
103
104
  end
104
105
  end
105
106
 
@@ -112,7 +113,7 @@ module Eco
112
113
  person.add_details(@person_parser.schema) if !person.details || !person.details.schema_id
113
114
  scoped_attrs = @emap.details_attrs - into_a(exclude)
114
115
  @internal_entry.slice(*scoped_attrs).each do |attr, value|
115
- set_to_details(person, attr, value)
116
+ _set_to_details(person, attr, value)
116
117
  end
117
118
  end
118
119
 
@@ -126,7 +127,7 @@ module Eco
126
127
  person.account.permissions_preset = nil unless person.account.permissions_preset = "custom"
127
128
  scoped_attrs = @emap.account_attrs - into_a(exclude)
128
129
  @internal_entry.slice(*scoped_attrs).each do |attr, value|
129
- set_to_account(person, attr, value)
130
+ _set_to_account(person, attr, value)
130
131
  end
131
132
  end
132
133
 
@@ -148,14 +149,50 @@ module Eco
148
149
  end
149
150
  end
150
151
 
152
+ def internal_entry
153
+ @internal_entry
154
+ end
155
+
156
+ def doc
157
+ return @person.doc if instance_variable_defined?(:@person) && @person
158
+
159
+ core_attrs = @emap.core_attrs
160
+ details_attrs = @emap.details_attrs
161
+ account_attrs = @emap.account_attrs
162
+
163
+ internal_entry.slice(*core_attrs).tap do |core_hash|
164
+ unless details_attrs.empty?
165
+ schema_id = @person_parser.schema.id
166
+ details_fields = @person_parser.schema.doc["fields"].each_with_object([]) do |fld, flds|
167
+ if details_attrs.include?(fld.alt_id)
168
+ flds << fld.merge("value" => internal_entry[fld.alt_id]).slice("id", "alt_id", "type", "name", "shared", "multiple", "value")
169
+ end
170
+ end
171
+ core_hash.merge!({
172
+ "details" => {
173
+ "schema_id" => schema_id,
174
+ "fields" => details_fields
175
+ }
176
+ })
177
+ end
178
+
179
+ unless account_attrs.empty?
180
+ account_hash = internal_entry.slice(*account_attrs)
181
+ core_hash.merge!({
182
+ "account" => account_hash
183
+ })
184
+ end
185
+ end
186
+ end
187
+
151
188
  private
152
189
 
153
- def set_to_core(person, attr, value)
190
+ def _set_to_core(person, attr, value)
154
191
  value = value&.downcase if attr == "email"
155
192
  person.send("#{attr}=", value&.strip)
156
193
  end
157
194
 
158
- def set_to_account(person, attr, value)
195
+ def _set_to_account(person, attr, value)
159
196
  return if !person.account
160
197
  multiple = ["policy_group_ids", "filter_tags"].include?(attr)
161
198
  if multiple
@@ -169,7 +206,7 @@ module Eco
169
206
  person.account.send("#{attr}=", value)
170
207
  end
171
208
 
172
- def set_to_details(person, attr, value)
209
+ def _set_to_details(person, attr, value)
173
210
  return if !person.details
174
211
  unless field = person.details.get_field(attr)
175
212
  fatal("Field '#{attr}' does not exist in details of schema: '#{person.details.schema_id}'")
@@ -184,18 +221,18 @@ module Eco
184
221
  person.details[attr] = value
185
222
  end
186
223
 
187
- def get_from_core (person, attr)
224
+ def _get_from_core (person, attr)
188
225
  person.send(attr)
189
226
  end
190
227
 
191
- def get_from_account (person, attr)
228
+ def _get_from_account (person, attr)
192
229
  return nil if !person.account
193
230
  multiple = ["policy_group_ids", "filter_tags"].include?(attr)
194
231
  value = person.account.send(attr)
195
232
  @person_parser.serialize(:multiple, value) if multiple
196
233
  end
197
234
 
198
- def get_from_details(person, attr)
235
+ def _get_from_details(person, attr)
199
236
  return nil if !person.details || !person&.details&.schema_id
200
237
  unless field = person.details.get_field(attr)
201
238
  fatal("Field '#{attr}' does not exist in details of schema: '#{person.details.schema_id}'")
@@ -210,16 +247,16 @@ module Eco
210
247
  # To obtain an entry with internal names but external values.
211
248
  # @param data [Hash] external or raw entry (when parsing) or internal or parsed entry (when serializing).
212
249
  # @return [Hash] entry with **internal names** and **external values**.
213
- def mapped_entry(data)
214
- return aliased_entry(data) if parsing?
215
- serialized_entry(data)
250
+ def _mapped_entry(data)
251
+ return _aliased_entry(data) if parsing?
252
+ _serialized_entry(data)
216
253
  end
217
254
 
218
255
  # Parsing helper that aliases attribute names (from internal to external names)
219
256
  # @note **Parse**: here we aliase internal attribute names into external ones.
220
257
  # @param ext_entry [Hash] entry in raw, with **external** names and values.
221
258
  # @return [Hash] entry with **internal names** and **external values**.
222
- def aliased_entry(ext_entry)
259
+ def _aliased_entry(ext_entry)
223
260
  aliased_hash = @emap.aliased_attrs.map do |attr|
224
261
  [attr, ext_entry[@emap.to_external(attr)]]
225
262
  end.to_h
@@ -238,7 +275,7 @@ module Eco
238
275
  # 2. when running the serializers, it overrides existing keys.
239
276
  # @param unserialized_entry [Hash] entry with **internal** names and values.
240
277
  # @return [Hash] entry with **internal names** and **external values**.
241
- def serialized_entry(unserialized_entry)
278
+ def _serialized_entry(unserialized_entry)
242
279
  serial_attrs = @person_parser.defined_attrs.reduce({}) do |serial_hash, attr|
243
280
  deps = @deps[attr] || {}
244
281
  serial_attr = @person_parser.serialize(attr, @person, deps: deps)
@@ -250,15 +287,15 @@ module Eco
250
287
  # To obtain an entry with internal names but external values.
251
288
  # @param data [Hash, Ecoportal::API::V1::Person] alised_entry (when parsing) or person (when serializing).
252
289
  # @return [Hash] the `internal entry` with the **internal** attributes names and values.
253
- def internal_entry(data)
254
- return parsed_entry(data) if parsing?
255
- unserialized_entry(data)
290
+ def _internal_entry(data)
291
+ return _parsed_entry(data) if parsing?
292
+ _unserialized_entry(data)
256
293
  end
257
294
 
258
295
  # Parsing helper that just **parses the values** that have a parser/serializer defined.
259
296
  # @param aliased_entry [Hash] the entry with the _internal attribute_ names but the _external values_.
260
297
  # @return [Hash] the `internal entry` with the **internal** attributes names and values.
261
- def parsed_entry(aliased_entry)
298
+ def _parsed_entry(aliased_entry)
262
299
  parsed = @person_parser.defined_attrs.map do |attr|
263
300
  value = @person_parser.parse(attr, aliased_entry)
264
301
  [attr, value]
@@ -274,19 +311,19 @@ module Eco
274
311
  # - `core`, `account`, `details`.
275
312
  # @param person [Ecoportal::API::V1::Person] the `Person` object to transform into an _internal entry_.
276
313
  # @return [Hash] the `internal entry` with the **internal** attributes names and values.
277
- def unserialized_entry(person)
314
+ def _unserialized_entry(person)
278
315
  core_hash = @person_parser.target_attrs_core.reduce({}) do |hash, attr|
279
- value = get_from_core(person, attr)
316
+ value = _get_from_core(person, attr)
280
317
  hash.merge(hash_attr(attr, value))
281
318
  end
282
319
 
283
320
  details_hash = @person_parser.target_attrs_details.reduce({}) do |hash, attr|
284
- value = get_from_details(person, attr)
321
+ value = _get_from_details(person, attr)
285
322
  hash.merge(hash_attr(attr, value))
286
323
  end
287
324
 
288
325
  account_hash = @person_parser.target_attrs_account.reduce({}) do |hash, attr|
289
- value = get_from_account(person, attr)
326
+ value = _get_from_account(person, attr)
290
327
  hash.merge(hash_attr(attr, value))
291
328
  end
292
329
 
@@ -7,5 +7,6 @@ module Eco
7
7
  end
8
8
  end
9
9
 
10
- #require_relative 'version_patches/base_model.rb'
10
+ require_relative 'version_patches/base_model.rb'
11
11
  require_relative 'version_patches/external_person.rb'
12
+ require_relative 'version_patches/internal_person.rb'
@@ -0,0 +1,27 @@
1
+ module Ecoportal
2
+ module API
3
+ module Common
4
+ class BaseModel
5
+
6
+ attr_reader :initial_doc
7
+
8
+ def initialize(doc = {}, parent: self, key: nil)
9
+ @_parent = parent
10
+ @_key = key
11
+ if !_parent || !_key
12
+ @doc = doc
13
+ @original_doc = JSON.parse(@doc.to_json)
14
+ @initial_doc = JSON.parse(@doc.to_json)
15
+ end
16
+ end
17
+
18
+ def initial_doc
19
+ raise UnlinkedModel.new unless linked?
20
+ return @initial_doc if is_root?
21
+ _parent.initial_doc.dig(*[_key].flatten)
22
+ end
23
+
24
+ end
25
+ end
26
+ end
27
+ end
@@ -88,6 +88,12 @@ module Eco
88
88
  end
89
89
  end
90
90
 
91
+ # TODO: should keep it uniq
92
+ #def merge(data)
93
+ # newFrom to_a + data
94
+ #end
95
+
96
+
91
97
  def exclude(object)
92
98
  exclude_people(into_a(object))
93
99
  end
@@ -6,6 +6,7 @@ module Eco
6
6
  class TagTree
7
7
  attr_reader :tag, :nodes
8
8
  attr_reader :depth, :path
9
+ attr_reader :enviro
9
10
 
10
11
  # @example Node format:
11
12
  # {"tag": "NODE NAME", "nodes": subtree}
@@ -40,6 +41,11 @@ module Eco
40
41
  init_hashes
41
42
  end
42
43
 
44
+ # @return [Boolean] `true` if there are tags in the node, `false` otherwise.
45
+ def empty?
46
+ @has_tags.empty?
47
+ end
48
+
43
49
  # List of tag nodes of the entire subtree.
44
50
  # @return [Array<String>]
45
51
  def tags
@@ -4,9 +4,9 @@ module Eco
4
4
  class Policy < Eco::API::UseCases::UseCase
5
5
  TYPES = [:transform]
6
6
 
7
- def initialize(name, type: :transform, root:, options: {}, &block)
7
+ def initialize(name, type: :transform, root:, &block)
8
8
  raise "Undefine usecase type #{type}. Please, use any of #{TYPES}" unless self.class.valid_type?(type)
9
- super(name, type: type, root: root, options: options, &block)
9
+ super(name, type: type, root: root, &block)
10
10
  end
11
11
 
12
12
  def root=(value)
@@ -1,9 +1,8 @@
1
1
  module Eco
2
2
  module API
3
3
  class Session < Common::Session::BaseSession
4
- attr_reader :batch, :policy_groups, :tagtree
5
- attr_reader :job_groups
6
- attr_reader :schemas
4
+ attr_reader :batch
5
+ attr_reader :job_groups
7
6
  attr_accessor :schema
8
7
 
9
8
  def initialize(init = {})
@@ -21,33 +20,19 @@ module Eco
21
20
 
22
21
  @use_cases = Eco::API::UseCases::DefaultCases.new.merge(config.usecases)
23
22
 
24
- @schemas = config.schemas
25
- self.schema = config.people.default_schema || @schemas.first
23
+ self.schema = config.people.default_schema || schemas.first
26
24
  @policy_groups = config.policy_groups
27
25
 
28
- @tagtree = nil
29
- if tree_file = config.dig('org', 'tagtree')
30
- tree = file_manager.load_json(tree_file) unless !tree_file
31
- @tagtree = Eco::API::Organization::TagTree.new(tree, enviro: enviro)
32
- end
33
-
34
26
  presets_custom_file = config.people.presets_custom
35
27
  presets_map_file = config.people.presets_map
36
28
 
37
29
  @presets_factory = Eco::API::Organization::PresetsFactory.new({
38
- #policy_groups: @policy_groups,
39
30
  presets_custom: file_manager.dir.file(presets_custom_file, should_exist: true),
40
31
  presets_map: file_manager.dir.file(presets_map_file, should_exist: true),
41
32
  enviro: enviro
42
33
  })
43
34
  end
44
35
 
45
- def self.configure
46
- Session::Config.new.tap do |conf|
47
- yield(conf) if block_given?
48
- end
49
- end
50
-
51
36
  # TASKS & JOBS
52
37
  def do
53
38
  @task
@@ -71,27 +56,22 @@ module Eco
71
56
  end
72
57
  end
73
58
 
74
- def job_group(name, order: :last)
75
- case
76
- when job_groups.exists?(name)
77
- job_groups[name]
78
- else
79
- job_groups.new(name, order: order)
80
- end
59
+ def policy_groups
60
+ config.policy_groups
81
61
  end
82
62
 
83
- def jobs_launch(simulate: false)
84
- job_groups.launch(simulate: simulate)
63
+ def tagtree
64
+ config.tagtree(enviro: enviro)
85
65
  end
86
66
 
87
- def policy_groups
88
- @policy_groups ||= config.policy_groups
67
+ def schemas
68
+ config.schemas
89
69
  end
90
70
 
91
71
  def schema=(value)
92
72
  case value
93
73
  when String
94
- unless @schema = @schemas.schema(value)
74
+ unless @schema = schemas.schema(value)
95
75
  fatal "The schema with id or name '#{value}' does not exist"
96
76
  end
97
77
  when Ecoportal::API::V1::PersonSchema
@@ -167,6 +147,19 @@ module Eco
167
147
  @use_cases.case(name, type: type).launch(**args)
168
148
  end
169
149
 
150
+ def job_group(name, order: :last)
151
+ case
152
+ when job_groups.exists?(name)
153
+ job_groups[name]
154
+ else
155
+ job_groups.new(name, order: order)
156
+ end
157
+ end
158
+
159
+ def jobs_launch(simulate: false)
160
+ job_groups.launch(simulate: simulate)
161
+ end
162
+
170
163
  end
171
164
  end
172
165
  end
@@ -50,7 +50,7 @@ module Eco
50
50
  entries = status.queue
51
51
  puts "\n"
52
52
  entries.each_with_index do |entry, i|
53
- if (i % 500 == 0)
53
+ if (i % 10 == 0)
54
54
  percent = i * 100 / entries.length
55
55
  print "Searching: #{percent.round}% (#{i}/#{entries.length} entries)\r"
56
56
  $stdout.flush
@@ -107,7 +107,7 @@ module Eco
107
107
  people += people_res
108
108
 
109
109
  total_iterations ||= response.body["total_pages"]
110
- no_pages = !response.body["total_pages"]
110
+ no_pages = !response.body["total_pages"]
111
111
  total_results ||= response.body["total_results"]
112
112
 
113
113
  if !total_iterations
@@ -115,6 +115,7 @@ module Eco
115
115
  end
116
116
 
117
117
  msg = "iteration number: #{page}/#{total_iterations}, got num people #{people_res.length}, with total #{people.length} people got"
118
+ msg = "search(q=#{params[:q]}) " + msg if params.key?(:q)
118
119
  logger.info(msg) unless silent
119
120
 
120
121
  iterate = (no_pages && results_from = response.body["next_results_from"]) || (looping && page < total_iterations)