Class: Command::Setting

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

Constant Summary collapse

INVISIBLE =
true
SETTING_VARIABLES =
{
  local: {
    # 変数名 => [受け付ける型, 説明(, 不可視化フラグ)]
    "convert.no-epub" => [:boolean, "EPUB変換を無効にするか"],
    "convert.no-mobi" => [:boolean, "MOBI変換を無効にするか"],
    "convert.no-strip" => [:boolean, "MOBIのstripを無効にするか\n" +
                                     " " * 6 + "※注意:KDP用のMOBIはstripしないでください"],
    "convert.no-zip" => [:boolean, "i文庫用のzipファイル作成を無効にするか"],
    "convert.no-open" => [:boolean, "変換時に保存フォルダを開かないようにするか"],
    "convert.copy-to" => [:directory, "変換したらこのフォルダにコピーする\n" +
                                      " " * 6 + "※注意:存在しないフォルダだとエラーになる"],
    "convert.copy_to" => [:directory, "copy-toの昔の書き方(非推奨)", INVISIBLE],
    "convert.inspect" => [:boolean, "常に変換時に調査結果を表示するか"],
    "convert.multi-device" => [:string, "複数の端末用に同時に変換する。deviceよりも優先される。端末名をカンマ区切りで入力。ただのEPUBを出力したい場合はepubを指定"],
    "download.interval" => [:float, "各話DL時に指定した秒数待機する。デフォルト0"],
    "download.wait-steps" => [:integer, "この値で指定した話数ごとにウェイトを入れる\n" +
                                   " " * 6 + "※注意:11以上を設定してもなろうの場合は10話ごとにウェイトが入ります"],
    "download.use-subdirectory" => [:boolean, "小説を一定数ごとにサブフォルダへ分けて保存するか\n" +
                                              " " * 6 + "※注意:小説を大量に同一フォルダに保存するとパフォーマンスが劣化する回避策"],
    "send.without-freeze" => [:boolean, "一括送信時に凍結された小説は対象外に"],
    "send.backup-bookmark" => [:boolean, "一括送信時に栞データを自動でバックアップするか(KindlePW系用)"],
    "update.strong" => [:boolean, "更新漏れが無い様に改稿日の分は必ずDLするか"],
    "update.logging" => [:boolean, "更新時のログを保存するかどうか"],
    "update.convert-only-new-arrival" => [:boolean, "更新時に新着のみ変換を実行するかどうか"],
    "device" => [:string, "変換、送信対象の端末(sendの--help参照)"],
    "multiple-delimiter" => [:string, "--multiple指定時の区切り文字"],
  },
  global: {
    "aozoraepub3dir" => [:directory, "AozoraEpub3のあるフォルダを指定", INVISIBLE],
    "difftool" => [:string, "Diffで使うツールのパスを指定する"],
    "difftool.arg" => [:string, "difftoolで使う引数を設定(オプション)"],
    "no-color" => [:boolean, "カラー表示を無効にするか"],
    "server-port" => [:integer, "WEBサーバ起動時のポート"],
    "server-bind" => [:string, "WEBサーバのホスト制限(未設定時:起動PCのIP)", INVISIBLE],
    "over18" => [:boolean, "18歳以上かどうか", INVISIBLE],
    "dismiss-notice" => [:boolean, "お知らせを消すかどうか", INVISIBLE],
  }
}

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.



18
19
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
# File 'lib/command/setting.rb', line 18

def initialize
  super("[<name>=<value> ...] [options]")
  @opt.separator <<-EOS

  ・各コマンドの設定の変更が出来ます。
  ・Global な設定はユーザープロファイルに保存され、すべての narou コマンドで使われます
  ・下の一覧は一部です。すべてを確認するには -a オプションを付けて確認して下さい

  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
  }
end

Class Method Details

.get_setting_variablesObject



218
219
220
# File 'lib/command/setting.rb', line 218

def self.get_setting_variables
  SETTING_VARIABLES
end

.oneline_helpObject



14
15
16
# File 'lib/command/setting.rb', line 14

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

Instance Method Details

#casting_variable(name, value) ⇒ Object

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



69
70
71
72
73
74
75
76
# File 'lib/command/setting.rb', line 69

def casting_variable(name, value)
  scope = get_scope_of_variable_name(name)
  unless scope
    raise Helper::InvalidVariableName, "#{name} は不明な名前です"
  end
  casted_value = Helper.string_cast_to_type(value, SETTING_VARIABLES[scope][name][0])
  [scope, casted_value]
end

#display_variable_listObject



210
211
212
213
214
215
216
# File 'lib/command/setting.rb', line 210

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



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
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
# File 'lib/command/setting.rb', line 111

def execute(argv)
  super
  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 => 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



56
57
58
59
60
61
62
63
# File 'lib/command/setting.rb', line 56

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



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

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

#modify_settings(scoped_settings, name, value) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/command/setting.rb', line 166

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



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/command/setting.rb', line 179

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



94
95
96
97
98
# File 'lib/command/setting.rb', line 94

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

#output_setting_listObject



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/command/setting.rb', line 78

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



100
101
102
103
104
105
106
107
108
109
# File 'lib/command/setting.rb', line 100

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