public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
From: Jaben Carsey <jaben.carsey@intel.com>
To: edk2-devel@lists.01.org
Cc: Liming Gao <liming.gao@intel.com>, Yonghong Zhu <yonghong.zhu@intel.com>
Subject: [PATCH v1 01/11] BaseTools: decorate base classes to prevent instantiation
Date: Mon, 14 May 2018 11:09:10 -0700	[thread overview]
Message-ID: <1d945b8d75cc240968753642344be9909eb265bb.1526321052.git.jaben.carsey@intel.com> (raw)
In-Reply-To: <cover.1526321052.git.jaben.carsey@intel.com>
In-Reply-To: <cover.1526321052.git.jaben.carsey@intel.com>

use python's ABC (abstract base class) to raise type errors if we instantiate
classes we designed to be used only as base classes for other classes.

Cc: Liming Gao <liming.gao@intel.com>
Cc: Yonghong Zhu <yonghong.zhu@intel.com>
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Jaben Carsey <jaben.carsey@intel.com>
---
 BaseTools/Source/Python/AutoGen/AutoGen.py                      | 4 ++++
 BaseTools/Source/Python/AutoGen/GenMake.py                      | 4 ++++
 BaseTools/Source/Python/AutoGen/ValidCheckingInfoObject.py      | 4 ++++
 BaseTools/Source/Python/Common/Expression.py                    | 4 ++++
 BaseTools/Source/Python/Common/VariableAttributes.py            | 6 +++++-
 BaseTools/Source/Python/Ecc/MetaFileWorkspace/MetaFileParser.py | 4 ++++
 BaseTools/Source/Python/Table/Table.py                          | 6 +++++-
 BaseTools/Source/Python/Workspace/BuildClassObject.py           | 7 +++++++
 BaseTools/Source/Python/Workspace/MetaFileParser.py             | 4 ++++
 9 files changed, 41 insertions(+), 2 deletions(-)

diff --git a/BaseTools/Source/Python/AutoGen/AutoGen.py b/BaseTools/Source/Python/AutoGen/AutoGen.py
index 54f6b1f173b2..619e1e41e32b 100644
--- a/BaseTools/Source/Python/AutoGen/AutoGen.py
+++ b/BaseTools/Source/Python/AutoGen/AutoGen.py
@@ -47,6 +47,7 @@ import hashlib
 from GenVar import VariableMgr,var_info
 from collections import OrderedDict
 from collections import defaultdict
+from abc import ABCMeta, abstractmethod
 
 ## Regular expression for splitting Dependency Expression string into tokens
 gDepexTokenPattern = re.compile("(\(|\)|\w+| \S+\.inf)")
@@ -197,6 +198,9 @@ class AutoGen(object):
             cls.__ObjectCache[Key] = super(AutoGen, cls).__new__(cls)
             return cls.__ObjectCache[Key]
 
+    __metaclass__ = ABCMeta
+    # prevent this class from being accidentally instantiated
+    @abstractmethod
     def __init__ (self, Workspace, MetaFile, Target, Toolchain, Arch, *args, **kwargs):
         super(AutoGen, self).__init__(self, Workspace, MetaFile, Target, Toolchain, Arch, *args, **kwargs)
 
diff --git a/BaseTools/Source/Python/AutoGen/GenMake.py b/BaseTools/Source/Python/AutoGen/GenMake.py
index a37350742240..68ec9a817133 100644
--- a/BaseTools/Source/Python/AutoGen/GenMake.py
+++ b/BaseTools/Source/Python/AutoGen/GenMake.py
@@ -26,6 +26,7 @@ from Common.String import *
 from BuildEngine import *
 import Common.GlobalData as GlobalData
 from collections import OrderedDict
+from abc import ABCMeta, abstractmethod
 
 ## Regular expression for finding header file inclusions
 gIncludePattern = re.compile(r"^[ \t]*#?[ \t]*include(?:[ \t]*(?:\\(?:\r\n|\r|\n))*[ \t]*)*(?:\(?[\"<]?[ \t]*)([-\w.\\/() \t]+)(?:[ \t]*[\">]?\)?)", re.MULTILINE | re.UNICODE | re.IGNORECASE)
@@ -171,6 +172,9 @@ class BuildFile(object):
     #
     #   @param  AutoGenObject   Object of AutoGen class
     #
+    __metaclass__ = ABCMeta
+    # prevent this class from being accidentally instantiated
+    @abstractmethod
     def __init__(self, AutoGenObject):
         self._AutoGenObject = AutoGenObject
         self._FileType = gMakeType
diff --git a/BaseTools/Source/Python/AutoGen/ValidCheckingInfoObject.py b/BaseTools/Source/Python/AutoGen/ValidCheckingInfoObject.py
index 64d4965e9662..e2b4795129ef 100644
--- a/BaseTools/Source/Python/AutoGen/ValidCheckingInfoObject.py
+++ b/BaseTools/Source/Python/AutoGen/ValidCheckingInfoObject.py
@@ -20,6 +20,7 @@ from Common.Misc import *
 from StringIO import StringIO
 from struct import pack
 from Common.DataType import *
+from abc import ABCMeta, abstractmethod
 
 class VAR_CHECK_PCD_VARIABLE_TAB_CONTAINER(object):
     def __init__(self):
@@ -222,6 +223,9 @@ class VAR_CHECK_PCD_VARIABLE_TAB(object):
 
 
 class VAR_CHECK_PCD_VALID_OBJ(object):
+    __metaclass__ = ABCMeta
+    # prevent this class from being accidentally instantiated
+    @abstractmethod
     def __init__(self, VarOffset, data, PcdDataType):
         self.Type = 1
         self.Length = 0  # Length include this header
diff --git a/BaseTools/Source/Python/Common/Expression.py b/BaseTools/Source/Python/Common/Expression.py
index 9e9d9fdc02e7..9fa07c6add16 100644
--- a/BaseTools/Source/Python/Common/Expression.py
+++ b/BaseTools/Source/Python/Common/Expression.py
@@ -19,6 +19,7 @@ from Misc import GuidStringToGuidStructureString, ParseFieldValue, IsFieldValueA
 import Common.EdkLogger as EdkLogger
 import copy
 from Common.DataType import *
+from abc import ABCMeta, abstractmethod
 
 ERR_STRING_EXPR         = 'This operator cannot be used in string expression: [%s].'
 ERR_SNYTAX              = 'Syntax error, the rest of expression cannot be evaluated: [%s].'
@@ -202,6 +203,9 @@ def IntToStr(Value):
 SupportedInMacroList = ['TARGET', 'TOOL_CHAIN_TAG', 'ARCH', 'FAMILY']
 
 class BaseExpression(object):
+    __metaclass__ = ABCMeta
+    # prevent this class from being accidentally instantiated
+    @abstractmethod
     def __init__(self, *args, **kwargs):
         super(BaseExpression, self).__init__()
 
diff --git a/BaseTools/Source/Python/Common/VariableAttributes.py b/BaseTools/Source/Python/Common/VariableAttributes.py
index a2e22ca0409c..72f64fff3864 100644
--- a/BaseTools/Source/Python/Common/VariableAttributes.py
+++ b/BaseTools/Source/Python/Common/VariableAttributes.py
@@ -3,7 +3,7 @@
 # This file is used to handle the variable attributes and property information
 #
 #
-# Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
+# Copyright (c) 2015 - 2018, Intel Corporation. All rights reserved.<BR>
 # This program and the accompanying materials
 # are licensed and made available under the terms and conditions of the BSD License
 # which accompanies this distribution.  The full text of the license may be found at
@@ -12,6 +12,7 @@
 # THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
 # WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 #
+from abc import ABCMeta, abstractmethod
    
 class VariableAttributes(object):
     EFI_VARIABLE_NON_VOLATILE = 0x00000001
@@ -25,6 +26,9 @@ class VariableAttributes(object):
                      "RO":VAR_CHECK_VARIABLE_PROPERTY_READ_ONLY
                      }
     
+    __metaclass__ = ABCMeta
+    # prevent this class from being accidentally instantiated
+    @abstractmethod
     def __init__(self):
         pass
     
diff --git a/BaseTools/Source/Python/Ecc/MetaFileWorkspace/MetaFileParser.py b/BaseTools/Source/Python/Ecc/MetaFileWorkspace/MetaFileParser.py
index 4d61cd1cea91..e5c43b629151 100644
--- a/BaseTools/Source/Python/Ecc/MetaFileWorkspace/MetaFileParser.py
+++ b/BaseTools/Source/Python/Ecc/MetaFileWorkspace/MetaFileParser.py
@@ -35,6 +35,7 @@ from MetaFileTable import MetaFileStorage
 from GenFds.FdfParser import FdfParser
 from Common.LongFilePathSupport import OpenLongFilePath as open
 from Common.LongFilePathSupport import CodecOpenLongFilePath
+from abc import ABCMeta, abstractmethod
 
 ## A decorator used to parse macro definition
 def ParseMacro(Parser):
@@ -146,6 +147,9 @@ class MetaFileParser(object):
     #   @param      Owner           Owner ID (for sub-section parsing)
     #   @param      From            ID from which the data comes (for !INCLUDE directive)
     #
+    __metaclass__ = ABCMeta
+    # prevent this class from being accidentally instantiated
+    @abstractmethod
     def __init__(self, FilePath, FileType, Table, Owner=-1, From=-1):
         self._Table = Table
         self._RawTable = Table
diff --git a/BaseTools/Source/Python/Table/Table.py b/BaseTools/Source/Python/Table/Table.py
index c311df91c2ec..46bc92ea8377 100644
--- a/BaseTools/Source/Python/Table/Table.py
+++ b/BaseTools/Source/Python/Table/Table.py
@@ -1,7 +1,7 @@
 ## @file
 # This file is used to create/update/query/erase a common table
 #
-# Copyright (c) 2008, Intel Corporation. All rights reserved.<BR>
+# Copyright (c) 2008 - 2018, Intel Corporation. All rights reserved.<BR>
 # This program and the accompanying materials
 # are licensed and made available under the terms and conditions of the BSD License
 # which accompanies this distribution.  The full text of the license may be found at
@@ -15,6 +15,7 @@
 # Import Modules
 #
 import Common.EdkLogger as EdkLogger
+from abc import ABCMeta, abstractmethod
 
 ## TableFile
 #
@@ -26,6 +27,9 @@ import Common.EdkLogger as EdkLogger
 # @param TableName:  Name of the table
 #
 class Table(object):
+    __metaclass__ = ABCMeta
+    # prevent this class from being accidentally instantiated
+    @abstractmethod
     def __init__(self, Cursor):
         self.Cur = Cursor
         self.Table = ''
diff --git a/BaseTools/Source/Python/Workspace/BuildClassObject.py b/BaseTools/Source/Python/Workspace/BuildClassObject.py
index 209315d901b2..5f34e8e0bc69 100644
--- a/BaseTools/Source/Python/Workspace/BuildClassObject.py
+++ b/BaseTools/Source/Python/Workspace/BuildClassObject.py
@@ -18,6 +18,7 @@ from Common.Misc import RealPath2
 from Common.BuildToolError import *
 from Common.DataType import *
 import collections
+from abc import ABCMeta, abstractmethod
 
 ## PcdClassObject
 #
@@ -381,6 +382,9 @@ class ModuleBuildClassObject(object):
 #                       { [(PcdCName, PcdGuidCName)] : PcdClassObject}
 #
 class PackageBuildClassObject(object):
+    __metaclass__ = ABCMeta
+    # prevent this class from being accidentally instantiated
+    @abstractmethod
     def __init__(self):
         self.MetaFile                = ''
         self.PackageName             = ''
@@ -451,6 +455,9 @@ class PackageBuildClassObject(object):
 #                         { [BuildOptionKey] : BuildOptionValue }
 #
 class PlatformBuildClassObject(object):
+    __metaclass__ = ABCMeta
+    # prevent this class from being accidentally instantiated
+    @abstractmethod
     def __init__(self):
         self.MetaFile                = ''
         self.PlatformName            = ''
diff --git a/BaseTools/Source/Python/Workspace/MetaFileParser.py b/BaseTools/Source/Python/Workspace/MetaFileParser.py
index 36843643ed13..21b20bce4018 100644
--- a/BaseTools/Source/Python/Workspace/MetaFileParser.py
+++ b/BaseTools/Source/Python/Workspace/MetaFileParser.py
@@ -34,6 +34,7 @@ from Common.LongFilePathSupport import OpenLongFilePath as open
 from collections import defaultdict
 from MetaFileTable import MetaFileStorage
 from MetaFileCommentParser import CheckInfComment
+from abc import ABCMeta, abstractmethod
 
 ## RegEx for finding file versions
 hexVersionPattern = re.compile(r'0[xX][\da-f-A-F]{5,8}')
@@ -154,6 +155,9 @@ class MetaFileParser(object):
     #   @param      Owner           Owner ID (for sub-section parsing)
     #   @param      From            ID from which the data comes (for !INCLUDE directive)
     #
+    __metaclass__ = ABCMeta
+    # prevent this class from being accidentally instantiated
+    @abstractmethod
     def __init__(self, FilePath, FileType, Arch, Table, Owner= -1, From= -1):
         self._Table = Table
         self._RawTable = Table
-- 
2.16.2.windows.1



  reply	other threads:[~2018-05-14 18:09 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-05-14 18:09 [PATCH v1 00/11] BaseTools refactoring Jaben Carsey
2018-05-14 18:09 ` Jaben Carsey [this message]
2018-05-14 18:09 ` [PATCH v1 02/11] BaseTools: Workspace - create a base class Jaben Carsey
2018-05-14 18:09 ` [PATCH v1 03/11] BaseTools: remove unused code Jaben Carsey
2018-05-14 18:09 ` [PATCH v1 04/11] BaseTools: remove repeated calls to startswith/endswith Jaben Carsey
2018-05-14 18:09 ` [PATCH v1 05/11] BaseTools: use set presence instead of series of equality Jaben Carsey
2018-05-14 18:09 ` [PATCH v1 06/11] BaseTools: refactor section generation Jaben Carsey
2018-05-14 18:09 ` [PATCH v1 07/11] BaseTools: refactor file opening/writing Jaben Carsey
2018-05-14 18:09 ` [PATCH v1 08/11] BaseTools: refactor to change object types Jaben Carsey
2018-05-14 18:09 ` [PATCH v1 09/11] BaseTools: refactor to stop re-allocating strings Jaben Carsey
2018-05-14 18:09 ` [PATCH v1 10/11] BaseTools: change to set for membership testing Jaben Carsey
2018-05-14 18:09 ` [PATCH v1 11/11] BaseTools: remove extra assignment Jaben Carsey
  -- strict thread matches above, loose matches on Subject: below --
2018-06-20 21:08 [PATCH v2 00/11] BaseTools Refactoring Jaben Carsey
2018-06-20 21:08 ` [PATCH v1 01/11] BaseTools: decorate base classes to prevent instantiation Jaben Carsey

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1d945b8d75cc240968753642344be9909eb265bb.1526321052.git.jaben.carsey@intel.com \
    --to=devel@edk2.groups.io \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox