Commit 26c3f310 authored by Michael Büsch's avatar Michael Büsch

Move environment variable handling to common.env

Signed-off-by: Michael Büsch's avatarMichael Buesch <m@bues.ch>
parent 65f39cd8
# -*- coding: utf-8 -*-
#
# AWL simulator - Environment variables
#
# Copyright 2017 Michael Buesch <m@bues.ch>
#
# 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 2 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.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
from __future__ import division, absolute_import, print_function, unicode_literals
from awlsim.common.compat import *
from awlsim.common.util import *
from awlsim.common.exceptions import *
import os
__all__ = [
"AwlSimEnv",
]
class AwlSimEnv(object):
"""Central environment variable handler.
"""
@classmethod
def getEnv(cls):
"""Get a copy of the environment dict.
"""
return dict(os.environ)
@classmethod
def clearLang(cls, env, lang="C"):
"""Reset the language settings of an environment dict
to some expected value and return the result.
"""
env = dict(env)
env["LANG"] = lang
for i in {"LANGUAGE", "LC_CTYPE", "LC_NUMERIC",
"LC_TIME", "LC_COLLATE", "LC_MONETARY",
"LC_MESSAGES", "LC_PAPER", "LC_NAME",
"LC_ADDRESS", "LC_TELEPHONE", "LC_MEASUREMENT",
"LC_IDENTIFICATION",}:
env.pop(i, None)
return env
@classmethod
def __getVar(cls, name, default=None):
return cls.getEnv().get("AWLSIM_" + name, default)
@classmethod
def getProfileLevel(cls):
"""Get AWLSIM_PROFILE.
"""
profileLevel = cls.__getVar("PROFILE", "0")
try:
profileLevel = int(profileLevel)
except ValueError as e:
profileLevel = 0
return clamp(profileLevel, 0, 2)
......@@ -54,7 +54,6 @@ __all__ = [
"bytesToHexStr",
"toUnixEol",
"toDosEol",
"envClearLang",
"isInteger",
"isString",
"strEqual",
......@@ -274,20 +273,6 @@ def toDosEol(string):
"""
return toUnixEol(string).replace("\n", "\r\n")
def envClearLang(env, lang = "C"):
"""Reset the language settings of an environment dict
to some expected value and return the result.
"""
env = dict(env)
env["LANG"] = lang
for i in {"LANGUAGE", "LC_CTYPE", "LC_NUMERIC",
"LC_TIME", "LC_COLLATE", "LC_MONETARY",
"LC_MESSAGES", "LC_PAPER", "LC_NAME",
"LC_ADDRESS", "LC_TELEPHONE", "LC_MEASUREMENT",
"LC_IDENTIFICATION",}:
env.pop(i, None)
return env
def __isInteger_python2(value):
return isinstance(value, int) or\
isinstance(value, long)
......
......@@ -23,6 +23,7 @@ from __future__ import division, absolute_import, print_function, unicode_litera
from awlsim.common.compat import *
from awlsim.common.project import *
from awlsim.common.env import *
from awlsim.common.exceptions import *
from awlsim.core.util import *
......@@ -31,7 +32,6 @@ from awlsim.core.hardware import * #+cimport
from awlsim.core.hardware_loader import *
import sys
import os
__all__ = [
......@@ -81,11 +81,7 @@ class AwlSim(object): #+cdef
self.cpu.setPeripheralReadCallback(self.__peripheralReadCallback)
self.cpu.setPeripheralWriteCallback(self.__peripheralWriteCallback)
try:
profileLevel = int(dict(os.environ).get("AWLSIM_PROFILE", "0"))
except ValueError:
profileLevel = 0
self.__setProfiler(profileLevel)
self.__setProfiler(AwlSimEnv.getProfileLevel())
def getCPU(self):
return self.cpu
......
......@@ -2,7 +2,7 @@
#
# AWL simulator - SSH tunnel helper
#
# Copyright 2016 Michael Buesch <m@bues.ch>
# Copyright 2016-2017 Michael Buesch <m@bues.ch>
#
# 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
......@@ -24,6 +24,7 @@ from awlsim.common.compat import *
from awlsim.common.exceptions import *
from awlsim.common.net import *
from awlsim.common.env import *
from awlsim.common.util import *
from awlsim.common.subprocess_wrapper import *
......@@ -86,7 +87,7 @@ class SSHTunnel(object):
self.__sshPid = None
try:
# Prepare SSH environment and arguments.
env = envClearLang(os.environ)
env = AwlSimEnv.clearLang(AwlSimEnv.getEnv())
if osIsWindows and "plink" in self.sshExecutable.lower():
# Run plink.exe (PuTTY)
pw = self.getPassphrase("%s's Password:" % self.remoteHost)
......
......@@ -27,6 +27,7 @@ from awlsim.common.cpuspecs import * #+cimport
from awlsim.common.cpuconfig import *
from awlsim.common.sources import *
from awlsim.common.net import *
from awlsim.common.env import *
from awlsim.common.util import *
from awlsim.common.exceptions import *
......@@ -170,7 +171,7 @@ class AwlSimServer(object): #+cdef
# Prepare the environment for the server process.
# Inherit from the starter and add awlsim specific variables.
env = dict(os.environ)
env = AwlSimEnv.getEnv()
env[AwlSimServer.ENV_MAGIC] = AwlSimServer.ENV_MAGIC
env["AWLSIM_CORESERVER_HOST"] = str(listenHost)
env["AWLSIM_CORESERVER_PORT"] = str(int(listenPort))
......@@ -275,10 +276,10 @@ class AwlSimServer(object): #+cdef
def runFromEnvironment(self, env=None):
"""Run the server.
Configuration is passed via environment variables in 'env'.
If 'env' is not passed, os.environ is used."""
If 'env' is not passed, the current environment is used."""
if not env:
env = dict(os.environ)
env = AwlSimEnv.getEnv()
try:
loglevel = int(env.get("AWLSIM_CORESERVER_LOGLEVEL"))
......
......@@ -146,7 +146,7 @@ class MainWidget(QWidget):
if filename:
argv.append(filename)
try:
PopenWrapper(argv, env = os.environ)
PopenWrapper(argv, env=AwlSimEnv.getEnv())
except OSError as e:
QMessageBox.critical(self,
"Failed to execute '%s'" % executableName,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment