コード例 #1
0
ファイル: _add__init__.py プロジェクト: ozgeneral/classier
def _add__init__(some_class, options):
    state_attribute_name = ATTRIBUTE_OPTIONS.ATTRIBUTE_NAME_STATE.get_option(options)
    default_state = ATTRIBUTE_OPTIONS.ATTRIBUTE_VALUE_DEFAULT_STATE.get_option(options)
    post_pointer_init = MAGIC_METHODS_OPTIONS.__INIT__METHOD_POST_POINTER_INIT.get_option(options)
    pointer_name = MAGIC_METHODS_OPTIONS.__INIT__ARGUMENT_NAME_POINTER.get_option(options)
    from_pointer_default = METHOD_OPTIONS.METHOD_POINTER_DEFAULT.get_option(options)

    old__init__ = some_class.__init__
    old__init__signature = inspect.signature(old__init__)
    old__init__parameters = old__init__signature.parameters.keys()
    from_pointer = _get_from_pointer(options)

    def new__init__(self, *args, **kwargs):
        pointer = kwargs.get(pointer_name)

        copied_default_state = copy.deepcopy(default_state)
        # pass any other supplementary information to default function if needed
        def default(p):
            val = utils.convenience.call(from_pointer_default,
                                         args=args,
                                         kwargs=kwargs)
            return utils.convenience.set_default(val, copied_default_state)
        if pointer is not None and pointer_name not in old__init__parameters:
            from_pointer(self, pointer, default=default)
            if post_pointer_init is not None:
                utils.convenience.call(post_pointer_init, args=(self, *args), kwargs=kwargs)
        else:
            setattr(self, state_attribute_name, copied_default_state)
            utils.convenience.call(old__init__, args=(self, *args), kwargs=kwargs)

    method_name_init = "__init__"
    if not ClassMarker.does_mark_exist(some_class, _MARK_ATTRIBUTE_NAME, _MARK_TYPE_NAME, method_name_init):
        ClassMarker.add_mark_to_class(some_class, _MARK_ATTRIBUTE_NAME, _MARK_TYPE_NAME, method_name_init)
        some_class = utils.convenience.add_mixin(some_class, new__init__, method_name_init)
    return some_class
コード例 #2
0
def _add__getitem__(some_class, options):
    def new__getitem__(self, keys):
        if isinstance(keys, str):
            keys = (keys, )
        elif not isinstance(keys, tuple):
            keys = tuple(keys)
        state_attribute_name = ATTRIBUTE_OPTIONS.ATTRIBUTE_NAME_STATE.get_option(
            options)
        state = getattr(self, state_attribute_name)
        current = state
        for key in keys[:-1]:
            key = str(key)
            if key not in current:
                return None
            current = current[key]
        key = str(keys[-1])
        return current.get(key)

    method_name_getitem = "__getitem__"
    if not ClassMarker.does_mark_exist(some_class, _MARK_ATTRIBUTE_NAME,
                                       _MARK_TYPE_NAME, method_name_getitem):
        ClassMarker.add_mark_to_class(some_class, _MARK_ATTRIBUTE_NAME,
                                      _MARK_TYPE_NAME, method_name_getitem)
        some_class = utils.convenience.add_mixin(some_class, new__getitem__,
                                                 method_name_getitem)
    return some_class
コード例 #3
0
ファイル: _add_del_state.py プロジェクト: ozgeneral/classier
def _add_del_state(some_class, options):
    state_attribute_name = ATTRIBUTE_OPTIONS.ATTRIBUTE_NAME_STATE.get_option(
        options)
    state_pointer_attribute_name = ATTRIBUTE_OPTIONS.ATTRIBUTE_NAME_STATE_POINTER.get_option(
        options)

    get_id = METHOD_OPTIONS.METHOD_GET_ID.get_option(options)
    saver = METHOD_OPTIONS.METHOD_SAVER.get_option(options)
    index = METHOD_OPTIONS.METHOD_INDEX.get_option(options)
    index_path = METHOD_OPTIONS.PATH_INDEX.get_option(options)

    def del_state(self, remove_empty_directories=True):
        state = getattr(self, state_attribute_name)
        state_pointer = state.get(state_pointer_attribute_name)
        if state_pointer is not None:
            state_id = None if get_id is None else str(get_id(self))
            index_information = None
            if index is not None and state_id is not None:
                index_information = index(state_id, type(self), index_path)
            saver.delete(state_pointer,
                         index_information=index_information,
                         remove_empty_directories=remove_empty_directories)

    method_name_del = METHOD_OPTIONS.METHOD_NAME_DEL_STATE.get_option(options)
    if not ClassMarker.does_mark_exist(some_class, _MARK_ATTRIBUTE_NAME,
                                       _MARK_TYPE_NAME, method_name_del):
        ClassMarker.add_mark_to_class(some_class, _MARK_ATTRIBUTE_NAME,
                                      _MARK_TYPE_NAME, method_name_del)
        some_class = utils.convenience.add_mixin(some_class, del_state,
                                                 method_name_del)
    return some_class
コード例 #4
0
ファイル: _add_get_state.py プロジェクト: ozgeneral/classier
def _add_get_state(some_class, options):
    state_attribute_name = ATTRIBUTE_OPTIONS.ATTRIBUTE_NAME_STATE.get_option(options)

    def get_state(self):
        return getattr(self, state_attribute_name)

    method_name_get_state = METHOD_OPTIONS.METHOD_NAME_GET_STATE.get_option(options)
    if not ClassMarker.does_mark_exist(some_class, _MARK_ATTRIBUTE_NAME, _MARK_TYPE_NAME, method_name_get_state):
        ClassMarker.add_mark_to_class(some_class, _MARK_ATTRIBUTE_NAME, _MARK_TYPE_NAME, method_name_get_state)
        some_class = utils.convenience.add_mixin(some_class, get_state, method_name_get_state)
    return some_class
コード例 #5
0
def _add_from_pointer(some_class, options):
    method_name_from_pointer = METHOD_OPTIONS.METHOD_NAME_FROM_POINTER.get_option(
        options)
    if not ClassMarker.does_mark_exist(some_class, _MARK_ATTRIBUTE_NAME,
                                       _MARK_TYPE_NAME,
                                       method_name_from_pointer):
        ClassMarker.add_mark_to_class(some_class, _MARK_ATTRIBUTE_NAME,
                                      _MARK_TYPE_NAME,
                                      method_name_from_pointer)
        some_class = utils.convenience.add_mixin(some_class,
                                                 _get_from_pointer(options),
                                                 method_name_from_pointer)
    return some_class
コード例 #6
0
def _add__len__(some_class, options):
    def new__len__(self):
        state_attribute_name = ATTRIBUTE_OPTIONS.ATTRIBUTE_NAME_STATE.get_option(
            options)
        state = getattr(self, state_attribute_name)
        return len(state)

    method_name_len = "__len__"
    if not ClassMarker.does_mark_exist(some_class, _MARK_ATTRIBUTE_NAME,
                                       _MARK_TYPE_NAME, method_name_len):
        ClassMarker.add_mark_to_class(some_class, _MARK_ATTRIBUTE_NAME,
                                      _MARK_TYPE_NAME, method_name_len)
        some_class = utils.convenience.add_mixin(some_class, new__len__,
                                                 method_name_len)
    return some_class
コード例 #7
0
ファイル: _add__eq__.py プロジェクト: ozgeneral/classier
def _add__eq__(some_class, options):
    get_id_name = METHOD_OPTIONS.METHOD_NAME_GET_ID.get_option(options)

    def new__eq__(self, other):
        try:
            get_id_self = getattr(self, get_id_name)
            get_id_other = getattr(other, get_id_name)
            return get_id_self() == get_id_other()
        except:
            return False

    method_name_eq = "__eq__"
    if not ClassMarker.does_mark_exist(some_class, _MARK_ATTRIBUTE_NAME, _MARK_TYPE_NAME, method_name_eq):
        ClassMarker.add_mark_to_class(some_class, _MARK_ATTRIBUTE_NAME, _MARK_TYPE_NAME, method_name_eq)
        some_class = utils.convenience.add_mixin(some_class, new__eq__, method_name_eq)
    return some_class
コード例 #8
0
def add_does_state_exist(some_class, options):
	get_id = METHOD_OPTIONS.METHOD_GET_ID.get_option(options)

	def does_state_exist(self, file_path=None):
		state_id = None if get_id is None else str(get_id(self))
		if file_path is None:
			get_path = METHOD_OPTIONS.METHOD_GET_PATH.get_option(options)
			path = get_path(self)
			assert state_id is not None
			file_path = os.path.join(path, state_id)
		return os.path.exists(file_path)

	method_name_does_state_exist = METHOD_OPTIONS.METHOD_NAME_DOES_STATE_EXIST.get_option(options)
	if not ClassMarker.does_mark_exist(some_class, _MARK_ATTRIBUTE_NAME, _MARK_TYPE_NAME, method_name_does_state_exist):
		ClassMarker.add_mark_to_class(some_class, _MARK_ATTRIBUTE_NAME, _MARK_TYPE_NAME, method_name_does_state_exist)
		some_class = utils.convenience.add_mixin(some_class, does_state_exist, method_name_does_state_exist)
	return some_class
コード例 #9
0
def _add__str__(some_class, options):
    def new__str__(self):
        state_attribute_name = ATTRIBUTE_OPTIONS.ATTRIBUTE_NAME_STATE.get_option(
            options)
        state = getattr(self, state_attribute_name)
        return json.dumps(state,
                          default=lambda o: str(o),
                          ensure_ascii=False,
                          indent=4,
                          sort_keys=True)

    method_name_str = "__str__"
    if not ClassMarker.does_mark_exist(some_class, _MARK_ATTRIBUTE_NAME,
                                       _MARK_TYPE_NAME, method_name_str):
        ClassMarker.add_mark_to_class(some_class, _MARK_ATTRIBUTE_NAME,
                                      _MARK_TYPE_NAME, method_name_str)
        some_class = utils.convenience.add_mixin(some_class, new__str__,
                                                 method_name_str)
    return some_class
コード例 #10
0
def _add_get_state_path(some_class, options):
    state_attribute_name = ATTRIBUTE_OPTIONS.ATTRIBUTE_NAME_STATE.get_option(
        options)
    state_pointer_attribute_name = ATTRIBUTE_OPTIONS.ATTRIBUTE_NAME_STATE_POINTER.get_option(
        options)

    def get_state_path(self):
        state = getattr(self, state_attribute_name)
        return state.get(state_pointer_attribute_name)

    method_name_get_state_path = METHOD_OPTIONS.METHOD_NAME_GET_STATE_PATH.get_option(
        options)
    if not ClassMarker.does_mark_exist(some_class, _MARK_ATTRIBUTE_NAME,
                                       _MARK_TYPE_NAME,
                                       method_name_get_state_path):
        ClassMarker.add_mark_to_class(some_class, _MARK_ATTRIBUTE_NAME,
                                      _MARK_TYPE_NAME,
                                      method_name_get_state_path)
        some_class = utils.convenience.add_mixin(some_class, get_state_path,
                                                 method_name_get_state_path)
    return some_class
コード例 #11
0
ファイル: _add_save_state.py プロジェクト: ozgeneral/classier
def _add_save_state(some_class, options):
    state_attribute_name = ATTRIBUTE_OPTIONS.ATTRIBUTE_NAME_STATE.get_option(
        options)
    state_pointer_attribute_name = ATTRIBUTE_OPTIONS.ATTRIBUTE_NAME_STATE_POINTER.get_option(
        options)

    get_id = METHOD_OPTIONS.METHOD_GET_ID.get_option(options)
    saver = METHOD_OPTIONS.METHOD_SAVER.get_option(options)
    index = METHOD_OPTIONS.METHOD_INDEX.get_option(options)
    index_path = METHOD_OPTIONS.PATH_INDEX.get_option(options)

    def save_state(self, file_path=None):
        state = getattr(self, state_attribute_name)
        state_id = None if get_id is None else str(get_id(self))

        if file_path is None:
            get_path = METHOD_OPTIONS.METHOD_GET_PATH.get_option(options)
            path = get_path(self)
            assert state_id is not None
            file_path = os.path.join(path, state_id)

        index_information = None
        if index is not None and state_id is not None:
            index_information = index(state_id, type(self), index_path)
        state[state_pointer_attribute_name] = file_path
        saver.save(state, file_path, index_information)

    method_name_save_state = METHOD_OPTIONS.METHOD_NAME_SAVE_STATE.get_option(
        options)
    if not ClassMarker.does_mark_exist(some_class, _MARK_ATTRIBUTE_NAME,
                                       _MARK_TYPE_NAME,
                                       method_name_save_state):
        ClassMarker.add_mark_to_class(some_class, _MARK_ATTRIBUTE_NAME,
                                      _MARK_TYPE_NAME, method_name_save_state)
        some_class = utils.convenience.add_mixin(some_class, save_state,
                                                 method_name_save_state)
    return some_class