Browse Source

create cache dir

Bastien Sevajol 6 years ago
parent
commit
da3b9611a4
2 changed files with 26 additions and 0 deletions
  1. 11 0
      synergine2_cocos2d/gui.py
  2. 15 0
      synergine2_cocos2d/util.py

+ 11 - 0
synergine2_cocos2d/gui.py View File

@@ -13,6 +13,7 @@ from cocos import euclid
13 13
 from cocos.audio.pygame import mixer
14 14
 from cocos.layer import ScrollableLayer
15 15
 from synergine2.config import Config
16
+from synergine2.exceptions import SynergineException
16 17
 from synergine2.log import get_logger
17 18
 from synergine2.terminals import Terminal
18 19
 from synergine2.terminals import TerminalPackage
@@ -28,6 +29,7 @@ from synergine2_cocos2d.layer import LayerManager
28 29
 from synergine2_cocos2d.middleware import MapMiddleware
29 30
 from synergine2_cocos2d.middleware import TMXMiddleware
30 31
 from synergine2_cocos2d.user_action import UserAction
32
+from synergine2_cocos2d.util import ensure_dir_exist
31 33
 from synergine2_xyz.physics import Physics
32 34
 from synergine2_xyz.xyz import XYZSubjectMixin
33 35
 
@@ -733,6 +735,15 @@ class Gui(object):
733 735
         self.terminal = terminal
734 736
         self.cycle_duration = self.config.resolve('core.cycle_duration')
735 737
 
738
+        # Manager cache directory
739
+        cache_dir_path = self.config.resolve('global.cache_dir_path')
740
+        if not cache_dir_path:
741
+            raise SynergineException(
742
+                'This code require the "global.cache_dir_path" config',
743
+            )
744
+
745
+        ensure_dir_exist(cache_dir_path)
746
+
736 747
         cocos.director.director.init(
737 748
             width=640,
738 749
             height=480,

+ 15 - 0
synergine2_cocos2d/util.py View File

@@ -2,6 +2,8 @@
2 2
 import os
3 3
 import typing
4 4
 from os import path
5
+import shutil
6
+from pathlib import Path
5 7
 
6 8
 from synergine2_cocos2d.exception import FileNotFound
7 9
 
@@ -47,3 +49,16 @@ class PathManager(object):
47 49
             file_path,
48 50
             self._include_paths,
49 51
         ))
52
+
53
+
54
+def ensure_dir_exist(dir_path, clear_dir: bool=False) -> None:
55
+    """
56
+    Create directories if no exists
57
+    :param dir_path: path of wanted directory to exist
58
+    :param clear_dir: Remove content of given dir
59
+    """
60
+    path_ = Path(dir_path)
61
+    path_.mkdir(parents=True)
62
+    if clear_dir:
63
+        shutil.rmtree(dir_path)
64
+        path_.mkdir(parents=True)