1
- # coding: utf-8
2
1
require "ht/pairtree/version"
3
2
4
3
# Pairtree is ancient and throws warnings so we suppress them on require
5
4
oldverbose = $VERBOSE
6
5
$VERBOSE = nil
7
- require ' pairtree'
6
+ require " pairtree"
8
7
$VERBOSE = oldverbose
9
8
10
- require ' fileutils'
9
+ require " fileutils"
11
10
12
11
module HathiTrust
13
12
# A simple Pairtree implementation to make it easier to work with
14
13
# the HathiTrust pairtree structure.
15
14
class Pairtree
16
-
17
- class PairtreeError < StandardError ;
15
+ class PairtreeError < StandardError
18
16
end
19
17
20
18
class NamespaceDoesNotExist < PairtreeError
21
19
end
22
20
23
- SDR_DATA_ROOT_DEFAULT = ( ENV [ "SDRDATAROOT" ] || ' /sdr1' ) + ' /obj'
21
+ SDR_DATA_ROOT_DEFAULT = ( ENV [ "SDRDATAROOT" ] || " /sdr1" ) + " /obj"
24
22
25
23
# Create a new pairtree object rooted at the given directory
26
24
# @param [String] root The root of the über-pairtree (takes `ENV['SDRDATAROOT'] + 'obj'` by default)
27
25
def initialize ( root : SDR_DATA_ROOT_DEFAULT )
28
26
@root = Pathname . new ( root )
29
27
end
30
28
31
-
32
-
33
29
# Get a Pathname object corresponding to the directory holding data for the given HTID
34
30
# @param [String] htid The HathiTrust ID for an object
35
31
# @return [Pathname] Path to the given object's directory
@@ -40,58 +36,47 @@ def path_for(htid)
40
36
alias_method :dir , :path_for
41
37
alias_method :path_to , :path_for
42
38
43
-
44
39
# Get the underlying pairtree for the given obj.
45
40
# @param [String] htid The HathiTrust ID for an object
41
+ # @param [Boolean] create Whether to create the pairtree if it doesn't exist
46
42
# @return [Pairtree] the pairtree for that object
47
- def pairtree_for ( htid )
48
- pairtree_root ( htid )
43
+ def pairtree_for ( htid , create : false )
44
+ pairtree_root ( htid , create : create )
49
45
end
50
46
51
- # Create a pairtree for the given htid. Allow namespace creation
52
- # only if told to.
47
+ # Create the pairtree directory for the given htid. Allow namespace
48
+ # creation only if told to.
53
49
# @param htid [String] The HTID
54
50
# @param new_namespace_allowed [Boolean] Whether or not to error if the namespace DNE
55
51
# @raise [NamespaceDoesNotExist] if namespace DNE and new namespace not allowed
56
52
# @return [Pairtree::Obj] the underlying pairtree object
57
53
def create ( htid , new_namespace_allowed : false )
58
- if !namespace_exists? ( htid )
59
- if new_namespace_allowed
60
- create_namespace_dir ( htid )
61
- else
62
- raise NamespaceDoesNotExist . new ( "Namespace #{ namespace ( htid ) } does not exist" )
63
- end
54
+ unless namespace_exists? ( htid ) || new_namespace_allowed
55
+ raise NamespaceDoesNotExist . new ( "Namespace #{ namespace ( htid ) } does not exist" )
64
56
end
65
- pairtree_for ( htid ) . mk ( htid )
66
- end
67
-
68
- def namespace_exists? ( htid )
69
- namespace_dir ( htid ) . exists?
57
+ pairtree_for ( htid , create : new_namespace_allowed ) . mk ( htid )
70
58
end
71
59
60
+ private
72
61
73
- def create_namespace_dir ( htid )
74
- ndir = namespace_dir ( htid )
75
- return self if Dir . exists? ( ndir )
76
- Dir . mkdir ( ndir )
77
- File . open ( ndir + "pairtree_prefix" , 'w:utf-8' ) { |f | f . print namespace ( htid ) }
78
- File . open ( ndir + "pairtree_version0_1" , 'w:utf-8' ) { |f | }
79
- Dir . mkdir ( ndir + "pairtree_root" )
80
- return self
62
+ def namespace_exists? ( htid )
63
+ namespace_dir ( htid ) . exist?
81
64
end
82
65
83
-
84
66
def namespace_dir ( htid )
85
67
@root + namespace ( htid )
86
68
end
87
69
88
70
def namespace ( htid )
89
- htid . split ( '.' , 2 ) . first
71
+ htid . split ( "." , 2 ) . first
90
72
end
91
73
92
- def pairtree_root ( htid )
93
- ::Pairtree . at ( namespace_dir ( htid ) )
74
+ def pairtree_root ( htid , create : false )
75
+ ::Pairtree . at ( namespace_dir ( htid ) , prefix : pairtree_prefix ( htid ) , create : create )
94
76
end
95
77
78
+ def pairtree_prefix ( htid )
79
+ namespace ( htid ) + "."
80
+ end
96
81
end
97
82
end
0 commit comments