class PuppetPotGenerator

Parses and reports every instance of a translate function

version of this gem

Constants

PATH_TO_LOCALES
PATH_TO_MANIFESTS
PUPPET_POT_FILE
TRANSLATE_FUNCTION

Attributes

metrics[R]

Public Class Methods

generate_puppet_pot_file() click to toggle source
# File lib/puppet_pot_generator.rb, line 11
def self.generate_puppet_pot_file
  raise "PuppetPotGenerator: #{PATH_TO_LOCALES} folder does not exist" unless File.directory?(PATH_TO_LOCALES)
  raise 'PuppetPotGenerator: requires version 5 of greater of puppet' unless Puppet::Util::Package.versioncmp(Puppet.version, '5.0.0') >= 0
  parser = Puppet::Pops::Parser::EvaluatingParser.new
  ppg = PuppetPotGenerator.new
  open(PUPPET_POT_FILE, 'w') { |file| file << ppg.header }
  Dir[PATH_TO_MANIFESTS].each do |file|
    program = parser.parse_file(file)
    ppg.compute(program)
  end
end
new() click to toggle source
# File lib/puppet_pot_generator.rb, line 23
def initialize
  @potgenerator_visitor ||= Puppet::Pops::Visitor.new(nil, 'potgenerator', 0, 0)
end

Public Instance Methods

compute(target) click to toggle source
# File lib/puppet_pot_generator.rb, line 27
def compute(target)
  @path = []
  target._pcore_all_contents(@path) { |element| potgenerator(element) }
end
header() click to toggle source
# File lib/puppet_pot_generator.rb, line 32
  def header
    <<-HEADER
  #, fuzzy
  msgid ""
  msgstr ""
  "Project-Id-Version: PACKAGE VERSION\\n"
  "Report-Msgid-Bugs-To: \\n"
  "POT-Creation-Date: #{Time.now}\\n"
  "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\\n"
  "Last-Translator: FULL NAME <EMAIL@ADDRESS>\\n"
  "Language-Team: LANGUAGE <LL@li.org>\\n"
  "MIME-Version: 1.0\\n"
  "Content-Type: text/plain; charset=UTF-8\\n"
  "Content-Transfer-Encoding: 8bit\\n"
  "X-Generator: Translate Toolkit 2.0.0\\n"

  HEADER
  end
potgenerator(o) click to toggle source
# File lib/puppet_pot_generator.rb, line 68
def potgenerator(o)
  @potgenerator_visitor.visit_this_0(self, o)
end
potgenerator_CallExpression(o) click to toggle source
# File lib/puppet_pot_generator.rb, line 74
def potgenerator_CallExpression(o) # rubocop:disable Style/MethodName
  report_on_translate_function(o)
end
potgenerator_Object(o) click to toggle source
# File lib/puppet_pot_generator.rb, line 72
def potgenerator_Object(o); end
report_on_translate_function(statement) click to toggle source
# File lib/puppet_pot_generator.rb, line 51
def report_on_translate_function(statement)
  return unless statement.functor_expr.class == Puppet::Pops::Model::QualifiedName && statement.functor_expr.value == TRANSLATE_FUNCTION
  line_number = statement.arguments[0].locator.line_for_offset(statement.arguments[0].offset)
  path_to_file = statement.arguments[0].file
  string_to_be_translated = if defined?(statement.arguments[0].segments)
                              statement.arguments[0].segments[0].value
                            else
                              statement.arguments[0].value
                            end
  pot_entry =  '#. ' + path_to_file + ':' + line_number.to_s + "\n"
  pot_entry << 'msgid "' + string_to_be_translated + "\"\n"
  pot_entry << "msgstr \"\"\n"
  open(PUPPET_POT_FILE, 'a') do |file|
    file << pot_entry
  end
end