Class: Y2Packager::License

Inherits:
Object
  • Object
show all
Includes:
Yast::Logger
Defined in:
library/packages/src/lib/y2packager/license.rb

Overview

Represent a License which could be the same for multiple products.

This class represents a license.

Constant Summary collapse

DEFAULT_LANG =

Default language for licenses.

"en_US".freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(product_name: nil, content: nil, fetcher: nil, handler: nil) ⇒ License

Constructor

This class should be able to use the proper fetcher (see Y2Packager::LicensesFetchers) in order to retrieve license content (including translations). However, for compatibility reasons, the constructor can receive a content that will be used as licence's content. The reason is that, in some parts of YaST, the license content/translations is retrieved in different ways. We might need to unify them.

Bear in mind that fetcher will be ignored if content is specified.


103
104
105
106
107
108
109
110
111
# File 'library/packages/src/lib/y2packager/license.rb', line 103

def initialize(product_name: nil, content: nil, fetcher: nil, handler: nil)
  @accepted = false
  @translations = {}
  @product_name = product_name
  @fetcher = fetcher
  @handler = handler

  add_content_for(DEFAULT_LANG, content) if content
end

Instance Attribute Details

#acceptedBoolean (readonly) Also known as: accepted?


29
30
31
# File 'library/packages/src/lib/y2packager/license.rb', line 29

def accepted
  @accepted
end

#fetcherYast::LicensesFetchers::Base (readonly)


35
36
37
# File 'library/packages/src/lib/y2packager/license.rb', line 35

def fetcher
  @fetcher
end

#handlerYast::LicensesHandlers::Base (readonly)


38
39
40
# File 'library/packages/src/lib/y2packager/license.rb', line 38

def handler
  @handler
end

#translationsHash<String, String> (readonly)


32
33
34
# File 'library/packages/src/lib/y2packager/license.rb', line 32

def translations
  @translations
end

Class Method Details

.clear_cacheObject

Clean licenses cache


74
75
76
# File 'library/packages/src/lib/y2packager/license.rb', line 74

def clear_cache
  @cache = nil
end

.find(product_name, content: nil) ⇒ License?

Find a license for a given product

This method uses a cache to return the same license if it was already used for another product.


53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'library/packages/src/lib/y2packager/license.rb', line 53

def find(product_name, content: nil)
  log.info "Searching for a license for product #{product_name}"
  return cache[product_name] if cache[product_name]

  fetcher = LicensesFetchers.for(product_name) unless content
  handler = LicensesHandlers.for(fetcher, product_name) if fetcher

  license = License.new(product_name: product_name, fetcher: fetcher,
                        handler: handler, content: content)
  return unless license.id

  cached_license = cache.values.find { |l| l.id == license.id }
  if cached_license
    log.info "Found cached license: #{cached_license.id}"
  else
    log.info "Caching license: #{license.id}"
  end
  cache[product_name] = cached_license || license
end

Instance Method Details

#accept!Object

Set the license as accepted


160
161
162
# File 'library/packages/src/lib/y2packager/license.rb', line 160

def accept!
  @accepted = true
end

#add_content_for(lang, content) ⇒ String

Add the license translated content for the given language


155
156
157
# File 'library/packages/src/lib/y2packager/license.rb', line 155

def add_content_for(lang, content)
  @translations[lang] = content
end

#content_for(lang = DEFAULT_LANG) ⇒ String?

Return the license translated content for the given language


132
133
134
135
136
137
138
# File 'library/packages/src/lib/y2packager/license.rb', line 132

def content_for(lang = DEFAULT_LANG)
  return @translations[lang] if @translations[lang]
  return unless fetcher

  content = fetcher.content(lang)
  add_content_for(lang, content)
end

#idString?

License unique identifier

This identifier is based on the given default language translation.


118
119
120
121
122
123
124
125
# File 'library/packages/src/lib/y2packager/license.rb', line 118

def id
  return @id if @id

  content = content_for(DEFAULT_LANG)
  return unless content

  @id = Digest::SHA2.hexdigest(content)
end

#localesArray<String>

Return license's available locales


143
144
145
146
147
# File 'library/packages/src/lib/y2packager/license.rb', line 143

def locales
  return [DEFAULT_LANG] unless fetcher

  fetcher.locales
end

#reject!Object

Set the license as rejected


165
166
167
# File 'library/packages/src/lib/y2packager/license.rb', line 165

def reject!
  @accepted = false
end