def _executeLocalizationFileListRetrieval(filename, localizationLevels, localizedSite, localizationUser, loctype, thrift): ''' @param filename: the name of the localization file @param localizationLevels: a list of localization levels that should be checked for the specified localization file @param localizedSite: the site that localization information should be retrieved for (if applicable) @param localizationUser: the user that localization information should be retrieved for (if applicable) @param loctype: the type of localization files to retrieve @param thrift: an instance of the thrift client used to communicate with EDEX @return: a list of the localization files associated with the specified file name wrapped in a server response @summary: this function will execute a list utility command via thrift to retrieve a list of localization files that match the specified file name, that match the specified localization type, and that can be found within the specified localization levels. ''' directory = os.path.dirname(filename) cmds = [] # prepare the localization type localizationType = LocalizationType(loctype) # build the request message req = UtilityRequestMessage() for level in localizationLevels: cmd = ListUtilityCommand() cmd.setSubDirectory(directory) cmd.setRecursive(False) cmd.setFilesOnly(True) cmd.setLocalizedSite(localizedSite) # prepare the localization level localizationLevel = LocalizationLevel(level) # create a localization context localizationContext = LocalizationContext() localizationContext.setLocalizationType(localizationType) localizationContext.setLocalizationLevel(localizationLevel) if level in ['CONFIGURED', 'SITE' ]: localizationContext.setContextName(localizedSite) elif level == 'USER': localizationContext.setContextName(localizationUser) # build the utility command cmd.setContext(localizationContext) cmds.append(cmd) # add the command(s) to the request req.setCommands(cmds) try: serverResponse = thrift.sendRequest(req) except Exception, e: raise RuntimeError, 'Could not retrieve localization file list: ' + str(e)
def setType(self, lt): ''' @summary: Public interface for setting the localization type to use. @param type: Localization type, should be "COMMON_STATIC", "EDEX_STATIC", or "CAVE_STATIC" ''' locType = LocalizationType(lt) self.__context.setLocalizationType(locType)
def sendGridTempData(db, inputFile, userName): fh = None if inputFile != "-": try: fh = open(inputFile, 'r') except: logException("Unable to open the file " + inputFile + " for input") return ("", False) else: fh = sys.stdin # Store the main file # need to convert python bytearray type (which is just a list of ints) # to numpy.int8 type to ensure this data is serialized as bytes request = LocalizationStreamPutRequest() ctx = LocalizationContext() ll = LocalizationLevel("USER") type = LocalizationType("COMMON_STATIC") ctx.setLocalizationType(type) ctx.setLocalizationLevel(ll) ctx.setContextName(userName) request.setContext(ctx) request.setMyContextName(userName) request.setFileName("/gfe/ifpAG/ifpAG" + str(hash(__WsId())) + ".txt") totalSent = 0 finished = False okay = True aGrids = request.getFileName() while not finished: bytes = numpy.asarray(bytearray(fh.read(BUFFER_SIZE), 'ascii'), dtype=numpy.int8) sendSize = len(bytes) request.setOffset(totalSent) totalSent += sendSize request.setBytes(bytes) request.setEnd(sendSize == 0) finished = request.getEnd() try: serverResponse = db.sendRequest(request) if not isinstance(serverResponse, SuccessfulExecution): message = "" if hasattr(serverResponse, "getMessage"): message = serverResponse.getMessage() raise RuntimeError(message) serverResponse = serverResponse.getResponse() except: logException("Unable to save temp ASCII grids file " + aGrids + " to EDEX server.") okay = False break if fh is not None: fh.close() return (aGrids, okay)
def getTextInventory(category): if SourceUser not in ["SITE", "CONFIGURED", "BASE"]: locLevels = [LOCALIZATION_LEVELS[-1]] elif SourceUser == "SITE": locLevels = [LOCALIZATION_LEVELS[-2]] elif SourceUser == "CONFIGURED": locLevels = [LOCALIZATION_LEVELS[1]] else: locLevels = [LOCALIZATION_LEVELS[0]] invTuple = LOCALIZATION_DICT[category] inventory = {} for level in locLevels: req = UtilityRequestMessage() cmds = [] cmd = ListUtilityCommand() cmd.setSubDirectory(invTuple[1]) cmd.setRecursive(False) cmd.setFilesOnly(True) cmd.setLocalizedSite(SourceSite) ctx = LocalizationContext() ll = LocalizationLevel(level) type = LocalizationType(invTuple[0]) ctx.setLocalizationType(type) ctx.setLocalizationLevel(ll) if (level == "USER"): ctx.setContextName(SourceUser) if (level in ["CONFIGURED", "SITE"]): ctx.setContextName(SourceSite) cmd.setContext(ctx) cmds.append(cmd) req.setCommands(cmds) try: serverResponse = Source.sendRequest(req) except: logException("Error getting inventory.") return None for response in serverResponse.getResponses(): for entry in response.getEntries(): if not entry.getProtectedFile(): filePath = entry.getFileName() filename = os.path.split(filePath)[1] shortName = os.path.splitext(filename)[0] record = textInventoryRecord(filePath, entry.getContext(), entry.getProtectedFile()) inventory[shortName] = record return inventory
def __buildInventory(self): invTuple = self.LOCALIZATION_DICT[self.__classType] localLevels = ["BASE", "CONFIGURED", "SITE", "USER"] if self.__user == "SITE": localLevels = localLevels[:-1] if self.__user == "CONFIGURED": localLevels = localLevels[:-2] elif self.__user == "BASE": localLevels = localLevels[0] req = UtilityRequestMessage() cmds = [] for level in localLevels: cmd = ListUtilityCommand() cmd.setSubDirectory(invTuple[1]) cmd.setRecursive(False) cmd.setFilesOnly(True) cmd.setLocalizedSite(self.__siteID) ctx = LocalizationContext() ll = LocalizationLevel(level) type = LocalizationType(invTuple[0]) ctx.setLocalizationType(type) ctx.setLocalizationLevel(ll) if level in ["CONFIGURED", "SITE"]: ctx.setContextName(self.__siteID) elif (level == "USER"): ctx.setContextName(self.__user) cmd.setContext(ctx) cmds.append(cmd) req.setCommands(cmds) try: serverResponse = self.__thrift.sendRequest(req) except Exception, e: raise RuntimeError, "Could not retrieve product inventory: " + str( e)
def outputAG(db, databaseIds, parmIds, startTime, endTime, outputFile, coordConversionString, userName): # get the grid slices (serverFile, okay) = getGridSlices(db, databaseIds, parmIds, startTime, endTime, coordConversionString) if not okay: return 3 # output the grid slices to the output file fh = None if outputFile != "-": try: fh = open(outputFile, 'w') except: logException("Unable to open the file " + outputFile + " for output") return 2 else: fh = sys.stdout logEvent("Outputting ASCII Grid File") request = LocalizationStreamGetRequest() request.setOffset(0) request.setNumBytes(BUFFER_SIZE) ctx = LocalizationContext() ll = LocalizationLevel("USER") type = LocalizationType("COMMON_STATIC") ctx.setLocalizationType(type) ctx.setLocalizationLevel(ll) ctx.setContextName(userName) request.setContext(ctx) request.setMyContextName(ctx.getContextName()) request.setFileName(serverFile) finished = False while not finished: try: serverResponse = db.sendRequest(request) if not isinstance(serverResponse, SuccessfulExecution): message = "" if hasattr(serverResponse, "getMessage"): message = serverResponse.getMessage() raise RuntimeError(message) serverResponse = serverResponse.getResponse() except: logException("Could not retrieve ASCIIGrids file " + serverFile + " from localization server") okay = False break # serverResponse will be returned as a LocalizationStreamPutRequest # object. we'll use its methods to read back the serialized file # data. # bytes get returned to us as an numpy.ndarray bytes = serverResponse.getBytes() fh.write(bytes.tostring()) request.setOffset(request.getOffset() + len(bytes)) finished = serverResponse.getEnd() if fh is not None: fh.close() # delete server-side file req = PrivilegedUtilityRequestMessage() cmds = [] cmd = DeleteUtilityCommand() cmd.setContext(ctx) cmd.setFilename(serverFile) cmd.setMyContextName(ctx.getContextName()) cmds.append(cmd) req.setCommands(cmds) try: serverResponse = db.sendRequest(req) if not isinstance(serverResponse, SuccessfulExecution): message = "" if hasattr(serverResponse, "getMessage"): message = serverResponse.getMessage() raise RuntimeError(message) except: logException("Could not delete temporary file " + serverFile + " from localization server") okay = False if okay: return 0 else: return 3
def deserialize(context): typeString = context.readString() return LocalizationType(typeString)
def __saveText(self): #Saves a text file # read the file from disk f = open(self.__filename, 'r') txt = f.read() f.close() # verify the class based on the contents of the file if self.__classType == "Tool": self.__verifyClass(txt, "class Tool") elif self.__classType == "Procedure": self.__verifyClass(txt, "class Procedure") elif self.__classType == "TextProduct": self.__verifyClass(txt, "class TextProduct") # Store the main file # need to convert python bytearray type (which is just a list of ints) # to numpy.int8 type to ensure this data is serialized as bytes fileBytes = numpy.asarray(bytearray(txt, 'utf8'), dtype=numpy.int8) totalSize = len(fileBytes) request = LocalizationStreamPutRequest() request.setOffset(0) localizationInfo = self.LOCALIZATION_DICT[self.__classType] if self.__user != "SITE": levelName = "USER" else: levelName = self.__user ctx = LocalizationContext() ll = LocalizationLevel(levelName) locType = LocalizationType(localizationInfo[0]) ctx.setLocalizationType(locType) ctx.setLocalizationLevel(ll) if self.__user != "SITE": ctx.setContextName(self.__user) else: ctx.setContextName(self.__siteID) request.setContext(ctx) request.setMyContextName(ctx.getContextName()) request.setFileName(localizationInfo[1] + "/" + self.__name + self.EXTENSION_DICT[self.__classType]) totalSent = 0 finished = False while (not finished): request.setOffset(totalSent) sendBuffer = fileBytes[:self.BUFFER_SIZE] fileBytes = fileBytes[self.BUFFER_SIZE:] totalSent += len(sendBuffer) request.setBytes(sendBuffer) request.setEnd(totalSent == totalSize) finished = request.getEnd() try: serverResponse = self.__thrift.sendRequest(request) if not isinstance(serverResponse, SuccessfulExecution): message = "" if hasattr(serverResponse, "getMessage"): message = serverResponse.getMessage() raise RuntimeError(message) serverResponse = serverResponse.getResponse() except Exception, e: raise RuntimeError("Could not send file to localization server: " + str(e))