[Kimchi-devel] [PATCH 01/13] refactor model: Create a separated model for task resource
Royce Lv
lvroyce at linux.vnet.ibm.com
Tue Jan 21 08:59:49 UTC 2014
On 2014年01月17日 10:24, Aline Manera wrote:
> From: Aline Manera <alinefm at br.ibm.com>
>
> To avoid duplicating code in model and mockmodel, the common code related to
> task resource was added to model_/tasks.py and the specific code for each
> backend (libvirt or mock) was added to model_/libvirtbackend.py and
> model_/mockbackend.py
> The model_ module is a temporary one, as there already is a module named model
> in source code.
> When start using the new code, model_ will be renamed to model and the former
> model.py deleted.
>
> Signed-off-by: Aline Manera <alinefm at br.ibm.com>
> ---
> src/kimchi/model_/__init__.py | 21 ++++++++++++++++
> src/kimchi/model_/libvirtbackend.py | 28 ++++++++++++++++++++++
> src/kimchi/model_/mockbackend.py | 28 ++++++++++++++++++++++
> src/kimchi/model_/tasks.py | 45 +++++++++++++++++++++++++++++++++++
> 4 files changed, 122 insertions(+)
> create mode 100644 src/kimchi/model_/__init__.py
> create mode 100644 src/kimchi/model_/libvirtbackend.py
> create mode 100644 src/kimchi/model_/mockbackend.py
> create mode 100644 src/kimchi/model_/tasks.py
>
> diff --git a/src/kimchi/model_/__init__.py b/src/kimchi/model_/__init__.py
> new file mode 100644
> index 0000000..8a37cc4
> --- /dev/null
> +++ b/src/kimchi/model_/__init__.py
> @@ -0,0 +1,21 @@
> +#
> +# Project Kimchi
> +#
> +# Copyright IBM, Corp. 2013
> +#
> +# Authors:
> +# Aline Manera <alinefm at linux.vnet.ibm.com>
> +#
> +# This library is free software; you can redistribute it and/or
> +# modify it under the terms of the GNU Lesser General Public
> +# License as published by the Free Software Foundation; either
> +# version 2.1 of the License, or (at your option) any later version.
> +#
> +# This library is distributed in the hope that it will be useful,
> +# but WITHOUT ANY WARRANTY; without even the implied warranty of
> +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
> +# Lesser General Public License for more details.
> +#
> +# You should have received a copy of the GNU Lesser General Public
> +# License along with this library; if not, write to the Free Software
> +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
> diff --git a/src/kimchi/model_/libvirtbackend.py b/src/kimchi/model_/libvirtbackend.py
> new file mode 100644
> index 0000000..0c70116
> --- /dev/null
> +++ b/src/kimchi/model_/libvirtbackend.py
> @@ -0,0 +1,28 @@
> +#
> +# Project Kimchi
> +#
> +# Copyright IBM, Corp. 2013
> +#
> +# Authors:
> +# Adam Litke <agl at linux.vnet.ibm.com>
> +# Aline Manera <alinefm at linux.vnet.ibm.com>
> +#
> +# This library is free software; you can redistribute it and/or
> +# modify it under the terms of the GNU Lesser General Public
> +# License as published by the Free Software Foundation; either
> +# version 2.1 of the License, or (at your option) any later version.
> +#
> +# This library is distributed in the hope that it will be useful,
> +# but WITHOUT ANY WARRANTY; without even the implied warranty of
> +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
> +# Lesser General Public License for more details.
> +#
> +# You should have received a copy of the GNU Lesser General Public
> +# License along with this library; if not, write to the Free Software
> +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
> +
> +from kimchi.objectstore import ObjectStore
> +
> +class LibvirtBackend(object):
> + def __init__(self, objstore_loc=None):
> + self.objstore = ObjectStore(objstore_loc)
> diff --git a/src/kimchi/model_/mockbackend.py b/src/kimchi/model_/mockbackend.py
> new file mode 100644
> index 0000000..e3c6ab9
> --- /dev/null
> +++ b/src/kimchi/model_/mockbackend.py
> @@ -0,0 +1,28 @@
> +#
> +# Project Kimchi
> +#
> +# Copyright IBM, Corp. 2013
> +#
> +# Authors:
> +# Adam Litke <agl at linux.vnet.ibm.com>
> +# Aline Manera <alinefm at linux.vnet.ibm.com>
> +#
> +# This library is free software; you can redistribute it and/or
> +# modify it under the terms of the GNU Lesser General Public
> +# License as published by the Free Software Foundation; either
> +# version 2.1 of the License, or (at your option) any later version.
> +#
> +# This library is distributed in the hope that it will be useful,
> +# but WITHOUT ANY WARRANTY; without even the implied warranty of
> +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
> +# Lesser General Public License for more details.
> +#
> +# You should have received a copy of the GNU Lesser General Public
> +# License along with this library; if not, write to the Free Software
> +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
> +
> +from kimchi.objectstore import ObjectStore
> +
> +class MockBackend(object):
> + def __init__(self, objstore_loc=None):
> + self.objstore = ObjectStore(objstore_loc)
> diff --git a/src/kimchi/model_/tasks.py b/src/kimchi/model_/tasks.py
> new file mode 100644
> index 0000000..29eaddf
> --- /dev/null
> +++ b/src/kimchi/model_/tasks.py
> @@ -0,0 +1,45 @@
> +#
> +# Project Kimchi
> +#
> +# Copyright IBM, Corp. 2013
> +#
> +# Authors:
> +# Adam Litke <agl at linux.vnet.ibm.com>
> +# Aline Manera <alinefm at linux.vnet.ibm.com>
> +#
> +# This library is free software; you can redistribute it and/or
> +# modify it under the terms of the GNU Lesser General Public
> +# License as published by the Free Software Foundation; either
> +# version 2.1 of the License, or (at your option) any later version.
> +#
> +# This library is distributed in the hope that it will be useful,
> +# but WITHOUT ANY WARRANTY; without even the implied warranty of
> +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
> +# Lesser General Public License for more details.
> +#
> +# You should have received a copy of the GNU Lesser General Public
> +# License along with this library; if not, write to the Free Software
> +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
> +
> +from kimchi.exception import NotFoundError
> +
> +ERROR_TASK_NOT_FOUND = "Task id '%s' not found."
> +
> +class Tasks(object):
> + def __init__(self, backend):
> + self.objstore = backend.objstore
This makes me want a singleton and call it from get().
> +
> + def get_list(self):
> + with self.objstore as session:
> + return session.get_list('task')
> +
> +class Task(object):
> + def __init__(self, backend):
> + self.objstore = backend.objstore
> +
> + def lookup(self, ident):
> + if ident not in self.get_list():
> + raise NotFoundError(ERROR_TASK_NOT_FOUND % ident)
> +
> + with self.objstore as session:
> + return session.get('task', str(ident))
More information about the Kimchi-devel
mailing list