Changeset 111 for geniuswebcore/geniusweb/actions/FileLocation.py
- Timestamp:
- Jul 23, 2025, 10:18:43 AM (2 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
geniuswebcore/geniusweb/actions/FileLocation.py
r100 r111 1 from os import makedirs 2 import tempfile 3 from typing import Union 4 from uuid import UUID, uuid4 1 # Generated from java by J2P 2 from __future__ import annotations 3 from pathlib import Path 4 from pyson.JsonValue import JsonValue 5 from tudelft.utilities.tools.safehash import safehash 6 from tudelft.utilities.tools.systemproperties import systemproperties 7 from typing import Any 8 from typing import Optional 9 from typing import cast 10 from uuid import UUID 11 from uuid import uuid4 12 import os 5 13 6 from pyson.JsonValue import JsonValue7 14 8 import os.path as path 15 class FileLocation: 16 ''' 17 This contains a "system independent" {@link File}. This is represented by a 18 {@link UUID}. See {@link #getFile()} to get the file referenced by this. The 19 file may or may not yet exist (depending on whether your party already wrote 20 some contents in that file in previous runs), may or may not be empty (your 21 choice), and can contain any type of data. The original intent of this is 22 that parties can store learned data in this file, while the name can be kept 23 for later re-use in later runs (eg negotiation sessions) on the same machine. 24 <p> 25 This file is to be used locally on some other machine (typically a 26 partiesserver). This object itself only contains a file name. 27 {@link #getFile()} turns it into a system-specific {@link File}. <br> 28 <h2>WARNING</h2> if used multiple times on different machines, multiple, 29 separate files will be involved, there is no magical synchronization of 30 multiple items with the same UUID used on different machines. 9 31 10 NoneType = type(None) 32 ''' 33 34 def __init__(self, name:Optional[UUID]): 35 ''' 36 @param name the name of the file (must be UUID to prevent injection of 37 bad filenames like /.. or C:) Use {@link #FileLocation()} to 38 create new FileLocation. That should ensure a new UUID that 39 does not collide with existing ones. If null, a random UUID 40 is created. 11 41 12 class FileLocation : 13 ''' 14 This contains a "system independent" {@link File}. This is represented by a 15 {@link UUID}. See {@link #getFile()} to get the file referenced by this. The 16 file may or may not yet exist (depending on whether your party already wrote 17 some contents in that file in previous runs), may or may not be empty (your 18 choice), and can contain any type of data. The original intent of this is 19 that parties can store learned data in this file, while the name can be kept 20 for later re-use in later runs (eg negotiation sessions) on the same machine. 21 <p> 22 This file is to be used locally on some other machine (typically a 23 partiesserver). This object itself only contains a file name. 24 {@link #getFile()} turns it into a system-specific {@link File}. <br> 25 <h2>WARNING</h2> if used multiple times on different machines, multiple, 26 separate files will be involved, there is no magical synchronization of 27 multiple items with the same UUID used on different machines. 28 ''' 42 ''' 43 self.__name:UUID = None 44 super().__init__() 45 if (name is None): 46 name=uuid4() 47 if not(os.path.exists(FileLocation.__rootfolder)): 48 os.mkdir(FileLocation.__rootfolder) 49 self.__name=name 50 51 @JsonValue() 52 def getUUID(self) -> Optional[UUID]: 53 return self.__name 54 55 def getFile(self) -> str: 56 ''' 57 58 @return Actual file that can be used for read/write operations. This file 59 usually resides in the geniusweb folder inside the java.io.tmpdir 60 system property. This temp dir depends on the run configuration, 61 eg some directory inside the user's home directory, or a temp 62 folder inside tomcat. 29 63 30 _TMP = tempfile.gettempdir() 31 _GENIUSWEBPYTHON = "geniuswebpython"; 32 _rootpath = path.join(_TMP, _GENIUSWEBPYTHON) 33 34 def __init__(self, name: Union[UUID,NoneType]=None ): 35 ''' 36 @param name the name of the file (must be UUID to prevent injection of 37 bad filenames like /.. or C:) Use {@link #FileLocation()} to 38 create new FileLocation. That should ensure a new UUID that 39 does not collide with existing ones. If None, then this 40 creates a new random UUID. 41 ''' 42 if not path.exists(self._rootpath) : 43 makedirs(self._rootpath); 44 # don't print the location, python parties would crash 45 if name==None: 46 name=uuid4() 47 self._name:UUID = name #type:ignore 48 49 @JsonValue() 50 def getName(self) -> UUID: 51 return self._name 52 53 def getFile(self) ->str : 54 ''' 55 @return Actual filename that can be used for read/write operations. This file 56 usually resides in the geniuswebpython folder inside the tmpdir. 57 This temp dir depends on the run configuration, 58 eg some directory inside the user's home directory, or a temp 59 folder inside tomcat. 60 ''' 61 return path.join(self._rootpath, str(self._name)) 62 63 def __repr__(self): 64 return "FileLocation[" + self._name + "]" 65 66 def __hash__(self): 67 return hash(self._name) 68 69 70 def __eq__(self, other): 71 return isinstance(other, self.__class__) and \ 72 self._name==other._name 73 64 ''' 65 return str(Path() / FileLocation.__TMP / FileLocation.__GENIUSWEB / str(self.__name)) 66 67 #Override 68 def __repr__(self) -> str: 69 return "FileLocation[" + str(self.__name) + "]" 70 71 #Override 72 def __hash__(self) -> int: 73 prime:int = 31 74 result:int = 1 75 result=((prime * result) + (0 if ((self.__name is None)) else safehash(self.__name))) 76 return result 77 78 #Override 79 def __eq__(self,obj:Optional[Any]) -> bool: 80 if (self is obj): 81 return True 82 if (obj is None): 83 return False 84 if (type(self) is not type(obj)): 85 return False 86 other:Optional[FileLocation] = cast(FileLocation,obj) 87 if (self.__name is None): 88 if (other.__name is not None): 89 return False 90 else: 91 if not(self.__name == other.__name): 92 return False 93 return True 94 @staticmethod 95 def _static_init_(): 96 FileLocation.__TMP:str = systemproperties()["java.io.tmpdir"] 97 FileLocation.__GENIUSWEB:str = "geniusweb" 98 FileLocation.__rootpath:Path = Path() / FileLocation.__TMP / FileLocation.__GENIUSWEB 99 FileLocation.__rootfolder:str = str(FileLocation.__rootpath) 100 FileLocation._static_init_()
Note:
See TracChangeset
for help on using the changeset viewer.