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
|
---|
13 |
|
---|
14 |
|
---|
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.
|
---|
31 |
|
---|
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.
|
---|
41 |
|
---|
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.
|
---|
63 |
|
---|
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_()
|
---|