taggata 0.1.0 → 0.1.1

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 316d77bbeae3f1cb3224e6a8c5e7a0e35398be06
4
- data.tar.gz: 847538ab0379d0a7db9d1470c6e04d9257190af5
3
+ metadata.gz: e853c0f197780d48d8bcda7ffb436a893b1aeac1
4
+ data.tar.gz: 78aeedd7b18c30966e0cf107b4959456952b4556
5
5
  SHA512:
6
- metadata.gz: d648822f874cf532bd2976541cfb790b4ab104a388c8d30424e83d34baa78cf47a13144874968a2fe4da249dc712e570d36116e9363be4b685a19920c5f3c6d5
7
- data.tar.gz: f49c703ac45544a2d3e6c5103aa226c98557c1f08f5984baf505cdfb3217d1340565c543b5cf22b422e1419296cf329678ac810c1ec7e4c9a71b7b39e209cb6b
6
+ metadata.gz: 27a4de1718710bfeb5dc3515132499a46f4ac7dd817b575b32ef35f5e2b49a45bca2712ba8a238e0bd7f18862b8f0dde18e02e87c42d945c77d5420795ef9a5d
7
+ data.tar.gz: 00acf36ba798372a80b61a94358cd1a51151e4e165dbf5014fdc9e62483a2ff7ddef299c186db54bd90b8f63084a3727a0f070186986bcb89ca9b93c4415e167
@@ -1,8 +1,15 @@
1
- require 'clamp'
2
- require 'taggata'
3
-
4
1
  module Taggata
5
2
  module Cli
3
+
4
+ require 'clamp'
5
+ require 'taggata'
6
+ require 'taggata/cli/scan'
7
+ require 'taggata/cli/tag'
8
+ require 'taggata/cli/search'
9
+ require 'taggata/cli/remove'
10
+ require 'taggata/cli/list'
11
+ require 'taggata/cli/cleanup'
12
+
6
13
  Clamp do
7
14
 
8
15
  option "--db-path", "DB_PATH", "path to the DB", :attribute_name => :db, :required => true do |db_path|
@@ -11,92 +18,18 @@ module Taggata
11
18
  option ['-v', '--verbose'], :flag, 'be verbose', :default => false
12
19
  option ['-q', '--quiet'], :flag, 'be quite', :default => false
13
20
 
14
- subcommand "scan", "Scan the system" do
15
-
16
- parameter "[ROOT]", "the root of the scanned tree", :attribute_name => :root_path, :default => './'
17
-
18
- def execute
19
- scanner = Taggata::Scanner.new db
20
- root = Taggata::Persistent::Directory.find_or_create db, :name => root_path
21
- scanner.process(root)
22
- end
23
-
24
- end
25
-
26
- subcommand 'tag', 'tag a file matching query' do
27
-
28
- parameter 'TAG_QUERY', 'the tag query', :attribute_name => :tag_query, :required => true
29
- parameter 'SEARCH_QUERY', 'the query to search', :attribute_name => :search_query, :required => true
30
-
31
- def execute
32
- tags = ::Taggata::Parser::Tag.new(db).parse(tag_query)
33
- files = ::Taggata::Parser::Query.new(db).parse(search_query)
34
- db.transaction do
35
- files.each do |file|
36
- file.add_tags *tags[:add]
37
- file.remove_tags *tags[:del]
38
- end
39
- end
40
- end
41
-
42
- end
43
-
44
- subcommand 'search', "search the database" do
45
-
46
- option "-c", :flag, 'show only matched count', :attribute_name => :count, :default => false
47
- option "-n", :flag, 'show only names without paths', :attribute_name => :names, :default => false
48
- parameter "QUERY", 'the query to perform', :attribute_name => :query, :required => true
49
-
50
- def execute
51
- results = ::Taggata::Parser::Query.new(db).parse(query)
52
- if count?
53
- puts results.count
54
- else
55
- if results.empty?
56
- puts "No results matching the query."
57
- else
58
- method = names? ? :name : :path
59
- results.each { |file| puts file.send(method) }
60
- end
61
- end
62
- end
63
-
64
- end
65
-
66
- subcommand 'remove', 'remove files matching query' do
67
-
68
- parameter "QUERY", 'the query to perform', :attribute_name => :query, :required => true
69
-
70
- def execute
71
- parser = ::Taggata::Parser::Query.new db
72
- results = parser.parse query
73
- db.transaction do
74
- results.each(&:destroy)
75
- end
76
- end
77
-
78
- end
79
-
80
- subcommand 'list', 'list things in database' do
81
-
82
- parameter 'TYPE', 'type of records to list, can be one of file, directory, tag', :attribute_name => :type, :required => true do |type|
83
- signal_usage_error 'TYPE must be one of file, directory, tag' unless %(file directory tag).include? type
84
- type.to_sym
85
- end
86
-
87
- def execute
88
- case type
89
- when :file
90
- db.find(Taggata::Persistent::File, {}).each { |file| puts file.path }
91
- when :directory
92
- db.find(Taggata::Persistent::Directory, {}).each { |dir| puts dir.path }
93
- when :tag
94
- db.find(Taggata::Persistent::Tag, {}).each { |tag| puts tag.name }
95
- end
96
- end
97
-
98
- end
99
-
21
+ subcommand 'scan', 'Scan the system', ScanCommand
22
+
23
+ subcommand 'tag', 'Tag a file matching query', TagCommand
24
+
25
+ subcommand 'search', "Search the database", SearchCommand
26
+
27
+ subcommand 'remove', 'Remove files matching query', RemoveCommand
28
+
29
+ subcommand 'list', 'List things in database', ListCommand
30
+
31
+ subcommand 'cleanup', 'Remove stale entries from the database', CleanupCommand
32
+
100
33
  end
101
34
  end
102
35
  end
@@ -0,0 +1,25 @@
1
+ module Taggata
2
+ module Cli
3
+ class CleanupCommand < Clamp::Command
4
+
5
+ option %w(-t --tags), :flag, 'remove unused tags', :default => true, :attribute_name => :tags
6
+
7
+ def execute
8
+ remove_tags if tags?
9
+ end
10
+
11
+ private
12
+
13
+ def remove_tags
14
+ tags = @db.find_tags_without_files
15
+ count = @db.destroy(Taggata::Persistent::Tag, tags.map(&:to_hash)) unless tags.empty?
16
+ puts "Removed tags: #{format_count count}" unless @quiet
17
+ end
18
+
19
+ def format_count(count)
20
+ count.nil? ? 0 : count
21
+ end
22
+
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,23 @@
1
+ module Taggata
2
+ module Cli
3
+ class ListCommand < Clamp::Command
4
+
5
+ parameter 'TYPE', 'type of records to list, can be one of file, directory, tag', :attribute_name => :type, :required => true do |type|
6
+ signal_usage_error 'TYPE must be one of file, directory, tag' unless %(file directory tag).include? type
7
+ type.to_sym
8
+ end
9
+
10
+ def execute
11
+ case type
12
+ when :file
13
+ @db.find(Taggata::Persistent::File, {}).each { |file| puts file.path }
14
+ when :directory
15
+ @db.find(Taggata::Persistent::Directory, {}).each { |dir| puts dir.path }
16
+ when :tag
17
+ @db.find(Taggata::Persistent::Tag, {}).each { |tag| puts tag.name }
18
+ end
19
+ end
20
+
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,8 @@
1
+ module Taggata
2
+ module Cli
3
+ class MainCommand < Clamp::Command
4
+
5
+
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,17 @@
1
+ module Taggata
2
+ module Cli
3
+ class RemoveCommand < Clamp::Command
4
+
5
+ parameter "QUERY", 'the query to perform', :attribute_name => :query, :required => true
6
+
7
+ def execute
8
+ parser = ::Taggata::Parser::Query.new @db
9
+ results = parser.parse query
10
+ @db.transaction do
11
+ results.each(&:destroy)
12
+ end
13
+ end
14
+
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,15 @@
1
+ module Taggata
2
+ module Cli
3
+ class ScanCommand < Clamp::Command
4
+
5
+ parameter "[ROOT]", "the root of the scanned tree", :attribute_name => :root_path, :default => './'
6
+
7
+ def execute
8
+ scanner = Taggata::Scanner.new @db
9
+ root = Taggata::Persistent::Directory.find_or_create @db, :name => root_path
10
+ scanner.process(root)
11
+ end
12
+
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,25 @@
1
+ module Taggata
2
+ module Cli
3
+ class SearchCommand < Clamp::Command
4
+
5
+ option "-c", :flag, 'show only matched count', :attribute_name => :count, :default => false
6
+ option "-n", :flag, 'show only names without paths', :attribute_name => :names, :default => false
7
+ parameter "QUERY", 'the query to perform', :attribute_name => :query, :required => true
8
+
9
+ def execute
10
+ results = ::Taggata::Parser::Query.new(@db).parse(query)
11
+ if count?
12
+ puts results.count
13
+ else
14
+ if results.empty?
15
+ puts "No results matching the query."
16
+ else
17
+ method = names? ? :name : :path
18
+ results.each { |file| puts file.send(method) }
19
+ end
20
+ end
21
+ end
22
+
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,21 @@
1
+ module Taggata
2
+ module Cli
3
+ class TagCommand < Clamp::Command
4
+
5
+ parameter 'TAG_QUERY', 'the tag query', :attribute_name => :tag_query, :required => true
6
+ parameter 'SEARCH_QUERY', 'the query to search', :attribute_name => :search_query, :required => true
7
+
8
+ def execute
9
+ tags = ::Taggata::Parser::Tag.new(@db).parse(tag_query)
10
+ files = ::Taggata::Parser::Query.new(@db).parse(search_query)
11
+ @db.transaction do
12
+ files.each do |file|
13
+ file.add_tags *tags[:add]
14
+ file.remove_tags *tags[:del]
15
+ end
16
+ end
17
+ end
18
+
19
+ end
20
+ end
21
+ end
@@ -21,6 +21,10 @@ module Taggata
21
21
  adapter.find_untagged_files.map { |hash| Persistent::File.new_from_hash self, hash }
22
22
  end
23
23
 
24
+ def find_tags_without_files
25
+ adapter.find_tags_without_files.map { |hash| Persistent::Tag.new_from_hash self, hash }
26
+ end
27
+
24
28
  def destroy(klass, options)
25
29
  adapter.destroy(klass, options)
26
30
  end
@@ -12,6 +12,10 @@ module Taggata
12
12
  raise NotImplementedError
13
13
  end
14
14
 
15
+ def find_tags_without_files
16
+ raise NotImplementedError
17
+ end
18
+
15
19
  def transaction(&block)
16
20
  raise NotImplementedError
17
21
  end
@@ -22,6 +22,13 @@ module Taggata
22
22
  find(Taggata::Persistent::File, :id => untagged_ids)
23
23
  end
24
24
 
25
+ def find_tags_without_files
26
+ file_tags = db[Persistent::FileTag.table].select(:tag_id).map { |hash| hash[:tag_id] }
27
+ tag_ids = db[Persistent::Tag.table].select(:id).map { |hash| hash[:id] }
28
+ untagged_ids = tag_ids.reject { |id| file_tags.include? id }
29
+ find(Persistent::Tag, :id => untagged_ids)
30
+ end
31
+
25
32
  def destroy(klass, options)
26
33
  db[klass.table].where(options).delete
27
34
  end
@@ -32,7 +32,7 @@ module Taggata
32
32
  def remove_tags(*to_remove)
33
33
  return if to_remove.empty?
34
34
  current_tag_ids = tags.map(&:id)
35
- to_remove_ids = current_tag_ids - to_remove.map(&:id)
35
+ to_remove_ids = current_tag_ids & to_remove.map(&:id)
36
36
  options = to_remove_ids.map { |tag_id| { :file_id => id, :tag_id => tag_id } }
37
37
  db.destroy(FileTag, options)
38
38
  end
@@ -1,3 +1,3 @@
1
1
  module Taggata
2
- VERSION = '0.1.0'
2
+ VERSION = '0.1.1'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: taggata
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Ruzicka
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-07-26 00:00:00.000000000 Z
11
+ date: 2015-07-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sequel
@@ -216,6 +216,13 @@ files:
216
216
  - bin/taggata
217
217
  - lib/taggata.rb
218
218
  - lib/taggata/cli.rb
219
+ - lib/taggata/cli/cleanup.rb
220
+ - lib/taggata/cli/list.rb
221
+ - lib/taggata/cli/main.rb
222
+ - lib/taggata/cli/remove.rb
223
+ - lib/taggata/cli/scan.rb
224
+ - lib/taggata/cli/search.rb
225
+ - lib/taggata/cli/tag.rb
219
226
  - lib/taggata/constants.rb
220
227
  - lib/taggata/db.rb
221
228
  - lib/taggata/db_adapters.rb