biblioteque 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,18 @@
1
+ # http://www.gnu.org/software/automake
2
+
3
+ Makefile.in
4
+
5
+ # http://www.gnu.org/software/autoconf
6
+
7
+ /autom4te.cache
8
+ /aclocal.m4
9
+ /compile
10
+ /configure
11
+ /depcomp
12
+ /install-sh
13
+ /missing
14
+
15
+ doc
16
+ .yardoc
17
+ Gemfile.lock
18
+ /pkg
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in biblioteque.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Eremin Andrey
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,68 @@
1
+ # Biblioteque
2
+
3
+ Biblioteque is a little gem that provides all necessary methods to create databases of files stored in local directories.
4
+
5
+ ## Usage example
6
+
7
+ require 'biblioteque'
8
+ engine = Biblioteque::Engine.new
9
+
10
+ Now we can create a new database:
11
+
12
+ engine.create_db("<DBname>", "<PathToDatabaseFile>")
13
+
14
+ then we need to load created db:
15
+
16
+ db = engine.load_db("<PathToDatabaseFile>")
17
+
18
+ Let's add a new library:
19
+
20
+ db.create_library("<LibraryName>")
21
+
22
+ and load it:
23
+
24
+ library = db.libraries.first
25
+
26
+ and import files information into it:
27
+
28
+ library.add("<PathToCrawlForFiles>")
29
+
30
+ Now, we can search for added files in a library:
31
+
32
+ library.search('<FileNameOrPatternToSearch>')
33
+
34
+ or in entiry database:
35
+
36
+ db.search('<FileNameOrPatternToSearch>')
37
+
38
+ Oh! and do not forget to save all the changes:
39
+
40
+ db.save
41
+
42
+
43
+ ## Dependencies
44
+
45
+ This project doesn't have any runtime dependencies, only Ruby 1.9.3+.
46
+ During development, the dependencies are: redcarpet, yard, mocha.
47
+
48
+ ## Installation
49
+
50
+ Add this line to your application's Gemfile:
51
+
52
+ gem 'biblioteque'
53
+
54
+ And then execute:
55
+
56
+ $ bundle
57
+
58
+ Or install it yourself as:
59
+
60
+ $ gem install biblioteque
61
+
62
+ ## Contributing
63
+
64
+ 1. Fork it
65
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
66
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
67
+ 4. Push to the branch (`git push origin my-new-feature`)
68
+ 5. Create new Pull Request
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+ require 'rake/testtask'
4
+
5
+ Rake::TestTask.new(:test) do |t|
6
+ t.pattern = 'test/*_test.rb'
7
+ t.verbose = false
8
+ end
9
+
10
+ task :default => 'test'
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "biblioteque/version"
4
+
5
+ Gem::Specification.new do |gem|
6
+ gem.authors = ["Eremin Andrey"]
7
+ gem.email = ["dsoft88@gmail.com"]
8
+ gem.description = %q{Local files parser. It parses files stored in a local directories and writes the information into db in JSON format.}
9
+ gem.summary = %q{Local files parser. It parses files stored in a local directories and writes the information into db in JSON format.}
10
+ gem.homepage = "http://eremin.me"
11
+
12
+ gem.rubyforge_project = "biblioteque"
13
+
14
+ gem.files = `git ls-files`.split($\)
15
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
16
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
17
+ gem.name = "biblioteque"
18
+ gem.require_paths = ["lib"]
19
+ gem.version = Biblioteque::VERSION
20
+
21
+ gem.add_development_dependency "yard"
22
+ gem.add_development_dependency "minitest"
23
+ gem.add_development_dependency "mocha"
24
+ gem.add_development_dependency "redcarpet"
25
+ end
@@ -0,0 +1,19 @@
1
+ require "biblioteque/version"
2
+
3
+ module Biblioteque
4
+
5
+ require 'json'
6
+ require 'securerandom'
7
+ require 'fileutils'
8
+ require 'pathname'
9
+ require 'find'
10
+ require 'fileutils'
11
+
12
+ require "biblioteque/model"
13
+ require "biblioteque/library"
14
+ require "biblioteque/database"
15
+
16
+ require "biblioteque/engine"
17
+ require "biblioteque/crawler"
18
+
19
+ end
@@ -0,0 +1,44 @@
1
+ module Biblioteque
2
+ class Crawler
3
+
4
+ def initialize(path)
5
+ raise "No" unless path
6
+ @path = path
7
+ end
8
+
9
+ def search_for_library(library_id)
10
+ arr = []
11
+ Find.find(@path) do |path|
12
+ if FileTest.directory?(path)
13
+ if File.basename(path)[0] == ?. and File.basename(path) != '.'
14
+ Find.prune
15
+ else
16
+ arr << make_file_obj(path, library_id) if path != @path # add directory
17
+ end
18
+ else
19
+ arr << make_file_obj(path, library_id) # add file
20
+ end
21
+ end
22
+ return arr
23
+ end
24
+
25
+ private
26
+
27
+ def make_file_obj(path, library_id)
28
+ obj = File.stat(path)
29
+ {
30
+ id:Model.generate_id, library_id:library_id,
31
+ created_at:obj.ctime, updated_at:obj.mtime,
32
+ file:File::file?(path), name:filename(path), path:filepath(path)
33
+ }
34
+ end
35
+
36
+ def filename(path)
37
+ File.basename(path)
38
+ end
39
+
40
+ def filepath(path)
41
+ File.dirname(path.gsub(@path,""))
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,113 @@
1
+ module Biblioteque
2
+ class Database < Model
3
+ attr_accessor :path, :updated_at
4
+
5
+ def self.create(name, path)
6
+ raise "No" unless name || path
7
+ new_db = {id:generate_id,name:name,created_at:Time.now,updated_at:Time.now}
8
+ File.open(path,"w+") do |f|
9
+ f.write(new_db.to_json)
10
+ end
11
+ true
12
+ end
13
+
14
+ def self.clone(path_source, path_destination)
15
+ raise "No" unless path_source || path_destination
16
+ FileUtils.cp(path_source, path_destination)
17
+ true
18
+ end
19
+
20
+ def self.destroy(path_source)
21
+ File.delete(path_source) ? true : false
22
+ end
23
+
24
+ def initialize(params = {})
25
+ super
26
+ @libraries = []
27
+ @dbpath = params[:path]
28
+ @loaded = false
29
+ @library = nil
30
+ @to_hash_params = [:id, :name, :updated_at, :created_at]
31
+ end
32
+
33
+ def create_library(name)
34
+ raise "No" unless name
35
+ lib = Library.create(name, self.id)
36
+ @libraries << lib
37
+ lib
38
+ end
39
+
40
+ def load_library(library_id)
41
+ @libraries.each do |lib|
42
+ if lib.id == library_id
43
+ @library = lib
44
+ return lib
45
+ end
46
+ end
47
+ nil
48
+ end
49
+
50
+ def close_library
51
+ @library = nil
52
+ end
53
+
54
+ def current_library
55
+ @library
56
+ end
57
+
58
+ def libraries
59
+ @libraries
60
+ end
61
+
62
+ def load
63
+ json = File.read(@dbpath)
64
+ db = JSON.parse(json)
65
+ self.id = db["id"]
66
+ self.name = db["name"]
67
+ self.path = @dbpath
68
+ self.created_at = db["created_at"]
69
+ self.updated_at = db["updated_at"]
70
+ load_libraries(db["libraries"])
71
+ @loaded = true
72
+ end
73
+
74
+ def status
75
+ @loaded ? "Loaded" : "Not Loaded"
76
+ end
77
+
78
+ def save
79
+ db = self.to_hash
80
+ db[:libraries] = []
81
+ libraries.each{|l| db[:libraries] << l.to_hash }
82
+ save_to_file(@dbpath ,db.to_json)
83
+ end
84
+
85
+ def search(filter)
86
+ results = []
87
+ libraries.each do |l|
88
+ results << {library:l.id,results:l.search(filter)}
89
+ end
90
+ results
91
+ end
92
+
93
+ def delete_library(id)
94
+ return false unless id.to_i
95
+ libraries.delete_if{|l| l.id == id }
96
+ true
97
+ end
98
+
99
+ private
100
+
101
+ def load_libraries(libraries_arr)
102
+ return false unless libraries_arr
103
+ libraries_arr.each do |library|
104
+ lib = Library.new(id:library[:id],name:library[:id],
105
+ db_id:library[:db_id],created_at:library[:created_at],
106
+ updated_at:library[:updated_at])
107
+ lib.items = library[:items]
108
+ @libraries << lib
109
+ end
110
+ true
111
+ end
112
+ end
113
+ end
@@ -0,0 +1,48 @@
1
+ module Biblioteque
2
+ class Engine
3
+
4
+ def initialize
5
+ @dbpath = nil
6
+ @db = nil
7
+ end
8
+
9
+ def current_db
10
+ @db
11
+ end
12
+
13
+ def current_db_path
14
+ @dbpath
15
+ end
16
+
17
+ def load_db(path)
18
+ @dbpath = path
19
+ @db = Database.new(path:path)
20
+ @db.load
21
+ @db
22
+ end
23
+
24
+ def create_db(name, path)
25
+ Database.create(name, path)
26
+ end
27
+
28
+ def clone_db(path_source, path_destination)
29
+ Database.clone(path_source, path_destination)
30
+ end
31
+
32
+ def close_db
33
+ @dbpath = nil
34
+ @db = nil
35
+ end
36
+
37
+ def delete_db
38
+ dbpath = current_db_path
39
+ close_db
40
+ Database.destroy(dbpath)
41
+ end
42
+
43
+ def clone_current(path_destination)
44
+ return false unless current_db_path
45
+ Database.clone(current_db_path, path_destination)
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,60 @@
1
+ module Biblioteque
2
+ class Library < Model
3
+ attr_accessor :updated_at, :db_id
4
+
5
+ def self.create(name, db_id)
6
+ raise "No" unless name
7
+ lib = self.new
8
+ lib.id = generate_id
9
+ lib.name = name
10
+ lib.db_id = db_id
11
+ lib.created_at = Time.now
12
+ lib.updated_at = Time.now
13
+ lib
14
+ end
15
+
16
+ def initialize(params = {})
17
+ super
18
+ @items = []
19
+ @to_hash_params = [:id, :name, :updated_at, :created_at, :db_id, :items]
20
+ end
21
+
22
+ def items
23
+ @items
24
+ end
25
+
26
+ def stat
27
+ puts "#Library #{name} status#"
28
+ puts " created at: #{created_at}"
29
+ puts " updated at: #{updated_at}"
30
+ puts " items: #{self.items.size}"
31
+ puts " folders: #{folders.size}"
32
+ puts " files: #{files.size}"
33
+ end
34
+
35
+ def folders
36
+ @items.select{|i| i[:file] == false}
37
+ end
38
+
39
+ def files
40
+ @items.select{|i| i[:file] == true}
41
+ end
42
+
43
+ def add(path)
44
+ raise "No" unless path
45
+ crawler = Crawler.new(path)
46
+ @items = @items | crawler.search_for_library(id)
47
+ true
48
+ end
49
+
50
+ def search(filter)
51
+ return nil unless filter
52
+ filter.downcase!
53
+ items.select do |i|
54
+ i[:path].downcase.index(filter) ||
55
+ i[:name].downcase.index(filter) ||
56
+ i[:id] == filter
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,33 @@
1
+ module Biblioteque
2
+ class Model
3
+ attr_accessor :id, :name, :created_at
4
+
5
+ def initialize(params = {})
6
+ params.each do |attr, value|
7
+ self.public_send("#{attr}=", value) if self.respond_to? "#{attr}="
8
+ end if params
9
+ end
10
+
11
+ def save_to_file(path, data = {})
12
+ File.open(path,"w+") do |f|
13
+ f.write(data.to_json)
14
+ end
15
+ true
16
+ end
17
+
18
+ def to_hash
19
+ hash = {}
20
+ return hash unless @to_hash_params
21
+ @to_hash_params.each {|var| hash[var] = instance_variable_get("@#{var.to_s}") }
22
+ hash
23
+ end
24
+
25
+ def search(filter)
26
+ raise "Not availalable for parent class"
27
+ end
28
+
29
+ def self.generate_id
30
+ SecureRandom.hex
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,3 @@
1
+ module Biblioteque
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,33 @@
1
+ $:.unshift File.dirname(__FILE__)
2
+ require 'test_helper'
3
+ include TestFileOperations
4
+
5
+ describe 'Engine' do
6
+ describe 'create an empty database' do
7
+ let(:engine){setup_test_engine}
8
+ it "should create an empty database" do
9
+ status = engine.create_db("TestDB", "test_path/testdb.ml")
10
+ status.must_equal true
11
+ end
12
+
13
+ it "should load created db" do
14
+ db = engine.load_db("test_path/testdb.ml")
15
+ db.id.must_equal("3423dfgs322dd")
16
+ db.name.must_equal("testname")
17
+ db.status.must_equal("Loaded")
18
+ end
19
+
20
+ it "should save created db" do
21
+ db = engine.load_db("test_path/testdb.ml")
22
+ db.save.must_equal true
23
+ end
24
+
25
+ it "should close loaded db" do
26
+ db = engine.load_db("test_path/testdb.ml")
27
+ db.wont_be_nil
28
+ engine.current_db.wont_be_nil
29
+ engine.close_db
30
+ engine.current_db.must_be_nil
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,31 @@
1
+ $:.unshift File.dirname(__FILE__)
2
+ require 'test_helper'
3
+ include TestFileOperations
4
+
5
+ describe 'Engine' do
6
+ describe 'work with library in a database' do
7
+ let(:engine){setup_test_engine}
8
+ before(:each) do
9
+ @db = engine.load_db("test_path/testdb.ml")
10
+ @db.create_library("TestLibrary")
11
+ end
12
+
13
+ it "should create a new library in a loaded db" do
14
+ @db.id.must_equal("3423dfgs322dd")
15
+ @db.libraries.size.must_equal 1
16
+ @db.libraries.first.id.wont_be_nil
17
+ end
18
+
19
+ it "should add files to a loaded library" do
20
+ library = @db.libraries.first
21
+ library.add("test_crawler_path/")
22
+ library.files.size.must_equal 1
23
+ end
24
+
25
+ it "should delete created library from a loaded db" do
26
+ status = @db.delete_library(@db.libraries.first.id)
27
+ status.must_equal true
28
+ @db.libraries.size.must_equal 0
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,28 @@
1
+ $:.unshift File.dirname(__FILE__)
2
+ require 'test_helper'
3
+ include TestFileOperations
4
+
5
+ describe 'Engine' do
6
+ describe 'do some actions on a database' do
7
+ let(:engine){setup_test_engine}
8
+ it "should clone a database" do
9
+ status = engine.create_db("TestDB", "test_path/testdb.ml")
10
+ status.must_equal true
11
+ status = engine.clone_db("test_path/testdb2.ml", "test_path/testdb.ml")
12
+ status.must_equal true
13
+ end
14
+
15
+ it "should delete created db" do
16
+ db = engine.load_db("test_path/testdb.ml")
17
+ status = engine.delete_db
18
+ status.must_equal true
19
+ engine.current_db.must_be_nil
20
+ end
21
+
22
+ it "should delete db by using db method" do
23
+ File.stubs(:delete).returns(1)
24
+ status = Biblioteque::Database.destroy("test_path/testdb.ml")
25
+ status.must_equal true
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,29 @@
1
+ $:.unshift File.dirname(__FILE__)
2
+ require 'test_helper'
3
+ include TestFileOperations
4
+
5
+ describe 'Engine' do
6
+ describe 'search for file' do
7
+ let(:engine){setup_test_engine}
8
+ before(:each) do
9
+ @db = engine.load_db("test_path/testdb.ml")
10
+ @db.create_library("TestLibrary")
11
+ @library = @db.libraries.first
12
+ @library.add("test_crawler_path/")
13
+ end
14
+
15
+ it "should search entire database for '.txt' file" do
16
+ results = @db.search('.txt')
17
+ results.size.must_equal 1
18
+ results[0][:library].must_equal @library.id
19
+ results[0][:results].size.must_equal 1
20
+ results[0][:results][0][:id].must_equal @library.files.first[:id]
21
+ end
22
+
23
+ it "should search a library for 'path' file" do
24
+ results = @library.search('path')
25
+ results.size.must_equal 1
26
+ results[0][:id].must_equal @library.files.first[:id]
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,21 @@
1
+ require 'minitest/spec'
2
+ require 'minitest/autorun'
3
+ require 'mocha'
4
+ $:.unshift File.dirname(__FILE__) + '/../lib'
5
+ require 'biblioteque'
6
+
7
+ module TestFileOperations
8
+ def setup_test_engine
9
+ #Stubs IO operations for testing purposes
10
+ File.stubs(:open).returns(true)
11
+ File.stubs(:read).returns({id:"3423dfgs322dd",name:"testname",created_at:Time.now,updated_at:Time.now}.to_json)
12
+ File.stubs(:file?).returns(true)
13
+ File.stubs(:delete).returns(1)
14
+ File.stubs(:stat).returns(File::Stat.new("test"))
15
+ FileUtils.stubs(:cp).returns(true)
16
+ FileTest.stubs(:directory?).returns(false)
17
+ Find.stubs(:find).yields("/test_crawler_path/test/path.txt")
18
+ #
19
+ Biblioteque::Engine.new
20
+ end
21
+ end
metadata ADDED
@@ -0,0 +1,134 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: biblioteque
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Eremin Andrey
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-04-18 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: yard
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: minitest
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: mocha
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: redcarpet
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ description: Local files parser. It parses files stored in a local directories and
79
+ writes the information into db in JSON format.
80
+ email:
81
+ - dsoft88@gmail.com
82
+ executables: []
83
+ extensions: []
84
+ extra_rdoc_files: []
85
+ files:
86
+ - .gitignore
87
+ - Gemfile
88
+ - LICENSE
89
+ - README.md
90
+ - Rakefile
91
+ - biblioteque.gemspec
92
+ - lib/biblioteque.rb
93
+ - lib/biblioteque/crawler.rb
94
+ - lib/biblioteque/database.rb
95
+ - lib/biblioteque/engine.rb
96
+ - lib/biblioteque/library.rb
97
+ - lib/biblioteque/model.rb
98
+ - lib/biblioteque/version.rb
99
+ - test/creation_test.rb
100
+ - test/library_test.rb
101
+ - test/operation_test.rb
102
+ - test/search_test.rb
103
+ - test/test_helper.rb
104
+ homepage: http://eremin.me
105
+ licenses: []
106
+ post_install_message:
107
+ rdoc_options: []
108
+ require_paths:
109
+ - lib
110
+ required_ruby_version: !ruby/object:Gem::Requirement
111
+ none: false
112
+ requirements:
113
+ - - ! '>='
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ required_rubygems_version: !ruby/object:Gem::Requirement
117
+ none: false
118
+ requirements:
119
+ - - ! '>='
120
+ - !ruby/object:Gem::Version
121
+ version: '0'
122
+ requirements: []
123
+ rubyforge_project: biblioteque
124
+ rubygems_version: 1.8.25
125
+ signing_key:
126
+ specification_version: 3
127
+ summary: Local files parser. It parses files stored in a local directories and writes
128
+ the information into db in JSON format.
129
+ test_files:
130
+ - test/creation_test.rb
131
+ - test/library_test.rb
132
+ - test/operation_test.rb
133
+ - test/search_test.rb
134
+ - test/test_helper.rb