Skip to content

Commit

Permalink
rename the stream folders not the recording dir
Browse files Browse the repository at this point in the history
  • Loading branch information
beasteers committed Nov 7, 2023
1 parent c13c671 commit 484b836
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
2 changes: 1 addition & 1 deletion redis_record/storage/recorder/zip.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ def ensure_writer(self, record_name, force=False):
if self.writer is None:
self.writer = {}
self.recording_dir = os.path.join(self.out_dir, record_name)
move_with_suffix(self.recording_dir)
os.makedirs(self.recording_dir, exist_ok=True)
log.info("Created Recorder: %s", self.recording_dir)
return self

def ensure_channel(self, channel):
if channel not in self.writer:
out_dir = os.path.join(self.recording_dir, channel)
move_with_suffix(out_dir, prefix='_')
self.writer[channel] = ZipWriter(out_dir, self.max_len, self.max_size)


Expand Down
16 changes: 13 additions & 3 deletions redis_record/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
import datetime
from .config import *

import logging
log = logging.getLogger(__name__)
log.setLevel(logging.INFO)


def maybe_encode(s: str|bytes, encoding='utf-8'):
'''If the input is str, encode as utf-8 (or other).'''
Expand Down Expand Up @@ -103,13 +107,19 @@ def get_recording_filename(name, recording_dir=RECORDING_DIR):
return os.path.join(recording_dir, f'{name}.mcap')


def move_with_suffix(src):
def move_with_suffix(src, prefix=None, suffix=None, has_ext=None):
if os.path.exists(src):
base, ext = os.path.splitext(src)
has_ext = os.path.isfile(src) if has_ext is None else has_ext
if has_ext:
base, ext = os.path.splitext(src)
else:
base, ext = src, ''
base = f"{prefix or ''}{base}{suffix or ''}"

i = 1
dest = f"{base}_{i}{ext}"
while os.path.exists(dest):
i += 1
dest = f"{base}_{i}{ext}"
print("\nMOVING", src, 'to', dest)
log.info("MOVING %s to %s", src, dest)
os.rename(src, dest)

0 comments on commit 484b836

Please sign in to comment.