dircat 0.1.12 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (88) hide show
  1. data/LICENSE.txt +1 -1
  2. data/README.md +31 -6
  3. data/bin/dircat +5 -1
  4. data/bin/scat +9 -0
  5. data/dircat.gemspec +48 -15
  6. data/examples/example.rb +2 -1
  7. data/lib/dircat.rb +29 -34
  8. data/lib/dircat/cat_on_sqlite/cat_on_sqlite.rb +177 -0
  9. data/lib/dircat/cat_on_sqlite/directory_visitor.rb +106 -0
  10. data/lib/dircat/cat_on_sqlite/migration/00_create_dircat_properties.rb +13 -0
  11. data/lib/dircat/cat_on_sqlite/migration/01_create_categories.rb +12 -0
  12. data/lib/dircat/cat_on_sqlite/migration/02_create_images.rb +16 -0
  13. data/lib/dircat/cat_on_sqlite/migration/03_create_items.rb +23 -0
  14. data/lib/dircat/cat_on_sqlite/migration/04_create_taggings.rb +13 -0
  15. data/lib/dircat/cat_on_sqlite/migration/05_create_tags.rb +13 -0
  16. data/lib/dircat/cat_on_sqlite/model/category.rb +16 -0
  17. data/lib/dircat/cat_on_sqlite/model/image.rb +24 -0
  18. data/lib/dircat/cat_on_sqlite/model/item.rb +19 -0
  19. data/lib/dircat/cat_on_sqlite/model/tag.rb +58 -0
  20. data/lib/dircat/cat_on_sqlite/model/tagging.rb +9 -0
  21. data/lib/dircat/cat_on_sqlite/simple_cataloger_error.rb +9 -0
  22. data/lib/dircat/cat_on_sqlite_cli/cli_cat.rb +25 -0
  23. data/lib/dircat/cat_on_sqlite_cli/cli_server.rb +65 -0
  24. data/lib/dircat/cat_on_sqlite_cli/cmd_create.rb +56 -0
  25. data/lib/dircat/cat_on_sqlite_cli/cmd_list.rb +72 -0
  26. data/lib/dircat/cat_on_sqlite_cli/cmd_server.rb +62 -0
  27. data/lib/dircat/cat_on_sqlite_cli/cmd_update.rb +49 -0
  28. data/lib/dircat/{cat.rb → cat_on_yaml/cat_on_yaml.rb} +22 -10
  29. data/lib/dircat/cat_on_yaml/entry.rb +64 -0
  30. data/lib/dircat/{cli → cat_on_yaml_cli}/cli_dircat.rb +0 -0
  31. data/lib/dircat/{cli → cat_on_yaml_cli}/command_build.rb +53 -12
  32. data/lib/dircat/{cli → cat_on_yaml_cli}/command_diff.rb +4 -4
  33. data/lib/dircat/{cli → cat_on_yaml_cli}/command_query.rb +16 -2
  34. data/lib/dircat/config.rb +25 -0
  35. data/lib/dircat/extensions.rb +26 -0
  36. data/lib/dircat/server/helpers.rb +60 -0
  37. data/lib/dircat/server/my_static.rb +40 -0
  38. data/lib/dircat/server/web_server.rb +176 -0
  39. data/lib/dircat/version.rb +2 -1
  40. data/lib/dircat_on_sqlite.rb +31 -0
  41. data/lib/dircat_on_sqlite_cli.rb +37 -0
  42. data/lib/simple_cataloger_dm/cli.rb +32 -0
  43. data/lib/simple_cataloger_dm/core.rb +43 -0
  44. data/lib/simple_cataloger_dm/core/catalog.rb +141 -0
  45. data/lib/simple_cataloger_dm/core/directory_visitor.rb +107 -0
  46. data/lib/simple_cataloger_dm/core/extensions.rb +26 -0
  47. data/lib/simple_cataloger_dm/core/simple_cataloger_error.rb +9 -0
  48. data/lib/simple_cataloger_dm/models/category.rb +21 -0
  49. data/lib/simple_cataloger_dm/models/image.rb +30 -0
  50. data/lib/simple_cataloger_dm/models/item.rb +24 -0
  51. data/lib/simple_cataloger_dm/models/tag.rb +62 -0
  52. data/lib/simple_cataloger_dm/models/tagging.rb +11 -0
  53. data/lib/simple_cataloger_dm/server/helpers.rb +60 -0
  54. data/lib/simple_cataloger_dm/server/my_static.rb +40 -0
  55. data/lib/simple_cataloger_dm/server/web_server.rb +171 -0
  56. data/spec/dircat/cat_on_sqlite/cat_on_sqlite_spec.rb +41 -0
  57. data/spec/dircat/cat_on_sqlite_web/web_server_spec.rb +30 -0
  58. data/spec/dircat/{cat_spec.rb → cat_on_yaml/cat_on_yaml_spec.rb} +20 -14
  59. data/spec/dircat/{cli → cat_on_yaml_cli}/cli_dircat_spec.rb +0 -0
  60. data/spec/dircat/{cli → cat_on_yaml_cli}/command_build_spec.rb +4 -4
  61. data/spec/dircat/{cli → cat_on_yaml_cli}/command_diff_spec.rb +0 -0
  62. data/spec/dircat/{cli → cat_on_yaml_cli}/command_query_spec.rb +1 -1
  63. data/spec/fixtures/certified_output/cat_dir1_20120811.yaml +25 -0
  64. data/spec/fixtures/certified_output/cat_dir2_20120811.yaml +34 -0
  65. data/spec/fixtures/certified_output/{dircat1.yaml → dircat1_version_0.1.yaml} +0 -0
  66. data/spec/fixtures/certified_output/{dircat2.yaml → dircat2_version_0.1.yaml} +0 -0
  67. data/spec/fixtures/dir4/file1.txt +1 -0
  68. data/spec/fixtures/dir4/file2.txt +1 -0
  69. data/spec/fixtures/dir4/subdir/file3.txt +1 -0
  70. data/spec/fixtures/films/A-Z/A/Arancia meccanica [Stanley Kubrick][1971]/folder.jpg +0 -0
  71. data/spec/fixtures/films/A-Z/P/Plan 9 [Edward Wood][1959]/folder.jpg +0 -0
  72. data/spec/fixtures/films/Directors/[Akira Kurosawa]/[1940] Dersu Uzala [5]/folder.jpg +0 -0
  73. data/spec/fixtures/films/Directors/[Akira Kurosawa]/[1965] Barbarossa [Toshiro Mifune][4]/folder.jpg +0 -0
  74. data/spec/fixtures/films/Directors/[Federico Fellini]/[1963] 8 e mezzo [Marcello Mastroianni][Claudia Cardinale][5]/folder.jpg +0 -0
  75. data/spec/fixtures/films/Directors/[Woody Allen]/[1977] Annie Hall [Diane Keaton][5]/folder.jpg +0 -0
  76. data/{.gemtest → spec/fixtures/readme.txt} +0 -0
  77. data/spec/fixtures/tmp/test_ar.sqlite3 +0 -0
  78. data/spec/fixtures/tmp/test_ar.yml +7 -0
  79. data/spec/generate_directories_to_catalog.rb +80 -0
  80. data/spec/generate_mysql_catalog.rb +124 -0
  81. data/spec/spec_helper.rb +29 -0
  82. data/tasks/rspec.rake +0 -2
  83. metadata +390 -112
  84. data/lib/dircat/entry.rb +0 -61
  85. data/lib/dircat/extension_md5.rb +0 -25
  86. data/lib/dircat/extension_numeric.rb +0 -15
  87. data/spec/dircat/md5_spec.rb +0 -8
  88. data/spec/dircat/numeric_spec.rb +0 -13
@@ -0,0 +1,25 @@
1
+ module DirCat
2
+
3
+ class Config
4
+
5
+ # user config dir normally is
6
+ # ~/.dircat
7
+ def self.user_config_dir
8
+ # find directory where to store catalog data
9
+ # depends on OS
10
+ user_home_dir = ENV['HOME'] || ENV['APPDATA']
11
+ if user_home_dir
12
+ if RUBY_PLATFORM =~ /linux/
13
+ config_dir = File.expand_path(File.join(user_home_dir, "." + DirCat::NAME))
14
+ else
15
+ config_dir = File.expand_path(File.join(user_home_dir, DirCat::NAME))
16
+ end
17
+ Dir.mkdir(config_dir) unless File.directory?(config_dir)
18
+ config_dir
19
+ else
20
+ File.join(home_dir, "config")
21
+ end
22
+ end
23
+
24
+ end
25
+ end # end module DirCat
@@ -0,0 +1,26 @@
1
+ # -*- coding: utf-8 -*-
2
+ module SimpleCataloger
3
+ module CoreExtensions
4
+ module Array
5
+ def extract_options!
6
+ last.is_a?(::Hash) ? pop : { }
7
+ end
8
+ end
9
+ end
10
+ end
11
+
12
+ class Array
13
+ include SimpleCataloger::CoreExtensions::Array
14
+ end
15
+
16
+ module URI
17
+ class << self
18
+
19
+ def parse_with_safety(uri)
20
+ parse_without_safety uri.gsub('[', '%5B').gsub(']', '%5D')
21
+ end
22
+
23
+ # alias parse_without_safety parse
24
+ # alias parse parse_with_safety
25
+ end
26
+ end
@@ -0,0 +1,60 @@
1
+ # -*- coding: utf-8 -*-
2
+ module SimpleCataloger
3
+ module Helpers
4
+
5
+ def partial(template, *args)
6
+ options = args.extract_options!
7
+ options.merge!(:layout => false)
8
+ if collection = options.delete(:collection) then
9
+ collection.inject([]) do |buffer, member|
10
+ buffer << haml(template, options.merge(
11
+ :layout => false,
12
+ :locals => {template.to_sym => member}
13
+ )
14
+ )
15
+ end.join("\n")
16
+ else
17
+ haml(template, options)
18
+ end
19
+ end
20
+
21
+ def partial_erb(template)
22
+ erb template
23
+ end
24
+
25
+ def base_url
26
+ if Sinatra::Application.port == 80
27
+ "http://#{Sinatra::Application.host}/"
28
+ else
29
+ "http://#{Sinatra::Application.host}:#{Sinatra::Application.port}/"
30
+ end
31
+ end
32
+
33
+ def when_updated(time)
34
+ days = (Time.now - time) / (60 * 60 * 24)
35
+ case days.to_i
36
+ when 0
37
+ "today"
38
+ when 1
39
+ "yesterday"
40
+ else
41
+ "#{sprintf("%i", days) } days ago"
42
+ end
43
+ end
44
+
45
+ def gen_url(field, visible_field = nil)
46
+ visible_field ||= field
47
+ pos = request.env['REQUEST_URI'].index "?"
48
+ url = if pos
49
+ request.env['REQUEST_URI'][0...pos]
50
+ else
51
+ request.env['REQUEST_URI']
52
+ end
53
+ "<a href='#{url}?order=#{field}'>#{visible_field}</a>"
54
+ end
55
+
56
+ def rfc_3339(timestamp)
57
+ timestamp.strftime("%Y-%m-%dT%H:%M:%SZ")
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,40 @@
1
+ module Rack
2
+
3
+ # The Rack::Static middleware intercepts requests for static files
4
+ # (javascript files, images, stylesheets, etc) based on the url prefixes
5
+ # passed in the options, and serves them using a Rack::File object. This
6
+ # allows a Rack stack to serve both static and dynamic content.
7
+ #
8
+ # Examples:
9
+ # use Rack::Static, :urls => ["/media"]
10
+ # will serve all requests beginning with /media from the "media" folder
11
+ # located in the current directory (ie media/*).
12
+ #
13
+ # use Rack::Static, :urls => ["/css", "/images"], :root => "public"
14
+ # will serve all requests beginning with /css or /images from the folder
15
+ # "public" in the current directory (ie public/css/* and public/images/*)
16
+
17
+ class MyStatic
18
+
19
+ def initialize(app, options={})
20
+ @app = app
21
+ @urls = options[:urls] || ["/favicon.ico"]
22
+ root = options[:root] || Dir.pwd
23
+ @file_server = Rack::File.new(root)
24
+ end
25
+
26
+ def call(env)
27
+ path = env["PATH_INFO"]
28
+ puts "mystatic #{path} #{URI.decode(path)}"
29
+ path = URI.decode(path)
30
+ can_serve = @urls.any? { |url| path.index(url) == 0 }
31
+
32
+ if can_serve
33
+ @file_server.call(env)
34
+ else
35
+ @app.call(env)
36
+ end
37
+ end
38
+
39
+ end
40
+ end
@@ -0,0 +1,176 @@
1
+ # -*- coding: utf-8 -*-
2
+ module SimpleCataloger
3
+
4
+ class WebServer < Sinatra::Base
5
+
6
+ def self.run!(*args)
7
+ options = args.last.is_a?(::Hash) ? args.last : { }
8
+ catalog = options[:catalog]
9
+
10
+ catalog.roots.each do |root|
11
+ use Rack::MyStatic,
12
+ :urls => ["/#{File.basename(root)}"],
13
+ :root => File.dirname(root)
14
+ puts "mount #{File.basename(root)} -> #{File.dirname(root)}"
15
+ end
16
+
17
+ define_method :catalog do
18
+ catalog
19
+ end
20
+ super(*args)
21
+ end
22
+
23
+ # turns on static file serving for Sinatra::Base apps
24
+ enable :static
25
+
26
+ set :public_folder, File.join(File.dirname(__FILE__), %w{.. server_public})
27
+ set :views, File.join(File.dirname(__FILE__), %w{.. server_views})
28
+
29
+ helpers do
30
+ include Rack::Utils
31
+ alias_method :h, :escape_html
32
+ end
33
+
34
+ helpers Sinatra::GroupItems
35
+ helpers Helpers
36
+
37
+ get '/app.css' do
38
+ content_type 'text/css', :charset => 'utf-8'
39
+ sass :app
40
+ end
41
+
42
+ get "/js/tooltips/tooltips.css" do
43
+ content_type 'text/css', :charset => 'utf-8'
44
+ sass "/js/tooltips/tooltips".to_sym
45
+ end
46
+
47
+ get '/' do
48
+ @catalog = catalog
49
+ @items = Item.all
50
+ order = params[:o] || "alpha"
51
+
52
+ @render = proc { |item|
53
+ "<a href=\"/item/#{item.id}\" rel=\"#{item.id}\">#{item.name}</a>" +
54
+ if item.rating then
55
+ " (r:#{item.rating})"
56
+ else
57
+ " (r:unrated)"
58
+ end
59
+ }
60
+
61
+ case order
62
+ when "alpha"
63
+ @grouped_items = group_items(@items, :group_size => 5)
64
+ @title = "Films in alphabetical order (c:#{@items.length})"
65
+ when "time"
66
+ @grouped_items = group_items(@items, :group_size => 1, :header => proc { |film| film.added_at })
67
+ @title = "Films in time added order (c:#{@items.length})"
68
+ end
69
+ haml :items
70
+ end
71
+
72
+ get '/item/:id' do
73
+ @catalog = catalog
74
+ @item = Item.find(params[:id])
75
+ @grouped_items = group_items(@item.tags, :group_size => 1, :header => proc { |tag| tag.category.name })
76
+ haml :item
77
+ end
78
+
79
+ get '/categories' do
80
+ @catalog = catalog
81
+ @categories = Category.all
82
+ haml :categories
83
+ end
84
+
85
+ get "/category/:id" do
86
+ @catalog = catalog
87
+ @category = Category.find(params[:id])
88
+ haml :category
89
+ end
90
+
91
+ get "/tag/:id" do
92
+ @catalog = catalog
93
+ @tag = Tag.find(params[:id])
94
+ haml :tag
95
+ end
96
+
97
+ get "/random" do
98
+ max_peso = 0
99
+ Item.all.each do |f|
100
+ max_peso += f.peso
101
+ end
102
+ Random.new.rand(0..max_peso)
103
+ end
104
+
105
+ #######################################################################################
106
+ # Ajax
107
+
108
+ get '/ajax/ajax_item/:id' do
109
+ @item = Item.find(params[:id])
110
+ haml 'ajax/ajax_item'.to_sym, :layout => false
111
+ end
112
+
113
+ post '/ajax/open_folder' do
114
+ dir = params[:dir] || "/"
115
+ system("nautilus", dir)
116
+ "ok"
117
+ end
118
+
119
+ post "/ajax/set-category" do
120
+ id = params[:tag_id]
121
+ category_name = params[:category_name]
122
+ tag = Tag.find(id.to_i)
123
+ category = Category.find_or_create_by_name(category_name)
124
+ tag.category = category
125
+ tag.save
126
+ catalog.update_categories
127
+ catalog.write_config
128
+ "ok"
129
+ end
130
+
131
+ get '/ajax/ajax_set_flag/:id' do
132
+ @film = Item.find(params[:id])
133
+ if (params[:checked] == "true")
134
+ @film.flagged_at = Time.new
135
+ else
136
+ @film.flagged_at = nil
137
+ end
138
+ @film.save
139
+ "ok"
140
+ end
141
+
142
+ #######################################################################################
143
+ # Tests
144
+
145
+ get "/test/resize" do
146
+ @catalog = catalog
147
+ haml "/test/resize".to_sym
148
+ end
149
+
150
+ get "/test/jquery" do
151
+ @catalog = catalog
152
+ haml "/test/jquery".to_sym
153
+ end
154
+
155
+ get "/test/lightbox" do
156
+ @catalog = catalog
157
+ haml "/test/lightbox".to_sym
158
+ end
159
+
160
+ get "/test/select" do
161
+ @catalog = catalog
162
+ haml "/test/select".to_sym
163
+ end
164
+
165
+ get "/test/protovis" do
166
+ @catalog = catalog
167
+ haml "/test/protovis".to_sym
168
+ end
169
+
170
+ get "/test/qtip" do
171
+ @catalog = catalog
172
+ haml "/test/qtip".to_sym
173
+ end
174
+ end
175
+
176
+ end
@@ -1,4 +1,5 @@
1
1
  # -*- coding: utf-8 -*-
2
2
  module DirCat
3
- VERSION="0.1.12"
3
+ NAME="dircat"
4
+ VERSION="0.2.0"
4
5
  end
@@ -0,0 +1,31 @@
1
+ # -*- coding: utf-8 -*-
2
+ #
3
+ # std lib
4
+ #
5
+ require 'pp'
6
+ require 'optparse'
7
+ require 'fileutils'
8
+ require 'ostruct'
9
+
10
+ #
11
+ # rubygems
12
+ #
13
+ require 'tree_rb'
14
+
15
+ require 'active_record'
16
+ # gem 'mysql2', "<= 0.3"
17
+ # require 'mysql2'
18
+
19
+ #
20
+ # simple catalog
21
+ #
22
+ require 'dircat/version'
23
+ require 'dircat/extensions'
24
+ require 'dircat/config'
25
+ require 'dircat/cat_on_sqlite/simple_cataloger_error'
26
+
27
+ #
28
+ # app
29
+ #
30
+ require 'dircat/cat_on_sqlite/directory_visitor'
31
+ require 'dircat/cat_on_sqlite/cat_on_sqlite'
@@ -0,0 +1,37 @@
1
+ # -*- coding: utf-8 -*-
2
+ #
3
+ # rubygems
4
+ #
5
+ #require 'rubygems'
6
+ #begin
7
+ # require "bundler/setup"
8
+ #rescue LoadError
9
+ #end
10
+
11
+ require 'sinatra/base'
12
+ require 'sinatra/group_items'
13
+
14
+ require 'haml'
15
+ require 'sass'
16
+ require 'json'
17
+
18
+ require 'dircat_on_sqlite'
19
+
20
+ #
21
+ # server
22
+ #
23
+ require 'dircat/server/helpers'
24
+ require 'dircat/server/my_static'
25
+ require 'dircat/server/web_server'
26
+
27
+ #
28
+ # cli
29
+ #
30
+ require 'optparse-command'
31
+
32
+ command_dir = File.join(File.dirname(__FILE__), "dircat", "cat_on_sqlite_cli")
33
+ require File.join(command_dir, 'cli_cat')
34
+ unless Dir.exist? command_dir
35
+ raise "cannot found directory '#{command_dir}'"
36
+ end
37
+ Dir[ File.join(command_dir, "cmd*.rb") ].each { |f|require f }
@@ -0,0 +1,32 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ #
4
+ # rubygems
5
+ #
6
+ #require 'rubygems'
7
+ #begin
8
+ # require "bundler/setup"
9
+ #rescue LoadError
10
+ #end
11
+
12
+ require 'sinatra/base'
13
+
14
+ gem 'sinatra-group-items', "0.0.1"
15
+ require 'sinatra/group_items'
16
+
17
+ require 'haml'
18
+ require 'sass'
19
+ require 'json'
20
+
21
+ #
22
+ # server
23
+ #
24
+ require 'simple_cataloger_server/helpers'
25
+ require 'simple_cataloger_server/my_static'
26
+ require 'simple_cataloger_server/web_server'
27
+
28
+ #
29
+ # cli
30
+ #
31
+ require 'simple_cataloger_cli/cli_cat'
32
+ Dir[ File.join(File.dirname(__FILE__), "simple_cataloger_cli", "cmd*.rb") ].each { |f|require f }