Now we use KiCad to detect where is its configuration.

No more KiCad copycat code.
This commit is contained in:
Salvador E. Tropea 2021-01-06 13:49:24 -03:00
parent ab9071b693
commit b3e2d67871
3 changed files with 13 additions and 59 deletions

View File

@ -23,7 +23,7 @@ import platform
import sysconfig
from ..gs import GS
from .. import log
from ..misc import W_NOHOME, W_NOUSER, W_BADSYS, W_NOCONFIG, W_NOKIENV, W_NOLIBS, W_NODEFSYMLIB
from ..misc import W_NOCONFIG, W_NOKIENV, W_NOLIBS, W_NODEFSYMLIB
# Check python version to determine which version of ConfirParser to import
if sys.version_info.major >= 3:
@ -123,54 +123,11 @@ class KiConf(object):
def find_kicad_common():
""" Looks for kicad_common config file.
Returns its name or None. """
# User option has the higher priority
user_set = os.environ.get('KICAD_CONFIG_HOME')
if user_set:
cfg = os.path.join(user_set, KICAD_COMMON)
cfg = ''
if GS.kicad_conf_path:
cfg = os.path.join(GS.kicad_conf_path, KICAD_COMMON)
if os.path.isfile(cfg):
return cfg
# XDG option is second
xdg_set = os.environ.get('XDG_CONFIG_HOME')
if xdg_set:
cfg = os.path.join(xdg_set, 'kicad', KICAD_COMMON)
if os.path.isfile(cfg):
return cfg
# Others depends on the OS
system = platform.system()
if system == 'Linux':
# Linux: ~/.config/kicad/
home = os.environ.get('HOME')
if not home:
logger.warning(W_NOHOME + 'Environment variable `HOME` not defined, using `/`')
home = '/'
cfg = os.path.join(home, '.config', 'kicad', KICAD_COMMON)
if os.path.isfile(cfg):
return cfg
elif system == 'Darwin': # pragma: no cover
# MacOSX: ~/Library/Preferences/kicad/
home = os.environ.get('HOME')
if not home:
logger.warning(W_NOHOME + 'Environment variable `HOME` not defined, using `/`')
home = '/'
cfg = os.path.join(home, 'Library', 'Preferences', 'kicad', KICAD_COMMON)
if os.path.isfile(cfg):
return cfg
elif system == 'Windows': # pragma: no cover
# Windows: C:\Users\username\AppData\Roaming\kicad
# or C:\Documents and Settings\username\Application Data\kicad
username = os.environ.get('username')
if not username:
logger.warning(W_NOUSER + 'Unable to determine current user')
return None
cfg = os.path.join('C:', 'Users', username, 'AppData', 'Roaming', 'kicad', KICAD_COMMON)
if os.path.isfile(cfg):
return cfg
cfg = os.path.join('C:', 'Documents and Settings', username, 'Application Data', 'kicad', KICAD_COMMON)
if os.path.isfile(cfg):
return cfg
else: # pragma: no cover
logger.warning(W_BADSYS + 'Unsupported system `{}`'.format(system))
return None
logger.warning(W_NOCONFIG + 'Unable to find KiCad configuration file ({})'.format(cfg))
return None

View File

@ -15,7 +15,7 @@ PdfBrowserName=
UseSystemBrowser=1
[EnvironmentVariables]
KICAD_SYMBOL_DIR=/usr/share/kicad/library
KICAD_TEMPLATE_DIR=/usr/share/kicad/template
KICAD_TEMPLATE_DIR=/usr/share/kicad/template_test
KICAD_USER_TEMPLATE_DIR=/home/salvador/kicad/template
KIGITHUB=https://github.com/KiCad
KISYS3DMOD=/usr/share/kicad/modules/packages3d/

View File

@ -69,9 +69,11 @@ def kiconf_de_init():
KiConf.lib_aliases = {}
def check_load_conf(caplog, dir='kicad', fail=False, catch_conf_error=False):
def check_load_conf(caplog, dir='kicad', fail=False, catch_conf_error=False, no_conf_path=False):
caplog.set_level(logging.DEBUG)
kiconf_de_init()
import pcbnew
GS.kicad_conf_path = None if no_conf_path else pcbnew.GetKicadConfigPath()
cov.load()
cov.start()
if catch_conf_error:
@ -97,7 +99,7 @@ def test_kicad_conf_user(caplog, monkeypatch):
with monkeypatch.context() as m:
m.setenv("KICAD_CONFIG_HOME", 'tests/data/kicad_ok')
check_load_conf(caplog, dir='kicad_ok')
assert 'KICAD_TEMPLATE_DIR="/usr/share/kicad/template"' in caplog.text, caplog.text
assert 'KICAD_TEMPLATE_DIR="/usr/share/kicad/template_test"' in caplog.text, caplog.text
def test_kicad_conf_xdg(caplog, monkeypatch):
@ -108,23 +110,19 @@ def test_kicad_conf_xdg(caplog, monkeypatch):
assert 'KiCad config without EnvironmentVariables section' in caplog.text, caplog.text
def test_kicad_conf_miss_home(caplog, monkeypatch):
def test_kicad_conf_guess_libs(caplog, monkeypatch):
""" Check no HOME and fail to load kicad_common.
Also check we correctly guess the libs dir. """
with monkeypatch.context() as m:
m.setenv("HOME", '')
check_load_conf(caplog, fail=True)
assert '`HOME` not defined' in caplog.text, caplog.text
check_load_conf(caplog, fail=True, no_conf_path=True)
assert 'Detected KICAD_SYMBOL_DIR="/usr/share/kicad/library"' in caplog.text, caplog.text
def test_kicad_conf_lib_env(caplog, monkeypatch):
""" Check we can use KICAD_SYMBOL_DIR as fallback """
with monkeypatch.context() as m:
m.setenv("HOME", '')
m.setenv("KICAD_SYMBOL_DIR", 'tests')
check_load_conf(caplog, fail=True)
assert '`HOME` not defined' in caplog.text, caplog.text
check_load_conf(caplog, fail=True, no_conf_path=True)
assert 'Detected KICAD_SYMBOL_DIR="tests"' in caplog.text, caplog.text
@ -178,6 +176,5 @@ def test_kicad_conf_no_conf(caplog, monkeypatch):
""" Test a complete fail to find libs """
with monkeypatch.context() as m:
m.setattr("sysconfig.get_path", lambda a, b: '')
m.setenv('HOME', '')
check_load_conf(caplog, fail=True)
check_load_conf(caplog, fail=True, no_conf_path=True)
assert 'Unable to find KiCad libraries' in caplog.text, caplog.text