Added more simple url validation.

This commit is contained in:
Joey Payne 2015-09-19 15:10:22 -06:00
commit c0a7127247
2 changed files with 17 additions and 12 deletions

28
main.py
View file

@ -7,6 +7,7 @@ import sys
import codecs import codecs
import platform import platform
import requests import requests
import validators
from PySide import QtGui, QtCore from PySide import QtGui, QtCore
from PySide.QtGui import QApplication, QHBoxLayout, QVBoxLayout from PySide.QtGui import QApplication, QHBoxLayout, QVBoxLayout
@ -22,8 +23,9 @@ from utils import get_data_path, get_data_file_path
MAX_RECENT = 10 MAX_RECENT = 10
def url_exists(path): def url_exists(path):
r = requests.head(path) if validators.url(path):
return r.status_code == requests.codes.ok return True
return False
class Validator(QtGui.QRegExpValidator): class Validator(QtGui.QRegExpValidator):
def __init__(self, regex, action, parent=None): def __init__(self, regex, action, parent=None):
@ -340,16 +342,18 @@ class MainWindow(QtGui.QMainWindow, CommandBase):
self.tab_widget.setTabIcon(tab, self.warning_settings_icon) self.tab_widget.setTabIcon(tab, self.warning_settings_icon)
if (setting.type == 'file' and if (setting.type == 'file' and
setting.value and setting.value):
(not os.path.exists(setting_path) and not url_exists(setting.value))): setting_path_invalid = not os.path.exists(setting_path)
log(setting.value, "does not exist") setting_url_invalid = not url_exists(setting.value)
settings_valid = False if setting_path_invalid and setting_url_invalid:
widget = self.find_child_by_name(setting.name) log(setting.value, "does not exist")
if widget is not None: settings_valid = False
widget.setStyleSheet(red_border) widget = self.find_child_by_name(setting.name)
widget.setToolTip(u'The file "{}" does not exist.'.format(os.path.join(self.project_dir(),setting.value))) if widget is not None:
tab = self.get_tab_index_for_setting_name(setting.name) widget.setStyleSheet(red_border)
self.tab_widget.setTabIcon(tab, self.warning_settings_icon) widget.setToolTip(u'The file or url "{}" does not exist.'.format(setting.value))
tab = self.get_tab_index_for_setting_name(setting.name)
self.tab_widget.setTabIcon(tab, self.warning_settings_icon)
if (setting.type == 'folder' and if (setting.type == 'folder' and
setting.value and setting.value and

View file

@ -4,3 +4,4 @@ pyside
configobj configobj
semantic_version semantic_version
appdirs appdirs
validators