dirtravel 0.0.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.
- data/lib/dirtravel.rb +233 -0
- metadata +65 -0
data/lib/dirtravel.rb
ADDED
@@ -0,0 +1,233 @@
|
|
1
|
+
# DirTravel is a library for getting information about files and
|
2
|
+
# directories recursively to a tree structure. This library extends
|
3
|
+
# the RubyTree classes to include directory and files info. Please
|
4
|
+
# refer to RubyTree documentation for RubyTree related features.
|
5
|
+
#
|
6
|
+
# A proxy object "DirTravel::Travel" is used to gather the directory
|
7
|
+
# content. The "filetree" class method provides interface for getting
|
8
|
+
# the content with few options (see: DirTravel::Travel.filetree for
|
9
|
+
# details).
|
10
|
+
#
|
11
|
+
# Examples:
|
12
|
+
# require 'dirtravel'
|
13
|
+
#
|
14
|
+
# # Collect all entries under '.'
|
15
|
+
# d = DirTravel::Travel.filetree( '.' )
|
16
|
+
#
|
17
|
+
# # Display names of the entries in the hierarhcy.
|
18
|
+
# d.children.each do |i|
|
19
|
+
# puts i.name
|
20
|
+
# end
|
21
|
+
#
|
22
|
+
# # Display absolute path for file (leaf level) entries.
|
23
|
+
# d.each_leaf do |i|
|
24
|
+
# puts i.abspath
|
25
|
+
# end
|
26
|
+
#
|
27
|
+
# # Get MP3 files and create a list of all album directories
|
28
|
+
# # (assuming "...<album>/<song>" hierarhcy).
|
29
|
+
# d = DirTravel::Travel.filetree( '.', { :suffix => '.mp3' } )
|
30
|
+
# albums = d.select_level( d.node_height - 1 )
|
31
|
+
#
|
32
|
+
|
33
|
+
module DirTravel
|
34
|
+
|
35
|
+
require 'rubytree'
|
36
|
+
|
37
|
+
|
38
|
+
# Extend RubyTree base class with file and directory features.
|
39
|
+
class Entry < Tree::TreeNode
|
40
|
+
attr_accessor :name
|
41
|
+
|
42
|
+
def initialize( name )
|
43
|
+
super( name, nil )
|
44
|
+
end
|
45
|
+
|
46
|
+
|
47
|
+
# Return path components as Array.
|
48
|
+
#
|
49
|
+
# @param basedir [Entry] Starting level for the hierarchy.
|
50
|
+
# @return [Array] Array of names in hierarchy.
|
51
|
+
def pathArray( basedir = self )
|
52
|
+
parents = []
|
53
|
+
while basedir
|
54
|
+
parents.push basedir.name
|
55
|
+
basedir = basedir.parent
|
56
|
+
end
|
57
|
+
parents.reverse
|
58
|
+
end
|
59
|
+
|
60
|
+
# Relative path.
|
61
|
+
def path
|
62
|
+
pathArray.join( '/' )
|
63
|
+
end
|
64
|
+
|
65
|
+
# Relative path under root.
|
66
|
+
def subpath
|
67
|
+
pathArray[ 1 .. -1 ].join( '/' )
|
68
|
+
end
|
69
|
+
|
70
|
+
# Absolute path.
|
71
|
+
def abspath
|
72
|
+
File.absolute_path( path )
|
73
|
+
end
|
74
|
+
|
75
|
+
# Return containing directory name.
|
76
|
+
#
|
77
|
+
# @param basedir [Entry] Starting level for the hierarchy.
|
78
|
+
# @return [String] Containing directory.
|
79
|
+
def dir( basedir = self )
|
80
|
+
pathArray( basedir.parent ).join( '/' )
|
81
|
+
end
|
82
|
+
|
83
|
+
# Select all siblings from given node depth.
|
84
|
+
#
|
85
|
+
# @param level [Integer] Selected level in hierachy. Level is
|
86
|
+
# number of step down in hierarhcy.
|
87
|
+
# @return [Array] Array of siblings in selected hierarchy.
|
88
|
+
def select_level( level )
|
89
|
+
select do |i| i.node_depth == level; end
|
90
|
+
end
|
91
|
+
|
92
|
+
# Return all file entries in hierarchy.
|
93
|
+
def files
|
94
|
+
select do |i| i.kind_of?( FileEntry ) end
|
95
|
+
end
|
96
|
+
|
97
|
+
# File.stat data for the Entry.
|
98
|
+
def stat
|
99
|
+
File.stat( path )
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
|
104
|
+
# Directory type entry.
|
105
|
+
class DirEntry < Entry
|
106
|
+
def initialize( name )
|
107
|
+
super( name )
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
|
112
|
+
# File type entry.
|
113
|
+
class FileEntry < Entry
|
114
|
+
attr_reader :suffix, :basename
|
115
|
+
|
116
|
+
def initialize( name )
|
117
|
+
super( name )
|
118
|
+
@suffix = File.extname( name )
|
119
|
+
@basename = File.basename( name, @suffix )
|
120
|
+
end
|
121
|
+
|
122
|
+
end
|
123
|
+
|
124
|
+
|
125
|
+
# Create directory recursion tree (with
|
126
|
+
# Travel.filetree). Optionally filter with suffix and modify
|
127
|
+
# tree building with options Hash (see below).
|
128
|
+
#
|
129
|
+
# == Parameters:
|
130
|
+
# sort::
|
131
|
+
# Sort directory entries (default: false).
|
132
|
+
# suffix::
|
133
|
+
# Limit search to files with "suffix" (default: nil).
|
134
|
+
# files::
|
135
|
+
# Include files to search (default: true).
|
136
|
+
#
|
137
|
+
# Example:
|
138
|
+
# d1 = DirTravel::Travel.filetree( dir1, { :sort => true, :suffix => '.mp3' } )
|
139
|
+
|
140
|
+
class Travel
|
141
|
+
|
142
|
+
# Root DirEntry of Travel.
|
143
|
+
attr_accessor :root
|
144
|
+
|
145
|
+
# Default options for Travel.
|
146
|
+
attr_accessor :defaults
|
147
|
+
|
148
|
+
# Starting directory for Travel
|
149
|
+
attr_accessor :basedir
|
150
|
+
|
151
|
+
# Create directory recursion tree.
|
152
|
+
# @param basedir [String] Starting directory (top).
|
153
|
+
# @param options [Hash] Hash optionally including keys: :sort, :suffix, :files.
|
154
|
+
# @return [DirEntry] Root item of the file system hierarchy.
|
155
|
+
def Travel.filetree( basedir = '.', options = {} )
|
156
|
+
r = Travel.new( basedir, options )
|
157
|
+
r.travel
|
158
|
+
r.root
|
159
|
+
end
|
160
|
+
|
161
|
+
|
162
|
+
def initialize( basedir = '.', options = {} )
|
163
|
+
@basedir = basedir
|
164
|
+
|
165
|
+
@defaults = {
|
166
|
+
:suffix => nil,
|
167
|
+
:sort => false,
|
168
|
+
:files => true,
|
169
|
+
}
|
170
|
+
|
171
|
+
@defaults.merge!( options )
|
172
|
+
|
173
|
+
@root = DirEntry.new( @basedir )
|
174
|
+
end
|
175
|
+
|
176
|
+
|
177
|
+
# Recursively get all files with suffix. Ignore suffix if
|
178
|
+
# suffix is nil.
|
179
|
+
def travel( suffix = @defaults[ :suffix ] )
|
180
|
+
entriesIn( @basedir, @root, suffix )
|
181
|
+
end
|
182
|
+
|
183
|
+
|
184
|
+
private
|
185
|
+
|
186
|
+
# Recursively get all files with suffix. Ignore suffix if
|
187
|
+
# suffix is nil.
|
188
|
+
def entriesIn( dir, node, suffix = nil )
|
189
|
+
|
190
|
+
list = entries( dir )
|
191
|
+
|
192
|
+
list.each do |i|
|
193
|
+
|
194
|
+
if File.file?( dir + '/' + i )
|
195
|
+
|
196
|
+
# File entry.
|
197
|
+
|
198
|
+
if ( !suffix || suffix == File.extname( i ) ) &&
|
199
|
+
@defaults[ :files ]
|
200
|
+
node.add( FileEntry.new( i ) )
|
201
|
+
end
|
202
|
+
|
203
|
+
else
|
204
|
+
|
205
|
+
# Dir entry.
|
206
|
+
|
207
|
+
newNode = DirEntry.new( i )
|
208
|
+
node.add( newNode )
|
209
|
+
entriesIn( dir + '/' + i, newNode, suffix )
|
210
|
+
|
211
|
+
end
|
212
|
+
|
213
|
+
end
|
214
|
+
|
215
|
+
self
|
216
|
+
end
|
217
|
+
|
218
|
+
# Directory entries with dot entries filtered out.
|
219
|
+
def entries( dir )
|
220
|
+
entries = Dir.entries( dir ).select do |i| nonDotEntry( i ); end
|
221
|
+
entries.sort! if @defaults[ :sort ]
|
222
|
+
entries
|
223
|
+
end
|
224
|
+
|
225
|
+
# Test for non-dot-entry.
|
226
|
+
def nonDotEntry( name )
|
227
|
+
( name != '.' && name != '..' )
|
228
|
+
end
|
229
|
+
|
230
|
+
end
|
231
|
+
|
232
|
+
|
233
|
+
end
|
metadata
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: dirtravel
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Tero Isannainen
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-10-08 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rubytree
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 0.8.3
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 0.8.3
|
30
|
+
description: DirTravel provides content of filesystem directories by recursively travelling
|
31
|
+
the directory hierarchy. The content is organized in RubyTree structure which overloaded
|
32
|
+
directory entry methods.
|
33
|
+
email: tero.isannainen@gmail.com
|
34
|
+
executables: []
|
35
|
+
extensions: []
|
36
|
+
extra_rdoc_files: []
|
37
|
+
files:
|
38
|
+
- lib/dirtravel.rb
|
39
|
+
homepage:
|
40
|
+
licenses:
|
41
|
+
- Ruby
|
42
|
+
post_install_message: Check README...
|
43
|
+
rdoc_options: []
|
44
|
+
require_paths:
|
45
|
+
- lib
|
46
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
47
|
+
none: false
|
48
|
+
requirements:
|
49
|
+
- - ! '>='
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: 1.9.3
|
52
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
53
|
+
none: false
|
54
|
+
requirements:
|
55
|
+
- - ! '>='
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: '0'
|
58
|
+
requirements: []
|
59
|
+
rubyforge_project:
|
60
|
+
rubygems_version: 1.8.23
|
61
|
+
signing_key:
|
62
|
+
specification_version: 3
|
63
|
+
summary: Small and simple library for accessing filesystem directories.
|
64
|
+
test_files: []
|
65
|
+
has_rdoc:
|