svn: r7213
This commit is contained in:
Alex Roitman 2006-08-20 18:28:57 +00:00
parent e64b08618c
commit 1c44900b76

View File

@ -545,35 +545,40 @@ class Convert2Rel(BatchOp):
db_dir = os.path.normpath(os.path.dirname(self.db.full_name)) db_dir = os.path.normpath(os.path.dirname(self.db.full_name))
for handle in self.handle_list: for handle in self.handle_list:
obj = self.db.get_object_from_handle(handle) obj = self.db.get_object_from_handle(handle)
new_path = self.get_rel_path(db_dir,obj.path) new_path = get_rel_path(db_dir,obj.path)
obj.set_path(new_path) obj.set_path(new_path)
self.db.commit_media_object(obj,self.trans) self.db.commit_media_object(obj,self.trans)
self.update() self.update()
return True return True
def get_rel_path(self,db_dir,obj_path): #------------------------------------------------------------------------
obj_dir = os.path.dirname(os.path.normpath(obj_path)) #
obj_name = os.path.basename(os.path.normpath(obj_path)) # Helper functions
#
#------------------------------------------------------------------------
def get_rel_path(db_dir,obj_path):
obj_dir = os.path.dirname(os.path.normpath(obj_path))
obj_name = os.path.basename(os.path.normpath(obj_path))
# Get the list of dirnames for each # Get the list of dirnames for each
db_dir_list = [word for word in db_dir.split(os.path.sep) if word] db_dir_list = [word for word in db_dir.split(os.path.sep) if word]
obj_dir_list = [word for word in obj_dir.split(os.path.sep) if word] obj_dir_list = [word for word in obj_dir.split(os.path.sep) if word]
# The worst case scenario: nothing in common: # The worst case scenario: nothing in common:
# we would need to go ndirs up and then use the full obj path # we would need to go ndirs up and then use the full obj path
ndirs = len(db_dir_list) ndirs = len(db_dir_list)
# Compare words in both lists # Compare words in both lists
for word_ix in range(len(db_dir_list)): for word_ix in range(len(db_dir_list)):
# A common word reduces the trip by one .. and one word # A common word reduces the trip by one '../' and one word
if db_dir_list[word_ix] == obj_dir_list[word_ix]: if db_dir_list[word_ix] == obj_dir_list[word_ix]:
ndirs -= 1 ndirs -= 1
else: else:
break break
up_from_db = '../'*ndirs up_from_db = '../'*ndirs
obj_dir_rem = os.path.sep.join(obj_dir_list[-ndirs:]) obj_dir_rem = os.path.sep.join(obj_dir_list[len(db_dir_list)-ndirs:])
return os.path.join(up_from_db,obj_dir_rem,obj_name) return os.path.join(up_from_db,obj_dir_rem,obj_name)
#------------------------------------------------------------------------ #------------------------------------------------------------------------
# #