Пример #1
0
    def save(self):
        """
            Writes the user-settings in ASCII yet Pythonic style to the user's
            configfile, so that it can be edited manually and read-in using
            FastScript.execFile().

            If there are no user-settings at all the fill be removed
            (if present).
        """
        if self._userSettings:

            from ToolBOSCore.Packages.CopyrightHeader import getCopyrightHeader

            content = getCopyrightHeader('python', 'User preferences')

            for key, value in sorted(self._userSettings.items()):
                if Any.isText(value):
                    value = "'%s'" % value  # write Python string, not just value

                content += '%s = %s\n\n' % (key, str(value))

            content += '\n# EOF\n'

            logging.debug('writing %s', self._userFile)
            FastScript.setFileContent(self._userFile, content)

        else:
            # delete entire file if there are no settings left

            logging.debug('deleting empty configfile')
            FastScript.remove(self._userFile)
def _setUserConfigOptions(config):
    """
        Writes the dict 'config' in ASCII yet Pythonic style to the user's
        configfile, so that it can be edited manually and read-in using
        FastScript.execFile().
    """
    from ToolBOSCore.Packages.CopyrightHeader import getCopyrightHeader

    Any.requireIsDict(config)

    content = getCopyrightHeader('python', 'User-preferences for ToolBOS SDK')

    for key, value in config.items():
        if Any.isText(value):
            value = "'%s'" % value  # write Python string, not just value

        content += '%s = %s\n\n' % (key, str(value))

    content += '\n# EOF\n'

    fileName = getSettingsFile_user()
    dirName = os.path.dirname(fileName)

    FastScript.mkdir(dirName)
    FastScript.setFileContent(fileName, content)
Пример #3
0
def tryImport(modules):
    """
        Checks that the provided modules can be imported.
        Stops with a message to the user if some are not found.
    """
    import importlib
    import six

    if Any.isText(modules):
        modules = [modules]

    Any.requireIsIterable(modules)

    notFound = set()

    for moduleName in modules:
        Any.requireIsTextNonEmpty(moduleName)

        if six.PY2:
            errorClass = ImportError
        else:
            errorClass = ModuleNotFoundError

        try:
            importlib.import_module(moduleName)
        except errorClass:
            notFound.add(moduleName)

    if notFound:
        raise SystemExit( 'Python module(s) not installed: %s' % \
                          ' '.join( sorted( notFound ) ) )
Пример #4
0
def getTreeView(nestedList, level=0, levelPadding=''):
    """
        Returns a string representation of the given (optionally nested)
        list.

        The parameters 'level' and 'levelPadding' are only internally used
        for recursion and should not be set by the user.
    """
    Any.requireIsList(nestedList)
    result = ''

    if level > 20:  # loop guard
        raise ValueError

    for i, entry in enumerate(nestedList):
        if Any.isText(entry):
            entryPadding = levelPadding + '%c---' % _getTreeChar(nestedList, i)
            result += "%s%s\n" % (entryPadding, entry)
        elif Any.isList(entry):
            entryPadding = levelPadding + '%c   ' % _getTreeChar(nestedList, i)
            result += getTreeView(entry, level + 1, entryPadding)
        else:
            raise TypeError

    return result
Пример #5
0
def setEnv(varNameOrMap, varValue=''):
    """
        This function can be used to set the process environment variables.

        a) If 'varNameOrMap' is of type 'dict' (a whole map of environment
           variables mapped to their values), it will set the environment
           to this. The second parameter is ignored in such case.
               Please note that all environment variables will be modified,
           even those not present in the map (those effectively get deleted).

        b) If 'varNameOrMap' is of type "str" it will set an environment
           variable with this name. Optionally you can specify 'varValue' of
           type 'str' to assign a value to this environment variable.
    """
    if Any.isText(varNameOrMap):  # set single variable
        logging.debug('export %s="%s"', varNameOrMap, varValue)
        os.environ[varNameOrMap] = varValue
    else:  # set whole map
        os.environ.clear()
        os.environ.update(varNameOrMap)