pyjallib.nameToPath
nameToPath 모듈 - 이름과 경로 변환 관련 기능 이름 규칙에 따라 경로를 생성하거나 경로에서 이름을 추출하는 기능 제공
1#!/usr/bin/env python 2# -*- coding: utf-8 -*- 3 4""" 5nameToPath 모듈 - 이름과 경로 변환 관련 기능 6이름 규칙에 따라 경로를 생성하거나 경로에서 이름을 추출하는 기능 제공 7""" 8 9import os 10import json 11from typing import Optional, Dict, Any, List 12 13from pyjallib.naming import Naming 14from pyjallib.namePart import NamePartType 15 16class NameToPath(Naming): 17 """ 18 NameToPath 클래스는 Naming 클래스를 상속받아 이름을 기반으로 경로를 생성하는 기능을 제공합니다. 19 """ 20 def __init__(self, configPath: str, rootPath: str = None, sourceNaming: Naming = None): 21 """ 22 생성자 메서드입니다. 23 :param configPath: 설정 파일의 경로 24 :param rootPath: 루트 경로 (기본값: None) 25 :param sourceNaming: 소스 이름을 처리하기 위한 Naming 객체 (기본값: None) 26 """ 27 # 부모 클래스(Naming) 생성자 호출 28 super().__init__(configPath) 29 self.rootPath = None 30 if rootPath: 31 self.set_root_path(rootPath) 32 # 소스 네이밍 객체 설정 33 self.sourceNaming = sourceNaming 34 35 def set_root_path(self, inRootPath: str): 36 """ 37 루트 경로를 설정합니다. 38 입력된 경로를 정규화하고 유효성을 검증합니다. 39 40 :param inRootPath: 루트 경로 (문자열) 41 :return: 정규화된 경로 42 :raises ValueError: 경로가 존재하지 않는 경우 43 """ 44 if inRootPath: 45 # 경로 정규화 (상대 경로를 절대 경로로 변환, '/' 대신 '\' 사용 등) 46 normalized_path = os.path.normpath(os.path.abspath(inRootPath)) 47 48 # 경로 존재 여부 확인 (선택적) 49 if not os.path.exists(normalized_path): 50 raise ValueError(f"경로가 존재하지 않습니다: {normalized_path}") 51 52 self.rootPath = normalized_path 53 return self.rootPath 54 else: 55 self.rootPath = None 56 return None 57 58 def combine(self, inPartsDict={}, inFilChar=os.sep) -> str: 59 """ 60 딕셔너리의 값들을 설정된 순서에 따라 문자열로 결합합니다. (인덱스 제외) 61 62 :param inPartsDict: 결합할 키-값 쌍을 포함하는 딕셔너리 63 :param inFilChar: 값들을 구분할 구분자 (기본값: "_") 64 :return: 결합된 문자열 65 """ 66 # 결과 배열 초기화 (빈 문자열로) 67 combinedNameArray = [""] * len(self._nameParts) 68 69 # 각 namePart에 대해 70 for i, part in enumerate(self._nameParts): 71 partName = part.get_name() 72 # 딕셔너리에서 해당 부분의 값 가져오기 (없으면 빈 문자열 사용) 73 if partName in inPartsDict: 74 combinedNameArray[i] = inPartsDict[partName] 75 76 # 배열을 문자열로 결합 77 newName = self._combine(combinedNameArray, inFilChar) 78 return newName 79 80 81 def gen_path(self, inStr): 82 """ 83 입력된 문자열을 기반으로 경로를 생성합니다. 84 85 :param inStr: 경로를 생성할 문자열 (이름) 86 :return: 생성된 경로 (문자열) 87 :raises ValueError: 루트 경로가 설정되지 않았거나 이름을 변환할 수 없는 경우 88 """ 89 if not self.rootPath: 90 raise ValueError("루트 경로가 설정되지 않았습니다.") 91 92 # 이름을 딕셔너리로 변환 93 nameDict = self.sourceNaming.convert_to_dictionary(inStr) 94 if not nameDict: 95 raise ValueError(f"이름을 변환할 수 없습니다: {inStr}") 96 print(f"Name Dictionary: {nameDict}") 97 98 pathDict = {} 99 100 # 선택된 NamePart 값들을 설명으로 변환하여 폴더 이름으로 사용 101 for key, value in nameDict.items(): 102 namePart = self.sourceNaming.get_name_part(key) 103 if self.get_name_part(namePart.get_name()): 104 if namePart.get_type() == NamePartType.REALNAME: 105 # 실제 이름인 경우, 해당 이름을 사용 106 pathDict[key] = value 107 else: 108 pathDict[key] = namePart.get_description_by_value(value) 109 110 combinedPath = self.combine(pathDict) 111 finalPath = os.path.join(self.rootPath, combinedPath) 112 113 return os.path.normpath(finalPath)
17class NameToPath(Naming): 18 """ 19 NameToPath 클래스는 Naming 클래스를 상속받아 이름을 기반으로 경로를 생성하는 기능을 제공합니다. 20 """ 21 def __init__(self, configPath: str, rootPath: str = None, sourceNaming: Naming = None): 22 """ 23 생성자 메서드입니다. 24 :param configPath: 설정 파일의 경로 25 :param rootPath: 루트 경로 (기본값: None) 26 :param sourceNaming: 소스 이름을 처리하기 위한 Naming 객체 (기본값: None) 27 """ 28 # 부모 클래스(Naming) 생성자 호출 29 super().__init__(configPath) 30 self.rootPath = None 31 if rootPath: 32 self.set_root_path(rootPath) 33 # 소스 네이밍 객체 설정 34 self.sourceNaming = sourceNaming 35 36 def set_root_path(self, inRootPath: str): 37 """ 38 루트 경로를 설정합니다. 39 입력된 경로를 정규화하고 유효성을 검증합니다. 40 41 :param inRootPath: 루트 경로 (문자열) 42 :return: 정규화된 경로 43 :raises ValueError: 경로가 존재하지 않는 경우 44 """ 45 if inRootPath: 46 # 경로 정규화 (상대 경로를 절대 경로로 변환, '/' 대신 '\' 사용 등) 47 normalized_path = os.path.normpath(os.path.abspath(inRootPath)) 48 49 # 경로 존재 여부 확인 (선택적) 50 if not os.path.exists(normalized_path): 51 raise ValueError(f"경로가 존재하지 않습니다: {normalized_path}") 52 53 self.rootPath = normalized_path 54 return self.rootPath 55 else: 56 self.rootPath = None 57 return None 58 59 def combine(self, inPartsDict={}, inFilChar=os.sep) -> str: 60 """ 61 딕셔너리의 값들을 설정된 순서에 따라 문자열로 결합합니다. (인덱스 제외) 62 63 :param inPartsDict: 결합할 키-값 쌍을 포함하는 딕셔너리 64 :param inFilChar: 값들을 구분할 구분자 (기본값: "_") 65 :return: 결합된 문자열 66 """ 67 # 결과 배열 초기화 (빈 문자열로) 68 combinedNameArray = [""] * len(self._nameParts) 69 70 # 각 namePart에 대해 71 for i, part in enumerate(self._nameParts): 72 partName = part.get_name() 73 # 딕셔너리에서 해당 부분의 값 가져오기 (없으면 빈 문자열 사용) 74 if partName in inPartsDict: 75 combinedNameArray[i] = inPartsDict[partName] 76 77 # 배열을 문자열로 결합 78 newName = self._combine(combinedNameArray, inFilChar) 79 return newName 80 81 82 def gen_path(self, inStr): 83 """ 84 입력된 문자열을 기반으로 경로를 생성합니다. 85 86 :param inStr: 경로를 생성할 문자열 (이름) 87 :return: 생성된 경로 (문자열) 88 :raises ValueError: 루트 경로가 설정되지 않았거나 이름을 변환할 수 없는 경우 89 """ 90 if not self.rootPath: 91 raise ValueError("루트 경로가 설정되지 않았습니다.") 92 93 # 이름을 딕셔너리로 변환 94 nameDict = self.sourceNaming.convert_to_dictionary(inStr) 95 if not nameDict: 96 raise ValueError(f"이름을 변환할 수 없습니다: {inStr}") 97 print(f"Name Dictionary: {nameDict}") 98 99 pathDict = {} 100 101 # 선택된 NamePart 값들을 설명으로 변환하여 폴더 이름으로 사용 102 for key, value in nameDict.items(): 103 namePart = self.sourceNaming.get_name_part(key) 104 if self.get_name_part(namePart.get_name()): 105 if namePart.get_type() == NamePartType.REALNAME: 106 # 실제 이름인 경우, 해당 이름을 사용 107 pathDict[key] = value 108 else: 109 pathDict[key] = namePart.get_description_by_value(value) 110 111 combinedPath = self.combine(pathDict) 112 finalPath = os.path.join(self.rootPath, combinedPath) 113 114 return os.path.normpath(finalPath)
NameToPath 클래스는 Naming 클래스를 상속받아 이름을 기반으로 경로를 생성하는 기능을 제공합니다.
NameToPath( configPath: str, rootPath: str = None, sourceNaming: pyjallib.naming.Naming = None)
21 def __init__(self, configPath: str, rootPath: str = None, sourceNaming: Naming = None): 22 """ 23 생성자 메서드입니다. 24 :param configPath: 설정 파일의 경로 25 :param rootPath: 루트 경로 (기본값: None) 26 :param sourceNaming: 소스 이름을 처리하기 위한 Naming 객체 (기본값: None) 27 """ 28 # 부모 클래스(Naming) 생성자 호출 29 super().__init__(configPath) 30 self.rootPath = None 31 if rootPath: 32 self.set_root_path(rootPath) 33 # 소스 네이밍 객체 설정 34 self.sourceNaming = sourceNaming
생성자 메서드입니다.
Parameters
- configPath: 설정 파일의 경로
- rootPath: 루트 경로 (기본값: None)
- sourceNaming: 소스 이름을 처리하기 위한 Naming 객체 (기본값: None)
def
set_root_path(self, inRootPath: str):
36 def set_root_path(self, inRootPath: str): 37 """ 38 루트 경로를 설정합니다. 39 입력된 경로를 정규화하고 유효성을 검증합니다. 40 41 :param inRootPath: 루트 경로 (문자열) 42 :return: 정규화된 경로 43 :raises ValueError: 경로가 존재하지 않는 경우 44 """ 45 if inRootPath: 46 # 경로 정규화 (상대 경로를 절대 경로로 변환, '/' 대신 '\' 사용 등) 47 normalized_path = os.path.normpath(os.path.abspath(inRootPath)) 48 49 # 경로 존재 여부 확인 (선택적) 50 if not os.path.exists(normalized_path): 51 raise ValueError(f"경로가 존재하지 않습니다: {normalized_path}") 52 53 self.rootPath = normalized_path 54 return self.rootPath 55 else: 56 self.rootPath = None 57 return None
루트 경로를 설정합니다. 입력된 경로를 정규화하고 유효성을 검증합니다.
Parameters
- inRootPath: 루트 경로 (문자열)
Returns
정규화된 경로
Raises
- ValueError: 경로가 존재하지 않는 경우
def
combine(self, inPartsDict={}, inFilChar='\\') -> str:
59 def combine(self, inPartsDict={}, inFilChar=os.sep) -> str: 60 """ 61 딕셔너리의 값들을 설정된 순서에 따라 문자열로 결합합니다. (인덱스 제외) 62 63 :param inPartsDict: 결합할 키-값 쌍을 포함하는 딕셔너리 64 :param inFilChar: 값들을 구분할 구분자 (기본값: "_") 65 :return: 결합된 문자열 66 """ 67 # 결과 배열 초기화 (빈 문자열로) 68 combinedNameArray = [""] * len(self._nameParts) 69 70 # 각 namePart에 대해 71 for i, part in enumerate(self._nameParts): 72 partName = part.get_name() 73 # 딕셔너리에서 해당 부분의 값 가져오기 (없으면 빈 문자열 사용) 74 if partName in inPartsDict: 75 combinedNameArray[i] = inPartsDict[partName] 76 77 # 배열을 문자열로 결합 78 newName = self._combine(combinedNameArray, inFilChar) 79 return newName
딕셔너리의 값들을 설정된 순서에 따라 문자열로 결합합니다. (인덱스 제외)
Parameters
- inPartsDict: 결합할 키-값 쌍을 포함하는 딕셔너리
- inFilChar: 값들을 구분할 구분자 (기본값: "_")
Returns
결합된 문자열
def
gen_path(self, inStr):
82 def gen_path(self, inStr): 83 """ 84 입력된 문자열을 기반으로 경로를 생성합니다. 85 86 :param inStr: 경로를 생성할 문자열 (이름) 87 :return: 생성된 경로 (문자열) 88 :raises ValueError: 루트 경로가 설정되지 않았거나 이름을 변환할 수 없는 경우 89 """ 90 if not self.rootPath: 91 raise ValueError("루트 경로가 설정되지 않았습니다.") 92 93 # 이름을 딕셔너리로 변환 94 nameDict = self.sourceNaming.convert_to_dictionary(inStr) 95 if not nameDict: 96 raise ValueError(f"이름을 변환할 수 없습니다: {inStr}") 97 print(f"Name Dictionary: {nameDict}") 98 99 pathDict = {} 100 101 # 선택된 NamePart 값들을 설명으로 변환하여 폴더 이름으로 사용 102 for key, value in nameDict.items(): 103 namePart = self.sourceNaming.get_name_part(key) 104 if self.get_name_part(namePart.get_name()): 105 if namePart.get_type() == NamePartType.REALNAME: 106 # 실제 이름인 경우, 해당 이름을 사용 107 pathDict[key] = value 108 else: 109 pathDict[key] = namePart.get_description_by_value(value) 110 111 combinedPath = self.combine(pathDict) 112 finalPath = os.path.join(self.rootPath, combinedPath) 113 114 return os.path.normpath(finalPath)
입력된 문자열을 기반으로 경로를 생성합니다.
Parameters
- inStr: 경로를 생성할 문자열 (이름)
Returns
생성된 경로 (문자열)
Raises
- ValueError: 루트 경로가 설정되지 않았거나 이름을 변환할 수 없는 경우
Inherited Members
- pyjallib.naming.Naming
- get_padding_num
- get_name_part
- get_name_part_index
- get_name_part_predefined_values
- is_in_name_part_predefined_values
- get_name_part_value_by_description
- pick_name
- get_name
- get_RealName
- get_non_RealName
- convert_name_to_array
- convert_to_dictionary
- convert_to_description
- convert_to_korean_description
- has_name_part
- add_prefix_to_name_part
- add_suffix_to_name_part
- add_prefix_to_real_name
- add_suffix_to_real_name
- convert_digit_into_padding_string
- set_index_padding_num
- get_index_padding_num
- increase_index
- get_index_as_digit
- sort_by_index
- get_string
- gen_mirroring_name
- replace_filtering_char
- replace_name_part
- remove_name_part
- load_from_config_file
- load_default_config
- get_config_path