From b05b80b4cb9ae6ace2a9c1a6f3ca237db0e82e5c Mon Sep 17 00:00:00 2001 From: Jun Komoda <45822440+junkmd@users.noreply.github.com> Date: Mon, 2 Dec 2024 22:14:03 +0900 Subject: [PATCH] Split `server/w_getopt.py`. (#675) * Revert temporarily renaming modules. * Revert temporarily renaming modules. * Update test section. * Remove test section. * Update `test_w_getopt.py`. --- comtypes/server/register.py | 2 +- comtypes/server/{_w_getopt.py => w_getopt.py} | 32 ------ comtypes/test/test_w_getopt.py | 104 +++++------------- 3 files changed, 30 insertions(+), 108 deletions(-) rename comtypes/server/{_w_getopt.py => w_getopt.py} (54%) diff --git a/comtypes/server/register.py b/comtypes/server/register.py index e48fb1a2..41c6ffc7 100644 --- a/comtypes/server/register.py +++ b/comtypes/server/register.py @@ -44,7 +44,7 @@ import comtypes from comtypes.typeinfo import LoadTypeLibEx, UnRegisterTypeLib, REGKIND_REGISTER from comtypes.hresult import * -from comtypes.server import _w_getopt as w_getopt +from comtypes.server import w_getopt import comtypes.server.inprocserver from ctypes import windll, c_ulong, c_wchar_p, WinError, sizeof, create_string_buffer diff --git a/comtypes/server/_w_getopt.py b/comtypes/server/w_getopt.py similarity index 54% rename from comtypes/server/_w_getopt.py rename to comtypes/server/w_getopt.py index b0ae0b9e..6b0efac1 100644 --- a/comtypes/server/_w_getopt.py +++ b/comtypes/server/w_getopt.py @@ -41,35 +41,3 @@ def w_getopt(args, options): args = args[1:] return opts, arguments - - -if __debug__: - if __name__ == "__main__": - import unittest - - class TestCase(unittest.TestCase): - def test_1(self): - args = "-embedding spam /RegServer foo /UnregSERVER blabla".split() - opts, args = w_getopt(args, "regserver unregserver embedding".split()) - self.assertEqual( - opts, [("embedding", ""), ("regserver", ""), ("unregserver", "")] - ) - self.assertEqual(args, ["spam", "foo", "blabla"]) - - def test_2(self): - args = "/TLB Hello.Tlb HELLO.idl".split() - opts, args = w_getopt(args, ["tlb:"]) - self.assertEqual(opts, [("tlb", "Hello.Tlb")]) - self.assertEqual(args, ["HELLO.idl"]) - - def test_3(self): - # Invalid option - self.assertRaises( - GetoptError, w_getopt, "/TLIB hello.tlb hello.idl".split(), ["tlb:"] - ) - - def test_4(self): - # Missing argument - self.assertRaises(GetoptError, w_getopt, "/TLB".split(), ["tlb:"]) - - unittest.main() diff --git a/comtypes/test/test_w_getopt.py b/comtypes/test/test_w_getopt.py index b0ae0b9e..63cee25b 100644 --- a/comtypes/test/test_w_getopt.py +++ b/comtypes/test/test_w_getopt.py @@ -1,75 +1,29 @@ -class GetoptError(Exception): - pass - - -def w_getopt(args, options): - """A getopt for Windows. - - Options may start with either '-' or '/', the option names may - have more than one letter (/tlb or -RegServer), and option names - are case insensitive. - - Returns two elements, just as getopt.getopt. The first is a list - of (option, value) pairs in the same way getopt.getopt does, but - there is no '-' or '/' prefix to the option name, and the option - name is always lower case. The second is the list of arguments - which do not belong to an option. - - Different from getopt.getopt, a single argument not belonging to an option - does not terminate parsing. - """ - opts = [] - arguments = [] - while args: - if args[0][:1] in "/-": - arg = args[0][1:] # strip the '-' or '/' - arg = arg.lower() - - if arg + ":" in options: - try: - opts.append((arg, args[1])) - except IndexError: - raise GetoptError(f"option '{args[0]}' requires an argument") - args = args[1:] - elif arg in options: - opts.append((arg, "")) - else: - raise GetoptError(f"invalid option '{args[0]}'") - args = args[1:] - else: - arguments.append(args[0]) - args = args[1:] - - return opts, arguments - - -if __debug__: - if __name__ == "__main__": - import unittest - - class TestCase(unittest.TestCase): - def test_1(self): - args = "-embedding spam /RegServer foo /UnregSERVER blabla".split() - opts, args = w_getopt(args, "regserver unregserver embedding".split()) - self.assertEqual( - opts, [("embedding", ""), ("regserver", ""), ("unregserver", "")] - ) - self.assertEqual(args, ["spam", "foo", "blabla"]) - - def test_2(self): - args = "/TLB Hello.Tlb HELLO.idl".split() - opts, args = w_getopt(args, ["tlb:"]) - self.assertEqual(opts, [("tlb", "Hello.Tlb")]) - self.assertEqual(args, ["HELLO.idl"]) - - def test_3(self): - # Invalid option - self.assertRaises( - GetoptError, w_getopt, "/TLIB hello.tlb hello.idl".split(), ["tlb:"] - ) - - def test_4(self): - # Missing argument - self.assertRaises(GetoptError, w_getopt, "/TLB".split(), ["tlb:"]) - - unittest.main() +import unittest + +from comtypes.server.w_getopt import GetoptError, w_getopt + + +class TestCase(unittest.TestCase): + def test_1(self): + args = "-embedding spam /RegServer foo /UnregSERVER blabla".split() + opts, args = w_getopt(args, "regserver unregserver embedding".split()) + self.assertEqual( + opts, [("embedding", ""), ("regserver", ""), ("unregserver", "")] + ) + self.assertEqual(args, ["spam", "foo", "blabla"]) + + def test_2(self): + args = "/TLB Hello.Tlb HELLO.idl".split() + opts, args = w_getopt(args, ["tlb:"]) + self.assertEqual(opts, [("tlb", "Hello.Tlb")]) + self.assertEqual(args, ["HELLO.idl"]) + + def test_3(self): + # Invalid option + self.assertRaises( + GetoptError, w_getopt, "/TLIB hello.tlb hello.idl".split(), ["tlb:"] + ) + + def test_4(self): + # Missing argument + self.assertRaises(GetoptError, w_getopt, "/TLB".split(), ["tlb:"])