sparkmicro / Ki-nTree

Fast part creation for KiCad and InvenTree
GNU General Public License v3.0
171 stars 31 forks source link

parameters.yaml overwritten during startup #192

Closed Fivefold closed 5 months ago

Fivefold commented 7 months ago

I'm trying a fresh inventree + Ki-nTree setup and have the following problem:

When running python3 -m kintree.setup_inventree I get the following "Invalid physical unit" error during parameter creation:

Traceback (most recent call last):
  File "C:\Users\******\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 196, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "C:\Users\******\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "C:\Users\******\AppData\Local\Programs\Python\Python310\lib\site-packages\kintree\setup_inventree.py", line 54, in <module>
    setup_inventree()
  File "C:\Users\******\AppData\Local\Programs\Python\Python310\lib\site-packages\kintree\setup_inventree.py", line 46, in setup_inventree
    pk = inventree_api.create_parameter_template(name, unit)
  File "C:\Users\******\AppData\Local\Programs\Python\Python310\lib\site-packages\kintree\database\inventree_api.py", line 688, in create_parameter_template
    parameter_template = ParameterTemplate.create(inventree_api, {
  File "C:\Users\******\AppData\Local\Programs\Python\Python310\lib\site-packages\inventree\base.py", line 166, in create
    response = api.post(cls.URL, data, **kwargs)
  File "C:\Users\******\AppData\Local\Programs\Python\Python310\lib\site-packages\inventree\api.py", line 420, in post
    response = self.request(
  File "C:\Users\******\AppData\Local\Programs\Python\Python310\lib\site-packages\inventree\api.py", line 372, in request
    raise requests.exceptions.HTTPError(detail)
requests.exceptions.HTTPError: {'detail': 'Error occurred during API request', 'url': 'https://********/api/part/parameter/template/', 'method': 'POST', 'status_code': 400, 'body': '{"units":["Invalid physical unit"]}', 'headers': {'AUTHORIZATION': 'Token *********************************'}, 'params': {'format': 'json'}, 'data': {'name': 'DC Resistance', 'units': 'mΩ'}}

Note how it says 'units': 'mΩ' in the error message.

The offending line is https://github.com/sparkmicro/Ki-nTree/blob/7f894dd5f1d89bce128e022bcd39fa7fb603d890/kintree/config/inventree/parameters.yaml#L10 which gets changed to

DC Resistance: "m\xCE\xA9"

immediately upon running python3 -m kintree.setup_inventree (or simply running kintree) and before the parameter creation.

Also the two comment lines at the start get removed: https://github.com/sparkmicro/Ki-nTree/blob/7f894dd5f1d89bce128e022bcd39fa7fb603d890/kintree/config/inventree/parameters.yaml#L1-L2

It seems the file actually gets overwritten, because if I delete the offending lines and start kintree, I get them back (in the described state as above).

Things I've tried

I had an old version of kintree from over a year ago installed so I thought it might be related to old config files. But removing the config folder and reinstalling kintree via pip to start from fresh did not help.

I also tried manually overwriting the parameters.yaml directly after startup without success. Seems it's already loaded into memory at that point.

Making parameters.yaml write-protected leads to a Permission denied Error upon starting Ki-nTree. Maybe the error is helpful, since it shows where the file is accessed in the code:

Error message ``` Traceback (most recent call last): File "C:\Users\******\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 196, in _run_module_as_main return _run_code(code, main_globals, None, File "C:\Users\******\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 86, in _run_code exec(code, run_globals) File "C:\Users\******\AppData\Local\Programs\Python\Python310\lib\site-packages\kintree\setup_inventree.py", line 3, in from .config import settings File "C:\Users\******\AppData\Local\Programs\Python\Python310\lib\site-packages\kintree\config\settings.py", line 74, in if not load_user_config(): File "C:\Users\******\AppData\Local\Programs\Python\Python310\lib\site-packages\kintree\config\settings.py", line 67, in load_user_config return config_interface.load_user_config_files(path_to_root=CONFIG_ROOT, File "C:\Users\******\AppData\Local\Programs\Python\Python310\lib\site-packages\kintree\config\config_interface.py", line 78, in load_user_config_files load_config(config_files) File "C:\Users\******\AppData\Local\Programs\Python\Python310\lib\site-packages\kintree\config\config_interface.py", line 72, in load_config dump_file(user_settings, os.path.join(path_to_user_files, filename)) File "C:\Users\******\AppData\Local\Programs\Python\Python310\lib\site-packages\kintree\config\config_interface.py", line 29, in dump_file with open(file_path, 'w') as file: PermissionError: [Errno 13] Permission denied: 'C:\\Users\\******\\kintree\\user\\parameters.yaml' ```

Version information:

Windows 10 Pro 22H2 Inventree 0.12.10 Ki-nTree 1.0.4

eeintech commented 7 months ago

Hello @Fivefold

Yes this is an intended behavior. I'm not sure about special characters like the Ω symbol, I have never tested it on InvenTree side. Does it work there?

Fivefold commented 7 months ago

I can manually create the parameters in Inventree (e.g. using for the unit). If I use the variant with hex codes (m\xCE\xA9 or "m\xCE\xA9") I get the same error as in the API.

image image


Also, a question: if this is intended behaviour, the parameters.yaml is not user-changeable, right? I.e. I can't use it to create a bunch of custom parameters in inventree (via Ki-nTree). Because any changes I do to parameters.yaml get removed once I run Ki-nTree or it's setup command/module

eeintech commented 7 months ago

I'm not sure if the YAML file can store the Ω symbol? 🤔 It should be fine for you to set parameters in the parameters.yaml file, they shouldn't get overwritten. If they are then there is definitely a bug.

Fivefold commented 7 months ago

That's the behaviour I'm experiencing. parameters.yaml always gets changed to the same faulty state upon start of Ki-nTree, as described above. I can make a video if needed.

I worked around this by creating the parameters by hand in InvenTree, so I personally don't urgently need a fix.

lobra07 commented 5 months ago

i have the same problem to make it work i deleted the special chars and manually added them to inventree using the admin page