From c66ef9d218ebe9703de2c99cea80dde80a4a423d Mon Sep 17 00:00:00 2001 From: Julien Duponchelle Date: Thu, 16 Jun 2016 16:57:54 +0200 Subject: [PATCH] Support filename for project in the controller --- gns3server/controller/__init__.py | 3 ++- gns3server/controller/project.py | 19 ++++++++++--------- tests/controller/test_controller.py | 7 ++++--- tests/controller/test_project.py | 2 +- 4 files changed, 17 insertions(+), 14 deletions(-) diff --git a/gns3server/controller/__init__.py b/gns3server/controller/__init__.py index ca64408c..b665529e 100644 --- a/gns3server/controller/__init__.py +++ b/gns3server/controller/__init__.py @@ -217,9 +217,10 @@ class Controller: topo_data.pop("revision") topo_data.pop("type") - project = yield from self.add_project(path=os.path.dirname(path), status="closed", **topo_data) + project = yield from self.add_project(path=os.path.dirname(path), status="closed", filename=os.path.basename(path), **topo_data) if load: yield from project.open() + return project @property def projects(self): diff --git a/gns3server/controller/project.py b/gns3server/controller/project.py index b7df4d69..7b8c3768 100644 --- a/gns3server/controller/project.py +++ b/gns3server/controller/project.py @@ -43,7 +43,7 @@ class Project: :param status: Status of the project (opened / closed) """ - def __init__(self, name=None, project_id=None, path=None, controller=None, status="opened"): + def __init__(self, name=None, project_id=None, path=None, controller=None, status="opened", filename=None): self._controller = controller self._name = name @@ -60,6 +60,13 @@ class Project: if path is None: path = os.path.join(get_default_project_directory(), self._id) self.path = path + + if filename is None and name is None: + self._filename = "project.gns3" + elif filename is not None: + self._filename = filename + else: + self._filename = self.name + ".gns3" self.reset() def reset(self): @@ -322,14 +329,8 @@ class Project: raise aiohttp.web.HTTPInternalServerError(text="Could not create project directory: {}".format(e)) return path - def _filename(self): - if self.name is None: - return "untitled.gns3" - else: - return self.name + ".gns3" - def _topology_file(self): - return os.path.join(self.path, self._filename()) + return os.path.join(self.path, self._filename) @asyncio.coroutine def open(self): @@ -373,6 +374,6 @@ class Project: "name": self._name, "project_id": self._id, "path": self._path, - "filename": self._filename(), + "filename": self._filename, "status": self._status } diff --git a/tests/controller/test_controller.py b/tests/controller/test_controller.py index dd48bca1..34caa19c 100644 --- a/tests/controller/test_controller.py +++ b/tests/controller/test_controller.py @@ -188,7 +188,7 @@ def test_close(controller, async_run): def test_load_project(controller, async_run, tmpdir): data = { - "name": "Test", + "name": "Experience", "project_id": "c8d07a5a-134f-4c3f-8599-e35eac85eb17", "revision": 5, "type": "topology", @@ -242,11 +242,12 @@ def test_load_project(controller, async_run, tmpdir): controller._computes["my_remote"] = MagicMock() with asyncio_patch("gns3server.controller.node.Node.create") as mock_node_create: - async_run(controller.load_project(str(tmpdir / "test.gns3"))) + project = async_run(controller.load_project(str(tmpdir / "test.gns3"))) + assert project._topology_file() == str(tmpdir / "test.gns3") controller.add_compute.assert_called_with(compute_id='my_remote', host='127.0.0.1', name='My remote', port=3080, protocol='http') project = controller.get_project('c8d07a5a-134f-4c3f-8599-e35eac85eb17') - assert project.name == "Test" + assert project.name == "Experience" assert project.path == str(tmpdir) link = project.get_link("c44331d2-2da4-490d-9aad-7f5c126ae271") assert len(link.nodes) == 2 diff --git a/tests/controller/test_project.py b/tests/controller/test_project.py index 567a356a..9d29050e 100644 --- a/tests/controller/test_project.py +++ b/tests/controller/test_project.py @@ -43,7 +43,7 @@ def test_affect_uuid(): def test_json(tmpdir): p = Project() - assert p.__json__() == {"name": p.name, "project_id": p.id, "path": p.path, "status": "opened", "filename": "untitled.gns3"} + assert p.__json__() == {"name": p.name, "project_id": p.id, "path": p.path, "status": "opened", "filename": "project.gns3"} def test_path(tmpdir):