<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0"><channel><title>Ticket search results</title><link>https://forge.codesys.com/tol/scripting/snippets/</link><description>You searched for assigned_to:"ingo"</description><language>en</language><lastBuildDate>Fri, 30 Jun 2023 09:59:53 -0000</lastBuildDate><item><title>update project information</title><link>https://forge.codesys.com/tol/scripting/snippets/21/</link><description>~~~
proj = projects.load("D:\Some.library")

project_info = proj.get_project_info()

# Set some values
project_info.company = "Test Library Ltd"
project_info.title = "Script Test Project"
project_info.version = (0, 8, 15, 4711)
project_info.default_namespace = "testlibrary"
project_info.author = "Python von Scriptinger"

# some values recommended in the library toolchain
project_info.values["DefaultNamespace"] = "testlibrary"
project_info.values["Placeholder"] = "testlibrary"
project_info.values["DocFormat"] = "reStructuredText"

# now we set a custom / vendor specific value.
project_info.values["SpecialDeviceId"] = "PLC0815_4711"

# Enable generation of Accessor functions, so the IEC
# application can display the version in an info screen.
project_info.change_accessor_generation(True)

# And set the library to released
project_info.released = False;

proj.save()
~~~
</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">aliazzz</dc:creator><pubDate>Fri, 30 Jun 2023 09:59:53 -0000</pubDate><guid isPermaLink="false">https://forge.codesys.com/tol/scripting/snippets/21/</guid></item><item><title>Install missing libraries</title><link>https://forge.codesys.com/tol/scripting/snippets/19/</link><description>Thanks @jelle for the ideas I got from snippet [#18]. But as the snippet from @jelle was not complete, yet, a lot of transfer knowledge was necessary to use it. So after I implemented it by myself, I thought, that I should add my version here also.

It acts on the primary project. So you should open the project before calling this function.
And it uses WebClient from DotNet, because the "request" module, which was referenced in [#18] was not available.

~~~
import os, re
from System.Net import WebClient


#
# Install a library from a URL (starting with http(s))
#
def install_library(url, librarymanager):
    if not url.startswith("http"):
        return
    
    downloaddir = os.path.join(os.environ["USERPROFILE"], "downloads")
    if not os.path.exists(downloaddir):
        os.makedirs(downloaddir)
    basename, extension = os.path.splitext(url)
    localname = os.path.join(downloaddir, "dl" + extension)
    print("*** download %s to %s\n" % (url, localname))

    web_client = WebClient()
    web_client.DownloadFile(url, localname)
    repo = librarymanager.repositories[0]
    print("*** installing %s\n" % url)
    librarymanager.install_library (localname, repo, True)

#
# search for missing libraries in current primary project
#
def install_missing_libraries(librarymanager):
    proj = projects.primary
    # compile a regular expression to match lib placeholders
    p = re.compile('[^,]+, [^(]+\([^)]+\)')
    
    # search for libman
    objects = proj.get_children(recursive=True)
    for object in objects:
        if object.is_libman:
            for libref in iter(object):
                if libref.is_placeholder:
                    if libref.effective_resolution is None and libref.default_resolution is not None:
                        libInfo = str(libref.default_resolution)
                        m = re.findall('([^,]+), ([^(]+) \(([^)]+)\)', libInfo)
                        if len(m) &gt; 0:
                            libname, libversion, libvendor = m[0]
                            indexurl = "https://store.codesys.com/CODESYSLibs/%s/%s/%s/index" % (libvendor, libname, libversion)
                            web_client = WebClient()
                            try:
                                filename = web_client.DownloadString(indexurl).rstrip()
                            except:
                                filename = None

                            if filename != None:
                                liburl = "https://store.codesys.com/CODESYSLibs/%s/%s/%s/%s" % (libvendor, libname, libversion, filename)
                                install_library(liburl, librarymanager)

~~~

The function can be called like this:

~~~
install_missing_libraries(librarymanager)
~~~

The variable "librarymanager" is implicitly available in python scripts, running in CODESYS.</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Ingo</dc:creator><pubDate>Sun, 20 Sep 2020 19:27:40 -0000</pubDate><guid isPermaLink="false">https://forge.codesys.com/tol/scripting/snippets/19/</guid></item></channel></rss>