ptimelog 0.10.0 → 0.13.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,88 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'shellwords'
4
+
5
+ module Ptimelog
6
+ module Command
7
+ # show entries of one day or all of them
8
+ class Confirm < CmdParse::Command
9
+ attr_reader :entries
10
+
11
+ def initialize
12
+ super('confirm', takes_commands: false)
13
+ @durations = false
14
+ @dry_run = false
15
+ @entries = {}
16
+ @tags = []
17
+ @config = Configuration.instance
18
+
19
+ options.on('-d', '--durations', 'Summarize durations by ticket') { @durations = true }
20
+ options.on('-t', '--tag TAG', 'Only entries with this tag, repeat to narrow down') { |tag| @tags << tag }
21
+ options.on('-n', '--dry-run', 'Do not execute, just dry-run') { @dry_run = true }
22
+ end
23
+
24
+ def execute(maybe_named_day)
25
+ @day = Ptimelog::NamedDate.new.named_date(maybe_named_day)
26
+ @entries = Ptimelog::DataSource.new(@config, @day).entries
27
+
28
+ @entries.each do |date, list|
29
+ valids = selected_entries(list)
30
+ next if valids.empty? # an empty or filtered-out day is no day to confirm
31
+
32
+ output(date, valids, Ptimelog::Entry.format_duration(valids.sum(&:duration)))
33
+
34
+ upload(date) if ask('Upload this day? [Yn]', 'y').downcase == 'y'
35
+ end
36
+ end
37
+
38
+ private
39
+
40
+ def selected_entries(list)
41
+ Ptimelog::Joiner.new(compact_on_ticket_only: @durations).join_similar(
42
+ list.select { |entry| entry.valid? && entry.tagged_with?(@tags) }
43
+ .sort_by { |entry| entry.ticket.to_s }
44
+ ).sort_by(&:start_time)
45
+ end
46
+
47
+ def output(date, entries, total_duration)
48
+ puts date,
49
+ '----------'
50
+
51
+ entries.each do |entry|
52
+ puts entry
53
+ end
54
+
55
+ puts '----------',
56
+ "Total work done: #{total_duration} hours",
57
+ '----------------------------',
58
+ nil
59
+ end
60
+
61
+ def ask(question, default = nil)
62
+ puts question
63
+
64
+ answer = $stdin.gets.chomp.strip
65
+
66
+ if answer.empty?
67
+ raise 'No answer and no default given' if default.nil?
68
+
69
+ default
70
+ else
71
+ answer
72
+ end
73
+ end
74
+
75
+ def upload(date)
76
+ # the tags are escaped, as they come from the command-line and go back to a shell
77
+ options = [('-d' if @durations), *@tags.map { |tag| "-t #{tag.shellescape}" }].compact.join(' ')
78
+ cmd = "#{$PROGRAM_NAME} upload #{date} #{options}"
79
+
80
+ if @dry_run
81
+ puts cmd
82
+ else
83
+ system(cmd)
84
+ end
85
+ end
86
+ end
87
+ end
88
+ end
@@ -4,20 +4,46 @@ module Ptimelog
4
4
  module Command
5
5
  # edit one file. without argument, it will edit the timelog, otherwise a
6
6
  # parser-script is loaded
7
- class Edit < Base
8
- def initialize(file)
9
- super()
7
+ class Edit < CmdParse::Command
8
+ def initialize
9
+ super('edit', takes_commands: false)
10
10
 
11
+ options.on('--init', 'Add a starting template') do
12
+ @init_content = <<~BASH
13
+ #!/usr/bin/env bash
14
+ set -euo pipefail
15
+ IFS=$'\n\t'
16
+
17
+ echo "0" # this is the ID
18
+ echo "false" # true/false for billable
19
+ echo "true" # true/false for publishable
20
+ BASH
21
+ end
22
+
23
+ @config = Configuration.instance
11
24
  @scripts = Script.new(@config[:dir])
12
- @file = file
13
25
  end
14
26
 
15
- def run
16
- launch_editor(find_file(@file))
27
+ def execute(file = nil)
28
+ target = find_file(file)
29
+
30
+ return puts(no_file_hint) if target.is_a?(NullPathname)
31
+
32
+ launch_editor(target)
17
33
  end
18
34
 
19
35
  private
20
36
 
37
+ def no_file_hint
38
+ <<~MSG
39
+ No file to edit.
40
+
41
+ An entry can come from any note of the vault, so the Obsidian-Datasource
42
+ has no single file. Pass the name of an inferer-script instead,
43
+ e.g. 'ptimelog edit hit'.
44
+ MSG
45
+ end
46
+
21
47
  def launch_editor(file)
22
48
  editor = `which $EDITOR`.chomp
23
49
 
@@ -28,6 +54,7 @@ module Ptimelog
28
54
  %i[
29
55
  timelog
30
56
  existing_inferer
57
+ prefilled_inferer
31
58
  empty_inferer
32
59
  ].each do |file_lookup|
33
60
  valid, filename = send(file_lookup, requested_file)
@@ -37,7 +64,10 @@ module Ptimelog
37
64
  end
38
65
 
39
66
  def timelog(file)
40
- [file.nil?, Timelog.timelog_txt]
67
+ [
68
+ file.nil?,
69
+ Ptimelog::DataSource.new(@config, Ptimelog::NamedDate.new.named_date(:today)).file,
70
+ ]
41
71
  end
42
72
 
43
73
  def existing_inferer(file)
@@ -46,6 +76,16 @@ module Ptimelog
46
76
  [fn.exist?, fn]
47
77
  end
48
78
 
79
+ def prefilled_inferer(file)
80
+ fn = @scripts.inferer(file)
81
+
82
+ return [false, fn] unless fn.empty? && @init_content
83
+
84
+ fn.write(@init_content)
85
+
86
+ [fn.exist?, fn]
87
+ end
88
+
49
89
  def empty_inferer(file)
50
90
  [true, @scripts.inferer(file)]
51
91
  end
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ptimelog
4
+ module Command
5
+ # Extract and show entries from a Obsidian+DayPlanner list
6
+ class Info < CmdParse::Command
7
+ def initialize
8
+ super('info', takes_commands: false)
9
+ @configuration = Ptimelog::Configuration.instance
10
+ end
11
+
12
+ def execute
13
+ puts <<~TXT
14
+ Configuration
15
+ -------------
16
+ base_url: #{@configuration['base_url']}
17
+ rounding: #{@configuration['rounding']}
18
+ dir: #{@configuration['dir']}
19
+
20
+ #{obsidian_config}#{timelog_config}
21
+ TXT
22
+ end
23
+
24
+ def obsidian_config
25
+ return nil unless @configuration['obsidian']
26
+
27
+ <<~TXT
28
+ Datasource: Obsidian (Operon)
29
+ -----------------------------
30
+ vault: #{@configuration['obsidian_vault']}
31
+ custom keys:
32
+ ticket: #{Ptimelog::Operon.custom_keys['ticket']}
33
+ inferer: #{Ptimelog::Operon.custom_keys['inferer']}
34
+ TXT
35
+ end
36
+
37
+ def timelog_config
38
+ return nil unless @configuration['timelog']
39
+
40
+ <<~TXT
41
+ Datasource: Timelog
42
+ -------------------
43
+ file: #{@configuration['timelog_txt']}
44
+ TXT
45
+ end
46
+ end
47
+ end
48
+ end
@@ -3,47 +3,67 @@
3
3
  module Ptimelog
4
4
  module Command
5
5
  # show entries of one day or all of them
6
- class Show < Base
7
- def initialize(*args)
8
- @durations = Hash.new(0)
6
+ class Show < CmdParse::Command
7
+ attr_reader :entries
9
8
 
10
- super
11
- end
9
+ def initialize
10
+ super('show', takes_commands: false)
11
+ @debug = false
12
+ @durations = false
13
+ @entries = {}
14
+ @tags = []
15
+ @config = Configuration.instance
12
16
 
13
- def needs_entries?
14
- true
17
+ options.on('-d', '--durations', 'Summarize durations by ticket') { @durations = true }
18
+ options.on('-t', '--tag TAG', 'Only entries with this tag, repeat to narrow down') { |tag| @tags << tag }
19
+ options.on('--debug', 'Show debugging output') { @debug = true }
15
20
  end
16
21
 
17
- def run
22
+ def execute(maybe_named_day) # rubocop:disable Metrics/AbcSize
23
+ debug("Requested Day: #{maybe_named_day}")
24
+ @day = Ptimelog::NamedDate.new.named_date(maybe_named_day)
25
+ debug("Resolved Day: #{@day}")
26
+ data_source = Ptimelog::DataSource.new(@config, @day)
27
+ debug("Data Source: #{data_source.backend}")
28
+ @entries = data_source.entries
29
+ debug("Found Entries: #{@entries.values.sum(&:size)}")
30
+
18
31
  @entries.each do |date, list|
19
- puts date,
20
- '----------'
21
-
22
- next if list.empty?
23
-
24
- list.each do |entry|
25
- puts entry
26
- end
27
- puts '----------',
28
- "Total work done: #{duration(date)} hours",
29
- '----------------------------',
30
- nil
32
+ valids = selected_entries(list)
33
+ next if valids.empty? # an empty or filtered-out day is no day to report
34
+
35
+ output(date, valids, Ptimelog::Entry.format_duration(valids.sum(&:duration)))
31
36
  end
32
37
  end
33
38
 
34
- def entries=(entries)
35
- entries.each do |date, list|
36
- @entries[date] = []
39
+ private
40
+
41
+ def selected_entries(list)
42
+ Ptimelog::Joiner.new(compact_on_ticket_only: @durations).join_similar(
43
+ list.select { |entry| entry.valid? && entry.tagged_with?(@tags) }
44
+ .sort_by { |entry| entry.ticket.to_s }
45
+ ).sort_by(&:start_time)
46
+ end
47
+
48
+ def output(date, entries, total_duration)
49
+ puts date,
50
+ '----------'
37
51
 
38
- list.each do |entry|
39
- @durations[date] += entry.duration
40
- @entries[date] << entry.to_s
41
- end
52
+ entries.each do |entry|
53
+ debug(entry.instance_variable_get(:@inferer_cmd))
54
+ puts entry
42
55
  end
56
+
57
+ puts '----------',
58
+ "Total work done: #{total_duration} hours",
59
+ '----------------------------',
60
+ nil
43
61
  end
44
62
 
45
- def duration(date)
46
- Time.at(@durations[date]).utc.strftime('%H:%M')
63
+ def debug(msg)
64
+ return unless @debug
65
+
66
+ warn msg
47
67
  end
48
68
  end
49
69
  end
@@ -0,0 +1,65 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ptimelog
4
+ module Command
5
+ # Report the time of a day or a month by ticket, for invoicing and cross-checking
6
+ class Summarize < CmdParse::Command
7
+ def initialize
8
+ super('summarize', takes_commands: false)
9
+
10
+ @tags = []
11
+ @config = Configuration.instance
12
+
13
+ options.on('-t', '--tag TAG', 'Only entries with this tag, repeat to narrow down') { |tag| @tags << tag }
14
+ end
15
+
16
+ def execute(maybe_named_day)
17
+ named_date = Ptimelog::NamedDate.new
18
+ day = named_date.named_date(maybe_named_day)
19
+ entries = selected_entries(Ptimelog::DataSource.new(@config, day).entries)
20
+
21
+ return if entries.empty? # an empty or filtered-out range is nothing to report
22
+
23
+ output(named_date.title(day), groups(entries))
24
+ end
25
+
26
+ private
27
+
28
+ def selected_entries(entries)
29
+ entries.flat_map { |_date, list| list }
30
+ .select { |entry| entry.valid? && entry.tagged_with?(@tags) }
31
+ end
32
+
33
+ # The ticket is what gets invoiced and the tags decide its account, so both have to
34
+ # match. An entry without a ticket carries nothing to merge it by and stays on its own.
35
+ def groups(entries)
36
+ ticketed, ticketless = entries.partition { |entry| entry.ticket.to_s != '' }
37
+
38
+ ticketed.group_by { |entry| [entry.ticket, entry.tags.to_a.sort] }.values +
39
+ ticketless.zip # a group of one, each
40
+ end
41
+
42
+ # Tags, account and billability are equal within a group, only the descriptions can differ
43
+ def line(group)
44
+ entry = group.first.dup
45
+ entry.description = group.map(&:description).uniq.join(', ')
46
+
47
+ [duration(group), Ptimelog::Entry::SEPARATOR, entry.label].join(' ')
48
+ end
49
+
50
+ def output(title, groups)
51
+ puts title,
52
+ '----------'
53
+
54
+ groups.each { |group| puts line(group) }
55
+
56
+ puts '----------',
57
+ "Total work done: #{duration(groups.flatten)} hours",
58
+ '----------------------------',
59
+ nil
60
+ end
61
+
62
+ def duration(entries) = Ptimelog::Entry.format_duration(entries.sum(&:duration))
63
+ end
64
+ end
65
+ end
@@ -5,24 +5,45 @@ require 'erb'
5
5
  module Ptimelog
6
6
  module Command
7
7
  # Upload entries to puzzletime
8
- class Upload < Base
9
- attr_writer :entries
8
+ class Upload < CmdParse::Command
9
+ def initialize
10
+ super('upload', takes_commands: false)
10
11
 
11
- def needs_entries?
12
- true
12
+ @durations = false
13
+ @entries = {}
14
+ @tags = []
15
+ @config = Configuration.instance
16
+
17
+ options.on('-d', '--durations', 'Only upload durations') { @durations = true }
18
+ options.on('-t', '--tag TAG', 'Only entries with this tag, repeat to narrow down') { |tag| @tags << tag }
13
19
  end
14
20
 
15
- def run
21
+ def execute(maybe_named_day)
22
+ @day = Ptimelog::NamedDate.new.named_date(maybe_named_day)
23
+ @entries = Ptimelog::DataSource.new(@config, @day).entries
24
+
16
25
  @entries.each do |date, list|
17
- puts "Uploading #{date}"
18
- list.each do |entry|
19
- open_browser(entry)
20
- end
26
+ upload_date(date, list)
21
27
  end
22
28
  end
23
29
 
24
30
  private
25
31
 
32
+ def upload_date(date, list)
33
+ valids = selected_entries(list)
34
+ return if valids.empty? # an empty or filtered-out day is nothing to upload
35
+
36
+ puts "Uploading #{date}"
37
+ valids.each { |entry| open_browser(entry) }
38
+ end
39
+
40
+ def selected_entries(list)
41
+ Ptimelog::Joiner.new(compact_on_ticket_only: @durations).join_similar(
42
+ list.select { |entry| entry.valid? && entry.tagged_with?(@tags) }
43
+ .sort_by { |entry| entry.ticket.to_s }
44
+ )
45
+ end
46
+
26
47
  def open_browser(entry)
27
48
  xdg_open "'#{@config[:base_url]}/ordertimes/new?#{url_options(entry)}'", silent: true
28
49
  end
@@ -47,18 +68,34 @@ module Ptimelog
47
68
  end
48
69
 
49
70
  def url_options(entry)
50
- {
51
- work_date: entry.date,
52
- 'ordertime[ticket]': entry.ticket,
53
- 'ordertime[description]': entry.description,
54
- 'ordertime[from_start_time]': entry.start_time,
55
- 'ordertime[to_end_time]': entry.finish_time,
56
- 'ordertime[account_id]': entry.account,
57
- 'ordertime[billable]': entry.billable,
58
- }
71
+ base_params(entry)
72
+ .merge(duration_params(entry))
59
73
  .map { |key, value| [key, ERB::Util.url_encode(value)].join('=') }
60
74
  .join('&')
61
75
  end
76
+
77
+ def base_params(entry)
78
+ {
79
+ work_date: entry.date,
80
+ 'ordertime[ticket]': entry.ticket,
81
+ 'ordertime[description]': entry.description,
82
+ 'ordertime[account_id]': entry.account,
83
+ 'ordertime[billable]': entry.billable,
84
+ }
85
+ end
86
+
87
+ def duration_params(entry)
88
+ if @durations
89
+ {
90
+ 'ordertime[hours]': entry.duration_hours,
91
+ }
92
+ else
93
+ {
94
+ 'ordertime[from_start_time]': entry.start_time,
95
+ 'ordertime[to_end_time]': entry.finish_time,
96
+ }
97
+ end
98
+ end
62
99
  end
63
100
  end
64
101
  end
@@ -10,10 +10,14 @@ module Ptimelog
10
10
  include Singleton
11
11
 
12
12
  CONFIGURATION_DEFAULTS = {
13
- 'base_url' => 'https://time.puzzle.ch',
14
- 'rounding' => 15,
15
- 'dir' => '~/.config/ptimelog',
16
- 'timelog' => '~/.local/share/gtimelog/timelog.txt',
13
+ 'base_url' => 'https://time.puzzle.ch',
14
+ 'rounding' => 15,
15
+ 'dir' => '~/.config/ptimelog',
16
+ 'timelog' => true,
17
+ 'timelog_txt' => '~/.local/share/gtimelog/timelog.txt',
18
+ 'obsidian' => false,
19
+ 'obsidian_vault' => '~/path/to/obsidian-vault',
20
+ 'operon_custom_keys' => { 'ticket' => 'ticket', 'inferer' => 'ptimelog' }.freeze,
17
21
  }.freeze
18
22
 
19
23
  def initialize
@@ -27,7 +31,9 @@ module Ptimelog
27
31
  .join('config')
28
32
  )
29
33
  wrap_with_pathname('dir')
30
- wrap_with_pathname('timelog')
34
+
35
+ wrap_with_pathname('timelog_txt') if self['timelog']
36
+ wrap_with_pathname('obsidian_vault') if self['obsidian']
31
37
  end
32
38
 
33
39
  def load_config(fn)
@@ -43,13 +49,14 @@ module Ptimelog
43
49
  def []=(key, value)
44
50
  @config[key.to_s] = value
45
51
 
46
- wrap_with_pathname(key.to_s) if %w[dir timelog].include?(key.to_s)
52
+ wrap_with_pathname(key.to_s) if %w[dir obsidian_vault timelog_txt].include?(key.to_s)
47
53
  end
48
54
 
49
55
  private
50
56
 
51
57
  def wrap_with_pathname(key)
52
58
  return unless @config.key?(key)
59
+ return unless @config[key].is_a? String
53
60
  return @config[key] if @config[key].is_a? Pathname
54
61
 
55
62
  @config[key] = Pathname.new(@config[key]).expand_path
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ptimelog
4
+ # Facade around several datasources, currently intended to cover the
5
+ # timelog.txt and the tasks of an Obsidian vault
6
+ class DataSource
7
+ def initialize(configuration, day)
8
+ @configuration = configuration
9
+ @day = day
10
+ end
11
+
12
+ def type
13
+ if @configuration['timelog']
14
+ :timelog
15
+ elsif @configuration['obsidian']
16
+ :obsidian
17
+ end
18
+ end
19
+
20
+ def entries
21
+ case type
22
+ when :obsidian then backend.entries.group_by(&:date)
23
+ when :timelog then Ptimelog::Day.new(@day).entries
24
+ else
25
+ []
26
+ end
27
+ end
28
+
29
+ def file
30
+ case type
31
+ when :obsidian then backend.file
32
+ when :timelog then backend.timelog_txt
33
+ end
34
+ end
35
+
36
+ def backend
37
+ @backend ||= case type
38
+ when :obsidian then Operon.new(@day)
39
+ when :timelog then Timelog.instance
40
+ end
41
+ end
42
+ end
43
+ end
data/lib/ptimelog/day.rb CHANGED
@@ -4,6 +4,8 @@ module Ptimelog
4
4
  # Wrap loading of all entries of a day
5
5
  class Day
6
6
  def initialize(date)
7
+ raise(MonthNotSupported, date) if date.is_a?(Range)
8
+
7
9
  @date = NamedDate.new.date(date)
8
10
  end
9
11
 
@@ -12,7 +14,8 @@ module Ptimelog
12
14
  next unless date # guard against the machine
13
15
  next unless @date.to_s == 'all' || @date == date # limit to one day if passed
14
16
 
15
- entries[date] = join_similar(entries_of_day(lines)) # lines |> entries_of_day |> join_similar
17
+ # lines |> entries_of_day |> join_similar
18
+ entries[date] = join_similar(entries_of_day(lines))
16
19
  end
17
20
  end
18
21
 
@@ -22,6 +25,10 @@ module Ptimelog
22
25
  Timelog.load
23
26
  end
24
27
 
28
+ def join_similar(list)
29
+ Ptimelog::Joiner.new.join_similar(list)
30
+ end
31
+
25
32
  def entries_of_day(lines)
26
33
  entries = []
27
34
  start = nil # at the start of the day, we have no previous end
@@ -37,35 +44,5 @@ module Ptimelog
37
44
 
38
45
  entries
39
46
  end
40
-
41
- def join_similar(list)
42
- return [] if list.empty?
43
- return list if list.one?
44
-
45
- one, *tail = list
46
- two, *rest = tail
47
-
48
- joined = maybe_join(one, two)
49
-
50
- if joined.one?
51
- join_similar(joined + rest)
52
- else
53
- [one] + join_similar(tail)
54
- end
55
- end
56
-
57
- def maybe_join(one, two)
58
- if one.ticket == two.ticket &&
59
- one.description == two.description &&
60
- one.finish_time == two.start_time
61
-
62
- joined = one.dup
63
- joined.finish_time = two.finish_time
64
-
65
- [joined]
66
- else
67
- [one, two]
68
- end
69
- end
70
47
  end
71
48
  end