11from __future__ import annotations
2- import argparse
3- import sys
2+ import argparse
43import time
5- import json
64from .sensors import TemperatureSensor
75from .orchestrator import run_stream
8- import os
6+ from . io import jsonl_writer , csv_writer
97
108def _stdout_writer ():
11- def _w (sample : dict ):
12- sys .stdout .write (json .dumps (sample ) + "\n " )
13- return _w
9+ return jsonl_writer (None )
1410
1511def _jsonl_file_writer (path : str ):
16- d = os .path .dirname (path )
17- if d :
18- os .makedirs (d , exist_ok = True )
19- f = open (path , "a" , buffering = 1 , encoding = "utf-8" )
20- def _w (sample : dict ):
21- f .write (json .dumps (sample ) + "\n " )
22- return _w
12+ return jsonl_writer (path )
13+
14+ def _csv_file_writer (path : str ):
15+ return csv_writer (path )
2316
2417def _make_writer_map (out_args : list [str ] | None ):
2518 """
26- out args format: ["temp=out/temp.jsonl", "vibration=out/vib.jsonl", "*=stdout"]
19+ out args format: ["temp=out/temp.jsonl", "vibration=out/vib.csv", "*=stdout"]
20+ wybór formatu po rozszerzeniu pliku: .jsonl -> JSONL, .csv -> CSV
2721 """
2822 mapping = {}
2923 if not out_args :
@@ -35,14 +29,16 @@ def _make_writer_map(out_args: list[str] | None):
3529 if dest == "stdout" :
3630 mapping [key ] = _stdout_writer ()
3731 else :
38- mapping [key ] = _jsonl_file_writer (dest )
32+ if dest .lower ().endswith (".csv" ):
33+ mapping [key ] = _csv_file_writer (dest )
34+ else :
35+ mapping [key ] = _jsonl_file_writer (dest )
3936 return mapping
4037
4138def main (argv = None ):
4239 p = argparse .ArgumentParser (prog = "dummy-sensors" )
4340 sub = p .add_subparsers (dest = "cmd" , required = True )
4441
45- # generate
4642 gen = sub .add_parser ("generate" , help = "single temperature stream to stdout/file" )
4743 gen .add_argument ("--rate" , type = float , default = 5.0 )
4844 gen .add_argument ("--duration" , type = float , default = 5.0 )
@@ -52,13 +48,14 @@ def main(argv=None):
5248 gen .add_argument ("--noise" , type = float , default = 0.5 )
5349 gen .add_argument ("--jsonl" , type = str , default = None )
5450
55- # run
56- run = sub .add_parser ("run" , help = "multi-device/multi-sensor run via spec" )
51+ run = sub .add_parser ("run" , help = "multi-device/multi-sensor run via spec or config" )
5752 run .add_argument ("--rate" , type = float , default = 5.0 )
5853 run .add_argument ("--duration" , type = float , default = None )
5954 run .add_argument ("--count" , type = int , default = None )
60- run .add_argument ("--spec" , type = str , required = True , help = 'e.g. "device=A: temp*2,vibration*1; device=B: temp*3 "' )
55+ run .add_argument ("--spec" , type = str , help = 'e.g. "device=A: temp*2,vibration*1"' )
6156 run .add_argument ("--out" , action = "append" , help = 'mapping like "temp=out/temp.jsonl", "*=stdout"' , default = None )
57+ run .add_argument ("--partition-by" , choices = ["none" ,"type" ,"device" ], default = "none" )
58+ run .add_argument ("--config" , type = str , help = "YAML config path (overrides --spec/--out)" )
6259
6360 args = p .parse_args (argv )
6461
@@ -77,8 +74,15 @@ def main(argv=None):
7774 while time .perf_counter () < t_next :
7875 time .sleep (min (0.002 , t_next - time .perf_counter ()))
7976 else :
80- writers = _make_writer_map (args .out )
81- run_stream (args .spec , rate_hz = args .rate , duration_s = args .duration , total_count = args .count , writer_for_type = writers )
77+ # jeśli podano --config, jedziemy konfiguracją YAML (sekcja 4)
78+ if args .config :
79+ from .config import run_from_config
80+ run_from_config (args .config )
81+ return
8282
83- if __name__ == "__main__" :
84- main ()
83+ # spec + out
84+ writers = _make_writer_map (args .out )
85+ if not args .spec :
86+ p .error ("Provide --spec or --config" )
87+ run_stream (args .spec , rate_hz = args .rate , duration_s = args .duration , total_count = args .count ,
88+ writer_for_type = writers , partition_by = args .partition_by )
0 commit comments