Make db params and path configurable via quanlib.ini.
authorChris Jaekl <cejaekl@yahoo.com>
Sun, 19 Nov 2017 12:23:29 +0000 (21:23 +0900)
committerChris Jaekl <cejaekl@yahoo.com>
Sun, 19 Nov 2017 12:23:29 +0000 (21:23 +0900)
book.rb
main.rb
store.rb
walkdir.rb

diff --git a/book.rb b/book.rb
index a94e33cfcffdc34ecbba6876b07e30c5c5d00bd3..6d90c0e2947a0a243b3480758518159e075d8d85 100644 (file)
--- 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 4ea9c7057de2e456b65eecb2fcad0515b3fbc283..7c08cf2b66e90839b14bab5d5f392be2ccbe93f8 100644 (file)
--- 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?("--")
index 4895a5bb7483bbcf474c62daca224eee2227e85c..69e1278d40393ea9d3d7e75d34cdf452645e8ed1 100644 (file)
--- 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 = 
 <<EOS
       CREATE TABLE Authors (
         id          INTEGER PRIMARY KEY,
         grouping    VARCHAR(64),
-        reading     VARCHAR(196),
-        sort        VARCHAR(196)
+        reading     VARCHAR(256),
+        sort        VARCHAR(256)
       );
 EOS
 
@@ -137,9 +144,13 @@ EOS
       @conn.exec(stmt)
     end
 
-    populate_fast_table()
-    populate_classifications_table()
+    if skip_class == false
+      populate_fast_table()
+      populate_classifications_table()
+    end
+
     populate_series_table()
+
   end
 
   def dropSchema
@@ -178,7 +189,7 @@ EOS
     return nil
   end
 
-  def init_db
+  def init_db(skip_class)
     sql = "SELECT 1 FROM pg_tables WHERE tableowner='quanlib' AND tablename='books'"
     found = false
     @conn.exec(sql).each do |row|
@@ -186,12 +197,11 @@ EOS
     end
 
     if ! found
-      create_schema()
+      create_schema(skip_class)
     end
   end
 
   def load_author(id)
-    #puts 'DEBUG:  load_author(' + id + ')'
     sqlSelect = "SELECT grouping, reading, sort FROM Authors WHERE id=$1"
     args = [id]
     @conn.exec_params(sqlSelect, args) do |rs|
@@ -200,10 +210,8 @@ EOS
       end
       row = rs[0]
       author = Author.new(row['grouping'], row['reading'], row['sort'])
-      #puts 'DEBUG:  author:  ' + author.inspect()
       return author
     end
-    #puts 'DEBUG:  NOT FOUND'
     return nil
   end
 
@@ -227,7 +235,6 @@ EOS
   end
 
   def load_book(id)
-    #puts 'DEBUG:  load_book(' + id + ')'
     sql = "SELECT author, classification, cover, description, path, series, title, volume FROM Books WHERE id=$1;"
     book = nil
 
@@ -255,7 +262,6 @@ EOS
       puts $@
     end
 
-    #puts 'DEBUG:  loaded book:   ' + book.inspect()
     return book
   end
 
@@ -334,7 +340,7 @@ EOS
 
     (efspath, efsname) = construct_efs_path(id)
 
-    fullpath = @@BASEPATH + '/efs/' + efspath + '/' + efsname
+    fullpath = @basePath + '/efs/' + efspath + '/' + efsname
 
     return Cover.new(nil, fullpath, mime_type)
 
@@ -363,7 +369,7 @@ EOS
 
     (efspath, efsname) = construct_efs_path(efs_id)
 
-    efspath = @@BASEPATH + '/efs/' + efspath
+    efspath = @basePath + '/efs/' + efspath
 
     FileUtils.mkdir_p(efspath)
 
@@ -450,7 +456,7 @@ EOS
   def populate_classifications_table
     puts "Populating the Classifications table..."
     first = true
-    CSV.foreach(@@BASEPATH + '/csv/class.csv') do |row|
+    CSV.foreach(@basePath + '/csv/class.csv') do |row|
       if first
         # skip the header row
         first = false
@@ -490,7 +496,7 @@ EOS
   def populate_fast_table
     puts "Populating the FAST table..."
     first = true
-    CSV.foreach(@@BASEPATH + '/csv/fast.csv') do |row|
+    CSV.foreach(@basePath + '/csv/fast.csv') do |row|
       if first
         first = false  # skip the header row
       else
@@ -504,7 +510,7 @@ EOS
 
   def populate_series_table
     puts "Populating the Series table..."
-    CSV.foreach(@@BASEPATH + '/csv/series.csv') do |row|
+    CSV.foreach(@basePath + '/csv/series.csv') do |row|
       id = next_id('series_id')
       sqlInsert = "INSERT INTO Series (id, age, genre, grouping, code, descr) VALUES ($1, $2, $3, $4, $5, $6);"
       args = [id] + row
index 800b7fd40eb0db41318c85697e8418bb251ff206..b00645545fa984b5fbef17b09b45513957ab6f69 100644 (file)
@@ -96,7 +96,6 @@ class WalkDir
         result.push(fullName)
       end
     end
-    #puts result
     return result
   end
 end