1
0
Fork 0
flightgear/scripts
Florent Rougon c0e1f29a75 terrasync.py: add and use a VirtualPath class; also add MutableVirtualPath
Add classes VirtualPath and MutableVirtualPath (the latter derived from
the former) to manipulate slash-separated paths where the root '/'
represents the TerraScenery root. This makes it clear what a function
expects when you see that one of its arguments is a VirtualPath
instance: you don't have to ask yourself whether it can start or end
with a slash, how to interpret it, etc. Operating on these paths is also
easy[1], be it to assemble URLs in order to retrieve files or to join
their relative part with a local directory path in order to obtain a
real (deeper) local path.

VirtualPath and MutableVirtualPath are essentially the same; the former
is hashable and therefore has to be immutable, whereas the latter can be
modified in-place with the /= operator (used to append path components),
and therefore can't be hashable. As a consequence, MutableVirtualPath
instances can't be used as dictionary keys, elements of a set or
frozenset, etc.

VirtualPath and MutableVirtualPath use the pathlib.PurePath API where
applicable (part of this API has been implemented in
[Mutable]VirtualPath; more can be added, of course). These classes have
no assumptions related to TerraSync and thus should be fit for use in
other projects.

To convert a [Mutable]VirtualPath instance to a string, just use str()
on it. The result is guaranteed to start with a '/' and not to end with
a '/', except for the virtual root '/'. Upon construction, the given
string is interpreted relatively to the virtual root, i.e.:

  VirtualPath("") == VirtualPath("/")
  VirtualPath("abc/def/ghi") == VirtualPath("/abc/def/ghi")
  etc.

VirtualPath and MutableVirtualPath instances sort like the respective
strings str() converts them too. The __hash__() method of VirtualPath is
based on the type and this string representation, too. Such objects can
only compare equal (using ==) if they have the same type. If you want to
compare the underlying virtual paths inside a VirtualPath and a
MutableVirtualPath, use the samePath() method of either class.

For more info, see scripts/python/TerraSync/terrasync/virtual_path.py
and unit tests in scripts/python/TerraSync/tests/test_virtual_path.py.

[1] Most useful is the / operator, which works as for SGPath:

      VirtualPath("/abc/def/ghi") == VirtualPath("/abc") / "def" / "ghi"
      VirtualPath("/abc/def/ghi") == VirtualPath("/abc") / "def/ghi"
2018-02-07 11:38:41 +01:00
..
atis Update ATIS voice generation scripts 2012-10-13 15:45:15 +02:00
completion Mark enhanced lighting options and --adf as deprecated 2017-06-04 16:56:17 +02:00
debug Goodbye automake. 2011-11-01 11:15:53 +00:00
example Goodbye automake. 2011-11-01 11:15:53 +00:00
java/FGClient #718: java client example not compiling 2012-03-27 21:06:29 +02:00
perl Goodbye automake. 2011-11-01 11:15:53 +00:00
python terrasync.py: add and use a VirtualPath class; also add MutableVirtualPath 2018-02-07 11:38:41 +01:00
syntax gedit syntaxhighlight/snippet support for Nasal. 2013-11-07 14:30:03 +01:00
tools Fix syntax for IF DEFINED conditionals 2010-11-14 23:32:37 +00:00
CMakeLists.txt Add CMake Linux install directives for the icons and the .desktop file 2017-05-24 22:47:01 +02:00