orchard 0.1 → 0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,38 @@
1
+ Orchard is a Ruby library for working with Pairtrees, a filesystem hierarchy
2
+ mapping identifiers to object directories.
3
+
4
+ More information can be found at:
5
+
6
+ Pairtrees for Object Storage
7
+ https://confluence.ucop.edu/display/Curation/PairTree
8
+
9
+ ==== Usage Examples
10
+
11
+ ===== Encoding/decoding strings according to the Pairtree specification
12
+
13
+ Orchard::Pairtree.encode('ark:/13030/xt12t3')
14
+ # => ark+=13030=xt12t3
15
+
16
+ Orchard::Pairtree.decode('ark+=13030=xt12t3')
17
+ # => ark:/13030/xt12t3
18
+
19
+ ===== Converting IDs to pairpaths and pairpaths to IDs
20
+
21
+ Orchard::Pairtree.id_to_ppath('ark:/13030/xt12t3')
22
+ # => ar/k+/=1/30/30/=x/t1/2t/3
23
+
24
+ Orchard::Pairtree.ppath_to_id('ar/k+/=1/30/30/=x/t1/2t/3')
25
+ # => ark:/13030/xt12t3
26
+
27
+ ===== Iterating through a Pairtree for processing
28
+
29
+ Orchard::Pairtree.iterate('repo/pairtree_root/', {:raise_errors => true}) do |path|
30
+ puts path
31
+ end
32
+ # => /absolute_path/repo/pairtree_root/ab/cd/e/object
33
+ ...
34
+ # => /absolute_path/repo/pairtree_root/xy/z/object
35
+
36
+ ==== Thanks
37
+ Erik Hetzner for contributions.
38
+ Ben O'Steen (this module is a semi-port of his Python Pairtree module, http://github.com/benosteen/pairtree)
@@ -1,5 +1,6 @@
1
1
  require 'orchard/pairtree'
2
2
  require 'orchard/version'
3
+ require 'find'
3
4
 
4
5
  # Orchard is a Ruby library for working with Pairtrees, a filesystem hierarchy
5
6
  # mapping identifiers to object directories.
@@ -11,19 +12,19 @@ require 'orchard/version'
11
12
  #
12
13
  # ==== Usage Examples
13
14
  #
14
- # Pairtree.encode('ark:/13030/xt12t3')
15
+ # Orchard::Pairtree.encode('ark:/13030/xt12t3')
15
16
  # # => ark+=13030=xt12t3
16
17
  #
17
- # Pairtree.decode('ark+=13030=xt12t3')
18
+ # Orchard::Pairtree.decode('ark+=13030=xt12t3')
18
19
  # # => ark:/13030/xt12t3
19
20
  #
20
- # Pairtree.id_to_ppath('ark:/13030/xt12t3')
21
+ # Orchard::Pairtree.id_to_ppath('ark:/13030/xt12t3')
21
22
  # # => ar/k+/=1/30/30/=x/t1/2t/3
22
23
  #
23
- # Pairtree.ppath_to_id('ar/k+/=1/30/30/=x/t1/2t/3')
24
+ # Orchard::Pairtree.ppath_to_id('ar/k+/=1/30/30/=x/t1/2t/3')
24
25
  # # => ark:/13030/xt12t3
25
26
 
26
27
  module Orchard
27
- class InvalidPPathError < StandardError
28
- end
28
+ class InvalidPPathError < StandardError; end
29
+ class UnexpectedPairpathError < Exception; end
29
30
  end
@@ -1,3 +1,4 @@
1
+ require 'find'
1
2
  module Orchard
2
3
  # Provides a set of methods for working with Pairtree paths.
3
4
  class Pairtree
@@ -8,6 +9,53 @@ module Orchard
8
9
  CHAR_ENCODE_CONV = {'/'=>'=',':'=>'+','.'=>','}
9
10
  CHAR_DECODE_CONV = {'='=>'/','+'=>':',','=>'.'}
10
11
 
12
+ # ---------------------------
13
+ # Instance Methods
14
+ # ---------------------------
15
+ def initialize(*args)
16
+ path = args[0]
17
+ options = args[1] || {}
18
+
19
+ end
20
+
21
+ def each
22
+ dirs = ["pairtree_root"]
23
+ excludes = []
24
+ for dir in dirs
25
+ Find.find(dir) do |path|
26
+ if FileTest.directory?(path)
27
+ if excludes.include?(File.basename(path))
28
+ Find.prune # Don't look any further into this directory.
29
+ else
30
+ next
31
+ end
32
+ else
33
+ p path
34
+ end
35
+ end
36
+ end
37
+ end
38
+
39
+ def test(path)
40
+ begin
41
+ if File.lstat(path).directory?
42
+ begin
43
+ dir = Dir.open(path)
44
+ dir.each do |f|
45
+ unless f == "." or f == ".."
46
+ test(f)
47
+ end
48
+ end
49
+ ensure
50
+ dir.close
51
+ end
52
+ end
53
+ end
54
+ end
55
+
56
+ # ---------------------------
57
+ # Class Methods
58
+ # ---------------------------
11
59
  # Encodes a given +id+ <em>(String)</em> according to the "identifier string
12
60
  # cleaning" in the pairtree 0.1 specification.
13
61
  #
@@ -89,7 +137,7 @@ module Orchard
89
137
  #
90
138
  # ==== Options
91
139
  # * <tt>:prefix => Pairtree prefix</tt> - This will remove the prefix from the id
92
- # before creating a pairpath.
140
+ # before creating a pairpath. (String)
93
141
  #
94
142
  # ==== Examples
95
143
  #
@@ -143,7 +191,7 @@ module Orchard
143
191
  #
144
192
  # ==== Options
145
193
  # * <tt>:prefix => Pairtree prefix</tt> - This will remove the prefix from the id
146
- # before creating a pairpath.
194
+ # before creating a pairpath. (String)
147
195
  #
148
196
  # ==== Examples
149
197
  #
@@ -166,6 +214,52 @@ module Orchard
166
214
  options[:prefix].nil? ? id : options[:prefix] + id
167
215
  end
168
216
 
217
+ # Iterates a given +pairpath+ with a +block+.
218
+ #
219
+ # iterate(pairtree_path,options,&block)
220
+ # # pairtree_path is a String
221
+ #
222
+ # ==== Options
223
+ # * <tt>:raise_errors => Raise encountered errors/tt> - This will show (true) or surpress
224
+ # (false) ecountered errors. (Boolean)
225
+ # * <tt>:error_handling => Function to call on error</tt> - The Proc will execute if errors occur.
226
+ # Error passed into Proc as parameter. (Proc or Nil)
227
+ #
228
+ # ==== Examples
229
+ #
230
+ # Pairtree.iterate('repo/pairtree_root/', true) do |path|
231
+ # puts path
232
+ # end
233
+ # # => /absolute_path/repo/pairtree_root/ab/cd/e/object
234
+ # ...
235
+ # # => /absolute_path/repo/pairtree_root/xy/z/object
236
+ #
237
+ def self.iterate(*args,&block)
238
+ #pairtree_path,raise_errors=false,error_handling=nil,&block
239
+ ppath = args[0]
240
+ options = args[1] || {}
241
+ Find.find(ppath) do |entry|
242
+ begin
243
+ if File.directory?(entry)
244
+ case entry
245
+ when /^.*\/[^\/:.]{1,2}$/ # in pairtree
246
+ when /^.*[^\/]{3,}$/ # found object
247
+ block.call(File.absolute_path(entry))
248
+ Find.prune
249
+ when ppath # ignore initial path
250
+ else
251
+ raise UnexpectedPairpathError, File.absolute_path(entry)
252
+ end
253
+ else
254
+ raise UnexpectedPairpathError, File.absolute_path(entry)
255
+ end
256
+ rescue Exception => e
257
+ options[:error_handling].call(e) unless options[:error_handling].nil?
258
+ raise e if options[:raise_errors] == true
259
+ end
260
+ end
261
+ end
262
+
169
263
  private
170
264
  # Internal - split a string into a directory path by shorty length.
171
265
  def self.string_to_dirpath(s, dir_length_max)
@@ -1,3 +1,3 @@
1
1
  module Orchard
2
- VERSION = '0.1'
2
+ VERSION = '0.2'
3
3
  end
metadata CHANGED
@@ -4,8 +4,8 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 1
8
- version: "0.1"
7
+ - 2
8
+ version: "0.2"
9
9
  platform: ruby
10
10
  authors:
11
11
  - Stephanie Collett
@@ -13,7 +13,7 @@ autorequire:
13
13
  bindir: bin
14
14
  cert_chain: []
15
15
 
16
- date: 2010-12-17 00:00:00 -08:00
16
+ date: 2011-08-26 00:00:00 -07:00
17
17
  default_executable:
18
18
  dependencies:
19
19
  - !ruby/object:Gem::Dependency
@@ -43,9 +43,9 @@ files:
43
43
  - lib/orchard/version.rb
44
44
  - lib/orchard.rb
45
45
  - LICENSE
46
- - README.md
46
+ - README.rdoc
47
47
  has_rdoc: true
48
- homepage:
48
+ homepage: http://github.com/cdlib/orchard
49
49
  licenses: []
50
50
 
51
51
  post_install_message:
@@ -73,7 +73,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
73
73
  version: 1.3.6
74
74
  requirements: []
75
75
 
76
- rubyforge_project:
76
+ rubyforge_project: orchard
77
77
  rubygems_version: 1.3.7
78
78
  signing_key:
79
79
  specification_version: 3
data/README.md DELETED
@@ -1,21 +0,0 @@
1
- Orchard is a Ruby library for working with Pairtrees, a filesystem hierarchy
2
- mapping identifiers to object directories.
3
-
4
- More information can be found at:
5
-
6
- Pairtrees for Object Storage
7
- https://confluence.ucop.edu/display/Curation/PairTree
8
-
9
- ==== Usage Examples
10
-
11
- Pairtree.encode('ark:/13030/xt12t3')
12
- # => ark+=13030=xt12t3
13
-
14
- Pairtree.decode('ark+=13030=xt12t3')
15
- # => ark:/13030/xt12t3
16
-
17
- Pairtree.id_to_ppath('ark:/13030/xt12t3')
18
- # => ar/k+/=1/30/30/=x/t1/2t/3
19
-
20
- Pairtree.ppath_to_id('ar/k+/=1/30/30/=x/t1/2t/3')
21
- # => ark:/13030/xt12t3