Class: Command::Setting

Inherits:
CommandBase show all
Includes:
Narou::Eventable
Defined in:
lib/command/setting.rb

Defined Under Namespace

Classes: InvalidSelectValue

Constant Summary collapse

SETTING_TAB_NAMES =
{
  general: "一般",
  detail: "詳細",
  global: "Global",
  default: "default.*",
  force: "force.*",
  command: "コマンド",
}
SETTING_TAB_INFO =
{
  global: "Global な設定はユーザープロファイルに保存され、すべての narou コマンドで使われます",
  default: "default.* 系の設定は個別の変換設定で未設定の項目の挙動を指定することが出来ます",
  force: "force.* 系の設定は個別設定、default.* 等の設定を無視して反映されるようになります",
  command: "default_args.* 系の設定は、各種コマンドのオプションを省略した場合に使用されるオプションを指定出来ます",
}
SETTING_VARIABLES =
{
  local: {
    # 変数名 => { type: 受け付ける型, help: 説明[, invisible: 不可視化フラグ,
    #             select_keys: 選択肢型の時のキー(配列),
    #             select_summaries: 選択肢型の時のキーの概要(配列)] }
    "device" => {
      type: :select,
      help: "変換、送信対象の端末(sendの--help参照)",
      select_keys: Device::DEVICES.keys,
      select_summaries: Device::DEVICES.values.map { |d| d::DISPLAY_NAME },
      tab: :general
    },
    "hotentry" => {
      type: :boolean, help: "新着投稿だけをまとめたデータを作る",
      tab: :general
    },
    "hotentry.auto-mail" => {
      type: :boolean, help: "新着投稿作成時にメールを送る(mail設定済みの場合)",
      tab: :detail
    },
    "update.strong" => {
      type: :boolean, help: "更新漏れが無い様に改稿日の分は必ずDLする",
      tab: :general
    },
    "update.logging" => {
      type: :boolean, help: "更新時のログを保存する",
      tab: :detail
    },
    "update.convert-only-new-arrival" => {
      type: :boolean, help: "更新時に新着のみ変換を実行する",
      tab: :general
    },
    "update.sort-by" => {
      type: :select,
      help: "アップデートを指定した項目順で行う\n#{Narou.update_sort_key_summaries(40)}",
      select_keys: Narou::UPDATE_SORT_KEYS.keys,
      select_summaries: Narou::UPDATE_SORT_KEYS.values,
      tab: :general
    },
    "convert.copy-to" => {
      type: :directory,
      help: "変換したらこのフォルダにコピーする\n" \
            "      ※注意:存在しないフォルダだとエラーになる",
      tab: :general
    },
    "convert.copy_to" => {
      type: :directory, help: "copy-toの昔の書き方(非推奨)", invisible: true
    },
    "convert.no-epub" => {
      type: :boolean, help: "EPUB変換を無効にする", invisible: true,
      tab: :detail
    },
    "convert.no-mobi" => {
      type: :boolean, help: "MOBI変換を無効にする", invisible: true,
      tab: :detail
    },
    "convert.no-strip" => {
      type: :boolean,
      help: "MOBIのstripを無効にする\n" \
            "      ※注意:KDP用のMOBIはstripしないでください",
      invisible: true,
      tab: :detail
    },
    "convert.no-zip" => {
      type: :boolean, help: "i文庫用のzipファイル作成を無効にする", invisible: true,
      tab: :detail
    },
    "convert.no-open" => {
      type: :boolean, help: "変換時に保存フォルダを開かないようにする",
      tab: :general
    },
    "convert.inspect" => {
      type: :boolean, help: "常に変換時に調査結果を表示する",
      tab: :detail
    },
    "convert.multi-device" => {
      type: :multiple,
      help: "複数の端末用に同時に変換する。deviceよりも優先される。" \
            "端末名をカンマ区切りで入力。ただのEPUBを出力したい場合はepubを指定",
      select_keys: Device::DEVICES.keys,
      select_summaries: Device::DEVICES.values.map { |d| d::DISPLAY_NAME },
      tab: :general
    },
    "convert.copy-to-grouping" => {
      type: :boolean, help: "copy-toで指定したフォルダの中で更にdevice毎にフォルダを振り分ける",
      tab: :general
    },
    "download.interval" => {
      type: :float, help: "各話DL時に指定した秒数待機する。デフォルト0",
      tab: :detail
    },
    "download.wait-steps" => {
      type: :integer,
      help: "この値で指定した話数ごとにウェイトを入れる\n" \
            "      ※注意:11以上を設定してもなろうの場合は10話ごとにウェイトが入ります",
      tab: :detail
    },
    "download.use-subdirectory" => {
      type: :boolean,
      help: "小説を一定数ごとにサブフォルダへ分けて保存する\n" \
            "      ※注意:小説を大量に同一フォルダに保存するとパフォーマンスが劣化する回避策",
      tab: :detail
    },
    "send.without-freeze" => {
      type: :boolean, help: "一括送信時に凍結された小説は対象外にする",
      tab: :general
    },
    "send.backup-bookmark" => {
      type: :boolean, help: "一括送信時に栞データを自動でバックアップする(KindlePW系用)",
      tab: :detail
    },
    "multiple-delimiter" => {
      type: :string, help:  "--multiple指定時の区切り文字",
      tab: :detail
    },
    "theme" => {
      type: :select, help: "WEB UI 用テーマ選択",
      select_keys: Narou.get_theme_names,
      select_summaries: Narou.get_theme_names,
      tab: :general
    },
  },
  global: {
    "aozoraepub3dir" => {
      type: :directory, help: "AozoraEpub3のあるフォルダを指定", invisible: true
    },
    "difftool" => {
      type: :string, help: "Diffで使うツールのパスを指定する",
      tab: :global
    },
    "difftool.arg" => {
      type: :string, help: "difftoolで使う引数を設定(オプション)",
      tab: :global
    },
    "no-color" => {
      type: :boolean, help: "カラー表示を無効にする",
      tab: :global
    },
    "server-port" => {
      type: :integer, help: "WEBサーバ起動時のポート",
      tab: :global
    },
    "server-bind" => {
      type: :string, help: "WEBサーバのホスト制限(未設定時:起動PCのIP)", invisible: true,
      tab: :global
    },
    "over18" => {
      type: :boolean, help: "18歳以上かどうか", invisible: true,
      tab: :global
    },
    "dismiss-notice" => {
      type: :boolean, help: "お知らせを消す", invisible: true,
      tab: :global
    },
  }
}

Constants included from Narou::Eventable

Narou::Eventable::EVENTS_CONTAINER

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Narou::Eventable

#add_event_listener, included, #one, #remove_event_listener, #trigger_event

Methods inherited from CommandBase

execute!, #force_change_settings_function, help, #hook_call, #load_local_settings, #tagname_to_ids

Constructor Details

#initializeSetting

Returns a new instance of Setting.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/command/setting.rb', line 20

def initialize
  super "[<name>=<value> ...] [options]\n" \
        "--burn <target> [<target2> ...]"
  @opt.separator <<-EOS

  ・各コマンドの設定の変更が出来ます。
  ・Global な設定はユーザープロファイルに保存され、すべての narou コマンドで使われます
  ・下の一覧は一部です。すべてを確認するには -a オプションを付けて確認して下さい
  ・default. で始まる設定は、setting.ini で未設定時の項目の挙動を指定することが出来ます
  ・force. で始まる設定は、setting.ini や default.* 等の指定を全て無視して項目の挙動を強制出来ます

  Local Variable List:
    <name>           <value>              説明
  EOS
  @opt.separator(get_variable_list_strings(:local))

  @opt.separator("\n  Global Variable List:")
  @opt.separator(get_variable_list_strings(:global))

  @opt.separator <<-EOS

  Examples:
narou setting --list
narou setting convert.no-open=true
narou setting convert.no-epub=   # 右辺に何も書かないとその設定を削除できる
narou setting convert.copy-to=C:/dropbox/mobi
narou s convert.copy-to="C:\\Documents and Settings\\user\\epub"

  Options:
  EOS
  @opt.on("-l", "--list", "現在の設定値を確認する") {
    output_setting_list
    exit 0
  }
  @opt.on("-a", "--all", "設定できる全ての変数名を表示する") {
    @options["all"] = true
    display_variable_list
    exit 0
  }
  @opt.on("--burn", "指定した小説の未設定項目に共通設定を焼き付ける") {
    @options["burn"] = true
  }
end

Class Method Details

.get_setting_tab_infoObject



299
300
301
# File 'lib/command/setting.rb', line 299

def self.get_setting_tab_info
  SETTING_TAB_INFO
end

.get_setting_tab_namesObject



295
296
297
# File 'lib/command/setting.rb', line 295

def self.get_setting_tab_names
  SETTING_TAB_NAMES
end

.get_setting_variablesObject



291
292
293
# File 'lib/command/setting.rb', line 291

def self.get_setting_variables
  SETTING_VARIABLES
end

.oneline_helpObject



16
17
18
# File 'lib/command/setting.rb', line 16

def self.oneline_help
  "各コマンドの設定を変更します"
end

Instance Method Details

#burn_default_settings(argv) ⇒ Object

小説の setting.ini の未設定項目に共通設定を焼き付ける

default.* が設定されているならそれを、なければオリジナル設定を



253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# File 'lib/command/setting.rb', line 253

def burn_default_settings(argv)
  if argv.empty?
    error "対象小説を指定して下さい"
    exit Narou::EXIT_ERROR_CODE
  end
  msg = "指定された小説のsetting.iniの未項目設定に共通設定を焼き付けます。\n" \
        "(共通設定とはsetting.iniの項目が未設定時に使用される default.* 系設定およびNarou.rbオリジナル設定のこと)\n" \
        "よろしいですか"
  return unless Narou::Input.confirm(msg)

  tagname_to_ids(argv)
  argv.each do |arg|
    data = Downloader.get_data_by_target(arg)
    unless data
      error "#{arg} は存在しません"
      next
    end
    novel_setting = NovelSetting.new(arg, true, true)    # 空っぽの設定を作成
    novel_setting.settings = novel_setting.load_setting_ini["global"]
    original_settings = NovelSetting.get_original_settings
    default_settings = NovelSetting.load_default_settings

    original_settings.each do |original|
      name = original[:name]
      unless novel_setting.settings.include?(name)
        if default_settings.include?(name)
          novel_setting[name] = default_settings[name]
        else
          novel_setting[name] = original[:value]
        end
      end
    end

    novel_setting.save_settings
    puts "#{data["title"]} の設定を保存しました"
  end
end

#casting_variable(name, value) ⇒ Object

値の文字列を設定に基づいた型にキャストして、[scope, value] 形式で返す 不正な設定名もしくは値の場合は例外を吐く



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/command/setting.rb', line 77

def casting_variable(name, value)
  scope = get_scope_of_variable_name(name)
  unless scope
    raise Helper::InvalidVariableName, "#{name} は不明な名前です"
  end
  variable_definition = SETTING_VARIABLES[scope][name]
  case variable_definition[:type]
  when :select
    select_values = variable_definition[:select_keys]
    unless select_values.include?(value)
      raise InvalidSelectValue, "不明な値です。#{select_values.join(", ")} の中から指定して下さい"
    end
    casted_value = value
  when :multiple
    select_values = variable_definition[:select_keys]
    value.split(",").each do |input_value|
      unless select_values.include?(input_value)
        raise InvalidSelectValue, "不明な値です。#{select_values.join(", ")} の中から指定して下さい"
      end
    end
    casted_value = value
  else
    casted_value = Helper.string_cast_to_type(value, variable_definition[:type])
  end
  [scope, casted_value]
end

#display_variable_listObject



240
241
242
243
244
245
246
# File 'lib/command/setting.rb', line 240

def display_variable_list
  puts "Local Variable List:"
  puts get_variable_list_strings(:local).gsub(/^ {4}/, "")
  puts
  puts "Global Variable List:"
  puts get_variable_list_strings(:global).gsub(/^ {4}/, "")
end

#execute(argv) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/command/setting.rb', line 137

def execute(argv)
  super
  if @options["burn"]
    burn_default_settings(argv)
    return
  end
  if argv.empty?
    puts @opt.help
    return
  end
  settings = {
    local: Inventory.load("local_setting", :local),
    global: Inventory.load("global_setting", :global)
  }
  device = Narou.get_device
  @error_count = 0
  self.extend(device.get_hook_module) if device
  argv.each do |arg|
    name, value = arg.split("=", 2).map(&:strip)
    if name == ""
      output_error("書式が間違っています。変数名=値 のように書いて下さい")
      next
    end
    if value.nil?
      output_error("書式が間違っています。#{name}=値 のように書いて下さい", name)
      next
    end
    scope = get_scope_of_variable_name(name)
    unless scope
      if value == ""
        # 定義上ではすでに存在しないが、設定ファイルには残っている古い変数
        # を削除できるようにする
        if sweep_dust_variable(name, settings)
          puts "#{name} の設定を削除しました"
        else
          output_error("#{name} という変数は存在しません", name)
        end
      else
        output_error("#{name} という変数は設定出来ません", name)
      end
      next
    end
    if value == ""
      hook_call(:modify_settings, settings[scope], name, nil)
      next
    end
    begin
      scope, casted_value = casting_variable(name, value)
    rescue Helper::InvalidVariableName, Helper::InvalidVariableType, InvalidSelectValue => e
      output_error(e.message, name)
      next
    end
    hook_call(:modify_settings, settings[scope], name, casted_value)
  end
  settings[:local].save
  settings[:global].save
  exit @error_count if @error_count > 0
end

#get_scope_of_variable_name(name) ⇒ Object



64
65
66
67
68
69
70
71
# File 'lib/command/setting.rb', line 64

def get_scope_of_variable_name(name)
  [:local, :global].each do |scope|
    if SETTING_VARIABLES[scope].include?(name)
      return scope
    end
  end
  nil
end

#get_variable_list_strings(scope) ⇒ Object



229
230
231
232
233
234
235
236
237
238
# File 'lib/command/setting.rb', line 229

def get_variable_list_strings(scope)
  result = ""
  SETTING_VARIABLES[scope].each do |name, info|
    if @options["all"] || !info[:invisible]
      type_description = Helper.variable_type_to_description(info[:type])
      result << "    <bold><green>#{name.ljust(18)}</green></bold> #{type_description} #{info[:help]}\n".termcolor
    end
  end
  result
end

#modify_settings(scoped_settings, name, value) ⇒ Object



196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/command/setting.rb', line 196

def modify_settings(scoped_settings, name, value)
  if value.nil?
    scoped_settings.delete(name)
    puts "#{name} の設定を削除しました"
  else
    scoped_settings[name] = value
    puts "#{name}#{value} に設定しました"
  end
  if name == "device" && value
    modify_settings_when_device_changed(scoped_settings)
  end
end

#modify_settings_when_device_changed(settings) ⇒ Object



209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/command/setting.rb', line 209

def modify_settings_when_device_changed(settings)
  device = Device.create(settings["device"])
  message = StringIO.new
  device.get_relative_variables.each do |name, value|
    if value.nil?
      settings.delete(name)
      message.puts "  <bold><red>← #{name} が削除されました</red></bold>".termcolor
    elsif settings[name].nil? || settings[name] != value
      settings[name] = value
      message.puts "  <bold><green>→ #{name}#{value} に変更されました</green></bold>".termcolor
    end
  end
  if message.length > 0
    puts "端末を#{device.display_name}に指定したことで、以下の関連設定が変更されました"
    puts message.string
  end
rescue Device::UnknownDevice => e
  output_error("#{e.message}\n設定できるのは #{Device::DEVICES.keys.join(", ")} です", "device")
end

#output_error(msg, name = nil) ⇒ Object



120
121
122
123
124
# File 'lib/command/setting.rb', line 120

def output_error(msg, name = nil)
  @error_count += 1
  error msg
  trigger(:error, msg, name)
end

#output_setting_listObject



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/command/setting.rb', line 104

def output_setting_list
  settings = {
    local: Inventory.load("local_setting", :local),
    global: Inventory.load("global_setting", :global)
  }
  settings.each do |scope, scoped_settings|
    puts "[#{scope.capitalize} Variables]"
    scoped_settings.each do |name, value|
      if value =~ / /
        value = "'#{value}'"
      end
      puts "<bold><green>#{name}</green></bold>=#{value}".termcolor
    end
  end
end

#sweep_dust_variable(target_name, settings) ⇒ Object



126
127
128
129
130
131
132
133
134
135
# File 'lib/command/setting.rb', line 126

def sweep_dust_variable(target_name, settings)
  deleted = false
  settings.each_value do |scoped_settings|
    if scoped_settings.has_key?(target_name)
      scoped_settings.delete(target_name)
      deleted = true
    end
  end
  deleted
end