FreeCAD: Workbench Refactor

This commit is contained in:
Mark Voltov 2024-04-14 18:54:47 +00:00 committed by Igor Brylyov
parent 037827669a
commit a58dcdafb1
386 changed files with 997 additions and 64533 deletions

12
utils/README.md Normal file
View file

@ -0,0 +1,12 @@
## cg.utils
Общие утилиты для всех модулей фреймворка.
### cmd_proc.py
Позволяет запустить программу командной строки с параметрами и вернуть ее результат.
### custom_parser.py
Надстройка для ArgumentParser.
Позволяет использовать передачу аргументов командной строки для Bledner и FreeCAD.

32
utils/cmd_proc.py Normal file
View file

@ -0,0 +1,32 @@
# coding: utf-8
# Copyright (C) 2023 Ilia Kurochkin <brothermechanic@yandex.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
'''
DESCRIPTION.
Run cmd program with args and kwargs.
Get return via cmd output.
'''
__version__ = '0.1'
import subprocess
def cmd_proc(*args, **kwargs):
command = list(args)
for akey, aval in kwargs.items():
command.append(f'--{akey}')
command.append(str(aval))
return subprocess.run(command,
check=True,
stdout=subprocess.PIPE,
encoding='utf-8').stdout

73
utils/custom_parser.py Normal file
View file

@ -0,0 +1,73 @@
# coding: utf-8
# original code https://blender.stackexchange.com/a/134596
# Copyright (C) 2023 Ilia Kurochkin <brothermechanic@yandex.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
'''
DESCRIPTION.
Custom parser for Blender and FreeCAD.
This is an overlay for standard ArgumentParser.
'''
__version__ = '0.1'
import argparse
import sys
class CustomArgumentParser(argparse.ArgumentParser):
"""
This class is identical to its superclass, except for the parse_args
method (see docstring). It resolves the ambiguity generated when calling
Blender from the CLI with a python script, and both Blender and the script
have arguments. E.g., the following call will make Blender crash because
it will try to process the script's -a and -b flags:
>>> blender --python my_script.py -a 1 -b 2
To bypass this issue this class uses the fact that Blender will ignore all
arguments given after a double-dash ('--'). The approach is that all
arguments before '--' go to Blender, arguments after go to the script.
The following calls work fine:
>>> blender --python my_script.py -- -a 1 -b 2
>>> blender --python my_script.py --
"""
@staticmethod
def _get_argv_after_doubledash():
"""
Given the sys.argv as a list of strings, this method returns the
sublist right after the '--' element (if present, otherwise returns
an empty list).
"""
try:
idx = sys.argv.index("--")
return sys.argv[idx+1:] # the list after '--'
except ValueError as e: # '--' not in the list:
return None
# overrides superclass
def parse_args(self, args=None, namespace=None):
"""
This method is expected to behave identically as in the superclass,
except that the sys.argv list will be pre-processed using
_get_argv_after_doubledash before. See the docstring of the class for
usage examples and details.
"""
return super().parse_args(
args=args or self._get_argv_after_doubledash(),
namespace=namespace
)
def parse_known_args(self, args=None, namespace=None):
''' Parse only known args '''
return super().parse_known_args(
args=args or self._get_argv_after_doubledash(),
namespace=namespace
)