def _validate_uri(self, uri: Union[str, None]) -> str: """Format the uri provided to match mlflow expectations. Arguments: uri {Union[None, str]} -- A valid filepath for mlflow uri Returns: str -- A valid mlflow_tracking_uri """ # if no tracking uri is provided, we register the runs locally at the root of the project uri = uri or "mlruns" pathlib_uri = PurePath(uri) from urllib.parse import urlparse if pathlib_uri.is_absolute(): valid_uri = pathlib_uri.as_uri() else: parsed = urlparse(uri) if parsed.scheme == "": # if it is a local relative path, make it absolute # .resolve() does not work well on windows # .absolute is undocumented and have known bugs # Path.cwd() / uri is the recommend way by core developpers. # See : https://discuss.python.org/t/pathlib-absolute-vs-resolve/2573/6 valid_uri = (self.project_path / uri).as_uri() else: # else assume it is an uri valid_uri = uri return valid_uri
def to_uri(file_path): pure_path = PurePath(file_path) if pure_path.is_absolute(): return pure_path.as_uri() else: posix_path = pure_path.as_posix() # Replace backslashes with slashes. return urlparse.quote(posix_path) # %-encode special characters.
def uri_from_path(path: PurePath) -> str: """ Convert a python path object to an URI """ # TODO: needs way more tests... See note [URI:java-python] if not path.is_absolute(): raise ValueError("uri_from_path requires an absolute path") java_uri = str(_normalize_pathlib_uris(path.as_uri()).toString()) # fixme: this should be replaced with a rfc3896 compliant solution... if re.match("file://([^/]|$)", java_uri): uri = f"file:////{java_uri[7:]}" # network shares have redundant authority on the java side # vvv this would only be required if we wouldn't normalize the uri like above # elif re.match("file:///([^/]|$)", java_uri): # uri = f"file:/{java_uri[8:]}" # the local windows absolute paths don't else: uri = java_uri return uri
def _validate_mlflow_tracking_uri(project_path: str, uri: Optional[str]) -> str: """Format the uri provided to match mlflow expectations. Arguments: uri {Union[None, str]} -- A valid filepath for mlflow uri Returns: str -- A valid mlflow_tracking_uri """ # this is a special reserved keyword for mlflow which should not be converted to a path # se: https://mlflow.org/docs/latest/tracking.html#where-runs-are-recorded if uri is None: # do not use mlflow.get_tracking_uri() because if there is no env var, # it resolves to 'Path.cwd() / "mlruns"' # but we want 'project_path / "mlruns"' uri = os.environ.get("MLFLOW_TRACKING_URI", "mlruns") if uri == "databricks": return uri # if no tracking uri is provided, we register the runs locally at the root of the project pathlib_uri = PurePath(uri) if pathlib_uri.is_absolute(): valid_uri = pathlib_uri.as_uri() else: parsed = urlparse(uri) if parsed.scheme == "": # if it is a local relative path, make it absolute # .resolve() does not work well on windows # .absolute is undocumented and have known bugs # Path.cwd() / uri is the recommend way by core developpers. # See : https://discuss.python.org/t/pathlib-absolute-vs-resolve/2573/6 valid_uri = (Path(project_path) / uri).as_uri() LOGGER.info( f"The 'mlflow_tracking_uri' key in mlflow.yml is relative ('server.mlflow_tracking_uri = {uri}'). It is converted to a valid uri: '{valid_uri}'" ) else: # else assume it is an uri valid_uri = uri return valid_uri
def get_existing_resync_file(self, resync_file): # get the existing resync_file as file URI. #return 'file://'+self.config.get_cfg_resync_dir()+'/'+resync_file p = PurePath(self.config.cfg_resync_dir(), resync_file) return p.as_uri()
def convert_to_uri(): '''Put it in a web browser!''' pp = PurePath(__file__) print(pp.as_uri()) # file:///Users/austinchang/tutorials/python/language/python_37/popular_modules/pathlib_/purepath/methods.r2d2.py
""" # Path는 파일 시스템에 접근하기 때문에, 기본적으로 운영체제 상에 조작 대상 파일 경로가 존재해야 합니다. # PurePath는 순수 경로의 기반 클래스입니다. # PurePath는 파일 시스템에 접근하지 않기 때문에, 운영체제 상에 존재하지 않는 파일 경로를 다룰 수도 있습니다. # ㄴ 원격 혹은 네트워크 상에서 가져올 때 사용?... """ from pathlib import PurePath, PureWindowsPath #------------------------------------------------------------------- # 1 - 존재하지 않는 파일 #p = PurePath(r'a:\babo\python\myjob') #p = PurePath(r'\\192.168.0.111\Share\kosmo') p = PurePath('C:\Windows\System32\drivers\etc\hosts') print(p) print(p.drive) #존재하지 않는 파일임에도 p.drive가 나옴.. print(p.parts) #존재하지 않는 파일임에도 p.parts가 나옴.. #print(p.parts[3]) # \를 기준으로 문자열을 뽑아오는 듯? print(p.parents[0]) print(p.parents[1]) print(p.parents[2]) print(p.as_uri()) # 파일을 가져올 수 있게끔??... #------------------------------------------------------------------- # 2. 실제 경로로 아닌 가짜 경로를 관리하는 PurePath를 어디에 사용할까? # 아마도 경로나 파일명만 조작할 때 사용하지 않을까? # 해당 경로나 파일명이 현재 컴퓨터가 아니기에 이름만 관리하는 작업이 필요할 듯 싶다
""" # Path는 파일 시스템에 접근하기 때문에, 기본적으로 운영체제 상에 조작 대상 파일 경로가 존재해야 합니다. # PurePath는 순수 경로의 기반 클래스입니다. # 파일 시스템에 접근하지 않기 때문에, 운영체제 상에 존재하지 않는 파일 경로를 다룰 수도 있습니다. """ from pathlib import PurePath, PureWindowsPath #------------------------------------------------------------------- # 1 - 존재하지 않는 파일 # p = PurePath(r'\\192.168.0.111\Share\kosmo') p = PurePath('C:\Windows\System32\drivers\etc\hosts') print(p) print(p.drive) # 존재하지 않는 파일임에도 p.drive가 나옴 print(p.parts[3]) # \단위로 쪼개서 가져오는 듯함 print(p.parents[0]) print(p.parents[1]) print(p.parents[2]) print(p.parents[3]) print(p.parents[4]) # print(p.parents[5]) # 더 이상 부모가 없어서 에러 print(p.as_uri()) # 실제 파일을 가져다 올 수 있는 url로 제공 #------------------------------------------------------------------- # 2. 실제 경로로 아닌 가짜 경로를 관리하는 PurePath를 어디에 사용할까? # 아마도 경로나 파일명만 조작할 때 사용하지 않을까? # 해당 경로나 파일명이 현재 컴퓨터가 아니기에 이름만 관리하는 작업이 필요할 듯 싶다
def get_existing_resync_file(self, resync_file): # get the existing resync_file as file URI. # return 'file://'+self.config.get_cfg_resync_dir()+'/'+resync_file p = PurePath(self.config.cfg_resync_dir(), resync_file) return p.as_uri()