def check_data(self, value): """ Checks the data is a string or unicode, and checks data against a regular expression for a URL. If the user leaves off the protocol,then 'http://' is attached as a default. :return: String (UTF-8) """ if not value: return None if not (isinstance(value, str) or isinstance(value, unicode)): raise TypeError("Value is not a string or unicode.") value = _encode_as_utf8(value) if value and '://' not in value: value = 'http://%s' % value url_re = re.compile( r'^https?://' # http:// or https:// r'(?:(?:[A-Z0-9-]+\.)+[A-Z]{2,6}|' #domain... r'localhost|' #localhost... r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})' # ...or ip r'(?::\d+)?' # optional port r'(?:/?|/\S+)$', re.IGNORECASE) if re.search(url_re, value): return value else: raise ValueError("Enter a proper URL.")
def open_file(self, file_path, **kwargs): """ Open a given file and read XMP from file. File must be closed again with :func:`close_file` :param file_path: Path to file to open. :raises XMPError: in case of errors. .. todo:: Change signature into using kwargs to set option flag """ open_flags = options_mask(XMP_OPEN_OPTIONS, **kwargs) if kwargs else XMP_OPEN_NOOPTION if self._file_path != None: raise XMPError('A file is already open - close it first.') # Ensure file path is UTF-8 encoded (expected by Exempi) file_path = _encode_as_utf8(file_path) if not os.path.exists(file_path): raise XMPError('File does not exists.') if _exempi.xmp_files_open(self.xmpfileptr, file_path, open_flags): self._file_path = file_path else: _check_for_error()
def check_data(self, values): """ Checks that the data passed is a Python List, and that the elements are Date or Datetime objects. :return: List of Datetime objects in ISO format (i.e. Strings encoded as UTF-8) """ if not isinstance(values, list): raise TypeError("Data needs to be a list.") if not self.check_length(values): raise AVMListLengthError("Data is not the correct length.") checked_data = [] # Check data type in list for value in values: if value: if ( isinstance( value, datetime.date ) or isinstance( value, datetime.datetime ) ): value = _encode_as_utf8( value.isoformat() ) checked_data.append( value ) else: raise TypeError("Elements of the list need to be a Python Date or Datetime object.") else: checked_data.append( "-" ) if len(set(checked_data)) == 1 and checked_data[0] == "-": checked_data = [] return checked_data
def check_data(self, values): """ Checks that the data is of the correct type, length and elements are strings able to be represented as floats. :return: List of strings (UTF-8) """ checked_data = [] if values: # Check type for list if not isinstance(values, list): raise TypeError("Data needs to be a list.") # Check length if not self.check_length(values): raise AVMListLengthError("Data is not the correct length.") # Check data type in list for value in values: if value: value = _encode_as_utf8(value) try: float(value) checked_data.append(value) except Exception, e: raise TypeError( "Enter a string that can be represented as a number." ) else: checked_data.append("-") if len(set(checked_data)) == 1 and checked_data[0] == "-": checked_data = []
def check_data(self, values): """ Checks that the data passed is a Python List, and that the elements are Date or Datetime objects. :return: List of Datetime objects in ISO format (i.e. Strings encoded as UTF-8) """ if not isinstance(values, list): raise TypeError("Data needs to be a list.") if not self.check_length(values): raise AVMListLengthError("Data is not the correct length.") checked_data = [] # Check data type in list for value in values: if value: if (isinstance(value, datetime.date) or isinstance(value, datetime.datetime)): value = _encode_as_utf8(value.isoformat()) checked_data.append(value) else: raise TypeError( "Elements of the list need to be a Python Date or Datetime object." ) else: checked_data.append("-") if len(set(checked_data)) == 1 and checked_data[0] == "-": checked_data = [] return checked_data
def check_data(self, values): """ Checks that the data type is a Python List. Calls check_length() first. .. todo :: Redo this function. Implement the dash functionality only for ordered lists. :return: List (UTF-8 elements) """ if not values: return None # Check data type if not isinstance(values, list): raise TypeError("Data needs to be a Python List.") # Check length if not self.check_length(values): raise AVMListLengthError("Data is not the correct length.") # Convert to UTF-8 checked_data = [] length = 0 for value in values: value = _encode_as_utf8(value) length += len(value) if value is "": value = "-" checked_data.append(value) if len(set(checked_data)) == 1 and checked_data[0] == "-": checked_data = [] # if length is 0: # raise AVMEmptyValueError("Make sure to enter data into the elements.") return checked_data
def check_data(self, values): """ Check that the passed data is a Python List, and checks that the elements are strings or unicode. :return: List of strings (UTF-8) """ # Check that data is a list if not isinstance(values, list): raise TypeError("Data needs to be a Python List.") # Check length if not self.check_length(values): raise AVMListLengthError("Data is not the correct length.") checked_data = [] # Check data type in list for value in values: if (isinstance(value, str) or isinstance(value, unicode)): value = _encode_as_utf8(value) checked_data.append(value) else: raise TypeError( "Elements of list need to be string or unicode.") return checked_data
def check_data(self, values): """ Check that the passed data is a Python List, and checks that the elements are strings or unicode. :return: List of strings (UTF-8) """ # Check that data is a list if not isinstance(values, list): raise TypeError("Data needs to be a Python List.") # Check length if not self.check_length(values): raise AVMListLengthError("Data is not the correct length.") checked_data = [] # Check data type in list for value in values: if (isinstance(value, str) or isinstance(value, unicode)): value = _encode_as_utf8(value) checked_data.append(value) else: raise TypeError("Elements of list need to be string or unicode.") return checked_data
def check_data(self, values): """ Checks that the data is of the correct type, length and elements are strings able to be represented as floats. :return: List of strings (UTF-8) """ checked_data = [] if values: # Check type for list if not isinstance(values, list): raise TypeError("Data needs to be a list.") # Check length if not self.check_length(values): raise AVMListLengthError("Data is not the correct length.") # Check data type in list for value in values: if value: value = _encode_as_utf8(value) try: float(value) checked_data.append(value) except Exception, e: raise TypeError("Enter a string that can be represented as a number.") else: checked_data.append("-") if len(set(checked_data)) == 1 and checked_data[0] == "-": checked_data = []
def open_file(self, file_path, **kwargs): """ Open a given file and read XMP from file. File must be closed again with :func:`close_file` :param file_path: Path to file to open. :raises XMPError: in case of errors. .. todo:: Change signature into using kwargs to set option flag """ open_flags = options_mask(XMP_OPEN_OPTIONS, ** kwargs) if kwargs else XMP_OPEN_NOOPTION if self._file_path != None: raise XMPError('A file is already open - close it first.') # Ensure file path is UTF-8 encoded (expected by Exempi) file_path = _encode_as_utf8(file_path) if not os.path.exists(file_path): raise XMPError('File does not exists.') if _exempi.xmp_files_open(self.xmpfileptr, file_path, open_flags): self._file_path = file_path else: _check_for_error()
def check_data(self, value): """ All other data classes should define check_data() based on the type of data. Encoding of string into UTF-8 happens here. :return: String (UTF-8) """ return _encode_as_utf8(value)
def check_data(self, value): """ Checks for a Python date .. todo :: Implement a better way to determine class. """ if ( isinstance( value, datetime.date ) or isinstance( value, datetime.datetime ) ): return _encode_as_utf8( value.isoformat() ) else: raise TypeError("Elements of the list need to be a Python Date or Datetime object.")
def check_data(self, value): """ Check that the data is a string or unicode, otherwise it raises a TypeError. :return: String (UTF-8) """ if not value: return None if isinstance(value, str) or isinstance(value, unicode): return _encode_as_utf8(value) else: raise TypeError("Value is not a string or unicode.")
def check_data(self, value): """ Checks for a Python date .. todo :: Implement a better way to determine class. """ if (isinstance(value, datetime.date) or isinstance(value, datetime.datetime)): return _encode_as_utf8(value.isoformat()) else: raise TypeError( "Elements of the list need to be a Python Date or Datetime object." )
def check_data(self, value): """ Checks that data can be represented as a number. :return: String (UTF-8) """ if not value: return None value = _encode_as_utf8(value) try: float(value) except: raise TypeError("Enter a value that can be represented as a number.") return value
def check_data(self, value): """ Checks that data can be represented as a number. :return: String (UTF-8) """ if not value: return None value = _encode_as_utf8(value) try: float(value) except: raise TypeError( "Enter a value that can be represented as a number.") return value
def get_data(self, xmp_packet): """ Extract data from XMP packet :return: List (UTF-8 elements) or None if array does not have any elements """ num_items = xmp_packet.count_array_items(self.namespace, self.path) if num_items is 0: return None num_items += 1 items = [] for i in range(1, num_items): item = _encode_as_utf8(xmp_packet.get_array_item(self.namespace, self.path, i).keys()[0]) items.append(item) return items
def check_data(self, value): """ Check that the data is a string or unicode, formats the data appropriately using format_data() and calls check_cv() :return: String (UTF-8) """ if not value: return None if isinstance(value, str) or isinstance(value, unicode): value = _encode_as_utf8(value) value = self.format_data(value) if self.check_cv(value): return value else: raise AVMItemNotInControlledVocabularyError("Item is not in the controlled vocabulary.") else: raise TypeError("Value is not a string or unicode.")
def get_data(self, xmp_packet): """ Extract data from XMP packet :return: List (UTF-8 elements) or None if array does not have any elements """ num_items = xmp_packet.count_array_items(self.namespace, self.path) if num_items is 0: return None num_items += 1 items = [] for i in range(1, num_items): item = _encode_as_utf8( xmp_packet.get_array_item(self.namespace, self.path, i).keys()[0]) items.append(item) return items
def check_data(self, value): """ Check that the data is a string or unicode, formats the data appropriately using format_data() and calls check_cv() :return: String (UTF-8) """ if not value: return None if isinstance(value, str) or isinstance(value, unicode): value = _encode_as_utf8(value) value = self.format_data(value) if self.check_cv(value): return value else: raise AVMItemNotInControlledVocabularyError( "Item is not in the controlled vocabulary.") else: raise TypeError("Value is not a string or unicode.")
def check_data(self, values): """ Checks that the data is a list, elements are strings, and strings are in the specified controlled vocabulary. :return: List of CV-Strings (UTF-8) """ # Check that data is a list if not isinstance(values, list): raise TypeError("Data needs to be a Python List.") # Check length if not self.check_length(values): raise AVMListLengthError("List is not the correct length.") checked_data = [] length = 0 # Check data type in list for value in values: if (isinstance(value, str) or isinstance(value, unicode)): value = _encode_as_utf8(value) value = self.format_data(value) if self.check_cv(value): checked_data.append(value) else: raise AVMItemNotInControlledVocabularyError( "Item is not in the controlled vocabulary.") else: if value is None: checked_data.append("-") else: raise TypeError( "Elements of list need to be string or unicode.") if len(set(checked_data)) == 1 and checked_data[0] == "-": checked_data = [] return checked_data
def check_data(self, value): """ Checks data is a string or unicode, and checks against a regular expression for an email. If value is not a string or unicode, a TypeError is raised. If the value is not a proper email, then a ValueError is raised. :return: String (UTF-8) """ if not (isinstance(value, str) or isinstance(value, unicode)): raise TypeError("Value is not a string or unicode.") value = _encode_as_utf8(value) email_re = re.compile( r"(^[-!#$%&'*+/=?^_`{}|~0-9A-Z]+(\.[-!#$%&'*+/=?^_`{}|~0-9A-Z]+)*" # dot-atom r'|^"([\001-\010\013\014\016-\037!#-\[\]-\177]|\\[\001-011\013\014\016-\177])*"' # quoted-string r')@(?:[A-Z0-9-]+\.)+[A-Z]{2,6}$', re.IGNORECASE) if re.search(email_re, value): return value else: raise ValueError("Enter a proper email address.")
def check_data(self, value): """ Checks data is a string or unicode, and checks against a regular expression for an email. If value is not a string or unicode, a TypeError is raised. If the value is not a proper email, then a ValueError is raised. :return: String (UTF-8) """ if not (isinstance(value, str) or isinstance(value, unicode)): raise TypeError("Value is not a string or unicode.") value = _encode_as_utf8(value) email_re = re.compile( r"(^[-!#$%&'*+/=?^_`{}|~0-9A-Z]+(\.[-!#$%&'*+/=?^_`{}|~0-9A-Z]+)*" # dot-atom r'|^"([\001-\010\013\014\016-\037!#-\[\]-\177]|\\[\001-011\013\014\016-\177])*"' # quoted-string r')@(?:[A-Z0-9-]+\.)+[A-Z]{2,6}$', re.IGNORECASE ) if re.search(email_re, value): return value else: raise ValueError("Enter a proper email address.")
def check_data(self, values): """ Checks that the data is a list, elements are strings, and strings are in the specified controlled vocabulary. :return: List of CV-Strings (UTF-8) """ # Check that data is a list if not isinstance(values, list): raise TypeError("Data needs to be a Python List.") # Check length if not self.check_length(values): raise AVMListLengthError("List is not the correct length.") checked_data = [] length = 0 # Check data type in list for value in values: if (isinstance(value, str) or isinstance(value, unicode)): value = _encode_as_utf8(value) value = self.format_data(value) if self.check_cv(value): checked_data.append(value) else: raise AVMItemNotInControlledVocabularyError("Item is not in the controlled vocabulary.") else: if value is None: checked_data.append("-") else: raise TypeError("Elements of list need to be string or unicode.") if len(set(checked_data)) == 1 and checked_data[0] == "-": checked_data = [] return checked_data