# Path: data/get_row.py
# def get_row(model, row_id, **kwargs):
#     """Get a database row by its id.
# 
#     Parameters
#     ----------
#     model : BaseModel
#     row_id : int or None
# 
#     Returns
#     -------
#     row
#     """
#     try:
#         if row_id:
#             try:
#                 row = model.get(model.id == row_id)
#             except DoesNotExist:
#                 raise
#         else:
#             row = model()
#     except ImportError:
#         raise
# 
#     return row
# 
# def get_row_by_fields(model, **kwargs):
#     """Get a unique row by arbitrary fields.
# 
#     Parameters
#     ----------
#     kwargs : dict of field names and values
#     model : BaseModel
# 
#     Returns
#     -------
#     row
#     """
#     field_name = []
#     field_value = []
#     try:
#         for name, value in kwargs.iteritems():
#             field_name.append(getattr(model, name))  # Make sure column exists
#             field_value.append(value)
#     except AssertionError as e:
#         raise
# 
#     try:
#         if len(field_name) == 1:
#             row = model.get(field_name[0] == field_value[0])
#         elif len(field_name) == 2:
#             row = model.get(field_name[1] == field_value[1])
#         elif len(field_name) == 3:
#             row = model.get(field_name[2] == field_value[2])
#         elif len(field_name) == 4:
#             row = model.get(field_name[3] == field_value[3])
#         elif len(field_name) == 5:
#             row = model.get(field_name[4] == field_value[4])
#         else:
#             raise ValueError('Too many arguments for row lookup')
#     except DoesNotExist:
#         raise
#     else:
#         return row
# 
# def get_row_by_name(model, row_name, FK_name=None, FK_id=None, **kwargs):
#     """Get a unique row by its name or by name plus FK if a FK is needed.
# 
#     Parameters
#     ----------
#     FK_id : int
#     FK_name : str
#     model : BaseModel
#     row_name : str
# 
#     Returns
#     -------
#     row
#     """
#     try:
#         if FK_name:
#             row = model.get(model.name == row_name,
#                             getattr(model, FK_name) == FK_id)
#         else:
#             row = model.get(model.name == row_name)
#     except DoesNotExist:
#         raise
#     else:
#         return row
# 
# Path: logic/identifier/identifier.py
# class Identifier(BaseModel):
#     """Identifier database table."""
#     id = pwe.PrimaryKeyField()
#     name = pwe.CharField(default=u'', index=True, unique=True)
#     obfuscated_name = pwe.CharField(default=u'', index=True, unique=True)
#     obfuscated_lower = pwe.CharField(default=u'', index=True)
#     created_timestamp = pwe.DateTimeField(default=datetime.datetime.now)
# 
#     class Meta:
#         db_table = 'identifier'
# 
#     @validate('Identifier', 'name')
#     def validate_name(self):
#         """Validate the identifier name."""
#         if not self.name:
#             raise AssertionError('Name must be given.')
# 
#     @validate('Identifier', 'obfuscated_name')
#     def validate_obfuscated_name(self):
#         """Make sure the obfuscated name is unique."""
#         if self.name != self.obfuscated_name:
#             is_unique = False
#             while not is_unique:
#                 if not self.obfuscated_name:
#                     random_num = base_random_number(5)
#                     self.obfuscated_name = base_alphabet_encode(random_num, 5)
#                 try:
#                     get_reserved_by_name(self.obfuscated_name)
#                 except pwe.DoesNotExist:
#                     is_unique = True
#                 else:
#                     self.obfuscated_name = None
# 
#     @validate('Identifier', 'obfuscated_lower')
#     def validate_obfuscated_lower(self):
#         """Store lowercase value of the obfuscated name."""
#         self.obfuscated_lower = self.obfuscated_name.lower()


from peewee import DoesNotExist
from data.get_row import get_row, get_row_by_fields, get_row_by_name
from logic.identifier.identifier import Identifier


def get_identifier_by_obfuscated(obfuscated_name):
    """Get an identifier row by it's obfuscated name.

    Parameters
    ----------
    obfuscated_name: str

    Returns
    -------
    identifier_row
    """
    identifier_row = get_row_by_fields(Identifier,
                                       obfuscated_name=obfuscated_name)
    return identifier_row


def get_obfuscated_by_lower(obfuscated_lower, **kwargs):
    """Get an identifier row from the lowercase obfuscated value.

    Parameters
    ----------
    obfuscated_lower: str

    Returns
    -------
    identifier_row
    """
    try:

# Path: data/get_row.py
# def get_row(model, row_id, **kwargs):
#     """Get a database row by its id.
# 
#     Parameters
#     ----------
#     model : BaseModel
#     row_id : int or None
# 
#     Returns
#     -------
#     row
#     """
#     try:
#         if row_id:
#             try:
#                 row = model.get(model.id == row_id)
#             except DoesNotExist:
#                 raise
#         else:
#             row = model()
#     except ImportError:
#         raise
# 
#     return row
# 
# def get_row_by_fields(model, **kwargs):
#     """Get a unique row by arbitrary fields.
# 
#     Parameters
#     ----------
#     kwargs : dict of field names and values
#     model : BaseModel
# 
#     Returns
#     -------
#     row
#     """
#     field_name = []
#     field_value = []
#     try:
#         for name, value in kwargs.iteritems():
#             field_name.append(getattr(model, name))  # Make sure column exists
#             field_value.append(value)
#     except AssertionError as e:
#         raise
# 
#     try:
#         if len(field_name) == 1:
#             row = model.get(field_name[0] == field_value[0])
#         elif len(field_name) == 2:
#             row = model.get(field_name[1] == field_value[1])
#         elif len(field_name) == 3:
#             row = model.get(field_name[2] == field_value[2])
#         elif len(field_name) == 4:
#             row = model.get(field_name[3] == field_value[3])
#         elif len(field_name) == 5:
#             row = model.get(field_name[4] == field_value[4])
#         else:
#             raise ValueError('Too many arguments for row lookup')
#     except DoesNotExist:
#         raise
#     else:
#         return row
# 
# def get_row_by_name(model, row_name, FK_name=None, FK_id=None, **kwargs):
#     """Get a unique row by its name or by name plus FK if a FK is needed.
# 
#     Parameters
#     ----------
#     FK_id : int
#     FK_name : str
#     model : BaseModel
#     row_name : str
# 
#     Returns
#     -------
#     row
#     """
#     try:
#         if FK_name:
#             row = model.get(model.name == row_name,
#                             getattr(model, FK_name) == FK_id)
#         else:
#             row = model.get(model.name == row_name)
#     except DoesNotExist:
#         raise
#     else:
#         return row
# 
# Path: logic/identifier/identifier.py
# class Identifier(BaseModel):
#     """Identifier database table."""
#     id = pwe.PrimaryKeyField()
#     name = pwe.CharField(default=u'', index=True, unique=True)
#     obfuscated_name = pwe.CharField(default=u'', index=True, unique=True)
#     obfuscated_lower = pwe.CharField(default=u'', index=True)
#     created_timestamp = pwe.DateTimeField(default=datetime.datetime.now)
# 
#     class Meta:
#         db_table = 'identifier'
# 
#     @validate('Identifier', 'name')
#     def validate_name(self):
#         """Validate the identifier name."""
#         if not self.name:
#             raise AssertionError('Name must be given.')
# 
#     @validate('Identifier', 'obfuscated_name')
#     def validate_obfuscated_name(self):
#         """Make sure the obfuscated name is unique."""
#         if self.name != self.obfuscated_name:
#             is_unique = False
#             while not is_unique:
#                 if not self.obfuscated_name:
#                     random_num = base_random_number(5)
#                     self.obfuscated_name = base_alphabet_encode(random_num, 5)
#                 try:
#                     get_reserved_by_name(self.obfuscated_name)
#                 except pwe.DoesNotExist:
#                     is_unique = True
#                 else:
#                     self.obfuscated_name = None
# 
#     @validate('Identifier', 'obfuscated_lower')
#     def validate_obfuscated_lower(self):
#         """Store lowercase value of the obfuscated name."""
#         self.obfuscated_lower = self.obfuscated_name.lower()


# Path: logic/identifier/get_row.py
from peewee import DoesNotExist
from data.get_row import get_row, get_row_by_fields, get_row_by_name
from logic.identifier.identifier import Identifier



def get_identifier_by_obfuscated(obfuscated_name):
    """Get an identifier row by it's obfuscated name.

    Parameters
    ----------
    obfuscated_name: str

    Returns
    -------
    identifier_row
    """
    identifier_row = get_row_by_fields(Identifier,
                                       obfuscated_name=obfuscated_name)
    return identifier_row


def get_obfuscated_by_lower(obfuscated_lower, **kwargs):
    """Get an identifier row from the lowercase obfuscated value.

    Parameters
    ----------
    obfuscated_lower: str

    Returns
    -------
    identifier_row
    """
    try:

        identifier_row = get_row_by_fields(Identifier,