validate path string

This commit is contained in:
Ondřej Samohel 2020-10-02 23:57:44 +02:00 committed by Ondrej Samohel
parent 881c935846
commit 510170fdab
No known key found for this signature in database
GPG key ID: 8A29C663C672C2B7
4 changed files with 124 additions and 16 deletions

46
igniter/tools.py Normal file
View file

@ -0,0 +1,46 @@
# -*- coding: utf-8 -*-
"""Tools used in **Igniter** GUI."""
import os
import uuid
from urllib.parse import urlparse
from pymongo import MongoClient
from pymongo.errors import ServerSelectionTimeoutError
def validate_path_string(path: str) -> (bool, str):
"""Validate string if it is acceptable by Igniter.
``path` string can be either regular path, or mongodb url or Pype token.
Args:
path (str): String to validate.
Returns:
(bool, str): True if valid, False if not and in second part of tuple
the reason why it failed.
"""
parsed = urlparse(path)
if parsed.scheme == "mongodb":
# we have mongo connection string. Let's try if we can connect.
client = MongoClient(path, serverSelectionTimeoutMS=1)
try:
client.server_info()
except ServerSelectionTimeoutError:
return False, "Cannot connect to server"
else:
return True, "Connection is successful"
# test for uuid
try:
uuid.UUID(path)
except ValueError as e:
# not uuid
if not os.path.exists(path):
return False, "Path doesn't exist or invalid token"
return True, "Path exists"
else:
# we have pype token
# todo: implement
return False, "Not implemented yet"