source: geniuswebcore/geniusweb/actions/FileLocation.py@ 73

Last change on this file since 73 was 73, checked in by Bart Vastenhouw, 2 years ago

Fix for IssueValue hashcode.

File size: 2.5 KB
Line 
1from os import makedirs
2import tempfile
3from typing import Union
4from uuid import UUID, uuid4
5
6from pyson.JsonValue import JsonValue
7
8import os.path as path
9
10NoneType = type(None)
11
12class 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 '''
29
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
Note: See TracBrowser for help on using the repository browser.