Fixing distutils_src folder structure due to setuptools bug

Also: adding PCellDeclarationHelper to klayout.db
This commit is contained in:
Thomas Ferreira de Lima 2018-10-19 18:53:37 -04:00
parent d3fa4465f6
commit 87a49007ff
No known key found for this signature in database
GPG Key ID: 43E98870EAA0A86E
12 changed files with 291 additions and 12 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, Extension, Distribution
from setuptools import setup, Extension, Distribution, find_packages
import glob
import os
import platform
@ -380,6 +380,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(dbcore, "dbcore", "KLayout core module 'dbcore'", db_module_init)
DEFINE_PYMOD_WITH_INIT(dbcore, "db", "KLayout core module 'db'", db_module_init)

View File

@ -1 +0,0 @@
from dbcore import *

View File

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

View File

@ -0,0 +1,3 @@
from klayout.dbcore import *
from klayout.db.pcell_declaration_helper import PCellDeclarationHelper

View File

@ -0,0 +1,280 @@
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
__all__ = ['PCellDeclarationHelper']

View File

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

View File

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

View File

@ -1 +0,0 @@
from rdbcore import *

View File

@ -1 +0,0 @@
from tlcore import *

View File

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

View File

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