Cudatext: Opening a .cuda-session in Windows vs Linux

Created on 4 Jun 2020  路  19Comments  路  Source: Alexey-T/CudaText

Should be a quick and easy thing: opening a .cuda-session file on Windows opens the files in the session, molto bene.
Opening a .cuda-session file on Linux opens the files in the session PLUS the .cuda-session file itself (not what I want).

plugins

All 19 comments

@kvichans seems like a issue in Session Manager? Can you test on Linux?

cannot confirm. Ubuntu 20, command is "cudatext tst.cuda-session", it opens 2 files from sess and that's it.

So I double-checked this and yep, it works here as well... BUT ONLY if I use an empty settings directory and an empty user.json (other than the "pylib__linux" line of course). So it must be something gone haywire in my configuration. I will come back when I have found who or what is doing that... tomorrow is another day.

@Alexey-T @kvichans Here we go... this has got nothing to do with Windows vs Linux. The problem is in cuda_sess_manager.json. A version working correctly looks like this:

{
  "recent": [
    "D:\\Apps\\CudaText\\sessions\\defset1.cuda-session",
    "D:\\Apps\\CudaText\\sessions\\defset2.cuda-session"
  ]
}

If I add a single comma after the "defset2...." line Cud shows the behaviour I described. In general, if the last line ends in a comma, this happens and somehow my Linux cuda_sess_manager.json had acquired a final comma.

Stupidly (if I may say so), the JSON specs do not allow trailing commas, although some parsers are more lenient than others. Not sure what (if anything) to do.

@Alexey-T @kvichans I have looked further into this... apparently this JSON trailing-comma "feature" is bugging a lot of people. The issue is not so much with files which are 100% automatically written and read. It's more of a problem for JSON files which are user editable. Then it's very easy to create an invalid JSON file.
Some recommend using JSON Comment (https://pypi.org/project/jsoncomment/) as a wrapper, others think switching to JSON5 (which is JSON-compatible) is the way to go. I am no great expert for JSON so I just throw this into the air.

@kvichans please consider to use 鈥渏son wrapper" (2 are mentioned).
so user editable json configs will be read ok.

I'm seeing...

@Alexey-T @kvichans Some further thoughts on this... Cud itself (ie the non-plugin code) does read all trailing-comma JSON files I've thrown at it. This may not be 100% specs-compatible but it's certainly a lot more user-friendly as it's really easy to either forget to remove a comma if it's not needed or to include one if it's needed (the JSON spec is really broken in this regard).
By contrast, the moment Python code comes into the picture things get more murky. In the case of a trailing comma the json module just returns an error code (both Windows and Linux) and that's it.
However, there is a difference between the Windows and Linux versions when it comes to loading a .cuda-session. Even with a trailing comma in the cuda_sess_manager.json file this definitely loads just the files under Windows. However the Linux version loads the files and the .cuda-session file.

@Alexey-T @kvichans Final comment for this issue, I hope :-)
To get rid of the additional .cuda-session file that is opened if a session is loaded under Linux and cuda_sess_manager.json has a trailing comma, replace line 209 in cd_sess_manager.py:
sess = json.loads(open(SESS_JSON).read()) if os.path.exists(SESS_JSON) else self.dfltSess
with (apx is "import cudax_lib as apx")
sess = apx._json_loads(open(SESS_JSON).read()) if os.path.exists(SESS_JSON) else self.dfltSess

Additionally, perhaps it wouldn't be a bad idea to rename cudax_lib._json_loads() to cudax_lib.json_loads() or to include a json_loads() wrapper that calls _json_loads().

@kvichans we need an update.

Well, I wouldn't say we NEED an update... it's just a suggestion. I have rewritten the session code anyway.

But there's another general observation... perhaps all JSON files read by Cud/plugins should go through the filter that is implemented by cudax_lib._json_loads(). Would be an easy and transparent solution to such problems.

I agree - std json format is too hard: no "hanging commas", no "comment", "obligatory quotes" for keys and str-values. I now good alternative format - "hjson". But we need to use format that is common for plugins and app core.

About session plugin. I never thought that somebody will edit its data file. OK. Plugin will be more gentle.

.hjson? not a good idea, not popular fmt. Xml or json. Atom uses CSON (cofee script json).

I never thought that somebody will edit its data file. OK. Plugin will be more gentle.

Well... give people a good text editor and they will happily edit every text file in sight :-) I've learned the hard way that it's just not safe to assume that text files will not be edited, even if they carry a FAT warning...

Seriously though, there are many reasons, some admittedly more valid than others, to edit all sorts of files. I also regularly patched binary files but with the multi-MB files of today that's getting rarer.

there are many reasons, some admittedly more valid than others, to edit all sorts of files

eg replacement of old dir-name to a new one.

.hjson? not a good idea, not popular fmt. Xml or json. Atom uses CSON (cofee script json).

I didn't want to propose hjson as format for any Cud files - it is example a max comfortable fmt.

@kvichans Tmsg suggested to use cudax_lib._json_load in Sessions, can you do it?

Yes. Soon

Fixed and published

Was this page helpful?
0 / 5 - 0 ratings

Related issues

junguler picture junguler  路  5Comments

JairoMartinezA picture JairoMartinezA  路  7Comments

Alexey-T picture Alexey-T  路  5Comments

JairoMartinezA picture JairoMartinezA  路  5Comments

Alexey-T picture Alexey-T  路  4Comments