JDP Science Tools

cancel
Showing results for 
Search instead for 
Did you mean: 

Config File Sync

I'm exploring the use of JSONText with the Config File class as a universal reusable tool for handling configuration files.

My configuration actor will receive JSON objects from other actors and compile them into one JSON object for saving to file.

But also I want to recognise when the user has manually edited the JSON file itself and load those changes.

I remember that the JSONText Config class uses Differencing to recognise file changes and merge (where no conflict), which is great.

However, I'm not sure I fully appreciate how to wrap the methods "Read Config from File.vi" and "Write Config Changes to File.vi" such that I can efficiently achieve what I want.

 

Let me explain -

"Read Config file File.vi" reads the file into memory and stores certain info in the Config File class. Perfect during app initialisation and loading the configuration in.

"Write Config Changes to File.vi" write any changes you've made to your JSON to the file. Great for when your user has edited some settings within your application and you wish to commit them to disk. Beautifully it also preserves any unexpected file changes, and provides the new merged JSON content back. Use JSONText Difference.vi and you can see the new unexpected JSON content and act on that (propagate it throughout your app).

BUT, there's a scenario that I can't get working - "Write Config Changes to File.vi" does nothing when you haven't edited your JSON content because it recognises that there are no changes to the JSON in memory, and therefore it doesn't load the file. If there are file content changes they don't get loaded.

You could say "just call the Read vi?" but then I might lose local changes, because this doesn't merge like Write does.

How can I check if my local JSON is unchanged? Well, I could just store a simple flag and set it to True whenever I adjust the content, but that seems dirty when there's built in functionality in JSONtext. 

Am I misunderstanding how to call Read and Write Config File to ensure I can sync my JSON in memory with any local file changes? I feel like I'm missing the simple Resync Config File method which will reload the file and ensure we capture any changes there, whilst also commiting local changes to disk.

Thoric (CLA, CLED, CTD and LabVIEW Champion)


0 Kudos
Message 1 of 3
(94 Views)

Hi Richard,

I am unfortunately on holiday this next week and I can't check the code right now, but I have not previously made an "update-on-the-fly" configuration system like you are trying to make.  Thus I may be lacking a method that you need, that detects changes to the config file that we can react to.  If you prompt me the week after next, I will have a look.

Question: are you able to make a child class of the config class to add this functionality yourself?  I am interested in allowing people to easily make their own extensions and may want to add a protected method or two that allows this.

Message 2 of 3
(77 Views)

Hi James,

 

I'm trying a child class now, but hit a wall.

 

I want to create a Sync With File function which will load file changes and commit local JSON changes irrespective of whether there are local changes or not. This creates a function that I can call periodically to check for file changes (recall that your Write Config to File will not recognise file content changes if there are no changes to the stored JSON in the class).

 

Unfortunately, it's not so simple. Your "Config File.lvclass" has private data and no accessors. You provided public VIs for reading the "File Path" and "Original Config", but nothing for writing them, and nothing at all for "Comment out unparsable lines". Therefore my child class cannot function like your class, as it cannot write to the Config File.lvclass private data. 

 

I think I'll need to instead duplicate your Config File class and recreate the "Read Config from File", "Write Config to File" functions for myself, then add my own "Sync with File" function to my class. Since there's nothing inheritable, I don't benefit from inheritance.

 

On a side note, I don't see "Comment out unparsable lines" being written anywhere at all, which might be a bug.

 

 

Thoric (CLA, CLED, CTD and LabVIEW Champion)


Message 3 of 3
(58 Views)