From 3b37cb66fd71ece00cb38734c3f42074c35e1f8f Mon Sep 17 00:00:00 2001 From: Petr Schmied Date: Sat, 12 Sep 2015 16:43:18 +0200 Subject: [PATCH] Main file structure update, no global options --- lib/budik.rb | 130 +++++++++++++++++---------------- lib/budik/command.rb | 5 +- lib/budik/config.rb | 21 ++---- lib/budik/config/options.yml | 5 +- spec/lib/budik/config_spec.rb | 18 +---- spec/lib/budik/devices_spec.rb | 1 - spec/lib/budik/rng_spec.rb | 2 - spec/lib/budik/sources_spec.rb | 1 - 8 files changed, 78 insertions(+), 105 deletions(-) diff --git a/lib/budik.rb b/lib/budik.rb index 9bdcd17..d15ffaa 100644 --- a/lib/budik.rb +++ b/lib/budik.rb @@ -1,7 +1,7 @@ #!/usr/bin/env ruby #require 'awesome_print' -require 'commander/import' +require 'commander' require 'fileutils' #require 'json' require 'net/http' @@ -24,77 +24,81 @@ require './lib/budik/version' module Budik - program :name, 'Budík' - program :version, '0.0.1' - program :description, 'Alarm clock which randomly plays a song or a video from YouTube or your local collection.' + class Budik + include Commander::Methods - global_option('-L', '--language [string]', 'Specify custom path to your language file (default: "./config/lang.yml").') {} - global_option('-O', '--options [string]', 'Specify custom path to your options file (default: "./config/options.yml").') {} - global_option('-S', '--sources [string]', 'Specify custom path to your media sources file (default: "./config/sources.yml").') {} + def run + program :name, 'Budík' + program :version, '0.0.1' + program :description, 'Alarm clock which randomly plays a song or a video from YouTube or your local collection.' - default_command :run + default_command :run - command :config do |c| - c.syntax = 'Budík.rb config [options]' - c.summary = 'Edit program configuration.' - c.description = 'Open options file in your default editor.' - c.option '-r', '--reset [string]', String, 'Resets configuration file to default values.' - c.action do |_args, opts| - command_config(_args, opts) - end - end + command :config do |c| + c.syntax = 'Budík.rb config [options]' + c.summary = 'Edit program configuration.' + c.description = 'Open options file in your default editor.' + c.option '-r', '--reset [string]', String, 'Resets configuration file to default values.' + c.action do |_args, opts| + command_config(_args, opts) + end + end - command :run do |c| - c.syntax = 'Budík.rb run [options]' - c.summary = 'DEFAULT: Runs alarm.' - c.description = 'Runs alarm with specified options. CLI options > options.yml.' - c.option '-c', '--categories [string]', String, 'Limit selection by categories. Example usage: "cat1.subcat1 cat2.subcat1.subsubcat1 .excludethis.subcat etc."' - c.option '-d', '--download-method [string]', String, 'Override download method set in your active options.' - c.option '-n', '--number [integer]', Integer, 'Override selection using random number generator by specifying fixed number.' - c.option '-p', '--player [string]', String, 'Override which player to use.' - c.option '-r', '--rng [string]', String, 'Override random number generation method specified in your options.yml file. Possible values: "hwrng", "random.org", "rand-hwrng-seed", "rand". Default value: "hwrng".' - c.action do |_args, opts| - command_run(_args, opts) - end - end + command :run do |c| + c.syntax = 'Budík.rb run [options]' + c.summary = 'DEFAULT: Runs alarm.' + c.description = 'Runs alarm with specified options. CLI options > options.yml.' + c.option '-c', '--categories [string]', String, 'Limit selection by categories. Example usage: "cat1.subcat1 cat2.subcat1.subsubcat1 .excludethis.subcat etc."' + c.option '-d', '--download-method [string]', String, 'Override download method set in your active options.' + c.option '-n', '--number [integer]', Integer, 'Override selection using random number generator by specifying fixed number.' + c.option '-p', '--player [string]', String, 'Override which player to use.' + c.option '-r', '--rng [string]', String, 'Override random number generation method specified in your options.yml file. Possible values: "hwrng", "random.org", "rand-hwrng-seed", "rand". Default value: "hwrng".' + c.action do |_args, opts| + command_run(_args, opts) + end + end - command :set do |c| - c.syntax = 'Budík.rb set [options]' - c.summary = 'Set alarm.' - c.description = 'Set alarm using systemd timers, cron or schtasks.' - c.action do |_args, opts| - command_set(_args, opts) - end - end + command :set do |c| + c.syntax = 'Budík.rb set [options]' + c.summary = 'Set alarm.' + c.description = 'Set alarm using systemd timers, cron or schtasks.' + c.action do |_args, opts| + command_set(_args, opts) + end + end - command :sources do |c| - c.syntax = 'Budík.rb sources [options]' - c.summary = 'Manage source(s).' - c.description = 'List or download source(s) or edit your sources file in your default editor (default: edit).' - c.option '-l', '--list [string]', String, 'List sources. Can be restricted by categories. DEFAULT option. Usage: list "all|category.subcategory .exclude.this". Default: list "all"' - c.option '-d', '--download [string]', String, 'Downloads source(s). Usage: download "all|number|{category: category, number: number}|path|name". Default: download "all".' - c.action do |_args, opts| - command_sources(_args, opts) - end - end + command :sources do |c| + c.syntax = 'Budík.rb sources [options]' + c.summary = 'Manage source(s).' + c.description = 'List or download source(s) or edit your sources file in your default editor (default: edit).' + c.option '-l', '--list [string]', String, 'List sources. Can be restricted by categories. DEFAULT option. Usage: list "all|category.subcategory .exclude.this". Default: list "all"' + c.option '-d', '--download [string]', String, 'Downloads source(s). Usage: download "all|number|{category: category, number: number}|path|name". Default: download "all".' + c.action do |_args, opts| + command_sources(_args, opts) + end + end - alias_command :test, :'run', '--trace' + alias_command :test, :'run', '--trace' - command :translate do |c| - c.syntax = 'Budík.rb translate [options]' - c.summary = 'Translate app.' - c.description = 'Opens your language file in your default editor.' - c.action do |_args, opts| - command_translate(_args, opts) - end - end + command :translate do |c| + c.syntax = 'Budík.rb translate [options]' + c.summary = 'Translate app.' + c.description = 'Opens your language file in your default editor.' + c.action do |_args, opts| + command_translate(_args, opts) + end + end - command :unset do |c| - c.syntax = 'Budík.rb unset [options]' - c.summary = 'Unset alarm.' - c.description = 'Unset alarm.' - c.action do |_args, opts| - command_unset(_args, opts) + command :unset do |c| + c.syntax = 'Budík.rb unset [options]' + c.summary = 'Unset alarm.' + c.description = 'Unset alarm.' + c.action do |_args, opts| + command_unset(_args, opts) + end + end end end + + Budik.new.run end diff --git a/lib/budik/command.rb b/lib/budik/command.rb index 7c88f81..132c2f0 100644 --- a/lib/budik/command.rb +++ b/lib/budik/command.rb @@ -4,10 +4,9 @@ def self.command_config(_args, opts) end def self.command_run(_args, opts) - config = Config.instance - config.load(opts) + options = Config.instance.options - sources_path = config.options['sources']['path'] + sources_path = options['sources']['path'] Sources.instance.parse(sources_path, opts.categories) end diff --git a/lib/budik/config.rb b/lib/budik/config.rb index ef9645a..2710e30 100644 --- a/lib/budik/config.rb +++ b/lib/budik/config.rb @@ -3,11 +3,12 @@ class Config include Singleton def initialize - R18n.default_places = './lib/budik/config/lang/' + @options = YAML.load_file('./lib/budik/config/options.yml') + @sources = YAML.load_file(@options['sources']['path']) - @lang = nil - @options = nil - @sources = nil + R18n.default_places = './lib/budik/config/lang/' + R18n.set(@options['lang']) + @lang = R18n.t end attr_accessor :lang @@ -18,18 +19,6 @@ def edit(opts) # TODO end - def load(opts) - locale = opts.has_key?(:language) ? opts[:language] : 'en' - options_path = opts.has_key?(:options) ? opts[:options] : './lib/budik/config/options.yml' - sources_path = opts.has_key?(:sources) ? opts[:sources] : './lib/budik/config/sources.yml' - - R18n.set(locale) - @lang = R18n.t - @options = YAML.load_file(options_path) - @sources = YAML.load_file(sources_path) - - end - def reset # TODO end diff --git a/lib/budik/config/options.yml b/lib/budik/config/options.yml index 4ae911b..13f2e41 100644 --- a/lib/budik/config/options.yml +++ b/lib/budik/config/options.yml @@ -1,5 +1,6 @@ --- os: windows +lang: en player: omxplayer: default_volume: -2100 @@ -21,7 +22,7 @@ rng: source: "" method: random.org random.org: - apikey: + apikey: 791442f3-a099-4264-b459-7a1f1ad0849d sources: categories: all download: @@ -32,7 +33,7 @@ sources: partition: "" sleep: '' unmount: '' - path: sources.yml + path: ./lib/budik/config/sources.yml tv: available: false use_if_no_video: false diff --git a/spec/lib/budik/config_spec.rb b/spec/lib/budik/config_spec.rb index f9fa9c9..cf17bfa 100644 --- a/spec/lib/budik/config_spec.rb +++ b/spec/lib/budik/config_spec.rb @@ -5,30 +5,14 @@ require 'budik/config' -describe Budik::Config, '#load' do +describe Budik::Config, '#initialize' do context 'with default values' do it 'loads configuration files' do - opts = {} config = Budik::Config.instance - config.load(opts) expect(config.lang.lang.class).to eq R18n::TranslatedString expect(config.options).to eq YAML.load_file('./lib/budik/config/options.yml') expect(config.sources).to eq YAML.load_file('./lib/budik/config/sources.yml') end end - - context 'with file overrides' do - it 'loads configuration files' do - opts = { language: 'en', - options: './lib/budik/config/options.yml', - sources: './lib/budik/config/sources.yml' } - config = Budik::Config.instance - config.load(opts) - - expect(config.lang.lang.class).to eq R18n::TranslatedString - expect(config.options).to eq YAML.load_file('./lib/budik/config/options.yml') - expect(config.sources).to eq YAML.load_file('./lib/budik/config/sources.yml') - end - end end diff --git a/spec/lib/budik/devices_spec.rb b/spec/lib/budik/devices_spec.rb index 449170b..6ec3fca 100644 --- a/spec/lib/budik/devices_spec.rb +++ b/spec/lib/budik/devices_spec.rb @@ -3,7 +3,6 @@ describe Budik::Devices, '#initialize' do it 'correctly parses commands' do config = Budik::Config.instance - config.load(Hash.new) storage_options = config.options['sources']['download'] storage_options['device'] = '/dev/sda' storage_options['partition'] = '/dev/sda1' diff --git a/spec/lib/budik/rng_spec.rb b/spec/lib/budik/rng_spec.rb index bbacf91..40d3a84 100644 --- a/spec/lib/budik/rng_spec.rb +++ b/spec/lib/budik/rng_spec.rb @@ -7,7 +7,6 @@ context 'using hwrng' do it 'generates random number' do config = Budik::Config.instance - config.load(Hash.new) config.options['rng']['hwrng']['source'] = '/dev/hwrng' 5.times do @@ -32,7 +31,6 @@ context 'using random.org' do it 'generates random number' do config = Budik::Config.instance - config.load(Hash.new) if config.options['rng']['random.org']['apikey'] num = Budik::Rng.instance.generate(100, 'random.org') diff --git a/spec/lib/budik/sources_spec.rb b/spec/lib/budik/sources_spec.rb index 6627ae5..1021d4a 100644 --- a/spec/lib/budik/sources_spec.rb +++ b/spec/lib/budik/sources_spec.rb @@ -38,7 +38,6 @@ context 'using specified number' do it 'downloads an item' do config = Budik::Config.instance - config.load(Hash.new) config.options['sources']['download']['dir'] = './spec/' sources_example = [