-
-
Notifications
You must be signed in to change notification settings - Fork 338
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
[Bug]: manage access to pyrevit-config.ini at startup #2463
Comments
ref:
|
The problem is that the library that we use to parse the configuration always tries to open the file in write mode
but there's an override of that method (Line 144) that accepts a stream, so we could open the stream in read only mode and One note, though: the library is abandoned, and doesn't support .net core, so we should replace it with something else... |
Thanks for the analysis. gpted answer
To address the issue in the
Would you like help drafting a pull request or implementing these changes in a development branch? If you want to manage INI files natively in .NET without relying on external libraries, you can build a simple INI parser. While .NET doesn't have a built-in INI parser, it's straightforward to create one using the Here’s how you can do it: Step 1: Native INI Parsing Implementationusing System;
using System.Collections.Generic;
using System.IO;
public class IniParser
{
private readonly Dictionary<string, Dictionary<string, string>> _data = new();
public IniParser(string filePath)
{
Parse(File.OpenRead(filePath));
}
public IniParser(Stream iniStream)
{
Parse(iniStream);
}
private void Parse(Stream iniStream)
{
using var reader = new StreamReader(iniStream);
string? currentSection = null;
while (!reader.EndOfStream)
{
var line = reader.ReadLine()?.Trim();
// Skip empty lines and comments
if (string.IsNullOrWhiteSpace(line) || line.StartsWith(";") || line.StartsWith("#"))
continue;
// Detect section headers
if (line.StartsWith("[") && line.EndsWith("]"))
{
currentSection = line[1..^1].Trim();
if (!_data.ContainsKey(currentSection))
{
_data[currentSection] = new Dictionary<string, string>();
}
}
else if (currentSection != null)
{
// Parse key-value pairs
var keyValue = line.Split('=', 2);
if (keyValue.Length == 2)
{
var key = keyValue[0].Trim();
var value = keyValue[1].Trim();
_data[currentSection][key] = value;
}
}
}
}
public string? GetValue(string section, string key)
{
return _data.ContainsKey(section) && _data[section].ContainsKey(key)
? _data[section][key]
: null;
}
public Dictionary<string, string>? GetSection(string section)
{
return _data.ContainsKey(section) ? _data[section] : null;
}
public override string ToString()
{
var result = string.Empty;
foreach (var section in _data)
{
result += $"[{section.Key}]\n";
foreach (var kvp in section.Value)
{
result += $"{kvp.Key} = {kvp.Value}\n";
}
}
return result;
}
} Step 2: Example Usagepublic class Program
{
public static void Main()
{
var filePath = "config.ini";
// Read INI file
var iniParser = new IniParser(filePath);
// Access specific values
var value = iniParser.GetValue("Section1", "Key1");
Console.WriteLine($"Value: {value}");
// Get an entire section
var section = iniParser.GetSection("Section2");
if (section != null)
{
foreach (var kvp in section)
{
Console.WriteLine($"{kvp.Key} = {kvp.Value}");
}
}
}
} Explanation of Key Features
Benefits
Enhancements (Optional)
This approach ensures compatibility with .NET Core while avoiding reliance on third-party libraries. Let me know if you'd like to adapt this further! |
Yes there is. And it's the first answer that chatgpt gave you. It is the same thing that the As per the long term fix, I simply didn't have the time to check for alternative libraries yet. We can implement it ourselves, yes, but we have to make sure that the API are the same to avoid rewriting the code that accesses it (I didn't analyze the code yet, it might as well be that only the PyRevitConfig class uses it so we could just modify it) |
I think we can go ahead with the short term solution since @dosymep already took care of finding the .net core version of the library; |
@jmcouffin @sanzoghenzo |
✈ Pre-Flight checks
🐞 Describe the bug
https://discourse.pyrevitlabs.io/t/failed-to-save-config-in-use-by-another-program/8187/3
⌨ Error/Debug Message
'used by another process'
♻️ To Reproduce
Launch multiple sessions of Revit at once (within 10/30sec)
⏲️ Expected behavior
🖥️ Hardware and Software Setup (please complete the following information)
Additional context
old issue, never fixed
The text was updated successfully, but these errors were encountered: