Skip to content

Commit

Permalink
Converted many strings to unicode strings to reduce encoding errors.
Browse files Browse the repository at this point in the history
  • Loading branch information
Vultaire committed Jun 15, 2012
1 parent 2cd6bc3 commit 495f077
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 25 deletions.
26 changes: 13 additions & 13 deletions r21buddy/oggpatch.py
Expand Up @@ -209,7 +209,7 @@ def get_packets(pages):
# I *think* the patch should still work - the patch # I *think* the patch should still work - the patch
# operates on the page level, but this error is at the # operates on the page level, but this error is at the
# underlying packet; it shouldn't matter. # underlying packet; it shouldn't matter.
logger.error("WARNING: Unterminated packet detected, ignoring.") logger.error(u"WARNING: Unterminated packet detected, ignoring.")


self.pages = list(pages) # Needed to recreate stream with updated final page self.pages = list(pages) # Needed to recreate stream with updated final page


Expand Down Expand Up @@ -249,8 +249,8 @@ def patch_length(self, new_length, verbose=True):


last_page = self.pages[-1] last_page = self.pages[-1]
if verbose: if verbose:
logger.info("Current granule position: {0}".format(last_page.granule_pos)) logger.info(u"Current granule position: {0}".format(last_page.granule_pos))
logger.info("New granule position: {0}".format(new_granule_pos)) logger.info(u"New granule position: {0}".format(new_granule_pos))


# Replace last page with patched version # Replace last page with patched version
new_page_data = last_page.get_data_with_new_length(new_granule_pos) new_page_data = last_page.get_data_with_new_length(new_granule_pos)
Expand Down Expand Up @@ -460,49 +460,49 @@ def patch_file(input_file, target_length=TARGET_LENGTH,
output_file=None, verbose=True): output_file=None, verbose=True):
patched = False patched = False
if target_length < 0: if target_length < 0:
logger.error("Bad length ({0}), not patching file".format(target_length)) logger.error(u"Bad length ({0}), not patching file".format(target_length))
return return
with open(input_file, "rb") as infile: with open(input_file, "rb") as infile:
bitstreams = list(get_bitstreams(infile)) bitstreams = list(get_bitstreams(infile))
for bitstream in bitstreams: for bitstream in bitstreams:
length = bitstream.get_length() length = bitstream.get_length()
if verbose: if verbose:
logger.info("Current file length: {0}".format(pprint_time(length))) logger.info(u"Current file length: {0}".format(pprint_time(length)))
logger.info("Target file length: {0}".format(pprint_time(target_length))) logger.info(u"Target file length: {0}".format(pprint_time(target_length)))
if length > target_length: if length > target_length:
patched = True patched = True
bitstream.patch_length(target_length, verbose=verbose) bitstream.patch_length(target_length, verbose=verbose)
if patched: if patched:
if output_file is None: if output_file is None:
output_file = input_file output_file = input_file
if verbose: if verbose:
logger.info("Writing patched file to {0}".format(output_file)) logger.info(u"Writing patched file to {0}".format(output_file))
with open(output_file, "wb") as outfile: with open(output_file, "wb") as outfile:
for bitstream in bitstreams: for bitstream in bitstreams:
bitstream.write_to_file(outfile) bitstream.write_to_file(outfile)
elif verbose: elif verbose:
logger.info("Not patching file; file already appears to be {0} or shorter.".format( logger.info(u"Not patching file; file already appears to be {0} or shorter.".format(
pprint_time(target_length))) pprint_time(target_length)))


def check_file(input_file, target_length, verbose=True): def check_file(input_file, target_length, verbose=True):
if target_length < 0: if target_length < 0:
logger.error("Bad length ({0}), not patching file".format(target_length)) logger.error(u"Bad length ({0}), not patching file".format(target_length))
return return
with open(input_file, "rb") as infile: with open(input_file, "rb") as infile:
bitstreams = list(get_bitstreams(infile)) bitstreams = list(get_bitstreams(infile))
for bitstream in bitstreams: for bitstream in bitstreams:
length = bitstream.get_length() length = bitstream.get_length()
if verbose: if verbose:
logger.info("Current file length: {0}".format(pprint_time(length))) logger.info(u"Current file length: {0}".format(pprint_time(length)))
logger.info("Target file length: {0}".format(pprint_time(target_length))) logger.info(u"Target file length: {0}".format(pprint_time(target_length)))


if length > target_length: if length > target_length:
logger.error("File exceeds {0}. Length: {1}".format( logger.error(u"File exceeds {0}. Length: {1}".format(
pprint_time(target_length), pprint_time(length))) pprint_time(target_length), pprint_time(length)))
return False return False
else: else:
if verbose: if verbose:
logger.info("File passes length check.") logger.info(u"File passes length check.")
continue continue


return True return True
Expand Down
29 changes: 17 additions & 12 deletions r21buddy/r21buddy.py
Expand Up @@ -29,21 +29,26 @@ def parse_args():
return ap.parse_args() return ap.parse_args()


def create_target_dir_structure(target_dir, verbose=False): def create_target_dir_structure(target_dir, verbose=False):
song_dir = os.path.join(target_dir, "In The Groove 2", "Songs") song_dir = os.path.join(target_dir, u"In The Groove 2", u"Songs")
if not os.path.exists(song_dir): if not os.path.exists(song_dir):
os.makedirs(os.path.join(target_dir, "In The Groove 2", "Songs")) os.makedirs(os.path.join(target_dir, u"In The Groove 2", u"Songs"))
if verbose: if verbose:
logger.info("Created directory: {0}".format(song_dir)) logger.info(u"Created directory: {0}".format(song_dir))
elif not os.path.isdir(song_dir): elif not os.path.isdir(song_dir):
raise Exception("Target path is not a directory", song_dir) raise Exception("Target path is not a directory", song_dir)
else: else:
if verbose: if verbose:
logger.info("Directory already exists: {0}".format(song_dir)) logger.info(u"Directory already exists: {0}".format(song_dir))


def copy_songs(input_path, target_dir, verbose=False): def copy_songs(input_path, target_dir, verbose=False):
logger.info(u"INPUT DIR: {0}".format(repr(input_path)))
all_files = [os.path.join(input_path, f) for f in os.listdir(input_path)] all_files = [os.path.join(input_path, f) for f in os.listdir(input_path)]
dirs = [f for f in all_files if os.path.isdir(f)] dirs = [f for f in all_files if os.path.isdir(f)]


oddballs = [f for f in all_files if (not os.path.isdir(f)) and (not os.path.isfile(f))]
if len(oddballs) > 0:
logger.info(u"ODDBALLS: {0}".format(repr(oddballs)))

# If directories present: recurse into them. # If directories present: recurse into them.
if len(dirs) > 0: if len(dirs) > 0:
for d in dirs: for d in dirs:
Expand All @@ -66,22 +71,22 @@ def copy_songs(input_path, target_dir, verbose=False):
mp3_exists = any(f.endswith(".mp3") for f in all_files) mp3_exists = any(f.endswith(".mp3") for f in all_files)


if not sm_exists: if not sm_exists:
logger.error("Directory {0}: Could not find .sm; only .dwi was found. Skipping.".format(input_path)) logger.error(u"Directory {0}: Could not find .sm; only .dwi was found. Skipping.".format(input_path))
return return
if not ogg_exists: if not ogg_exists:
if any(f.endswith(".mp3") for f in all_files): if any(f.endswith(".mp3") for f in all_files):
logger.error("Directory {0}: Could not find .ogg; only .mp3 was found. Skipping.".format(input_path)) logger.error(u"Directory {0}: Could not find .ogg; only .mp3 was found. Skipping.".format(input_path))
else: else:
logger.error("Directory {0}: Could not find .ogg. Skipping.".format(input_path)) logger.error(u"Directory {0}: Could not find .ogg. Skipping.".format(input_path))
return return


# We are compatible. Check for destination directory; complain # We are compatible. Check for destination directory; complain
# LOUDLY if not able to create it. # LOUDLY if not able to create it.
song_dir_name = os.path.split(input_path)[-1] song_dir_name = os.path.split(input_path)[-1]
target_song_dir = os.path.join( target_song_dir = os.path.join(
target_dir, "In The Groove 2", "Songs", song_dir_name) target_dir, u"In The Groove 2", u"Songs", song_dir_name)
if os.path.exists(target_song_dir): if os.path.exists(target_song_dir):
logger.error("ERROR: {0} already exists; not copying files from {1}.".format(target_song_dir, input_path)) logger.error(u"ERROR: {0} already exists; not copying files from {1}.".format(target_song_dir, input_path))
return return


os.makedirs(target_song_dir) os.makedirs(target_song_dir)
Expand All @@ -90,19 +95,19 @@ def copy_songs(input_path, target_dir, verbose=False):
dest_file = os.path.join( dest_file = os.path.join(
target_song_dir, os.path.basename(src_file)) target_song_dir, os.path.basename(src_file))
if verbose: if verbose:
logger.info("Copying: {0}\n to: {1}".format(src_file, dest_file)) logger.info(u"Copying: {0}\n to: {1}".format(src_file, dest_file))
shutil.copyfile(src_file, dest_file) shutil.copyfile(src_file, dest_file)


def patch_length(target_dir, verbose=False): def patch_length(target_dir, verbose=False):
song_dir = os.path.join(target_dir, "In The Groove 2", "Songs") song_dir = os.path.join(target_dir, u"In The Groove 2", u"Songs")
all_files = [os.path.join(song_dir, f) for f in os.listdir(song_dir)] all_files = [os.path.join(song_dir, f) for f in os.listdir(song_dir)]
dirs = [d for d in all_files if os.path.isdir(d)] dirs = [d for d in all_files if os.path.isdir(d)]
for song_dir in dirs: for song_dir in dirs:
song_files = (os.path.join(song_dir, f) for f in os.listdir(song_dir)) song_files = (os.path.join(song_dir, f) for f in os.listdir(song_dir))
ogg_files = (f for f in song_files if f.endswith(".ogg")) ogg_files = (f for f in song_files if f.endswith(".ogg"))
for ogg_file in ogg_files: for ogg_file in ogg_files:
if verbose: if verbose:
logger.info("Patching file: {0}".format(ogg_file)) logger.info(u"Patching file: {0}".format(ogg_file))
oggpatch.patch_file(ogg_file, verbose=verbose) oggpatch.patch_file(ogg_file, verbose=verbose)


def run(target_dir, input_paths, length_patch=True, verbose=False, ext_logger=None): def run(target_dir, input_paths, length_patch=True, verbose=False, ext_logger=None):
Expand Down

0 comments on commit 495f077

Please sign in to comment.