ya2 · news · projects · code · about

baf951bddf6b76b2d8672e7bd56e08bf22a674f1
[pmachines.git] / ya2 / utils / logics.py
1 from sys import platform
2 from os.path import exists
3 from pathlib import Path
4 from platform import system
5 from panda3d.core import GraphicsWindow, Filename
6
7
8 class class_property(property):
9 def __get__(self, cls, owner):
10 return classmethod(self.fget).__get__(None, owner)()
11
12
13 class LogicsTools:
14
15 @class_property
16 def in_build(cls): return not exists('main.py')
17
18 @class_property
19 def windowed(cls): return base.win and isinstance(base.win, GraphicsWindow)
20
21 @staticmethod
22 def is_appimage(app_name):
23 parent_path = str(Path(__file__).parent.absolute())
24 mounted = parent_path.startswith(LogicsTools.appimage_path(app_name))
25 return mounted and parent_path.endswith('/usr/bin')
26
27 @staticmethod
28 def appimage_path(app_name):
29 folder_mounted_appimage = app_name[:6].capitalize()
30 return f'/tmp/.mount_{folder_mounted_appimage}'
31
32 @class_property
33 def appdata_path(cls):
34 appdata_path = str(Filename.get_user_appdata_directory())
35 home = str(Path.home()) # '/home/flavio' # we must force this for wine
36 if appdata_path.startswith('/c/users/') and exists(home + '/.wine/'):
37 appdata_path = home + '/.wine/drive_' + appdata_path[1:]
38 return appdata_path
39
40 @staticmethod
41 def current_path(app_name):
42 if not LogicsTools.in_build: return ''
43 if system() == 'Windows' or exists('main.py'): return ''
44 else: par_path = str(Path(__file__).parent.absolute())
45 if LogicsTools.is_appimage(app_name):
46 par_path = str(Path(par_path).absolute())
47 return par_path + '/'
48
49 @staticmethod
50 def platform_specific_path(path):
51 if LogicsTools.__is_windows_not_wine():
52 path = LogicsTools.__linux2windows_path(path)
53 return path
54
55 @staticmethod
56 def __is_windows_not_wine():
57 home = str(Path.home())
58 return platform == 'win32' and not exists(home + '/.wine/')
59
60 @staticmethod
61 def __linux2windows_path(path):
62 if path.startswith('/'):
63 path = path[1] + ':\\' + path[3:]
64 return path.replace('/', '\\')