Skip to content

Commit

Permalink
Merge 93f1994 into f34dff3
Browse files Browse the repository at this point in the history
  • Loading branch information
iberianpig committed Sep 29, 2020
2 parents f34dff3 + 93f1994 commit 34c024c
Show file tree
Hide file tree
Showing 22 changed files with 554 additions and 167 deletions.
3 changes: 3 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
inherit_from: .rubocop_todo.yml

AllCops:
TargetRubyVersion: '2.3'

Metrics/ModuleLength:
Exclude:
- "**/*_spec.rb"
Expand Down
12 changes: 12 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,15 @@ source 'https://rubygems.org'

# Specify your gem's dependencies in fusuma.gemspec
gemspec

gem 'bundler'
gem 'coveralls'
gem 'github_changelog_generator', '~> 1.14'
gem 'pry-byebug', '~> 3.4'
gem 'pry-doc'
gem 'pry-inline'
gem 'rake', '~> 13.0'
gem 'reek'
gem 'rspec', '~> 3.0'
gem 'rubocop', '0.81'
gem 'yard'
12 changes: 1 addition & 11 deletions fusuma.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,5 @@ Gem::Specification.new do |spec|
spec.metadata['yard.run'] = 'yri' # use "yard" to build full HTML docs.

spec.required_ruby_version = '>= 2.3' # https://packages.ubuntu.com/search?keywords=ruby&searchon=names&exact=1&suite=all&section=main
spec.add_development_dependency 'bundler'
spec.add_development_dependency 'coveralls'
spec.add_development_dependency 'github_changelog_generator', '~> 1.14'
spec.add_development_dependency 'pry-byebug', '~> 3.4'
spec.add_development_dependency 'pry-doc'
spec.add_development_dependency 'pry-inline'
spec.add_development_dependency 'rake', '~> 13.0'
spec.add_development_dependency 'reek'
spec.add_development_dependency 'rspec', '~> 3.0'
spec.add_development_dependency 'rubocop'
spec.add_development_dependency 'yard'
spec.add_dependency 'posix-spawn'
end
19 changes: 14 additions & 5 deletions lib/fusuma.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,9 @@ def initialize
end

def run
# TODO: run with multi thread
@inputs.first.run do |event|
loop do
event = input
event || next
clear_expired_events
filtered = filter(event) || next
parsed = parse(filtered) || next
Expand All @@ -74,6 +75,10 @@ def run
end
end

def input
Plugin::Inputs::Input.select(@inputs)
end

def filter(event)
event if @filters.any? { |f| f.filter(event) }
end
Expand Down Expand Up @@ -110,11 +115,15 @@ def merge(events)
def execute(event)
return unless event

executor = @executors.find do |e|
e.executable?(event)
l = lambda do
executor = @executors.find { |e| e.executable?(event) }
executor&.execute(event)
end

executor&.execute(event)
l.call ||
Config::Searcher.skip { l.call } ||
Config::Searcher.fallback { l.call } ||
Config::Searcher.skip { Config::Searcher.fallback { l.call } }
end

def clear_expired_events
Expand Down
35 changes: 7 additions & 28 deletions lib/fusuma/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require_relative './multi_logger.rb'
require_relative './config/index.rb'
require_relative './config/searcher.rb'
require_relative './config/yaml_duplication_checker.rb'
require 'singleton'
require 'yaml'
Expand All @@ -27,10 +28,11 @@ def custom_path=(new_path)

attr_reader :keymap
attr_reader :custom_path
attr_reader :searcher

def initialize
@searcher = Searcher.new
@custom_path = nil
@cache = nil
@keymap = nil
end

Expand All @@ -40,7 +42,7 @@ def custom_path=(new_path)
end

def reload
@cache = nil
@searcher = Searcher.new
path = find_filepath
MultiLogger.info "reload config: #{path}"
@keymap = validate(path)
Expand Down Expand Up @@ -68,22 +70,9 @@ def validate(path)
end

# @param index [Index]
def search(index)
cache(index.cache_key) do
index.keys.reduce(keymap) do |location, key|
if location.is_a?(Hash)
begin
if key.skippable
location.fetch(key.symbol, location)
else
location.fetch(key.symbol, nil)
end
end
else
location
end
end
end
# @param location [Hash]
def search(index, location: keymap)
@searcher.search_with_cache(index, location: location)
end

private
Expand Down Expand Up @@ -113,16 +102,6 @@ def expand_config_path(filename)
def expand_default_path(filename)
File.expand_path "../../#{filename}", __FILE__
end

def cache(key)
@cache ||= {}
key = key.join(',') if key.is_a? Array
if @cache.key?(key)
@cache[key]
else
@cache[key] = block_given? ? yield : nil
end
end
end
end

Expand Down
11 changes: 9 additions & 2 deletions lib/fusuma/config/index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,22 @@ def cache_key

# Keys in Index
class Key
def initialize(symbol_word, skippable: false)
def initialize(symbol_word, skippable: false, fallback: nil)
@symbol = begin
symbol_word.to_sym
rescue StandardError
symbol_word
end

@skippable = skippable

@fallback = begin
fallback.to_sym
rescue StandardError
fallback
end
end
attr_reader :symbol, :skippable
attr_reader :symbol, :skippable, :fallback
end
end
end
Expand Down
72 changes: 72 additions & 0 deletions lib/fusuma/config/searcher.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# frozen_string_literal: true

# Index for searching value from config.yml
module Fusuma
class Config
# Search config.yml
class Searcher
def initialize
@cache
end

# @param index [Index]
# @param location [Hash]
def search(index, location:)
key = index.keys.first
return location if key.nil?

return nil if location.nil?

return nil unless location.is_a?(Hash)

next_index = Index.new(index.keys[1..-1])
next_locations = [
location[key.symbol],
Searcher.use_fallback && key.fallback && location[key.fallback],
Searcher.use_skip && key.skippable && location
].compact

value = nil
next_locations.find do |next_location|
value = search(next_index, location: next_location)
end
value
end

def search_with_cache(index, location:)
cache([index.cache_key, Searcher.use_skip, Searcher.use_fallback]) do
search(index, location: location)
end
end

def cache(key)
@cache ||= {}
key = key.join(',') if key.is_a? Array
if @cache.key?(key)
@cache[key]
else
@cache[key] = block_given? ? yield : nil
end
end

class << self
attr_reader :use_fallback, :use_skip

# switch context for fallback
def fallback(&block)
@use_fallback = true
result = block.call
@use_fallback = false
result
end

def skip(&block)
@use_skip = true
result = block.call
@use_skip = false
result
end
end
end
end
end
32 changes: 13 additions & 19 deletions lib/fusuma/libinput_command.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# frozen_string_literal: true

require 'open3'
require 'timeout'
require 'posix/spawn'

module Fusuma
# Execute libinput command
Expand Down Expand Up @@ -34,26 +33,21 @@ def version
def list_devices
cmd = list_devices_command
MultiLogger.debug(list_devices: cmd)
Open3.popen3(cmd) do |_i, o, _e, _w|
o.each { |line| yield(line) }
end
p, i, o, e = POSIX::Spawn.popen4(cmd)
i.close
o.each { |line| yield(line) }
ensure
[i, o, e].each { |io| io.close unless io.closed? }
Process.waitpid(p)
end

# @yieldparam [String] gives a line in libinput debug-events output to the block
# @return [String] return a latest line libinput debug-events
def debug_events
MultiLogger.debug(debug_events: debug_events_with_options)
Open3.popen3(debug_events_with_options) do |_i, o, _e, _w|
loop do
line = begin
Timeout.timeout(wait_time) do
o.readline.chomp
end
rescue Timeout::Error
TIMEOUT_MESSAGE
end
yield(line)
end
end
@debug_events ||= begin
_p, i, o, _e = POSIX::Spawn.popen4(debug_events_with_options)
i.close
o
end
end

# @return [String] command
Expand Down
46 changes: 46 additions & 0 deletions lib/fusuma/plugin/buffers/timer_buffer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# frozen_string_literal: true

require_relative './buffer.rb'

module Fusuma
module Plugin
module Buffers
# manage events and generate command
class TimerBuffer < Buffer
DEFAULT_SOURCE = 'timer_input'
DEFAULT_SECONDS_TO_KEEP = 60

def config_param_types
{
'source': [String],
'seconds_to_keep': [Float, Integer]
}
end

# @param event [Event]
# @return [Buffer, NilClass]
def buffer(event)
return if event&.tag != source

@events.push(event)
self
end

def clear_expired(current_time: Time.now)
@seconds_to_keep ||= (config_params(:seconds_to_keep) || DEFAULT_SECONDS_TO_KEEP)
@events.each do |e|
break if current_time - e.time < @seconds_to_keep

MultiLogger.debug("#{self.class.name}##{__method__}")

@events.delete(e)
end
end

def empty?
@events.empty?
end
end
end
end
end
6 changes: 2 additions & 4 deletions lib/fusuma/plugin/executors/command_executor.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# frozen_string_literal: true

require 'posix/spawn'
require_relative './executor.rb'

module Fusuma
Expand All @@ -12,11 +13,8 @@ def execute(event)
break unless command

MultiLogger.info(command: command)
pid = fork do
Process.daemon(true)
exec(command.to_s)
end

pid = POSIX::Spawn.spawn(command.to_s)
Process.detach(pid)
end
end
Expand Down

0 comments on commit 34c024c

Please sign in to comment.