class Dbf2text
The main program class. Does it.
Public Class Methods
new(*args)
click to toggle source
# File lib/dbf2text.rb, line 41 def initialize(*args) if !args.empty? file = args[0] else args = ['-h'] end options = ArgParser::parse(args) init_logger() @log.level = (options.debug ? Logger::DEBUG : @def_log_level) # @log.debug('options are ' << options.to_s) @conf = Configuration.instance @conf.options = options # --- Do not use options after this line. Use @conf instead. --- if(file) msg = File_Checking::file_check(file, :exist, :file, :readable) if(msg) @log.error(trl("Cannot work with %s, because '%s'") %[file,msg]) exit false end args.shift else @log.warn( trl('no DBase-file given') ) exit true end ensure_mapping @log.info(trl("Running with the following configuration") << ":\n\t" << @conf.to_s.split(', ').join("\n\t") ) @dbf = DBF::Table.new(file) @dbf.each_with_index do |record, index| @log.debug(record.attributes) do_it(record, index) end end
Private Instance Methods
do_it(record, index)
click to toggle source
# File lib/dbf2text.rb, line 126 def do_it(record, index) #fill fields with values, not attribute-names. @log.debug('transforming data-record #' << index.next.to_s) outfile = @conf.output << '_' << index.next.to_s @log.debug('outfile: ' << outfile) File.open(outfile, 'w+') do |out| out << File.read(@conf.template) out.extend(Completable) # TODO make this an option. out.field_delimiter = @conf.delimiter replacements = Hash.new @mapping.each_pair do |k,v| replacements[v] = record[k] end @log.debug('replacements will be : ' << replacements.to_s) out.complete(replacements) end end
ensure_mapping()
click to toggle source
Verifies, that the mapping-file and the template exist and are readable. If not and if the missing file is one of the defaults defined in the constants, it is written to the configuration directory (see constants).
# File lib/dbf2text.rb, line 86 def ensure_mapping mapping = VCFMapping.instance msg = File_Checking::file_check($CONF_DIR, :exist, :directory, :readable, :writable) if(msg) @log.debug(msg) begin Dir.mkdir($CONF_DIR, 0700) rescue Exception => ex @log.error(trl("Cannot create the configuration directory %s, because ") %($CONF_DIR) << '"' << ex.message << '".') exit false end end conf = Configuration.instance [conf.template, conf.mapping].each do |file| msg = File_Checking::file_check(file, :exist, :file, :readable) if(msg) @log.warn( trl("File %s is not available, because %s") %[file, msg]) case file when $DEFAULT_TEMPLATE then File.write($DEFAULT_TEMPLATE, mapping.template) @log.info(trl("wrote the default template to %s.") %($DEFAULT_TEMPLATE)) when $DEFAULT_MAPPING then File.write($DEFAULT_MAPPING, mapping.mapping) @log.info(trl("wrote the default mapping to %s.") %($DEFAULT_MAPPING)) @log.warn(trl("PSE consider creating your own map-file as the default will probably not correspond to your given data structures.") ) else @log.error(trl("Cannot use %s, please indicate the correct file name or use the defaults.") %file) exit false end end end @log.debug('mapping-file is: ' << conf.mapping.to_s) @mapping = JSON.load(File.read(conf.mapping) ) @log.debug('Mapping: ' << @mapping.to_s) end