Merge pull request #186 from lightwave-lab/pymod-splitting

Pymod package splitting
This commit is contained in:
Matthias Köfferlein 2018-10-22 18:17:36 +02:00 committed by GitHub
commit 3074f05e97
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 316 additions and 18 deletions

View File

@ -54,7 +54,7 @@ and won't find them. So we need to take away the path with
"-Wl,-soname" on Linux (see Config.link_args).
"""
from setuptools import setup, Distribution
from setuptools import setup, Distribution, find_packages
from setuptools.extension import Extension, Library
import glob
import os
@ -320,7 +320,7 @@ class Config(object):
"""
Gets the version string
"""
return "0.26.0.dev6"
return "0.26.0.dev7"
config = Config()
@ -451,11 +451,11 @@ for pi in dbpi_dirs:
tl_path = os.path.join("src", "pymod", "tl")
tl_sources = set(glob.glob(os.path.join(tl_path, "*.cc")))
tl = Extension(config.root + '.tl',
tl = Extension(config.root + '.tlcore',
define_macros=config.macros(),
include_dirs=[_tl_path, _gsi_path, _pya_path],
extra_objects=[config.path_of('_tl', _tl_path), config.path_of('_gsi', _gsi_path), config.path_of('_pya', _pya_path)],
extra_link_args=config.link_args('tl'),
extra_link_args=config.link_args('tlcore'),
sources=list(tl_sources))
# ------------------------------------------------------------------
@ -464,11 +464,11 @@ tl = Extension(config.root + '.tl',
db_path = os.path.join("src", "pymod", "db")
db_sources = set(glob.glob(os.path.join(db_path, "*.cc")))
db = Extension(config.root + '.db',
db = Extension(config.root + '.dbcore',
define_macros=config.macros(),
include_dirs=[_db_path, _tl_path, _gsi_path, _pya_path],
extra_objects=[config.path_of('_db', _db_path), config.path_of('_tl', _tl_path), config.path_of('_gsi', _gsi_path), config.path_of('_pya', _pya_path)],
extra_link_args=config.link_args('db'),
extra_link_args=config.link_args('dbcore'),
sources=list(db_sources))
# ------------------------------------------------------------------
@ -477,11 +477,12 @@ db = Extension(config.root + '.db',
rdb_path = os.path.join("src", "pymod", "rdb")
rdb_sources = set(glob.glob(os.path.join(rdb_path, "*.cc")))
rdb = Extension(config.root + '.rdb',
rdb = Extension(config.root + '.rdbcore',
define_macros=config.macros(),
include_dirs=[_rdb_path, _db_path, _tl_path, _gsi_path, _pya_path],
extra_objects=[config.path_of('_rdb', _rdb_path), config.path_of('_tl', _tl_path), config.path_of('_gsi', _gsi_path), config.path_of('_pya', _pya_path)],
extra_link_args=config.link_args('rdb'),
extra_link_args=config.link_args('rdbcore'),
sources=list(rdb_sources))
# ------------------------------------------------------------------
@ -496,6 +497,6 @@ if __name__ == '__main__':
author='Matthias Koefferlein',
author_email='matthias@klayout.de',
url='https://github.com/klayoutmatthias/klayout',
packages=[config.root],
package_dir={config.root: 'src/pymod/distutils_src'},
packages=find_packages('src/pymod/distutils_src'),
package_dir={'': 'src/pymod/distutils_src'}, # https://github.com/pypa/setuptools/issues/230
ext_modules=[_tl, _gsi, _pya, _db, _rdb] + db_plugins + [tl, db, rdb])

View File

@ -33,4 +33,4 @@ static PyObject *db_module_init (const char *mod_name, const char *mod_descripti
return module_init (mod_name, mod_description);
}
DEFINE_PYMOD_WITH_INIT(db, "db", "KLayout core module 'db'", db_module_init)
DEFINE_PYMOD_WITH_INIT(dbcore, "db", "KLayout core module 'db'", db_module_init)

View File

@ -1,5 +0,0 @@
# klayout library definition file
__all__ = [ "tl", "db", "lay", "rdb" ]

View File

@ -0,0 +1,9 @@
### Python ###
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
# C extensions
*.so
*.dylib

View File

@ -0,0 +1 @@
# klayout library definition file

View File

@ -0,0 +1,6 @@
import klayout.dbcore
from klayout.dbcore import *
from klayout.db.pcell_declaration_helper import PCellDeclarationHelper
__all__ = klayout.dbcore.__all__ + ['PCellDeclarationHelper']

View File

@ -0,0 +1,278 @@
from klayout.db import Trans, PCellDeclaration, PCellParameterDeclaration
class _PCellDeclarationHelperLayerDescriptor(object):
"""
A descriptor object which translates the PCell parameters into class attributes
"""
def __init__(self, param_index):
self.param_index = param_index
def __get__(self, obj, type=None):
return obj._layers[self.param_index]
def __set__(self, obj, value):
raise AttributeError("can't change layer attribute")
class _PCellDeclarationHelperParameterDescriptor(object):
"""
A descriptor object which translates the PCell parameters into class attributes
In some cases (i.e. can_convert_from_shape), these placeholders are not
connected to real parameters (obj._param_values is None). In this case,
the descriptor acts as a value holder (self.value)
"""
def __init__(self, param_index):
self.param_index = param_index
self.value = None
def __get__(self, obj, type=None):
if obj._param_values:
return obj._param_values[self.param_index]
else:
return self.value
def __set__(self, obj, value):
if obj._param_values:
obj._param_values[self.param_index] = value
else:
self.value = value
class _PCellDeclarationHelper(PCellDeclaration):
"""
A helper class that somewhat simplifies the implementation
of a PCell
"""
def __init__(self):
"""
initialize this instance
"""
# "private" attributes
self._param_decls = []
self._param_values = None
self._layer_param_index = []
self._layers = []
# public attributes
self.layout = None
self.shape = None
self.layer = None
self.cell = None
def param(self, name, value_type, description, hidden=False, readonly=False, unit=None, default=None, choices=None):
"""
Defines a parameter
name -> the short name of the parameter
type -> the type of the parameter
description -> the description text
named parameters
hidden -> (boolean) true, if the parameter is not shown in the dialog
readonly -> (boolean) true, if the parameter cannot be edited
unit -> the unit string
default -> the default value
choices -> ([ [ d, v ], ...) choice descriptions/value for choice type
this method defines accessor methods for the parameters
{name} -> read accessor
set_{name} -> write accessor ({name}= does not work because the
Ruby confuses that method with variables)
{name}_layer -> read accessor for the layer index for TypeLayer parameters
"""
# create accessor methods for the parameters
param_index = len(self._param_decls)
setattr(type(self), name, _PCellDeclarationHelperParameterDescriptor(param_index))
if value_type == type(self).TypeLayer:
setattr(type(self), name + "_layer",
_PCellDeclarationHelperLayerDescriptor(len(self._layer_param_index)))
self._layer_param_index.append(param_index)
# store the parameter declarations
pdecl = PCellParameterDeclaration(name, value_type, description)
self._param_decls.append(pdecl)
# set additional attributes of the parameters
pdecl.hidden = hidden
pdecl.readonly = readonly
if not (default is None):
pdecl.default = default
if not (unit is None):
pdecl.unit = unit
if not (choices is None):
if not isinstance(choices, list) and not isinstance(choices, tuple):
raise "choices value must be an list/tuple of two-element arrays (description, value)"
for c in choices:
if (not isinstance(choices, list) and not isinstance(choices, tuple)) or len(c) != 2:
raise "choices value must be an list/tuple of two-element arrays (description, value)"
pdecl.add_choice(c[0], c[1])
# return the declaration object for further operations
return pdecl
def display_text(self, parameters):
"""
implementation of display_text
"""
self._param_values = parameters
text = self.display_text_impl()
self._param_values = None
return text
def get_parameters(self):
"""
gets the parameters
"""
return self._param_decls
def get_values(self):
"""
gets the temporary parameter values
"""
v = self._param_values
self._param_values = None
return v
def init_values(self, values=None, layers=None):
"""
initializes the temporary parameter values
"values" are the original values. If "None" is given, the
default values will be used.
"layers" are the layer indexes corresponding to the layer
parameters.
"""
if not values:
self._param_values = []
for pd in self._param_decls:
self._param_values.append(pd.default)
else:
self._param_values = values
self._layers = layers
def finish(self):
"""
Needs to be called at the end of produce() after init_values was used
"""
self._param_values = None
self._layers = None
def get_layers(self, parameters):
"""
get the layer definitions
"""
layers = []
for i in self._layer_param_index:
layers.append(parameters[i])
return layers
def coerce_parameters(self, layout, parameters):
"""
coerce parameters (make consistent)
"""
self.init_values(parameters)
self.layout = layout
self.coerce_parameters_impl()
self.layout = None
return self.get_values()
def produce(self, layout, layers, parameters, cell):
"""
coerce parameters (make consistent)
"""
self.init_values(parameters, layers)
self.cell = cell
self.layout = layout
self.produce_impl()
self.cell = None
self.layout = None
self.finish()
def can_create_from_shape(self, layout, shape, layer):
"""
produce a helper for can_create_from_shape
"""
self.layout = layout
self.shape = shape
self.layer = layer
ret = self.can_create_from_shape_impl()
self.layout = None
self.shape = None
self.layer = None
return ret
def transformation_from_shape(self, layout, shape, layer):
"""
produce a helper for parameters_from_shape
"""
self.layout = layout
self.shape = shape
self.layer = layer
t = self.transformation_from_shape_impl()
self.layout = None
self.shape = None
self.layer = None
return t
def parameters_from_shape(self, layout, shape, layer):
"""
produce a helper for parameters_from_shape
with this helper, the implementation can use the parameter setters
"""
self.init_values()
self.layout = layout
self.shape = shape
self.layer = layer
self.parameters_from_shape_impl()
param = self.get_values()
self.layout = None
self.shape = None
self.layer = None
return param
def display_text_impl(self):
"""
default implementation
"""
return ""
def coerce_parameters_impl(self):
"""
default implementation
"""
pass
def produce_impl(self):
"""
default implementation
"""
pass
def can_create_from_shape_impl(self):
"""
default implementation
"""
return False
def parameters_from_shape_impl(self):
"""
default implementation
"""
pass
def transformation_from_shape_impl(self):
"""
default implementation
"""
return Trans()
# import the Type... constants from PCellParameterDeclaration
for k in dir(PCellParameterDeclaration):
if k.startswith("Type"):
setattr(_PCellDeclarationHelper, k, getattr(PCellParameterDeclaration, k))
# Inject the PCellDeclarationHelper into pya module for consistency:
PCellDeclarationHelper = _PCellDeclarationHelper

View File

@ -0,0 +1,4 @@
import klayout.rdbcore
from klayout.rdbcore import *
__all__ = klayout.rdbcore.__all__

View File

@ -0,0 +1,4 @@
import klayout.tlcore
from klayout.tlcore import *
__all__ = klayout.tlcore.__all__

View File

@ -25,4 +25,4 @@
// to force linking of the rdb module
#include "../../rdb/rdb/rdbForceLink.h"
DEFINE_PYMOD(rdb, "rdb", "KLayout core module 'rdb'")
DEFINE_PYMOD(rdbcore, "rdb", "KLayout core module 'rdb'")

View File

@ -22,4 +22,4 @@
#include "../pymodHelper.h"
DEFINE_PYMOD(tl, "tl", "KLayout core module 'tl'")
DEFINE_PYMOD(tlcore, "tl", "KLayout core module 'tl'")