1 # This file is part of Librarian, licensed under GNU Affero GPLv3 or later.
 
   2 # Copyright © Fundacja Wolne Lektury. See NOTICE for more information.
 
   4 from functools import total_ordering
 
   5 from .base import MetaValue
 
   9 class Person(MetaValue):
 
  10     """Single person with last name and a list of first names."""
 
  11     def __init__(self, last_name, *first_names):
 
  12         self.last_name = last_name
 
  13         self.first_names = first_names
 
  16     def from_text(cls, text):
 
  17         parts = [token.strip() for token in text.split(',')]
 
  23                 "Invalid person name. "
 
  24                 "There should be at most one comma: \"%s\"."
 
  25                 % text.encode('utf-8')
 
  29             if len(parts[1]) == 0:
 
  30                 # there is no non-whitespace data after the comma
 
  32                     "Found a comma, but no names given: \"%s\" -> %r."
 
  35             names = parts[1].split()
 
  36         return cls(surname, *names)
 
  39         return " ".join(self.first_names + (self.last_name,))
 
  41     def __eq__(self, right):
 
  42         return (self.last_name == right.last_name
 
  43                 and self.first_names == right.first_names)
 
  45     def __lt__(self, other):
 
  46         return ((self.last_name, self.first_names)
 
  47                 < (other.last_name, other.first_names))
 
  50         return hash((self.last_name, self.first_names))
 
  53         if len(self.first_names) > 0:
 
  54             return '%s, %s' % (self.last_name, ' '.join(self.first_names))
 
  59         return 'Person(last_name=%r, first_names=*%r)' % (
 
  60             self.last_name, self.first_names