Class: Command::Setting
- Inherits:
-
CommandBase
- Object
- CommandBase
- Command::Setting
- 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.inspect" => [:boolean, "常に変換時に調査結果を表示するか"], "download.interval" => [:float, "各話DL時に指定した秒数待機する。デフォルト0"], "download.wait-steps" => [:integer, "この値で指定した話数ごとにウェイトを入れる\n" + " " * 6 + "※注意:11以上を設定してもなろうの場合は10話ごとにウェイトが入ります"], "download.use-subdirectory" => [:boolean, "小説を一定数ごとにサブフォルダへ分けて保存するか\n" + " " * 6 + "※注意:小説を大量に同一フォルダに保存するとパフォーマンスが劣化する回避策"], "send.without-freeze" => [:boolean, "`全話'送信時に凍結された小説は対象外に"], "update.strong" => [:boolean, "更新漏れが無い様に改稿日の分は必ずDLするか"], "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], } }
Constants included from Narou::Eventable
Narou::Eventable::EVENTS_CONTAINER
Class Method Summary collapse
Instance Method Summary collapse
-
#casting_variable(name, value) ⇒ Object
値の文字列を設定に基づいた型にキャストして、[scope, value] 形式で返す 不正な設定名もしくは値の場合は例外を吐く.
- #display_variable_list ⇒ Object
- #execute(argv) ⇒ Object
- #get_scope_of_variable_name(name) ⇒ Object
- #get_variable_list_strings(scope) ⇒ Object
-
#initialize ⇒ Setting
constructor
A new instance of Setting.
- #modify_settings(scoped_settings, name, value) ⇒ Object
- #modify_settings_when_device_changed(settings) ⇒ Object
- #output_error(msg, name = nil) ⇒ Object
- #output_setting_list ⇒ Object
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
#initialize ⇒ Setting
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_variables ⇒ Object
197 198 199 |
# File 'lib/command/setting.rb', line 197 def self.get_setting_variables SETTING_VARIABLES end |
.oneline_help ⇒ Object
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_list ⇒ Object
189 190 191 192 193 194 195 |
# File 'lib/command/setting.rb', line 189 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
100 101 102 103 104 105 106 107 108 109 110 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 |
# File 'lib/command/setting.rb', line 100 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 scope = get_scope_of_variable_name(name) unless scope output_error("#{name} という変数は存在しません", name) next end if value.nil? output_error("書式が間違っています。#{name}=値 のように書いて下さい", name) 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., 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
178 179 180 181 182 183 184 185 186 187 |
# File 'lib/command/setting.rb', line 178 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
145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/command/setting.rb', line 145 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
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/command/setting.rb', line 158 def modify_settings_when_device_changed(settings) device = Device.create(settings["device"]) = StringIO.new device.get_relative_variables.each do |name, value| if value.nil? settings.delete(name) .puts " <bold><red>← #{name} が削除されました</red></bold>".termcolor elsif settings[name].nil? || settings[name] != value settings[name] = value .puts " <bold><green>→ #{name} が #{value} に変更されました</green></bold>".termcolor end end if .length > 0 puts "端末を#{device.display_name}に指定したことで、以下の関連設定が変更されました" puts .string end rescue Device::UnknownDevice => e output_error("#{e.}\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_list ⇒ Object
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 |