Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Close file handle #21

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions tools/timeseries2csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import socket
import struct
import sys
from _csv import Dialect
from datetime import datetime, timedelta
from tempfile import mkstemp
from typing import Dict, Optional
Expand Down Expand Up @@ -43,6 +44,14 @@ def datetime_range(start: datetime, end: datetime, delta: relativedelta):
be_quiet: bool = False


class RFCDialect(Dialect):
delimiter = ';'
doublequote = True
lineterminator = '\r\n'
quotechar = '"'
escapechar = ''
quoting = csv.QUOTE_MINIMAL

def cprint(text: str) -> None:
'''
Custom print to output to stderr if quiet is not set.
Expand Down Expand Up @@ -291,7 +300,7 @@ def timeseries2csv(host: str, port: int, output: Optional[str], header_format: b
iter_end = True

if output is None:
output = f'data_{resolution}_{ts_start.isoformat("T")}.csv'
output = f'data_{resolution}_{ts_start.strftime("%Y%m%d_%H%M%S")}.csv'

if output == '-':
fd = sys.stdout
Expand All @@ -304,10 +313,11 @@ def timeseries2csv(host: str, port: int, output: Optional[str], header_format: b
fd.write('#datatype dateTime,field,field,field,field,field,field,field,field,field,field,field,field,field,'
'field,field,field,field,field,field,field,field,field\n')

writer = csv.writer(fd)

names = [oid.name.replace(name_prefix, '').replace('_log_ts', '') for oid in oids]

csv.register_dialect('rfc', RFCDialect)
writer = csv.writer(fd, dialect='rfc')

if header_format == 'simple':
writer.writerow(['timestamp'] + names)

Expand All @@ -317,7 +327,7 @@ def timeseries2csv(host: str, port: int, output: Optional[str], header_format: b

if output != '-':
fd.flush()
os.fsync(fd.fileno())
fd.close()
try:
os.rename(filepath, output)
except OSError as exc:
Expand Down