1- # coding: utf-8
21require "ht/pairtree/version"
32
43# Pairtree is ancient and throws warnings so we suppress them on require
54oldverbose = $VERBOSE
65$VERBOSE = nil
7- require ' pairtree'
6+ require " pairtree"
87$VERBOSE = oldverbose
98
10- require ' fileutils'
9+ require " fileutils"
1110
1211module HathiTrust
1312 # A simple Pairtree implementation to make it easier to work with
1413 # the HathiTrust pairtree structure.
1514 class Pairtree
16-
17- class PairtreeError < StandardError ;
15+ class PairtreeError < StandardError
1816 end
1917
2018 class NamespaceDoesNotExist < PairtreeError
2119 end
2220
23- SDR_DATA_ROOT_DEFAULT = ( ENV [ "SDRDATAROOT" ] || ' /sdr1' ) + ' /obj'
21+ SDR_DATA_ROOT_DEFAULT = ( ENV [ "SDRDATAROOT" ] || " /sdr1" ) + " /obj"
2422
2523 # Create a new pairtree object rooted at the given directory
2624 # @param [String] root The root of the über-pairtree (takes `ENV['SDRDATAROOT'] + 'obj'` by default)
2725 def initialize ( root : SDR_DATA_ROOT_DEFAULT )
2826 @root = Pathname . new ( root )
2927 end
3028
31-
32-
3329 # Get a Pathname object corresponding to the directory holding data for the given HTID
3430 # @param [String] htid The HathiTrust ID for an object
3531 # @return [Pathname] Path to the given object's directory
@@ -40,58 +36,47 @@ def path_for(htid)
4036 alias_method :dir , :path_for
4137 alias_method :path_to , :path_for
4238
43-
4439 # Get the underlying pairtree for the given obj.
4540 # @param [String] htid The HathiTrust ID for an object
41+ # @param [Boolean] create Whether to create the pairtree if it doesn't exist
4642 # @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 )
4945 end
5046
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.
5349 # @param htid [String] The HTID
5450 # @param new_namespace_allowed [Boolean] Whether or not to error if the namespace DNE
5551 # @raise [NamespaceDoesNotExist] if namespace DNE and new namespace not allowed
5652 # @return [Pairtree::Obj] the underlying pairtree object
5753 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" )
6456 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 )
7058 end
7159
60+ private
7261
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?
8164 end
8265
83-
8466 def namespace_dir ( htid )
8567 @root + namespace ( htid )
8668 end
8769
8870 def namespace ( htid )
89- htid . split ( '.' , 2 ) . first
71+ htid . split ( "." , 2 ) . first
9072 end
9173
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 )
9476 end
9577
78+ def pairtree_prefix ( htid )
79+ namespace ( htid ) + "."
80+ end
9681 end
9782end
0 commit comments