"""Provides the elements of a diagram."""
from abc import abstractmethod, ABCMeta
from collections import OrderedDict
from typing import (
Iterable,
Iterator,
List,
Mapping,
MutableMapping,
Optional,
Sequence,
Tuple,
Union,
)
from ..geometry import (
IntBounds,
IntPoint,
Orientation,
)
from ..util import log_warning
from .attributes import (
AttributeMap,
Attributes,
BlockAttributes,
ConnectionAttributes,
DiagramAttributes,
TextAttributes,
TextOrientation,
)
######################################################################
class RowDef:
"""Definition of a diagram row.
Holds the information necessary to create a row of diagram cells.
"""
def __init__(self, tags: Sequence[Optional[str]]):
"""Initialize with the given tags."""
self._tags = tags
@property
def tags(self) -> Sequence[Optional[str]]:
"""Return the tags for the cells."""
return list(self._tags)
######################################################################
class BlockDef:
"""Definition of a block.
Holds the information necessary to create a block.
"""
def __init__(
self,
name: Optional[str] = None,
tags: Iterable[str] = (),
**attrs: AttributeMap,
):
"""Initialize for a block with the given properties."""
self._name = name
self._tags = sorted(set(tags))
self._attributes = attrs
@property
def name(self) -> Optional[str]:
"""Name of the block."""
return self._name
@property
def tags(self) -> Iterable[str]:
"""Tags used to look up cells."""
return list(self._tags)
@property
def attributes(self) -> AttributeMap:
"""The attributes to create the block with."""
return self._attributes
######################################################################
# Connections can be made between whole blocks or nodes of blocks with
# a specifed tag. A "sub-block" can be defined either by a block name
# (whole block) or a block name and a tag (partial block).
SubBlockDef = Union[str, Tuple[str, Optional[str]]]
######################################################################
class ConnectionDef:
"""Definition of a connection between two blocks.
Holds the information necessary to create a connection.
"""
def __init__(
self,
start: SubBlockDef,
end: SubBlockDef,
group: Optional[str] = None,
**attrs: AttributeMap
):
"""Initialize given the names of two blocks."""
self._start = start
self._end = end
self._group = group
self._attributes = attrs
@property
def start(self) -> SubBlockDef:
"""Definition of source node."""
return self._start
@property
def end(self) -> SubBlockDef:
"""Definition of destination node."""
return self._end
@property
def group(self) -> Optional[str]:
"""Group to which the connection belongs."""
return self._group
@property
def attributes(self) -> AttributeMap:
"""Attributes to create the connection with."""
return self._attributes
######################################################################
# Multiple connection points can be specified as:
# - a single block name (string)
# - a list of block names (sequence of strings)
# - a list of block names and associated tag cells (mapping from
# string to string)
MultipleNodes = Union[str, Sequence[str], Mapping[str, str]]
######################################################################
class DiagramDef:
"""Definition of a diagram.
Holds the information necessary to create a diagram.
"""
def __init__(self, **attrs: AttributeMap):
"""Initialize with the given diagram attributes.
See DiagramAttributes for a list of available attributes.
"""
self._attributes = Attributes(**attrs)
self._auto_block_attributes = Attributes()
self._row_defs: List[RowDef] = []
self._block_defs: List[BlockDef] = []
self._block_defs_by_name: MutableMapping[str, BlockDef] = OrderedDict()
self._connection_defs: List[ConnectionDef] = []
@property
def attributes(self) -> Attributes:
"""The attributes to create the diagram with."""
return self._attributes
@property
def auto_block_attributes(self) -> Attributes:
"""Attributes for blocks generated automatically from tags."""
return self._auto_block_attributes
def set_auto_block_attributes(self, **attrs: AttributeMap) -> None:
"""Set the attributes of the autogenerated blocks."""
self._auto_block_attributes = Attributes(**attrs)
def add_row(self, tags: Sequence[Optional[str]]) -> None:
"""Add a row at the end of the diagram.
The input is a sequence of strings used to tag the cells with.
An empty string or None results in an untagged cell. The
length of the new row is equal to the number of tags (empty or
not).
"""
rdef = RowDef(tags)
self._row_defs.append(rdef)
def add_block(
self,
name: Optional[str],
tags: Iterable[str] = (),
**attrs: AttributeMap,
) -> None:
"""Add a new block to the diagram.
The 'tags' argument is a set of cell tags. The block will
cover the cells matching the tags in addition to the cells
tagged with the name of the block itself.
See BlockAttributes for a list of available attributes.
Rejects the block with a warning if there is already a block
registered with the same name.
"""
by_name = self._block_defs_by_name
if name and name in by_name:
log_warning("Block '{}' already exists".format(name))
return
bdef = BlockDef(name, tags, **attrs)
self._block_defs.append(bdef)
if name:
by_name[name] = bdef
def add_connections(
self,
starts: MultipleNodes,
ends: MultipleNodes,
group: Optional[str] = None,
**attrs: AttributeMap,
) -> None:
"""Create many connections at once.
If the number of start blocks is n and the number of end
blocks in m, then the number of connections created will be
n*m (assuming all blocks exist). See add_connection() for
further information.
"""
for start in self._to_sub_block_defs(starts):
for end in self._to_sub_block_defs(ends):
self.add_connection(start, end, group, **attrs)
@staticmethod
def _to_sub_block_defs(multi_nodes: MultipleNodes) -> Sequence[SubBlockDef]:
"""Produce the object necessary for a connection."""
if isinstance(multi_nodes, str):
return [multi_nodes]
if isinstance(multi_nodes, Sequence):
return multi_nodes
if isinstance(multi_nodes, Mapping):
seq = []
for block_name, tag in multi_nodes.items():
seq.append((block_name, tag))
return seq
raise RuntimeError("Bad connection point definition")
def add_connection(
self,
start: SubBlockDef, end: SubBlockDef,
group: Optional[str] = None,
**attrs: AttributeMap
) -> None:
"""Create a connection between two blocks.
You must supply the start and the end of the connection either
as block names or pairs of block name and cell tag.
Provide the name of a group if you want the connection to
belong to a group.
See ConnectionAttributes for a list of available attributes.
"""
cdef = ConnectionDef(start, end, group, **attrs)
self._co
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
资源分类:Python库 所属语言:Python 资源全名:orthogram-0.5.0.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
资源推荐
资源详情
资源评论
收起资源包目录
orthogram-0.5.0.tar.gz (32个子文件)
orthogram-0.5.0
PKG-INFO 2KB
README.rst 1KB
pyproject.toml 847B
orthogram
functions.py 3KB
define
build.py 18KB
attributes.py 21KB
__init__.py 461B
diagram.py 27KB
arrange
layout.py 1KB
route.py 21KB
label.py 5KB
__init__.py 156B
net.py 16KB
refine.py 25KB
draw
functions.py 3KB
blocks.py 5KB
drawing.py 17KB
labels.py 5KB
grid.py 19KB
names.py 597B
bands.py 6KB
shapes.py 6KB
__init__.py 75B
connections.py 10KB
containers.py 4KB
debug.py 360B
util.py 513B
__main__.py 268B
__init__.py 287B
geometry.py 10KB
LICENSE 34KB
setup.py 2KB
共 32 条
- 1
资源评论
挣扎的蓝藻
- 粉丝: 14w+
- 资源: 15万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功