def configure_logging(file_path, log_level=logging.WARNING, file_size=10 * MB, keep_logs=10): global _root_logger_configured assert _root_logger_configured is False, 'root_logger is already configured!' file_formatter = logging.Formatter( '%(asctime)s %(processName)-10s %(name)s %(levelname)-8s %(message)s') console_formatter = logging.Formatter( '%(name)-15s %(levelname)-8s %(message)s') log_file_path = Path(file_path) make_directories(log_file_path.parent, exist_ok=True) fh = logging.handlers.RotatingFileHandler(log_file_path, 'a', file_size, keep_logs) fh.setFormatter(file_formatter) fh.setLevel(logging.DEBUG) ch = logging.StreamHandler() ch.setFormatter(console_formatter) ch.setLevel(log_level) _root_logger.addHandler(fh) _root_logger.addHandler(ch) _root_logger.setLevel(logging.DEBUG) _root_logger_configured = True
def _ensure_file(self): if self._config_path.exists(): logger.debug(f'Config exists at {self._config_path!r}') return # self._load_defaults() # self._load_fields() make_directories(self._config_path.parent, exist_ok=True) self.save()
def create_project(project_name, project_path): if not project_name.isidentifier(): return f'Project name must be a valid identifier, got {project_name!r}.' if project_path is None: project_path = get_current_working_directory('.') project_directory = join_paths(project_path, project_name) try: make_directories(project_directory) except FileExistsError: return f'{project_directory!r} already exists' except OSError as err: return render_exception(err) create_file_structure(project_directory, PROJECT_STRUCTURE) return f'Project created at: {project_name!r}'
def create_project(project_name, project_path): if not project_name.isidentifier(): return f'Project name must be a valid identifier, got {project_name!r}.' if project_path is None: project_path = get_current_working_directory('.') project_directory = join_paths(project_path, project_name) try: make_directories(project_directory) except FileExistsError: return f'{project_directory!r} already exists' except OSError as err: return render_exception(err) for paths, file_content in PROJECT_TEMPLATE: paths_length = len(paths) file_name = paths[paths_length - 1] if paths_length == 1: paths = None else: paths = paths[:paths_length - 1] if (paths is None): folder_path = project_directory else: folder_path = join_paths(project_directory, *paths) make_directories(folder_path, exist_ok=True) if (file_name is not None): file_path = join_paths(folder_path, file_name) file = open(file_path, 'w') if (file_content is not None): file.write(file_content) file.close() return f'Project created at: {project_name!r}'
def create_file_structure(directory, structure): """ Creates the listed files based on the given structure. Parameters ---------- directory : `str` Directory to execute the file creation. structure : `tuple` of `tuple` (`tuple` of (`str`, `None`), (`str`, `None`)) File structure to create. """ for paths, file_content in structure: paths_length = len(paths) file_name = paths[paths_length - 1] if paths_length == 1: paths = None else: paths = paths[:paths_length - 1] if (paths is None): folder_path = directory else: folder_path = join_paths(directory, *paths) make_directories(folder_path, exist_ok=True) if (file_name is not None): file_path = join_paths(folder_path, file_name) file = open(file_path, 'w') if (file_content is not None): file.write(file_content) file.close()