Skip to content

Commit

Permalink
Main file structure update, no global options
Browse files Browse the repository at this point in the history
  • Loading branch information
JBlackN committed Sep 12, 2015
1 parent ba5d5aa commit 0b9374c
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 104 deletions.
130 changes: 67 additions & 63 deletions lib/budik.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env ruby

#require 'awesome_print'
require 'commander/import'
require 'commander'
require 'fileutils'
#require 'json'
require 'net/http'
Expand All @@ -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
5 changes: 2 additions & 3 deletions lib/budik/command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
21 changes: 5 additions & 16 deletions lib/budik/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion lib/budik/config/options.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
os: windows
lang: en
player:
omxplayer:
default_volume: -2100
Expand Down Expand Up @@ -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
Expand Down
18 changes: 1 addition & 17 deletions spec/lib/budik/config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 0 additions & 1 deletion spec/lib/budik/devices_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
2 changes: 0 additions & 2 deletions spec/lib/budik/rng_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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')
Expand Down
1 change: 0 additions & 1 deletion spec/lib/budik/sources_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down

0 comments on commit 0b9374c

Please sign in to comment.