Page MenuHomec4science

author_db.py
No OneTemporary

File Metadata

Created
Tue, Apr 30, 16:52

author_db.py

""" author_db.py: Handling authors"""
__author__ = "Guillaume Anciaux and Nicolas Richart"
__credits__ = [
"Guillaume Anciaux <guillaume.anciaux@epfl.ch>",
"Nicolas Richart <nicolas.richart@epfl.ch>",
]
__copyright__ = "Copyright (©) 2010-2021 EPFL (Ecole Polytechnique Fédérale" \
" de Lausanne) Laboratory (LSMS - Laboratoire de Simulation" \
" en Mécanique des Solides)"
__license__ = "GPLv3"
__version__ = "2.0"
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,
" <{}>".format(email) if email is not "None" else "")
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
def __add_missing_author(self, entry):
print(
'The author identified by the key \'{0}\' is not know please'
' enter the information for this author.'.format(entry))
valid_entry = False
auth = {'first_name': {'str': 'First name', 'value': None},
'last_name': {'str': 'Last name', 'value': None},
'e_mail': {'str': 'E-Mail', 'value': None},
'user_name': {'str': 'Username', 'value': None}
}
for key, value in entry.items():
auth[key]['value'] = value
while not valid_entry:
for key, value in auth.items():
if key not in entry:
answer = input("{str} [{value}]: ".format(**value))
value['value'] = answer if not answer == '' \
else value['value']
print('\n{}'.format(
'\n'.join(['{str}: {value}'.format(**val)
for _, val in auth.items()])))
valid_entry = licenser_ask_question("Are this information correct")
if valid_entry:
for val in auth:
if auth[val] is None:
auth[val] = 'None'
author = Author(auth['user_name']['value'],
auth['e_mail']['value'],
auth['last_name']['value'],
auth['first_name']['value'], '')
self.__db.add_author(author)
self._authors.append(author)
return author
def is_author_in_ignore_list(self, author):
return self.__db.is_author_in_ignore_list(author)
def find_by_name(self, first_name, last_name):
find = [author for author in
self._authors if ((author.first_name == first_name) and
(author.last_name == last_name))]
if len(find) == 0:
find.append(self.__add_missing_author({'first_name': first_name,
'last_name': last_name}))
return find[0]
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': user_name}))
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': e_mail}))
return find[0]

Event Timeline