Page MenuHomec4science

author_db.py
No OneTemporary

File Metadata

Created
Wed, Jun 12, 15:19

author_db.py

__author__ = "Guillaume Anciaux, and Nicolas Richart"
__copyright__ = "Copyright (C) 2015, EPFL (Ecole Polytechnique Fédérale de Lausanne) Laboratory " \
"(LSMS - Laboratoire de Simulation en Mécanique des Solides)"
__credits__ = ["Guillaume Anciaux", "Nicolas Richart"]
__license__ = "GPL"
__version__ = "1.0"
__maintainer__ = "Nicolas Richart"
__email__ = "nicolas.richart@epfl.ch"
from . import export, licenser_ask_question
from builtins import property as _property, tuple as _tuple
from operator import itemgetter as _itemgetter
from collections import OrderedDict
@export
class Author(tuple):
"""Author(user_name, e_mail, last_name, first_name, real_email)"""
__slots__ = ()
_fields = ('user_name', 'e_mail', 'last_name', 'first_name', 'real_email')
# noinspection PyInitNewSignature
def __new__(cls, user_name, e_mail, last_name, first_name, real_email):
"""Create new instance of Author(user_name, e_mail, last_name, first_name, real_email)"""
return _tuple.__new__(cls, (user_name, e_mail, last_name, first_name, real_email))
# noinspection PyIncorrectDocstring
@classmethod
def _make(cls, iterable, new=tuple.__new__, len=len):
"""Make a new Author object from a sequence or iterable"""
result = new(cls, iterable)
if len(result) != 5:
raise TypeError('Expected 5 arguments, got %d' % len(result))
return result
# noinspection PyArgumentList
@classmethod
def make(cls, iterable, new=tuple.__new__, len=len):
cls._make(cls, iterable, new=new, len=len)
# noinspection PyMethodParameters
def _replace(_self, **kwds):
"""Return a new Author object replacing specified fields with new values"""
result = _self._make(map(kwds.pop, ('user_name', 'e_mail', 'last_name', 'first_name', 'real_email'), _self))
if kwds:
raise ValueError('Got unexpected field names: %r' % list(kwds))
return result
def __repr__(self):
# noinspection PyTypeChecker
if self.real_email is not None and len(self.real_email) > 0:
email = self.real_email
else:
email = self.e_mail
return "{0} {1} <{2}>".format(self.first_name, self.last_name, email)
def __eq__(self, other):
return self.user_name == other.user_name
def __hash__(self):
return hash(self.user_name)
def _asdict(self):
"""Return a new OrderedDict which maps field names to their values."""
return OrderedDict(zip(self._fields, self))
def __getnewargs__(self):
"""Return self as a plain tuple. Used by copy and pickle."""
return tuple(self)
user_name = _property(_itemgetter(0), doc='Alias for field number 0')
e_mail = _property(_itemgetter(1), doc='Alias for field number 1')
last_name = _property(_itemgetter(2), doc='Alias for field number 2')
first_name = _property(_itemgetter(3), doc='Alias for field number 3')
real_email = _property(_itemgetter(4), doc='Alias for field number 4')
@export
class AuthorDB:
def __init__(self, db):
self.__db = db
self._authors = db.authors
__EMAIL = 0
__USERNAME = 1
def __add_missing_author(self, key, key_type):
print(
"The author identified by the key \'{0}\' is not know please enter the information for this author.".format(
key))
ans = False
auth = {'first_name': '', 'last_name': '',
'e_mail': '', 'user_name': ''}
while not ans:
answer = input("First name [{0}]: ".format(auth['first_name']))
auth['first_name'] = answer if not answer == '' else auth['first_name']
answer = input("Last name [{0}]: ".format(auth['last_name']))
auth['last_name'] = answer if not answer == '' else auth['last_name']
if key_type == self.__EMAIL:
answer = input("Username [{0}]: ".format(auth['user_name']))
auth['user_name'] = answer if not answer == '' else auth['user_name']
auth['e_mail'] = key
else:
auth['user_name'] = key
answer = input("E-Mail [{0}]: ".format(auth['e_mail']))
auth['e_mail'] = answer if not answer == '' else auth['e_mail']
print('\nFirst name: {0}\nLast name: {1}\nUsername: {2}\nE-Mail: {3}'.format(auth['first_name'],
auth['last_name'],
auth['user_name'],
auth['e_mail']))
ans = licenser_ask_question("Are this information correct")
if ans:
auth = Author(auth['user_name'], auth['e_mail'], auth['last_name'], auth['first_name'], '')
self.__db.add_author(auth)
self._authors.append(auth)
return auth
def is_author_in_ignore_list(self, author):
return self.__db.is_author_in_ignore_list(author)
def find_by_user_name(self, user_name):
find = [author for author in self._authors if author.user_name == user_name]
if len(find) == 0:
find.append(self.__add_missing_author(user_name, self.__USERNAME))
return find[0]
def find_by_email(self, e_mail):
find = [author for author in self._authors
if author.e_mail == e_mail]
if len(find) == 0:
find.append(self.__add_missing_author(e_mail, self.__EMAIL))
return find[0]

Event Timeline