0
votes

I've been provided a (trivial) code example in C# that I'm now trying to convert to IronPython:

Clearcore2.Licensing.LicenseKeys.Keys = new[] {
          @"<?xml version=""1.0"" encoding=""utf-8""?>
            <license_key>
                <company_name>Company </company_name>
                <product_name>ProcessingFramework</product_name>
                <features>WiffReaderSDK</features>
                <key_data>
                    2F923C31D1E7E16B191EF2F87DCA7F15831A3F18DED11E05582A98822==
                </key_data>
            </license_key>"};

The actual license key is stored in file "license". I run:

Licensing.LicenseKeys.Keys = Array[str]([open('license').read()])

but get

System.TypeInitializationException: The type initializer for 'Clearcore2.Data.WiffReader.WiffSampleRun' threw an exception. ---> Clearcore2.Licensing.LicenseKeyFormatException: License key can't be parsed! ---> System.ArgumentNullException: String reference not set to an instance of a String.
Parameter name: s
    at System.Text.Encoding.GetBytes(String s)
    at Clearcore2.Licensing.XmlKeyConversions.LoadXmlDocumentForString(String text)
    at Clearcore2.Licensing.XmlKeyConversions.LoadLicenseKey(String licenseKey, String& companyName, String& productName, String& features)
    --- End of inner exception stack trace ---
    at Clearcore2.Licensing.XmlKeyConversions.LoadLicenseKey(String licenseKey, String& companyName, String& productName, String& features)
    at Clearcore2.Licensing.Verifier.VerifyLicenseKey(String publicKey, String licenseKey)
    at Clearcore2.Utility.Licensing.Protection.Verify()
    at Clearcore2.Data.WiffReader.WiffSampleRun..cctor()
    --- End of inner exception stack trace ---
    at Clearcore2.Data.WiffReader.WiffSampleRun..ctor(WiffFile wiffFile, WiffSampleSubTree wiffSampleSubTree, Int32 sampleIndex)
    at Clearcore2.Data.WiffReader.WiffSampleSubTree.GetWiffSampleRun(Int32 sampleIndex)
    at Clearcore2.Data.AnalystDataProvider.AnalystWiffDataProvider.GetWiffSampleRun(String wiffPath, Int32 sampleIndex)
    at Clearcore2.Data.AnalystDataProvider.SampleImplementation.PopulateSampleInfo()
    at Clearcore2.Data.AnalystDataProvider.SampleImplementation..ctor(SampleLocator locator, AnalystWiffDataProvider dataProvider)
    at Clearcore2.Data.AnalystDataProvider.AnalystWiffDataProvider.CreateSampleFromLocator(SampleLocator locator)
    at Clearcore2.Data.AnalystDataProvider.BatchImp.GetSample(Int32 index)
    at Microsoft.Scripting.Interpreter.FuncCallInstruction`3.Run(InterpretedFrame frame)
    at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame)
    at Microsoft.Scripting.Interpreter.LightLambda.Run4[T0,T1,T2,T3,TRet](T0 arg0, T1 arg1, T2 arg2, T3 arg3)
    at System.Dynamic.UpdateDelegates.UpdateAndExecute3[T0,T1,T2,TRet](CallSite site, T0 arg0, T1 arg1, T2 arg2)
    at Microsoft.Scripting.Interpreter.DynamicInstruction`4.Run(InterpretedFrame frame)
    at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame)

I also tried

Licensing.LicenseKeys.Keys = Array[str](open('license').read())

but with the same effect.

In fact Licensing.LicenseKeys.Keys expects Array[str], but I'm not sure what @ means in the C# example.

What is the equivalent code for the C# example in IronPython?

1

1 Answers

0
votes

A close equivalent in IronPython would be

Clearcore2.Licensing.LicenseKeys.Keys = Array[str]([r"""<?xml version=""1.0"" encoding=""utf-8""?>
            <license_key>
                <company_name>Company </company_name>
                <product_name>ProcessingFramework</product_name>
                <features>WiffReaderSDK</features>
                <key_data>
                    2F923C31D1E7E16B191EF2F87DCA7F15831A3F18DED11E05582A98822==
                </key_data>
            </license_key>"""])

The @ in C# specifies a verbatim string literal. This means that no escape sequences etc. are handled and that the string may span multiple lines. A raw, triple quoted string should be the equivalent literal.

Your initial code might fail because of the encoding and or mode you read the file with. Depending on what should happen with the file data, you might want to specify an alternate mode for opening the file (e.g. b for binary) or use a .NET BCL function like File.ReadAllText which you can check from C# as well.