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
 from cocos.audio.pygame import mixer
13
 from cocos.audio.pygame import mixer
14
 from cocos.layer import ScrollableLayer
14
 from cocos.layer import ScrollableLayer
15
 from synergine2.config import Config
15
 from synergine2.config import Config
16
+from synergine2.exceptions import SynergineException
16
 from synergine2.log import get_logger
17
 from synergine2.log import get_logger
17
 from synergine2.terminals import Terminal
18
 from synergine2.terminals import Terminal
18
 from synergine2.terminals import TerminalPackage
19
 from synergine2.terminals import TerminalPackage
28
 from synergine2_cocos2d.middleware import MapMiddleware
29
 from synergine2_cocos2d.middleware import MapMiddleware
29
 from synergine2_cocos2d.middleware import TMXMiddleware
30
 from synergine2_cocos2d.middleware import TMXMiddleware
30
 from synergine2_cocos2d.user_action import UserAction
31
 from synergine2_cocos2d.user_action import UserAction
32
+from synergine2_cocos2d.util import ensure_dir_exist
31
 from synergine2_xyz.physics import Physics
33
 from synergine2_xyz.physics import Physics
32
 from synergine2_xyz.xyz import XYZSubjectMixin
34
 from synergine2_xyz.xyz import XYZSubjectMixin
33
 
35
 
733
         self.terminal = terminal
735
         self.terminal = terminal
734
         self.cycle_duration = self.config.resolve('core.cycle_duration')
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
         cocos.director.director.init(
747
         cocos.director.director.init(
737
             width=640,
748
             width=640,
738
             height=480,
749
             height=480,

+ 15 - 0
synergine2_cocos2d/util.py View File

2
 import os
2
 import os
3
 import typing
3
 import typing
4
 from os import path
4
 from os import path
5
+import shutil
6
+from pathlib import Path
5
 
7
 
6
 from synergine2_cocos2d.exception import FileNotFound
8
 from synergine2_cocos2d.exception import FileNotFound
7
 
9
 
47
             file_path,
49
             file_path,
48
             self._include_paths,
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)