Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/tools/contributed/sumopy/agilepy/lib_base/exports.py
169689 views
1
# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
2
# Copyright (C) 2016-2025 German Aerospace Center (DLR) and others.
3
# SUMOPy module
4
# Copyright (C) 2012-2021 University of Bologna - DICAM
5
# This program and the accompanying materials are made available under the
6
# terms of the Eclipse Public License 2.0 which is available at
7
# https://www.eclipse.org/legal/epl-2.0/
8
# This Source Code may also be made available under the following Secondary
9
# Licenses when the conditions for such availability set forth in the Eclipse
10
# Public License 2.0 are satisfied: GNU General Public License, version 2
11
# or later which is available at
12
# https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
13
# SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
14
15
# @file exports.py
16
# @author Joerg Schweizer
17
# @date 2012
18
19
20
import os
21
import sys
22
import types
23
import numpy as np
24
import agilepy.lib_base.classman as cm
25
import agilepy.lib_base.arrayman as am
26
27
28
from agilepy.lib_base.processes import Process
29
30
#global IS_EXCEL
31
32
try:
33
from openpyxl import Workbook
34
35
IS_EXCEL = True
36
37
38
except:
39
print 'WARNING: No Exel export possible. Install openpyxl python package.'
40
IS_EXCEL = False
41
42
# if 'Workbook' in dir():
43
# print 'detected Exel'
44
# IS_EXCEL = True
45
# else:
46
# IS_EXCEL = False
47
48
print 'IS_EXCEL', IS_EXCEL
49
50
51
def export_excel(filepath, obj, ids=None, attrconfigs=None, groupnames=None,
52
is_header=True, is_ident=False, is_timestamp=True,
53
show_parentesis=True, name_id='ID', is_export_not_save=True):
54
55
print 'export_excel' # ,attrconfigs,'groupnames',groupnames,
56
57
wb = Workbook()
58
if (wb.worksheets) >= 1:
59
ws = wb.worksheets[0]
60
ws.title = "Scalar attributes"
61
else:
62
ws = wb.create_sheet(title="Scalar attributes")
63
64
attrsman = obj.get_attrsman()
65
if attrconfigs is None:
66
attrconfigs = attrsman.get_configs(is_all=False, filtergroupnames=groupnames)
67
#attrconfigs_cols = attrsman.get_configs(is_all = False, structs = cm.STRUCTS_COL, filtergroupnames = groupnames)
68
ind_row = 1
69
70
for attrconf in attrconfigs:
71
# print ' attrconfig', attrconf.attrname,attrconf.struct
72
if (attrconf.is_save() | is_export_not_save) & (attrconf.struct in cm.STRUCTS_SCALAR):
73
# print ' attrconf.attrname',attrconf.attrname,'val',attrconf.get_value()
74
cell = ws.cell(row=ind_row, column=1)
75
cell.value = attrconf.attrname+attrconf.format_unit(show_parentesis)
76
cell = ws.cell(row=ind_row, column=2)
77
value = attrconf.get_value()
78
tv = type(value)
79
if tv in cm.NODATATYPES:
80
value = str(value)
81
elif tv in (types.ListType, types.TupleType, np.ndarray):
82
value = str(value)
83
84
cell.value = value
85
#d.value = 3.14
86
# print " cell",ind_row,'value',cell.value
87
ind_row += 1
88
89
# check if attributes are all column attribute and indicted for save
90
91
ws = wb.create_sheet(title="Table attributes")
92
if hasattr(obj, 'get_ids'):
93
if ids is None:
94
ids = obj.get_ids()
95
96
attrconfigs_checked = []
97
for attrconf in attrconfigs:
98
if (attrconf.is_save() | is_export_not_save) & (attrconf.struct in cm.STRUCTS_COL):
99
attrconfigs_checked.append(attrconf)
100
101
# first table row
102
ind_row = 1
103
ind_col = 1
104
cell = ws.cell(row=ind_row, column=ind_col)
105
cell.value = name_id
106
107
for attrconf in attrconfigs_checked:
108
109
cell = ws.cell(row=ind_row, column=ind_col)
110
cell.value = attrconf.format_symbol(show_parentesis=show_parentesis)
111
ind_col += 1
112
113
# rest
114
for _id in ids:
115
ind_row += 1
116
ind_col = 1
117
cell = ws.cell(row=ind_row, column=ind_col)
118
cell.value = _id
119
120
for attrconf in attrconfigs_checked:
121
122
cell = ws.cell(row=ind_row, column=ind_col)
123
value = attrconf[_id]
124
# print ' attrconfig', attrconf.attrname,type(value)
125
tv = type(value)
126
mt = attrconf.metatype
127
if tv in cm.NODATATYPES:
128
value = str(value)
129
130
elif mt == 'id':
131
value = attrconf.get_linktab().format_ids([value])
132
elif tv in (types.ListType, types.TupleType, np.ndarray):
133
value = str(value)
134
cell.value = value
135
ind_col += 1
136
137
wb.save(filepath)
138
139
140
class CsvExporter(Process):
141
def __init__(self, obj, ident='csvexporter', name='CSV exporter',
142
info='Export data from a CSV file into object',
143
logger=None, **kwargs):
144
print 'CsvExporter.__init__'
145
self._init_common(ident,
146
parent=obj,
147
name=name,
148
logger=logger,
149
info=info,
150
)
151
152
attrsman = self.set_attrsman(cm.Attrsman(self))
153
self.csvfilepath = attrsman.add(cm.AttrConf('csvfilepath', kwargs.get('csvfilepath', ''),
154
groupnames=['options'],
155
perm='rw',
156
name='CSV file',
157
wildcards='CSV file (*.csv)|*.csv|*.CSV',
158
metatype='filepath',
159
info="CSV plain text file path.",
160
))
161
162
self.sep = attrsman.add(cm.AttrConf('sep', kwargs.get('sep', ","),
163
groupnames=['options', ],
164
perm='rw',
165
name='Separator',
166
info="""Seperator used in SCV file. Exampe: ; , <space key>""",
167
))
168
169
self.is_header = attrsman.add(cm.AttrConf('is_header', kwargs.get('is_header', True),
170
groupnames=['options', ],
171
perm='rw',
172
name='Make header',
173
info="""Make header with date and time.""",
174
))
175
176
self.show_parentesis = attrsman.add(cm.AttrConf('show_parentesis', kwargs.get('show_parentesis', True),
177
groupnames=['options', ],
178
perm='rw',
179
name='Show units in parenthesis',
180
info="""Show units (if any) in parenthesis.""",
181
))
182
# self.name_id = attrsman.add(cm.AttrConf('name_id', kwargs.get('name_id',"ID"),
183
# groupnames = ['options',],
184
# perm='rw',
185
# name = 'ID string',
186
# info = """String to be used to indicate IDs""",
187
# ))
188
189
def do(self):
190
print 'CsvExporter.do',
191
obj = self.parent
192
attrsman = self.get_attrsman()
193
sep = self.sep
194
#logger = self._logger
195
196
obj.export_csv(self.csvfilepath, sep=self.sep,
197
#ids = None,
198
attrconfigs=None,
199
groupnames=None,
200
show_parentesis=self.show_parentesis,
201
is_export_not_save=True, # export attr, also non save
202
# name_id='ID',
203
is_header=self.is_header)
204
205