diff options
author | Chris Schlaeger <chris@linux.com> | 2014-06-21 23:02:49 +0200 |
---|---|---|
committer | Chris Schlaeger <chris@linux.com> | 2014-06-21 23:02:49 +0200 |
commit | cc72a9807699b95363d0ae58aa5237d982946470 (patch) | |
tree | e120b723992e473de620f29d6d7ba2fcd235e505 | |
download | postrunner-cc72a9807699b95363d0ae58aa5237d982946470.zip |
Adding dump filter
-rw-r--r-- | .gitignore | 22 | ||||
-rw-r--r-- | Gemfile | 4 | ||||
-rw-r--r-- | LICENSE.txt | 22 | ||||
-rw-r--r-- | README.md | 29 | ||||
-rw-r--r-- | Rakefile | 2 | ||||
-rw-r--r-- | lib/postrunner.rb | 11 | ||||
-rw-r--r-- | lib/postrunner/ActivitiesDB.rb | 123 | ||||
-rw-r--r-- | lib/postrunner/Activity.rb | 17 | ||||
-rw-r--r-- | lib/postrunner/Main.rb | 116 | ||||
-rw-r--r-- | lib/postrunner/version.rb | 3 | ||||
-rw-r--r-- | postrunner.gemspec | 24 |
11 files changed, 373 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..31cafb5 --- /dev/null +++ b/.gitignore @@ -0,0 +1,22 @@ +*.gem +*.rbc +.bundle +.config +.yardoc +Gemfile.lock +InstalledFiles +_yardoc +coverage +doc/ +lib/bundler/man +pkg +rdoc +spec/reports +test/tmp +test/version_tmp +tmp +*.bundle +*.so +*.o +*.a +mkmf.log @@ -0,0 +1,4 @@ +source 'https://rubygems.org' + +# Specify your gem's dependencies in postrunner.gemspec +gemspec diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..5f512c0 --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,22 @@ +Copyright (c) 2014 Chris Schlaeger + +MIT License + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..4d2d9f4 --- /dev/null +++ b/README.md @@ -0,0 +1,29 @@ +# Postrunner + +TODO: Write a gem description + +## Installation + +Add this line to your application's Gemfile: + + gem 'postrunner' + +And then execute: + + $ bundle + +Or install it yourself as: + + $ gem install postrunner + +## Usage + +TODO: Write usage instructions here + +## Contributing + +1. Fork it ( https://github.com/[my-github-username]/postrunner/fork ) +2. Create your feature branch (`git checkout -b my-new-feature`) +3. Commit your changes (`git commit -am 'Add some feature'`) +4. Push to the branch (`git push origin my-new-feature`) +5. Create a new Pull Request diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..809eb56 --- /dev/null +++ b/Rakefile @@ -0,0 +1,2 @@ +require "bundler/gem_tasks" + diff --git a/lib/postrunner.rb b/lib/postrunner.rb new file mode 100644 index 0000000..681c583 --- /dev/null +++ b/lib/postrunner.rb @@ -0,0 +1,11 @@ +$:.unshift(File.join(File.dirname(__FILE__), '..', '..', 'fit4ruby', 'lib')) +$:.unshift(File.dirname(__FILE__)) + +require 'postrunner/version' +require 'postrunner/Main' + +module PostRunner + + Main.new(ARGV) + +end diff --git a/lib/postrunner/ActivitiesDB.rb b/lib/postrunner/ActivitiesDB.rb new file mode 100644 index 0000000..df0c105 --- /dev/null +++ b/lib/postrunner/ActivitiesDB.rb @@ -0,0 +1,123 @@ +require 'fileutils' +require 'yaml' + +require 'fit4ruby' +require 'postrunner/Activity' + +module PostRunner + + class ActivitiesDB + + def initialize(db_dir) + @db_dir = db_dir + @fit_dir = File.join(@db_dir, 'fit') + @archive_file = File.join(@db_dir, 'archive.yml') + + if Dir.exists?(@db_dir) + begin + if File.exists?(@archive_file) + @activities = YAML.load_file(@archive_file) + else + @activities = [] + end + rescue + Log.fatal "Cannot load archive file #{@archive_file}: #{$!}" + end + else + create_directories + @activities = [] + end + end + + def add(fit_file) + base_fit_file = File.basename(fit_file) + if @activities.find { |a| a.fit_file == base_fit_file } + Log.warn "Activity #{fit_file} is already included in the archive" + return false + end + + begin + fit_activity = Fit4Ruby.read(fit_file) + rescue + Log.error "Cannot read #{fit_file}: #{$!}" + return false + end + + begin + FileUtils.cp(fit_file, @fit_dir) + rescue + Log.fatal "Cannot copy #{fit_file} into #{@fit_dir}: #{$!}" + end + + @activities << Activity.new(base_fit_file, fit_activity) + sync + Log.info "#{fit_file} successfully added to archive" + + true + end + + def map_to_files(query) + case query + when /\A-?\d+$\z/ + index = query.to_i + # The UI counts the activities from 1 to N. Ruby counts from 0 - + # (N-1). + index -= 1 if index > 0 + if (a = @activities[index]) + return [ File.join(@fit_dir, a.fit_file) ] + end + when /\A-?\d+--?\d+\z/ + idxs = query.match(/(?<sidx>-?\d+)-(?<eidx>-?[0-9]+)/) + sidx = idxs['sidx'].to_i + eidx = idxs['eidx'].to_i + # The UI counts the activities from 1 to N. Ruby counts from 0 - + # (N-1). + sidx -= 1 if sidx > 0 + eidx -= 1 if eidx > 0 + puts "iv: #{sidx} - #{eidx}" + unless (as = @activities[sidx..eidx]).empty? + files = [] + as.each do |a| + files << File.join(@fit_dir, a.fit_file) + end + return files + end + else + Log.error "Invalid activity query: #{query}" + end + + [] + end + + def list + i = 0 + @activities.each do |a| + i += 1 + puts "#{"%4d" % i} #{"%12s" % a.fit_file} #{a.start_time}" + end + end + + private + + def sync + File.open(@archive_file, 'w') { |f| f.write(@activities.to_yaml) } + end + + def create_directories + Log.info "Creating data directory #{@db_dir}" + begin + Dir.mkdir(@db_dir) + rescue + Log.fatal "Cannot create data directory #{@db_dir}: #{$!}" + end + begin + Dir.mkdir(@fit_dir) + rescue + Log.fatal "Cannot create fit directory #{@fit_dir}: #{$!}" + end + end + + end + +end + diff --git a/lib/postrunner/Activity.rb b/lib/postrunner/Activity.rb new file mode 100644 index 0000000..96c9887 --- /dev/null +++ b/lib/postrunner/Activity.rb @@ -0,0 +1,17 @@ +require 'fit4ruby' + +module PostRunner + + class Activity + + attr_reader :fit_file, :start_time + + def initialize(fit_file, fit_activity) + @fit_file = fit_file + @start_time = fit_activity.start_time + end + + end + +end + diff --git a/lib/postrunner/Main.rb b/lib/postrunner/Main.rb new file mode 100644 index 0000000..26e1005 --- /dev/null +++ b/lib/postrunner/Main.rb @@ -0,0 +1,116 @@ +require 'optparse' +require 'logger' +require 'fit4ruby' +require 'postrunner/ActivitiesDB' + +module PostRunner + + Log = Fit4Ruby::ILogger.new(STDOUT) + + class Main + + def initialize(args) + @filter = nil + @activities = ActivitiesDB.new(File.join(ENV['HOME'], '.postrunner')) + + execute_command(parse_options(args)) + end + + private + + def parse_options(args) + parser = OptionParser.new do |opts| + opts.banner = "Usage postrunner <command> [options]" + opts.separator "" + opts.separator "Options for the 'dump' command:" + + opts.on('--filter-msg N', Integer, + 'Only dump messages of type number N') do |n| + @filter = Fit4Ruby::FitFilter.new unless @filter + @filter.record_numbers = [] unless @filter.record_numbers + @filter.record_numbers << n.to_i + end + opts.on('--filter-msg-idx N', Integer, + 'Only dump the N-th message of the specified types') do |n| + @filter = Fit4Ruby::FitFilter.new unless @filter + @filter.record_indexes = [] unless @filter.record_indexes + @filter.record_indexes << n.to_i + end + opts.on('--filter-field name', String, + 'Only dump the field \'name\' of the selected messages') do |n| + @filter = Fit4Ruby::FitFilter.new unless @filter + @filter.field_names = [] unless @filter.field_names + @filter.field_names << n + end + end + + parser.parse!(args) + end + + def execute_command(args) + case args.shift + when 'check' + process_files(args, :check) + when 'dump' + @filter = Fit4Ruby::FitFilter.new unless @filter + process_files_or_activities(args, :dump) + when 'import' + process_files(args, :import) + when 'list' + @activities.list + when 'summary' + process_files(args, :summary) + else + Log.fatal("No command provided") + end + end + + def process_files_or_activities(files_or_activities, command) + files_or_activities.each do |foa| + if foa[0] == ':' + files = @activities.map_to_files(foa[1..-1]) + if files.empty? + Log.warn "No matching activities found for '#{foa}'" + return + end + + process_files(files, command) + else + process_files(foa, command) + end + end + end + + def process_files(files_or_dirs, command) + if files_or_dirs.empty? + Log.fatal("You must provide at least one .FIT file name") + end + + files_or_dirs.each do |fod| + if File.directory?(fod) + Dir.glob(File.join(fod, '*.FIT')).each do |file| + process_file(file, command) + end + else + process_file(fod, command) + end + end + end + + def process_file(file, command) + if command == :import + @activities.add(file) + else + begin + activity = Fit4Ruby::read(file, @filter) + #rescue + # Log.error("File '#{file}' is corrupted!: #{$!}") + end + puts activity.to_s if command == :summary + end + end + + end + +end + diff --git a/lib/postrunner/version.rb b/lib/postrunner/version.rb new file mode 100644 index 0000000..3861ec6 --- /dev/null +++ b/lib/postrunner/version.rb @@ -0,0 +1,3 @@ +module PostRunner + VERSION = "0.0.1" +end diff --git a/postrunner.gemspec b/postrunner.gemspec new file mode 100644 index 0000000..cc6c537 --- /dev/null +++ b/postrunner.gemspec @@ -0,0 +1,24 @@ +# coding: utf-8 +lib = File.expand_path('../lib', __FILE__) +$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) +require 'postrunner/version' + +Gem::Specification.new do |spec| + spec.name = "postrunner" + spec.version = Postrunner::VERSION + spec.authors = ["Chris Schlaeger"] + spec.email = ["chris@linux.com"] + spec.summary = %q{Application to manage and analyze Garmin FIT files.} + spec.description = %q{This application will allow you to manage and analyze .FIT files as generated by Garmin GPS devices. The application was developed for the Garmin Forerunner 620. Other devices may or may not work. Only devices that expose themselves as USB Mass Storage devices are supported.} + spec.homepage = "TBD" + spec.license = "MIT" + + spec.files = `git ls-files -z`.split("\x0") + spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) } + spec.test_files = spec.files.grep(%r{^(test|spec|features)/}) + spec.require_paths = ["lib"] + + spec.add_development_dependency "bundler", "~> 1.6" + spec.add_development_dependency "rake" + spec.add_development_dependency "fit4ruby" +end |