Skip to content
Snippets Groups Projects
Commit 7fe833d5 authored by cleemy desu wayo's avatar cleemy desu wayo
Browse files

Modify: revamp the features around post_procs

This commit renews the functionality around post_procs.

This is a breaking change.

With this change, post_procs_cc has been removed. Even in the
case of hookcc, the Proc registered in post_procs will be
executed. This Proc can change the processing depending on
whether it is hook or hookcc by the third argument.

A new feature, output_mode, is added.

By using output_mode, common patterns can be handled without
having to manipulate post_procs directly.

For output_mode, there are built-in presets such as "terminal",
"jsonl", and "yaml".

This commit is related to 20f8d9c1 and d1cec6a4.
parent 6996a06f
Branches main
No related tags found
No related merge requests found
#!/usr/bin/env ruby
#
# ==========================================================================
# Outvoke -- version 0.0.99.20250614
# Outvoke -- version 0.0.99.20250706
#
# written by cleemy desu wayo / Licensed under CC0 1.0
#
......@@ -31,7 +31,7 @@ class Outvoke
@version = Struct.new(:branch, :body).new(
'0.1',
'0.0.99.20250614'
'0.0.99.20250706'
)
@ds = Hash.new
......@@ -303,7 +303,11 @@ class Outvoke
# post process
ds.post_procs.each do |proc|
if proc.arity == 2
proc.call(event, hook_result_for_post_process)
elsif proc.arity == 3
proc.call(event, hook_result_for_post_process, event.hookby)
end
end
end
......@@ -371,8 +375,13 @@ module OutvokeDSL
Thread.current["hook_result_for_post_process"] = Thread.current["hookcc_result"]
end
@ds.post_procs_cc.each do |proc|
# post process
@ds.post_procs.each do |proc|
if proc.arity == 2
proc.call(e, Thread.current["hook_result_for_post_process"])
elsif proc.arity == 3
proc.call(e, Thread.current["hook_result_for_post_process"], hookby)
end
end
end
......@@ -464,7 +473,7 @@ end
class OutvokeDataSource
attr_accessor :label, :status, :log_lines, :mutex_lo, :is_first_time_log_check,
:pre_procs, :post_procs, :post_procs_cc
:pre_procs, :post_procs
attr_reader :lo_data
......@@ -490,30 +499,58 @@ class OutvokeDataSource
# (Procs to process the value returned by Proc registered by hook)
#
@post_procs = []
@post_procs << ->(e, hook_result) {
return unless hook_result
@preset_post_procs = Hash.new
@preset_post_procs["terminal"] = ->(e, hook_result, hookby) {
return hook_result unless hook_result
if e.status.quiet_mode
puts hook_result.to_s
else
puts "[#{e.time}] #{hook_result}".gsub("\n", " ")
time_str = ""
if hookby == "hook"
time_str = e.time.to_s
elsif hookby == "hookcc"
time_str = Time.now.to_s
end
puts "[#{time_str}] #{hook_result}".gsub("\n", " ")
end
hook_result
}
#
# Proc objects for post process
# (Procs to process the value returned by Proc registered by hook)
#
@post_procs_cc = []
@post_procs_cc << ->(e, hook_result) {
return unless hook_result
@preset_post_procs["jsonl"] = ->(e, hook_result, hookby) {
return hook_result unless hook_result
if e.status.quiet_mode
puts hook_result.to_s
else
puts "[#{Time.now}] #{hook_result}".gsub("\n", " ")
if hook_result.is_a?(Hash)
time_str = ""
if hookby == "hook"
time_str = e.time.to_s
elsif hookby == "hookcc"
time_str = Time.now.to_s
end
hook_result["time"] = time_str
end
puts hook_result.to_json
hook_result
}
@preset_post_procs["yaml"] = ->(e, hook_result, hookby) {
return hook_result unless hook_result
if hook_result.is_a?(Hash)
time_str = ""
if hookby == "hook"
time_str = e.time.to_s
elsif hookby == "hookcc"
time_str = Time.now.to_s
end
hook_result["time"] = time_str
end
puts hook_result.to_yaml
hook_result
}
@post_procs << @preset_post_procs["terminal"]
end
def enabled?
......@@ -535,6 +572,21 @@ class OutvokeDataSource
new_msgid
end
def output_mode
@status.output_mode
end
def output_mode=(mode)
@status.output_mode = mode
@post_procs = []
if @preset_post_procs[mode].respond_to?(:call)
@post_procs << @preset_post_procs[mode]
else
raise "ERROR: valid preset_post_proc \"#{mode}\" not found"
end
end
def each_event
raise NotImplementedError
end
......@@ -561,13 +613,16 @@ end
#
# (specifications are subject to sudden change without notice)
#
StructDSStdinStatus = Struct.new(:now, :quiet_mode, :last_time)
StructDSStdinStatus = Struct.new(:now, :quiet_mode, :output_mode, :last_time)
class OutvokeDataSourceStdin < OutvokeDataSource
def initialize(outvoke, label)
super
@status = StructDSStdinStatus.new
@status.last_time = Time.now.floor
self.output_mode = "terminal"
@stdin = $stdin.each_line
@stdin_new_lines = []
......@@ -650,7 +705,7 @@ end
#
# (specifications are subject to sudden change without notice)
#
StructDSAuxinStatus = Struct.new(:now, :quiet_mode, :last_time)
StructDSAuxinStatus = Struct.new(:now, :quiet_mode, :output_mode, :last_time)
class OutvokeDataSourceAuxinFifo < OutvokeDataSource
def initialize(outvoke, label)
......@@ -658,6 +713,8 @@ class OutvokeDataSourceAuxinFifo < OutvokeDataSource
@status = StructDSAuxinStatus.new
@status.last_time = Time.now.floor
self.output_mode = "terminal"
@new_lines = []
@mutex_new_lines = Mutex.new
......@@ -762,7 +819,7 @@ end
#
# (specifications are subject to sudden change without notice)
#
StructDSLoopbackStatus = Struct.new(:now, :quiet_mode, :last_time)
StructDSLoopbackStatus = Struct.new(:now, :quiet_mode, :output_mode, :last_time)
class OutvokeDataSourceLoopback < OutvokeDataSource
......@@ -770,6 +827,8 @@ class OutvokeDataSourceLoopback < OutvokeDataSource
super
@status = StructDSLoopbackStatus.new
@status.last_time = Time.now.floor
self.output_mode = "terminal"
end
def each_event
......@@ -810,7 +869,7 @@ end
#
# (specifications are subject to sudden change without notice)
#
StructDSWebStatus = Struct.new(:now, :quiet_mode, :last_time, :ds_label)
StructDSWebStatus = Struct.new(:now, :quiet_mode, :output_mode, :last_time, :ds_label)
class OutvokeEventWeb < OutvokeEvent
attr_accessor :req, :get, :post, :res
......@@ -825,6 +884,12 @@ class OutvokeDataSourceWeb < OutvokeDataSource
@status = StructDSWebStatus.new
@status.last_time = Time.now.floor
@preset_post_procs["web"] = ->(e, hook_result) {
register_first_res(e.msgid, hook_result)
}
self.output_mode = "web"
@websrv = nil
@port = 8080
@document_root = nil
......@@ -833,11 +898,6 @@ class OutvokeDataSourceWeb < OutvokeDataSource
@first_res_list = Hash.new
@mutex_first_res = Mutex.new
@post_procs = []
@post_procs << ->(e, hook_result) {
register_first_res(e.msgid, hook_result)
}
end
# TODO
......@@ -965,7 +1025,7 @@ class OutvokeDataSourceWeb < OutvokeDataSource
end
end
StructDSVrchatStatus = Struct.new(:now, :quiet_mode, :logfile, :file_size,
StructDSVrchatStatus = Struct.new(:now, :quiet_mode, :output_mode, :logfile, :file_size,
:last_joined_time, :elapsed, :count, :lineno)
class OutvokeEventVrchat < OutvokeEvent
......@@ -985,6 +1045,8 @@ class OutvokeDataSourceVrchat < OutvokeDataSource
@status.lineno = 0
@status.last_joined_time = Time.now
self.output_mode = "terminal"
@is_first_time_log_check = true
@log_dir = ''
......@@ -1103,7 +1165,7 @@ class OutvokeDataSourceVrchat < OutvokeDataSource
end
end
StructDSEverySecStatus = Struct.new(:now, :quiet_mode, :last_time)
StructDSEverySecStatus = Struct.new(:now, :quiet_mode, :output_mode, :last_time)
class OutvokeDataSourceEverySec < OutvokeDataSource
......@@ -1111,6 +1173,8 @@ class OutvokeDataSourceEverySec < OutvokeDataSource
super
@status = StructDSEverySecStatus.new
@status.last_time = Time.now.floor
self.output_mode = "terminal"
end
def each_event
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment