|
# -*- coding: utf-8 -*- |
|
|
|
import os, sys |
|
import logging |
|
from datetime import datetime |
|
import re |
|
|
|
g_logger = logging.getLogger() |
|
g_logger.setLevel(logging.INFO) |
|
|
|
|
|
if __name__ == '__main__': |
|
formatter = logging.Formatter('%(message)s') |
|
fh = logging.FileHandler('taging_{:%Y-%m-%d}.log'.format(datetime.now())) |
|
ch = logging.StreamHandler(sys.stdout) |
|
fh.setFormatter(formatter) |
|
ch.setFormatter(formatter) |
|
g_logger.addHandler(fh) |
|
g_logger.addHandler(ch) |
|
g_logger.info('-' * 80) |
|
|
|
re_date = re.compile('^.*\\'+os.sep+'(\d{4})\.(\d{2})\.(\d{2}) .+$') |
|
re_cuedate = re.compile('REM DATE (.+)\n') |
|
for path, _, files in os.walk(os.getcwd()): |
|
g_logger.info('\n{}'.format(path)) |
|
for fname in [x for x in files if x.endswith('.cue')]: |
|
fullpath = os.path.join(path, fname) |
|
res = re_date.match(path) |
|
with open(fullpath, 'r') as f: |
|
cuedata = f.read() |
|
match = re_cuedate.search(cuedata) |
|
newdata = None |
|
if match and res: |
|
newdata = cuedata.replace(str(match.group(0)), 'REM DATE {}-{}-{}\n'.format(res.group(1), res.group(2), res.group(3))) |
|
g_logger.info('\t{} OK'.format(fname)) |
|
elif res: |
|
with open(fullpath, 'r') as f: |
|
for i, line in enumerate(f): |
|
if i == 0: |
|
newdata = line |
|
continue |
|
elif i == 1: |
|
newdata += 'REM DATE {}-{}-{}\n'.format(res.group(1), res.group(2), res.group(3)) |
|
newdata += line |
|
g_logger.info('\t{} OK (warning: added REM DATE)'.format(fname)) |
|
else: |
|
g_logger.info('\t{} NO DATE FOUND (error)'.format(fname)) |
|
if newdata: |
|
with open(fullpath, 'w') as f: |
|
f.write(newdata) |