Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Subdirectories #1567

Merged
merged 48 commits into from
May 28, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
253d087
Path handling types
tomekpiotrowski Mar 28, 2016
d9a2917
Add directory support for Archive and Harddisk
tomekpiotrowski Apr 4, 2016
fad654a
PathValue structure
tomekpiotrowski Apr 4, 2016
bf763e2
path() command
tomekpiotrowski Apr 5, 2016
6e42e0f
scriptpath() command
tomekpiotrowski Apr 5, 2016
baf2311
Initialize VolumeItem suffixes
tomekpiotrowski Apr 5, 2016
039c6e5
Make Volume:NAME settable
tomekpiotrowski Apr 5, 2016
3b02c7b
Change how create file/directory handles conflicts
tomekpiotrowski Apr 5, 2016
84c86c4
Add deprecation messages to old rename/delete/copy syntax
tomekpiotrowski Apr 5, 2016
307a909
Add VolumeItem:ISFILE suffix
tomekpiotrowski Apr 5, 2016
8cb2dd6
Add volume() function
tomekpiotrowski Apr 5, 2016
22c454e
Copy implemented
tomekpiotrowski Apr 6, 2016
3361a65
Harddisk persistence fix
tomekpiotrowski Apr 6, 2016
1096094
Bugfix in VolumeManager
tomekpiotrowski Apr 7, 2016
8cceb42
Added missing type names.
tomekpiotrowski Apr 7, 2016
864e45c
Bug fixes
tomekpiotrowski Apr 7, 2016
ee8201f
Moar bugfixes.
tomekpiotrowski Apr 7, 2016
f777034
Fix edit()
tomekpiotrowski Apr 7, 2016
39ed5f6
Copying unit tests
tomekpiotrowski Apr 7, 2016
d6c34fa
More copy() unit tests
tomekpiotrowski Apr 7, 2016
53abaed
move() unit tests
tomekpiotrowski Apr 10, 2016
e88c262
Bug fixes
tomekpiotrowski Apr 11, 2016
dc71b4f
Path serialization fix
tomekpiotrowski Apr 11, 2016
a805f7d
Handle volumes without names
tomekpiotrowski Apr 11, 2016
217862f
Fix handling cooked file names by delete()
tomekpiotrowski Apr 11, 2016
0986d10
Look for boot files in 'boot' directory
tomekpiotrowski Apr 12, 2016
4fac4fa
Revert "Add deprecation messages to old rename/delete/copy syntax"
tomekpiotrowski Apr 12, 2016
1c94210
Volume:open should return false when item does not exist
tomekpiotrowski Apr 13, 2016
16acf6a
Rename COPY(), MOVE() and DELETE()
tomekpiotrowski Apr 18, 2016
92e1c01
Updated Volume documentation
tomekpiotrowski Apr 26, 2016
a3b9bb0
Few more GlobalPath tests
tomekpiotrowski Apr 26, 2016
2e1c8df
Path documentation
tomekpiotrowski Apr 26, 2016
9d3366e
Update VOLUME:OPEN documentation
tomekpiotrowski Apr 26, 2016
e97817d
Bugfixes
tomekpiotrowski Apr 28, 2016
62c84a7
copypath() bugfixes
tomekpiotrowski Apr 28, 2016
ec7bbf5
More copy() tests and bugfixes
tomekpiotrowski Apr 28, 2016
c531db5
Volumes and VolumeItems can now be used as paths
tomekpiotrowski May 7, 2016
08188bc
Commands->File I/O documentation page
tomekpiotrowski May 7, 2016
bb45190
Add PathValue:CHANGENAME
tomekpiotrowski May 7, 2016
3ed5a0f
Make directories enumerable
tomekpiotrowski May 7, 2016
f509340
Removed VolumeManager's electric power code
tomekpiotrowski May 7, 2016
6e46db0
Fix some errors on the Commands/FileIO page
tomekpiotrowski May 7, 2016
1084423
Add Volume:ROOT suffix
tomekpiotrowski May 7, 2016
2ba2514
Fix VolumeDirectory enumeration
tomekpiotrowski May 7, 2016
b05a5d6
Add VolumeDirectory and VolumeItem doc pages
tomekpiotrowski May 7, 2016
05527a5
Update Volume documentation page
tomekpiotrowski May 7, 2016
b3fda23
Fix typos
tomekpiotrowski May 14, 2016
7a4439e
Volume:NAME is now settable
tomekpiotrowski May 14, 2016
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
606 changes: 380 additions & 226 deletions doc/source/commands/files.rst

Large diffs are not rendered by default.

55 changes: 40 additions & 15 deletions doc/source/general/volumes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,12 @@
Files and Volumes
=================

Using the COPY, SWITCH, DELETE, and RENAME commands, you can manipulate the archive and the volumes as described in the :ref:`File I/O page <files>`. But before you do that, it's useful to know how kOS manages the archive and the volumes, and what they mean.
Using the COPYPATH, SWITCH, DELETEPATH, and RENAMEPATH commands, you can manipulate the archive and the volumes as described in the :ref:`File I/O page <files>`. But before you do that, it's useful to know how kOS manages the archive and the volumes, and what they mean.

.. contents::
:local:
:depth: 2

.. warning::

.. versionchanged:: 0.15

**Archive location and file extension change**

The Archive where KerboScript files are kept has been changed from ``Plugins/PluginData/Archive`` to ``Ships/Script``, but still under the top-level **KSP** installation directory. The file name extensions have also changes from ``.txt`` to ``.ks``.

Script Files
------------

Expand Down Expand Up @@ -53,7 +45,7 @@ program on it. To simulate the sense that this game takes place at the
dawn of the space race with 1960's and 1970's technology, the storage
capacity of a volume is very limited.

For example, the CX-4181 Scriptable Control System part defaults to only
For example, the CX-4181 Scriptable Control System part defaults to only
allowing 1000 bytes of storage.

The byte count of a program is just the
Expand Down Expand Up @@ -99,8 +91,7 @@ volume inside it.
If you have multiple CX-4181 parts on the same craft, they are assumed
to be networked together on the same system, and capable of reading each
other's hard drives. Their disk drives each have a different Volume, and
by default they are simply numbered 1,2,3, … unless you rename them with
the RENAME command.
by default they are simply numbered 1,2,3, … unless you rename them.

For example, if you have two CX-4181's on the same craft, called 1 and
2, with volumes on them called 1 and 2, respectively, it is possible for
Expand All @@ -116,11 +107,13 @@ different CPUs. The same volume which was called '2' when one CPU was
looking at it might instead be called '1' when a different CPU is
looking at it. Each CPU thinks of its OWN volume as number '1'.

Therefore using the RENAME command on the volumes is useful when dealing
Therefore using the SET command on the volumes is useful when dealing
with multiple CX-4181's on the same vessel, so they all will refer to
the volumes using the same names.
the volumes using the same names::

SET VOLUME("0"):NAME TO "newname".

If a kOS processor has a name tag set, then that processor's volume
If a kOS processor has a name tag set, then that processor's volume
will have its name initially set to the value of the name tag.

Archive
Expand Down Expand Up @@ -164,3 +157,35 @@ volume but with the following exceptions:
constitutes a form of cheating similar to any other edit of the
persistence file.

.. _boot:

Special handling of files in the "boot" directory
-------------------------------------------------

For users requiring even more automation, the feature of custom boot scripts
was introduced. If you have at least 1 file in the :code:`boot` directory on
your Archive volume, you will be presented with the option to choose one of
those files as a boot script for your kOS CPU.

.. image:: http://i.imgur.com/05kp7Sy.jpg

As soon as you vessel leaves VAB/SPH and is being initialised on the launchpad
(e.g. its status is PRELAUNCH) the assigned script will be copied to CPU's
local hard disk with the same name. If kOS is configured to start on the
archive, the file will not be copied locally automatically. This script will
be run as soon as CPU boots, e.g. as soon as you bring your CPU in physics
range or power on your CPU if it was turned off. You may get or set the name
of the boot file using the :attr:`kOSProcessor:BOOTFILENAME` suffix.

Important things to consider:

* kOS CPU hard disk space is limited, avoid using complex boot scripts or increase disk space using MM config.
* Boot script runs immediately on initialisation, it should avoid interaction with parts/modules until physics fully load. It is best to wait for couple seconds or until certain trigger.

Possible uses for boot scripts:

* Automatically activate sleeper/background scripts which will run on CPU until triggered by certain condition.
* Create basic station-keeping scripts - you will only have to focus your probes once in a while and let the boot script do the orbit adjustment automatically.
* Create multi-CPU vessels with certain cores dedicated to specific tasks, triggered by user input or external events (Robotic-heavy Vessels)
* Anything else you can come up with

144 changes: 144 additions & 0 deletions doc/source/structures/volumes_and_files/path.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
.. _path:

Path
====

Represents a path. Contains suffixes that can be helpful when using and manipulating paths. You can use
:ref:`path() <path_command>` to create new instances.

Instances of this structure can be passed as arguments instead of ordinary, string paths, for example::

copypath("../file", path()).

.. structure:: Path

.. list-table::
:header-rows: 1
:widths: 2 1 4

* - Suffix
- Type
- Description

* - :attr:`VOLUME`
- :struct:`Volume`
- Volume this path belongs to
* - :attr:`SEGMENTS`
- :struct:`List` of :struct:`String`
- List of this path's segments
* - :attr:`LENGTH`
- :struct:`Scalar`
- Number of segments in this path
* - :attr:`NAME`
- :struct:`String`
- Name of file or directory this path points to
* - :attr:`HASEXTENSION`
- :struct:`Boolean`
- True if path contains an extension
* - :attr:`EXTENSION`
- :struct:`String`
- This path's extension
* - :attr:`ROOT`
- :struct:`Path`
- Root path of this path's volume
* - :attr:`PARENT`
- :struct:`Path`
- Parent path
* - :meth:`CHANGENAME(name)`
- :struct:`Path`
- Returns a new path with its name (last segment) changed
* - :meth:`CHANGEEXTENSION(extension)`
- :struct:`Path`
- Returns a new path with extension changed
* - :meth:`ISPARENT(path)`
- :struct:`Boolean`
- True if `path` is the parent of this path
* - :meth:`COMBINE(name1, [name2, ...])`
- :struct:`Path`
- Returns a new path created from this one

.. attribute:: Path:VOLUME

:type: :struct:`Volume`
:access: Get only

Volume this path belongs to.

.. attribute:: Path:SEGMENTS

:type: :struct:`List` of :struct:`String`
:access: Get only

List of segments this path contains. Segments are parts of the path separated by `/`. For example path `0:/directory/subdirectory/script.ks` contains the following segments:
`directory`, `subdirectory` and `script.ks`.

.. attribute:: Path:LENGTH

:type: :struct:`Scalar`
:access: Get only

Number of this path's segments.

.. attribute:: Path:NAME

:type: :struct:`String`
:access: Get only

Name of file or directory this path points to (same as the last segment).


.. attribute:: Path:HASEXTENSION

:type: :struct:`Boolean`
:access: Get only

True if the last segment of this path has an extension.

.. attribute:: Path:EXTENSION

:type: :struct:`String`
:access: Get only

Extension of the last segment of this path.

.. attribute:: Path:ROOT

:type: :struct:`Path`
:access: Get only

Returns a new path that points to the root directory of this path's volume.

.. attribute:: Path:PARENT

:type: :struct:`Path`
:access: Get only

Returns a new path that points to this path's parent. This method will throw an exception if this path does not have a parent (its length is 0).

.. method:: Path:CHANGENAME(name)

:parameter name: :struct:`String` new path name
:return: :struct:`Path`

Will return a new path with the value of the last segment of this path replaced (or added if there's none).

.. method:: Path:CHANGEEXTENSION(extension)

:parameter extension: :struct:`String` new path extension
:return: :struct:`Path`

Will return a new path with the extension of the last segment of this path replaced (or added if there's none).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the rationale for making the extension different from the rest of the name, in the sense that the extension can be changed with a suffix method but the rest of the name cannot?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't the way to change the rest of the path name be to do a chain of

path:parent:combine("newname.ks").

Or are you specifically looking for a way to generate a path with the same folder and extension but a different file name?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the Unix world (and therefore also Macs from OSX onward), the extension was never a separate special field. The filename IS just one string including the extension. (From the way the filesystem "thinks" in most UNIX systems, the humans who choose the filename strings seem to like using ones with exactly one period character in them a lot of the time for some mysterious reason the filesystem doesn't understand or care about.) The notion of an "extension" is a higher level logical abstraction consisting of that substring of the whole filename that comes after the lastmost period, if there is one. Things like GUI file managers, and system libraries, apply this abstraction on top. The filesystem itself has no such thing as "extensions".

For all I know the newer Windows filesystems may be the same way now too. The notion of a file "extension" came from DOS and FAT originally.

So this is why it looks very arbitrary and weird to me for there to be a rule that you can change the extension but not the rest of the name, via the PATH suffixes. The extension is nothing more than a subset of the filename. Either you should be able to change it via PATH, or you shouldn't be able to. Not this strange "it depends on which part of it you're trying to change".

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good point.

To be honest IIRC I took a couple of suffixes straight from C#'s Path class, this was one of them. I thought it could be useful to expose that functionality to the user. I see no problem with adding a similar CHANGENAME suffix.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From the description in the RST file, I didn't understand how the combine suffix works.

If it really is just "return a new path with a new filename in the same directory as this path", then it gives me what I'm looking for. I'm not looking for a suffix that ONLY changes the base substring of the name. I see that as just as arbitrary as one that only changes the extension substring of the name. I'm looking for how to change the whole name in one go, and treat it like it really is - as a single string - ignoring the "base dot extension" abstraction layer entirely and just changing the file's actual raw name, which includes the base, the period, and the extension in one string.

Copy link
Member

@Dunbaratu Dunbaratu May 6, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd be fine with CHANGENAME iff path:changename(newname) does exactly the same thing as path:parent:combine(newname) currently does, but not if it only works on the base name and not the extension. (The thing @hvacengi was guessing I might have meant up above is definitely not what I wanted.) We need to be fundamentally consistent in our terminology. When we say "file name" elsewhere, we do include the extension (i.e. path:name returns "prog.ks" rather than just "prog") :changename should be the same and the "name" it is changing should be the same "name" that :name is returning.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was my intention to make it behave like path:parent:combine(newname).

I assumed extension to be the part of the file name after last dot (if any). So file name includes extension.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd love it if we could get to a point where we always refer to files with the extension. run test. should run a file named "test" not "test.ks" (or "test.ksm", that inference is part of the problem).

Copy link
Member Author

@tomekpiotrowski tomekpiotrowski May 6, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the behavior that was introduced by one of my previous PRs is a good compromise between what you mention and being backwards compatible.

That is when you create a file you need to specify the full name, no extensions are added by kOS. However when you access a file kOS tries to add '.ks' and '.ksm' to the file name if it doesn't find the original one. I've kept the same behavior in this PR.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added PATH:CHANGENAME(name)

.. method:: Path:ISPARENT(path)

:parameter path: :struct:`Path` path to check
:return: :struct:`Boolean`

Returns true if `path` is the parent of this path.

.. method:: Path:COMBINE(name1, [name2, ...])

:parameter name: :struct:`String` segments to add
:return: :struct:`Path`

Will return a new path created by adding segments to this path.
Loading