From: Chris Jaekl Date: Sun, 19 Nov 2017 12:23:29 +0000 (+0900) Subject: Make db params and path configurable via quanlib.ini. X-Git-Url: https://jaekl.net/gitweb/?a=commitdiff_plain;h=2891b7126f522211f6d38f1ea3f684ffefb27b94;p=quanlib.git Make db params and path configurable via quanlib.ini. --- diff --git a/book.rb b/book.rb index a94e33c..6d90c0e 100644 --- a/book.rb +++ b/book.rb @@ -1,5 +1,6 @@ require 'nokogiri' +require 'rubygems' require 'zip' require 'author' diff --git a/main.rb b/main.rb index 4ea9c70..7c08cf2 100644 --- a/main.rb +++ b/main.rb @@ -3,31 +3,38 @@ require 'page' require 'store' require 'walkdir' -outputDir = 'output' +@outputDir = 'output' book_ids = [] -imageCount = 0 +@config_file = 'quanlib.ini' +@skip_class = false def handleArg(arg) - if "--purge" == arg + if arg.start_with?("--config=") + @config_file = arg[9..-1] + puts 'Using config file "' + @config_file + '".' + elsif "--purge" == arg puts 'Purging database...' @store.dropSchema() - if File.exists?(Store.unclassified_csv) - File.delete(Store.unclassified_csv) + if File.exists?(@store.unclassified_csv) + File.delete(@store.unclassified_csv) end + elsif "--skip-class" == arg + puts 'Skipping load of classification table.' + @skip_class = true elsif arg.start_with?("--") abort('ERROR: Unrecognized option "' + arg + '".') end end -@store = Store.new() +@store = Store.new(@config_file) @store.connect() for arg in ARGV handleArg(arg) end -@store.init_db() +@store.init_db(@skip_class) for arg in ARGV if ! arg.start_with?("--") diff --git a/store.rb b/store.rb index 4895a5b..69e1278 100644 --- a/store.rb +++ b/store.rb @@ -1,32 +1,39 @@ require 'csv' require 'fileutils' +require 'inifile' require 'pg' require 'series' class Store - @@BASEPATH = '/arc/quanlib' # TODO: FIXME: configure this in a sane way - @@UNCLASSIFIED_CSV = @@BASEPATH + '/unclassified.csv' - - def self.unclassified_csv - @@UNCLASSIFIED_CSV + def unclassified_csv + @basePath + '/csv/unclassified.csv' end - def initialize + def initialize(config_file) @conn = nil - #@dburl = 'dbi:Pg:quanlib:localhost' - @dbhost = "localhost" + config = IniFile.load(config_file) + if nil == config + puts 'FATAL: Failed to load config file "' + config_file + '". Aborting initialization.' + return + end + + section = config['database'] + @dbhost = section['host'] @dbport = 5432 - @dbname = 'quanlib' - @dbuser = 'quanlib' - @dbpass = 'quanlib' + @dbname = section['name'] + @dbuser = section['user'] + @dbpass = section['pass'] + + section = config['filesystem'] + @basePath = section['basePath'] end def connect # @conn = PGconn.connect('localhost', 5432, '', '', 'quanlib', 'quanlib', 'quanlib') - @conn = PG.connect('localhost', 5432, '', '', 'quanlib', 'quanlib', 'quanlib') + @conn = PG.connect(@dbhost, @dbport, '', '', @dbname, @dbuser, @dbpass) return @conn end @@ -41,14 +48,14 @@ class Store return path, name end - def create_schema + def create_schema(skip_class) create_authors = <