Pythonov razred Platform

Vsebina
Kazalo 🔎︎

# Prepoznavanje operacijskega sistema

Prepoznavanje operacijskega sistema lahko izvedete z jezikom Python ali Basic.

LibreOffice Basicu manjka domorodno prepoznavanje macOS, medtem ko je lastnost ComputerName na voljo le v okoljih Windows. Klici Basica na makre Python pomagajo pri premoščanju takšnih omejitev.

## Z uporabo razreda Python:

            import os, platform
            class Platform():
                @property
                def ComputerName(self): return platform.node()
                @property
                def DirSeparator(self): return os.sep
                @property
                def isLinux(self): return (self.OSName=='Linux')
                @property
                def isMacOSX(self): return (self.OSName=='Darwin')
                @property
                def isWindows(self): return (self.OSName=='Windows')
                @property
                def OSName(self): return platform.system()
                @property
                def PathDelimiter(self): return os.pathsep

## Uporaba razrednega modula Basic:

LibreOffice Basic ne prepozna sistema macOS. Identifikacija platforme je tako možna z uporabo programskega vmesnika API LibreOffice (angl. Application Programming Interface).

            Option Compatible
            Option ClassModule
            Option Explicit

            Public Property Get ComputerName As String
                If isWindows Then ComputerName = Environ("ComputerName")
            End Property ' Platform.ComputerName

            Public Property Get DirSeparator As String
                DirSeparator = GetPathSeparator()
            End Property ' Platform.DirSeparator

            Public Property Get IsLinux As Boolean
                isLinux = ( GetGUIType()=4 ) ' Applies to macOS as well
            End Property ' Platform.isLinux

            Public Property Get IsMacOSX As Boolean
                isMacOSX = ( OSName="MAC" )
            End Property ' Platform.isMacOSX

            Public Property Get IsWindows As Boolean
                isWindows = ( GetGUIType()=1 )
            End Property ' Platform.isWindows

            Public Property Get OSName As String
                ' Vrni ime platforme kot "MAC", "UNIX", "WIN"
                ' Sklepanje po funkciji "Tools.UCB.ShowHelperDialog"
                With GlobalScope.Basiclibraries
                    If Not .IsLibraryLoaded("Tools") Then .LoadLibrary("Tools")
                End With
                Dim keyNode As Object ' com.sun.star.configuration.ConfigurationAccess
                keyNode = Tools.Misc.GetRegistryKeyContent("org.openoffice.Office.Common/Help")
                OSName = keyNode.GetByName("System")
            End Property ' Platform.OSName

            Public Property Get PathDelimiter As String
                Select Case OSName
                    Case "MAC", "UNIX" : PathDelimiter = ":"
                    Case "WIN" : PathDelimiter = ";"
                 End Select
            End Property ' Platform.PathDelimiter

## Primeri:

S Pythonom

>>> from import Platform

>>> print(Platform().isMacOSX) # lastnost predmeta

True

>>> input(Platform().OSName) # lastnost predmeta

Darwin

Izberite Orodja – Makri – Zaženi makro.

            from  import Platform
            import screen_io as ui
            p = Platform()
            ui.MsgBox(''.join(['isMacOS: ',str(p.isMacOSX)]),0,p.OSName)

Z LibreOffice Basicom

            Sub Platform_example()
                Dim p As New Platform ‘ instanca razreda Platform
                MsgBox p.isLinux ‘ lastnost predmeta
                Print p.isWindows, p.OSName ‘ lastnosti predmeta
            End Sub ' Platform_example

Podobne teme

Zaslonski vhod/izhod

Funkcija GetGuiType

Funkcija GetPathSeparator

Primeri programov v Pythonu

Pomoč LibreOffice za skripte v jeziku Python