def _create_basic_folder_structure(self): """Trigger creation of the logs, misc and online folders. Also copy example tracklogs, if necessary. """ self._get_tf_sub_path("logs") self._get_tf_sub_path("online") self._get_tf_sub_path("misc") # if there is no example folder, create it # and copy example tracklogs into it tfp = self.modrana.paths.tracklog_folder_path examplesDestinationPath = os.path.join(tfp, 'examples') if not os.path.exists(examplesDestinationPath): utils.create_folder_path(examplesDestinationPath) self.log.info(' ** copying example tracklogs') try: examplesSourcePath = 'data/tracklog_examples' # copy all files from this folder for item in os.listdir(examplesSourcePath): path = os.path.join(examplesSourcePath, item) if os.path.isfile(path): self.log.info(' ** copying: %r', item) shutil.copy(path, os.path.join(examplesDestinationPath, item)) self.log.info(' ** DONE') except Exception: self.log.exception("could not copy example tracklogs")
def _get_tf_sub_path(self, subPath): """Return a tracklog folder sub path. Also assure the patch exists before returning it. """ tracklogFolderPath = self.modrana.paths.tracklog_folder_path if tracklogFolderPath is None: self.log.error("can't get tracklog sub path - tracklog folder path is unknown") return None # tracklog folder path is unknown else: TFSubPath = os.path.join(tracklogFolderPath, subPath) utils.create_folder_path(TFSubPath) return TFSubPath
def __init__(self, modrana): self.modrana = modrana # get profile folder path # -> first check for device module override if self.modrana.dmod.profilePath: self._profileFolderPath = self.modrana.dmod.profilePath else: self._profileFolderPath = self.modrana.getProfilePath() # check the profile path and create the folders if necessary utils.create_folder_path(self._profileFolderPath) # load version string self.versionString = None
def getProfilePath(self): """return the profile folder (create it if it does not exist) NOTE: this function is provided here in the main class as some ordinary modRana modules need to know the profile folder path before the option module that normally handles it is fully initialized (for example the config module might need to copy default configuration files to the profile folder in its init) """ # get the path modRanaProfileFolderName = '.modrana' userHomePath = os.getenv("HOME", "") profileFolderPath = os.path.join(userHomePath, modRanaProfileFolderName) # make sure it exists utils.create_folder_path(profileFolderPath) # return it return profileFolderPath
def __init__(self, tsubame): self.tsubame = tsubame # TODO: actually use this # get profile folder path # -> first check for device module override # if self.tsubame.dmod.profile_path: # self._profile_folder_path = self.tsubame.dmod.profile_path # else: # self._profile_folder_path = self.tsubame.get_profile_path() # # check the profile path and create the folders if necessary self._profile_folder_path = get_XDG_config_path() utils.create_folder_path(self._profile_folder_path) # cached XDG paths (so that the xdg-user-dir mus be called only once # per path per Tsubame run) self._xdg_pictures_path = None
def download_file_(url, download_folder, filename): # try to make sure the download directory exists path_to_file = os.path.join(download_folder, filename) log.debug("downloading URL: %s to: %s", url, path_to_file) if not utils.create_folder_path(download_folder): log.error("can't create folder for the file download") return False # NOTE the stream=True parameter # TODO: proper error handling/reporting ;-P r = requests.get(url, stream=True) with open(path_to_file, 'wb') as f: for chunk in r.iter_content(chunk_size=1024): if chunk: # filter out keep-alive new chunks f.write(chunk) return True
def saveToFile(self, filePath): # first try to make sure the folder for storing # the JSON file exists with self._mutex: success = False if utils.create_folder_path(os.path.dirname(filePath)): try: # the Python JSON module has some issues with serializing # unicode strings, so we need to make it dump the dict to # string, utf encode it and then save it to file manually jsonString = json.dumps(self, ensure_ascii=False, indent=True) jsonString.encode('utf8') with open(filePath, "w") as f: f.write(jsonString) success = True except Exception: log.exception("saving to JSON file failed") else: log.error("JSONDict: can't save file to: %s", filePath) return success
def _assure_path(self, path): """assure path exists and return it back""" # check if the path exists and create it if not utils.create_folder_path(path) return path
def profile_path(self): """return path to the profile folder""" # check if the path exists and create it if not utils.create_folder_path(self._profile_folder_path) return self._profile_folder_path
def enable_log_file(self, compression=False): """Enable logging modRana log messages to file. If this is called during startup, early log messages preceding the log file activation are dumped to the log, so no messages from a modRana run should be missing from the log file. """ # attempt to enable the log file with self._log_file_enabled_lock: # check if the log file is not already enabled if self._log_file_enabled: self._root_modrana_logger.error("log file already exist") return # first try to make sure the logging folder actually exists if not utils.create_folder_path(self.log_folder_path): self._root_modrana_logger.error( "failed to create logging folder in: %s", self.log_folder_path) return self._log_file_compression = compression # create a file logger that logs everything log_file_path = os.path.join( self.log_folder_path, self._get_log_filename(compression=compression)) if compression: if sys.version_info >= (3, 0): self._compressed_log_file = gzip.open(log_file_path, mode="wt", encoding="utf-8") else: self._compressed_log_file = gzip.open(log_file_path, mode="wb") self._file_handler = logging.StreamHandler( self._compressed_log_file) else: self._file_handler = logging.FileHandler(log_file_path) self._file_handler.setLevel(logging.DEBUG) full_formatter = logging.Formatter( '%(asctime)s %(levelname)s %(name)s: %(message)s') self._file_handler.setFormatter(full_formatter) # dump any early log messages to the log file if self._memory_handler: self._memory_handler.setTarget(self._file_handler) self._memory_handler.flush() # write all the early log records self._file_handler.flush() # now attach the log file to the root logger self._root_modrana_logger.addHandler(self._file_handler) # flush the memory logger again in case any messages arrived before # the last flush and connecting the log file to the root logger # (this might duplicate some messages, but we should not loose any # as both the MemoryHandler and root logger are connected at the moment) # now flush & nuke the MemoryHandler self._root_modrana_logger.removeHandler(self._memory_handler) self._memory_handler.flush() self._memory_handler.close() self._memory_handler = None else: # just attach the log file to the root logger self._root_modrana_logger.addHandler(self._file_handler) self.log_file_path = log_file_path self._log_file_enabled = True self._root_modrana_logger.info("log file enabled: %s" % log_file_path)