[Zooimage-commits] r117 - pkg/zooimage/R

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Mon Apr 27 14:50:24 CEST 2009


Author: romain
Date: 2009-04-27 14:50:23 +0200 (Mon, 27 Apr 2009)
New Revision: 117

Modified:
   pkg/zooimage/R/zid.r
Log:
simplifications

Modified: pkg/zooimage/R/zid.r
===================================================================
--- pkg/zooimage/R/zid.r	2009-04-27 12:30:13 UTC (rev 116)
+++ pkg/zooimage/R/zid.r	2009-04-27 12:50:23 UTC (rev 117)
@@ -291,23 +291,22 @@
 
 # {{{ compress.zid
 # Compress one sample as a single .zid zipped file	
-"compress.zid" <- function(zidir, type = "ZI1", check = TRUE, check.vignettes = TRUE,
+"compress.zid" <- function(zidir, type = "ZI1", check = TRUE, 
+	check.vignettes = TRUE,
     replace = FALSE, delete.source = replace,
 	check.zip = TRUE, show.log = TRUE) {
 		
-	# {{{ check the format
+	# check the format
 	if (type != "ZI1") {
 		stop("only 'ZI1' is currently supported for 'type'!")
 	}
-	# }}}
 	
-	# {{{ We need to switch to the root of sample dir first for correct path in the zip file
+	# We need to switch to the root of sample dir first for correct path in the zip file
 	rootdir <- dirname(zidir)
 	inidir <- setwd(rootdir); on.exit(setwd(inidir))
 	zidir <- basename(zidir) # Use only the latest dir (the "sample dir")
-	# }}}
 	
-	# {{{ The .zid file is located in the "root" dir, same name as the "sample dir", with .zid extension
+	# The .zid file is located in the "root" dir, same name as the "sample dir", with .zid extension
 	zidfile <- paste(zidir, ".zid", sep = "")
 	if (!replace && file.exists(zidfile)) {
 		# It is not advised to delete source without rebuilding the .zid file
@@ -318,46 +317,19 @@
 		}
 		return(invisible(TRUE))	# Nothing else to do
 	}
-	# }}}
 	
-	# {{{ This requires the 'zip' program!
-	# Make sure it is available
-	if (check.zip ) {
-		checkCapable( "zip" )
-	}
-	# }}}
-	
-	# {{{ Make sure everything is fine for this directory
-	ok <- TRUE
+	# Make sure everything is fine for this directory
 	if (check) {
-		tryCatch( { 
-			verify.zid(zidir, type = type, check.vignettes = check.vignettes, show.log = FALSE)
-		} , zooImageError = function( e ){
-			logError( e )
-			ok <<- FALSE
-		} )
+		verify.zid(zidir, type = type, check.vignettes = check.vignettes, show.log = FALSE)
 	}
-	if (!ok) {
-		return(invisible(FALSE)) 
-	}
-	# }}}
 	
-	# {{{ Make sure the .RData file is created (or refreshed)
-	# TODO: this needs to be simplified, the calling handlers should be invisible 
-	#       here.
-	tryCatch( { 
-		make.RData(zidir, type = type, replace = replace, show.log = FALSE)
-	}, zooImageError = function(e){ # "has no .RData (error while creating it)!"
-		logError( e, msg = "has no .RData (error while creating it)!" )
-		ok <<- FALSE
-	} )
-	if( !ok ) return( invisible( FALSE ) )
-	# }}}
+	# Make sure the .RData file is created (or refreshed)
+	make.RData(zidir, type = type, replace = replace, show.log = FALSE)
 	
-	# {{{ Do compress the directory in the .zip file
-	#     Copy or move all corresponding files to a .zid zip-compressed file
+	# Do compress the directory in the .zip file
+	# Copy or move all corresponding files to a .zid zip-compressed file
 	invisible( zip( zidfile, zidir, delete.source = delete.source ) )
-	# }}}
+	
 }
 # }}}
 
@@ -370,18 +342,14 @@
 	# {{{ check the type 
 	#     COMMENT: do we reallyt need to do that since it will be done also 
 	#              in the compress.zid function ?
-	if (type != "ZI1") stop("only 'ZI1' is currently supported for 'type'!")
+	if (type != "ZI1"){
+		stop("only 'ZI1' is currently supported for 'type'!")
+	}
 	# }}}
 	
-	# {{{ This requires the 'zip' program!, Make sure it is available
-	#     COMMENT : do we really need that
-	checkZipAvailable()
-	# }}}
-	
 	# {{{ First, switch to that directory                                       
 	inidir <- getwd()
-	if (!file.exists(path) || !file.info(path)$isdir)
-		stop(path, " does not exist, or it is not a directory!")
+	checkDirExists( path )
 	setwd(path)
 	on.exit(setwd(inidir))
 	path <- "."	# Indicate we are now in the right path
@@ -395,16 +363,18 @@
 	# }}}
 	
 	# {{{ If there is no dir, exit now
-	if (is.null(samples) || length(samples) == 0)
+	if (is.null(samples) || length(samples) == 0){
 		stop("There is no directories to process in ", getwd())
+	}
 	# }}}
 		
 	# {{{ Start the process
 	logClear()
 	if (check) {
 		logProcess("Verification...")
-		verify.zid.all(path = path, samples = samples, check.vignettes = check.vignettes, 
-            show.log = FALSE, bell = FALSE)
+		verify.zid.all(path = path, samples = samples, 
+			check.vignettes = check.vignettes, 
+			show.log = FALSE, bell = FALSE)
 		# COMMENT: the previous version did log this message instead of the one
 		# that is generated by verify.zid.all
 		# "contains corrupted files, compression not started!"
@@ -422,31 +392,27 @@
 		# from compress.zid when it starts to indicates it has started
 		Progress(s, smax)  
 		
-		# if (!compress.zid(samples[s], type = type, check = FALSE, check.vignettes = check.vignettes, 
-        #     replace = replace, delete.source = delete.source, check.zip = FALSE, show.log = FALSE)) {
-		# 	ok <- FALSE
-		# } else {
-		# 	logProcess("OK", samples[s])
-		# }
-		withRestarts( withCallingHandlers({  
-			compress.zid(samples[s], type = type, check = FALSE, check.vignettes = check.vignettes, 
-        	     replace = replace, delete.source = delete.source, check.zip = FALSE, show.log = FALSE)
+		tryCatch({  
+			compress.zid(samples[s], type = type, check = FALSE, 
+				check.vignettes = check.vignettes, 
+				replace = replace, delete.source = delete.source, 
+				check.zip = FALSE, show.log = FALSE)
 			}, zooImageError = function( e ){
 				logError( e )
-				invokeRestart( "zooImageError" )
+				ok <<- FALSE
 			}, zooImageWarning = function(w){
-				logWarning( w )
-			}) , zooImageError = function( e){
+				logWarning( e )
 				ok <<- FALSE
-			}) 
-		
+			})
 	}
 	ClearProgress()
 	# }}}
 	
 	# {{{ Possibly clean the whole directory (move .zim files to \_raw
 	#     and delete the \_work subdir if everything is fine
-	if (ok) clean.after.zid(path = path, samples = samples) 
+	if (ok){
+		clean.after.zid(path = path, samples = samples)
+	}
     # }}}
 	
 	# {{{ clean up
@@ -460,13 +426,14 @@
 "clean.after.zid" <- function(path = ".", samples = NULL) {
     
 	# {{{ Do we have samples to process
-    if (length(samples) == 0) return()
+    if (length(samples) == 0){
+		return()
+	}
 	# }}}
 	
     # {{{ First, switch to that directory
 	inidir <- getwd()
-    if (!file.exists(path) || !file.info(path)$isdir)
-		stop(path, " does not exist, or it is not a directory!")
+    checkDirExists( path )
 	setwd(path); on.exit(setwd(inidir))
 	# }}}
 	
@@ -476,8 +443,8 @@
 	# }}}
     
 	# {{{ identify paths
-	zimfiles <- list.zim(path = "." )
-    zimsamples <- sub("^(.*)[+].+", "\\1", zimfiles)
+	zimfiles   <- list.zim(path = "." )
+	zimsamples <- sub("^(.*)[+].+", "\\1", zimfiles)
 	# }}}
 	
     # {{{ Keep only those .zim files related to samples
@@ -488,19 +455,17 @@
     if (length(zimfiles) > 0) {
         rawdir <- file.path(".", "_raw")
         
-		# {{{ If the _raw subdirectory does not exists, create it
+		# If the _raw subdirectory does not exists, create it
         if (!file.exists(rawdir)){
 			dir.create(rawdir)
 		}
-		# }}}
-		
-        copyto <- file.path(".", "_raw", zimfiles)
+		copyto <- file.path(".", "_raw", zimfiles)
         
-		# {{{ Move these .zim files
+		# Move these .zim files
         for (i in 1:length(zimfiles)){
 			file.rename(zimfiles[i], copyto[i])
 		}
-		# }}}
+		
     }
 	# }}}
 	
@@ -515,13 +480,12 @@
 #' Use 'delete.source = TRUE' with caution!
 "uncompress.zid" <- function(zidfile, path = dirname(zidfile), delete.source = FALSE, show.log = TRUE) {
 	
-	# {{{ Check if the file provided is a .zid file, and if it exists
+	# Check if the file provided is a .zid file, and if it exists
 	checkFileExists( zidfile, extension = "zid" )
-	# }}}
-		
-	# {{{ Uncompress it
+	
+	# Uncompress it
 	unzip( zidfile, path, delete.source = delete.source )
-	# }}}
+	
 }
 # }}}
 
@@ -531,70 +495,50 @@
 	path.extract = path, skip.existing.dirs = TRUE, 
 	delete.source = FALSE, show.log = TRUE, bell = FALSE) {
 	
-	# {{{ initial checks
+	# initial checks
 	if (is.null(zidfiles) || length(zidfiles) == 0) {
         stop("no .zid files found!" )
 	}
-	# }}}
 	
-	# {{{ start the process
+	# start the process
 	logClear()
 	ok <- TRUE
-	# }}}
+
+	# Check that dirs / files with corresponding names exist in path.extract
+	checkdirs  <- file.path(path.extract, noext(zidfiles))
+	fileExists <- file.exists(checkdirs) & !file.info(checkdirs)$isdir
+	dirExists  <- file.exists(checkdirs) & file.info(checkdirs)$isdir
 	
-	# {{{ Check that dirs / files with corresponding names exist in path.extract
-    checkdirs <- file.path(path.extract, noext(zidfiles))
-	fileExists <- rep(FALSE, length(checkdirs))
-	dirExists <- rep(FALSE, length(checkdirs))
-	for (i in 1:length(checkdirs)) {
-		if (file.exists(checkdirs[i])) {
-			if (file.info(checkdirs[i])$isdir) {
-				dirExists[i] <- TRUE
-			} else {
-				fileExists[i] <- TRUE
-			}
-		}
-	}
-	# }}}
-	
-	# {{{ If any file not being a dir exist there, stop the process
-	if (any(fileExists)) {
+	# If any file not being a dir exist there, stop the process
+	if ( any(fileExists) ) {
         stop("one or several files have same name as uncompressed dirs!" )
 	}
-	# }}}
 	
-	# {{{ Should we eliminate files whose corresponding dirs exist?
+	# Should we eliminate files whose corresponding dirs exist?
 	if (skip.existing.dirs && any(dirExists)) {
 		cat(sum(dirExists), "file(s) already uncompressed is/are skipped!\n")
         warning( paste( "Skipping already uncompressed file(s):", 
 						  paste( zidfiles[dirExists], collapse = ",") )  )
 	}
 	zidfiles <- zidfiles[!dirExists]
-	# }}}
 	
-	# {{{ Decompress the files remaining in the list
+	# Decompress the files remaining in the list
 	smax <- length(zidfiles)
 	if (smax == 0) {
 		cat("-- Done! - (nothing to decompress)\n")
 		stop("\n -- Done! -- (nothing to decompress)", show.log = show.log)
 	}
-	# }}}
 	
-	# {{{ Start decompression
+	# Start decompression
 	cat("Decompression...\n")
 	logProcess("\nDecompression...")
 	for (s in 1:smax) {
 		Progress(s, smax) 
 		uncompress.zid(zidfiles[s], path = path.extract,
-		  delete.source = delete.source, show.log = FALSE)
+			delete.source = delete.source, show.log = FALSE)
 	}
-	# }}}
-	
-	# TODO: set the calling handlers in a way that ok is created
-	ok <- TRUE
-	
 	ClearProgress()
-	finish.loopfunction( ok, bell = bell, show.log = show.log )
+	finish.loopfunction( TRUE, bell = bell, show.log = show.log )
 }
 # }}}
 
@@ -611,9 +555,9 @@
 "read.zid" <- function(zidfile) {
 	
 	# {{{ identify the file and stop if it does not exists
-	sample <- noext(basename(zidfile))
-	RdataFile <- paste(sample, "_dat1.RData", sep ="")
-	deletefile <- FALSE
+	sample      <- noext(basename(zidfile))
+	RdataFile   <- paste(sample, "_dat1.RData", sep ="")
+	deletefile  <- FALSE
 	checkFileExists( zidfile, "%s not found!" )
 	# }}}
 	
@@ -635,8 +579,9 @@
 		# {{{ This is a .zid file
 		rdata <- file.path(sample, RdataFile)
 		rdata <- zid.extract(rdata, zidfile)
-		if (rdata == "")
+		if (rdata == ""){
 			stop( sprintf( "Error reading .RData file from %s", zidfile) )
+		}
 		deletefile <- TRUE
 		# }}}
 	}



More information about the Zooimage-commits mailing list