예제 #1
0
파일: manager.py 프로젝트: albesca/books
def _check_element(element):
    if element is None:
        raise generic_exceptions.UndefinedParameterError('element')

    try:
        dict_utils.get_value(db_models.ELEMENTS, element)
    except generic_exceptions.MissingValueError:
        raise db_exceptions.UnrecognizedElementException(element)
예제 #2
0
파일: base.py 프로젝트: albesca/books
def has_value(element, value):
    value_exists = True
    try:
        dict_utils.get_value(element, value)
    except generic_exception.MissingValueError:
        value_exists = False

    return value_exists
예제 #3
0
파일: manager.py 프로젝트: albesca/books
def _build_where_condition(column, condition, value):
    base_where = dict_utils.get_value(db_queries.ELEMENTS, db_constants.WHERE)
    query_condition = dict_utils.get_value(db_queries.ELEMENTS, condition)
    where = base_where.format(column=column,
                              condition=query_condition,
                              value=value
                              )
    return where
예제 #4
0
파일: base.py 프로젝트: albesca/books
def get_value(element, key, constants=None, none_allowed=False):
    value = dict_utils.get_value(element, key, True)
    if value is None and not none_allowed and constants is not None:
        value = dict_utils.get_value(constants.DEFAULTS, key, True)

    if value is None and not none_allowed:
        value = get_default_value(key)

    return value
예제 #5
0
파일: book.py 프로젝트: albesca/books
def get_details_text(book):
    try:
        details_text = dict_utils.get_value(book, book_const.DETAILS_TEXT)
    except gen_exception.MissingValueError:
        # TODO log exception
        details_text = dict_utils.get_value(book_const.DEFAULTS,
                                            book_const.DETAILS_TEXT
                                            )

    return details_text
예제 #6
0
파일: book.py 프로젝트: albesca/books
def get_synopsis(book):
    try:
        synopsis = dict_utils.get_value(book, book_const.SYNOPSIS)
    except gen_exception.MissingValueError:
        # TODO log exception
        synopsis = dict_utils.get_value(book_const.DEFAULTS,
                                        book_const.SYNOPSIS
                                        )

    return synopsis
예제 #7
0
파일: manager.py 프로젝트: albesca/books
def _get_element_id_column(element_type):
    element = dict_utils.get_value(db_models.ELEMENTS, element_type)
    element_id = dict_utils.get_value(element, generic_constants.ID)
    element_id_column = dict_utils.get_value(element_id,
                                             db_constants.COLUMN_NAME,
                                             True
                                             )
    if element_id_column is None:
        element_id_column = element_id

    return element_id_column
예제 #8
0
파일: manager.py 프로젝트: albesca/books
def _get_column_name(property_name, element_type):
    element = dict_utils.get_value(db_models.ELEMENTS,
                                   element_type
                                   )
    column = dict_utils.get_value(element, property_name)
    db_column = dict_utils.get_value(column,
                                     db_constants.COLUMN_NAME,
                                     True
                                     )
    if db_column is None:
        column_name = property_name
    else:
        column_name = db_column

    return column_name
예제 #9
0
파일: choice.py 프로젝트: albesca/books
def parse_choice(choice, choice_list):
    try:
        parsed_choice = dict_utils.get_value(choice_list, choice)
    except gen_exception.MissingValueError:
        # TODO log catched exception
        raise chc_exception.ChoiceNotAvailableError(choice)

    return parsed_choice
예제 #10
0
파일: manager.py 프로젝트: albesca/books
def get_element_columns(element_type):
    element = dict_utils.get_value(db_models.ELEMENTS,
                                   element_type
                                   )
    column_list = element.keys()
    columns = []
    for column_key in column_list:
        column = _get_column_name(column_key, element_type)
        columns.append(column)

    return columns
예제 #11
0
파일: manager.py 프로젝트: albesca/books
def _build_element(values, columns, element_type):
    result_map = zip(columns, values)
    result = {generic_constants.TYPE: element_type}
    for entry in result_map:
        if entry[1] is not None:
            property_name = _get_property_name(entry[0], element_type)
            element = dict_utils.get_value(db_models.ELEMENTS,
                                           element_type
                                           )
            element_property = dict_utils.get_value(element, property_name)
            property_reference = dict_utils.get_value(element_property,
                                                      db_constants.REFERENCE,
                                                      True
                                                      )
            if property_reference is not None:
                reference = _create_reference(entry, property_reference)
                result[generic_constants.PARENT] = reference
            else:
                result[property_name] = entry[1]

    return result
예제 #12
0
파일: factory.py 프로젝트: albesca/books
def _get_managed_element(element_type):
    method_name = "_get_managed_element"
    log_text = "element_type[{}]".format(str(element_type))
    log_utils.debug(log_text, method_name, __name__)
    try:
        managed_element = dict_utils.get_value(MANAGED_ELEMENTS, element_type)
    except generic_exceptions.MissingValueError as mve:
        log_utils.error(mve, method_name, __name__)
        raise generic_exceptions.UnrecognizedElementTypeError(element_type)
    except generic_exceptions.UndefinedParameterError as upe:
        log_utils.error(upe, method_name, __name__)
        raise generic_exceptions.UnrecognizedElementTypeError(element_type)

    return managed_element
예제 #13
0
파일: manager.py 프로젝트: albesca/books
def _get_property_name(column_name, element_type):
    property_name = None
    element = dict_utils.get_value(db_models.ELEMENTS,
                                   element_type
                                   )
    element_properties = element.keys()
    if column_name in element_properties:
        property_name = column_name
    else:
        for element_property in element_properties:
            column = dict_utils.get_value(element, element_property)
            db_column = dict_utils.get_value(column,
                                             db_constants.COLUMN_NAME,
                                             True
                                             )
            if db_column is not None and db_column == column_name:
                property_name = element_property

    if property_name is None:
        raise db_exceptions.UnrecognizedPropertiesException([
            generic_constants.ID
        ])

    return property_name
예제 #14
0
파일: manager.py 프로젝트: albesca/books
def get_element(data_provider, element_reference):
    if data_provider is None:
        raise generic_exceptions.UndefinedParameterError('data_provider')
    try:
        element_type = dict_utils.get_value(element_reference,
                                            generic_constants.TYPE
                                            )
        element_id = dict_utils.get_value(element_reference,
                                          generic_constants.ID
                                          )
    except TypeError:
        # TODO log exception
        raise db_exceptions.UnrecognizedElementException(element_reference)
    except generic_exceptions.MissingValueError:
        # TODO log exception
        raise db_exceptions.UnrecognizedElementException(element_reference)

    element_columns = get_element_columns(element_type)
    element_id_column = _get_element_id_column(element_type)
    query = get_query(db_constants.SELECT,
                      element_type,
                      columns=element_columns,
                      search_column=element_id_column,
                      search_value=element_id
                      )
    query_result = execute_query(data_provider, query[0], query[1])
    result_list = _parse_query_result(query_result,
                                      element_columns,
                                      element_type
                                      )
    if len(result_list) > 1:
        raise db_exceptions.DuplicateElementsException(result_list)

    element = result_list[0]

    return element
예제 #15
0
파일: element.py 프로젝트: albesca/books
def get_method(manager, method_name):
    try:
        custom_method_list = manager.CUSTOM_METHODS
        method = dict_utils.get_value(custom_method_list, method_name, True)
    except AttributeError as error:
        logged_method_name = 'get_method'
        log_text = 'exception[{}]'.format(
            str(error)
        )
        log_utils.debug(log_text, logged_method_name, __name__)
        method = None

    if method is None:
        method = get_default_method(method_name)

    return method
예제 #16
0
파일: element.py 프로젝트: albesca/books
def add_choices(choice_list, choices):
    try:
        choice = dict_utils.get_value(choices,0)
        base_manager.get_type(choice)
        for choice in choices:
            choice_list.append(choice)

    except gen_exception.MissingValueError:
        choice_list.append(choices)
    except IndexError as error:
        method_name = 'add_choices'
        log_text = 'exception[{}]'.format(
            str(error)
        )
        log_utils.debug(log_text, method_name, __name__)

    return choice_list
예제 #17
0
파일: menu.py 프로젝트: albesca/books
def get_error_text(menu):
    error_text = dict_utils.get_value(menu, menu_const.ERROR_TEXT, True)
    return error_text
예제 #18
0
파일: manager.py 프로젝트: albesca/books
def get_query(query_type, element, **kwargs):
    if query_type is None:
        raise generic_exceptions.UndefinedParameterError('query_type')

    _check_element(element)

    try:
        base_query = dict_utils.get_value(db_queries.ELEMENTS, query_type)
    except generic_exceptions.MissingValueError:
        raise db_exceptions.UnrecognizedQueryTypeException(query_type)

    try:
        if query_type == db_constants.SELECT:
            columns_list = dict_utils.get_value(kwargs, 'columns')
            _check_properties(element, columns_list)
            columns = _build_elements_list(columns_list)
            search_column = dict_utils.get_value(kwargs,
                                                 'search_column',
                                                 True
                                                 )
            if search_column is not None:
                _check_properties(element, [search_column])
                condition = db_constants.EQUALS
                value = dict_utils.get_value(kwargs, 'search_value')
                where = _build_where_condition(
                        search_column,
                        condition,
                        db_queries.ELEMENTS[
                            db_constants.PLACEHOLDER
                        ]
                )
                values = (value,)
            else:
                where = ''
                values = None

            query = (base_query.format(table=element,
                                       columns=columns,
                                       where=where
                                       ),
                     values
                     )
        elif query_type == db_constants.INSERT:
            columns_list = dict_utils.get_value(kwargs, 'columns')
            _check_properties(element, columns_list)
            columns = _build_elements_list(columns_list)
            values_list = dict_utils.get_value(kwargs, 'values')
            if len(columns_list) != len(values_list):
                raise db_exceptions.MismatchedParametersException(
                        columns_list,
                        values_list
                )

            values = _build_elements_tuple(values_list)
            query_values = _build_elements_list(_build_values_placeholders(values_list))
            query = (base_query.format(table=element,
                                       columns=columns,
                                       values=query_values
                                       ),
                     values
                     )
        elif query_type == db_constants.UPDATE:
            column = dict_utils.get_value(kwargs, 'column')
            _check_properties(element, [column])
            value = dict_utils.get_value(kwargs, 'value')
            search_column = dict_utils.get_value(kwargs,
                                                 'search_column',
                                                 True
                                                 )
            if search_column is not None:
                _check_properties(element, [search_column])
                condition = db_constants.EQUALS
                search_value = dict_utils.get_value(kwargs, 'search_value')
                where = _build_where_condition(
                        search_column,
                        condition,
                        db_queries.ELEMENTS[
                            db_constants.PLACEHOLDER
                        ]
                )
                values = (value, search_value)
            else:
                where = ''
                values = (value,)

            query = (base_query.format(table=element,
                                       column=column,
                                       value=db_queries.ELEMENTS[
                                           db_constants.PLACEHOLDER
                                       ],
                                       where=where
                                       ),
                     values
                     )
        elif query_type == db_constants.DELETE:
            search_column = dict_utils.get_value(kwargs,
                                                 'search_column',
                                                 True
                                                 )
            if search_column is not None:
                _check_properties(element, [search_column])
                condition = db_constants.EQUALS
                value = dict_utils.get_value(kwargs, 'search_value')
                where = _build_where_condition(
                        search_column,
                        condition,
                        db_queries.ELEMENTS[
                            db_constants.PLACEHOLDER
                        ]
                )
                values = (value,)
            else:
                where = ''
                values = None

            query = (base_query.format(table=element,
                                       where=where
                                       ),
                     values
                     )
        else:
            raise db_exceptions.UnrecognizedQueryTypeException(query_type)

    except generic_exceptions.MissingValueError as error:
        raise generic_exceptions.UndefinedParameterError(str(error))

    return query
예제 #19
0
파일: menu.py 프로젝트: albesca/books
def get_question(menu):
    question = dict_utils.get_value(menu, generic_const.QUESTION)
    return question
예제 #20
0
파일: book.py 프로젝트: albesca/books
def get_author(book):
    author = dict_utils.get_value(book, book_const.AUTHOR)
    return author
예제 #21
0
파일: branch.py 프로젝트: albesca/books
def get_target(element):
    target = dict_utils.get_value(element, branch_const.TARGET)
    return target
예제 #22
0
파일: test_dict.py 프로젝트: albesca/books
 def test_key_missing(self):
     with self.assertRaises(generic_exceptions.UndefinedParameterError):
         self.test_result = dict_utils.get_value(self.test_dict,
                                                 None
                                                 )
예제 #23
0
파일: test_dict.py 프로젝트: albesca/books
 def test_value_not_in_allow_none(self):
     self.test_result = dict_utils.get_value(self.test_dict,
                                             self.other_key,
                                             True
                                             )
     self.assertIsNone(self.test_result, 'wrong value')
예제 #24
0
파일: test_dict.py 프로젝트: albesca/books
 def test_value_not_in(self):
     with self.assertRaises(generic_exceptions.MissingValueError):
         self.test_result = dict_utils.get_value(self.test_dict,
                                                 self.other_key,
                                                 False
                                                 )
예제 #25
0
파일: menu.py 프로젝트: albesca/books
def get_choice_list(menu):
    choice_list = dict_utils.get_value(menu, menu_const.CHOICE_LIST)
    return choice_list
예제 #26
0
파일: choice.py 프로젝트: albesca/books
def get_action(choice):
    action = dict_utils.get_value(choice, choice_const.ACTION)
    return action
예제 #27
0
파일: manager.py 프로젝트: albesca/books
def _build_values_placeholders(values_list):
    placeholder = dict_utils.get_value(db_queries.ELEMENTS,
                                       db_constants.PLACEHOLDER
                                       )
    value_placeholders = placeholder * len(values_list)
    return value_placeholders
예제 #28
0
파일: element.py 프로젝트: albesca/books
def get_default_method(method_name):
    default_method = dict_utils.get_value(DEFAULT_METHODS, method_name)
    return default_method
예제 #29
0
파일: test_dict.py 프로젝트: albesca/books
 def test_value_in(self):
     self.test_result = dict_utils.get_value(self.test_dict, self.test_key)
     self.assertEqual(self.test_result, self.test_value, 'wrong value')
예제 #30
0
파일: list.py 프로젝트: albesca/books
def get_elements(elements_list):
    elements = dict_utils.get_value(elements_list, list_const.ELEMENTS)
    return elements