#!/usr/bin/env python3
# encoding: utf-8

import argparse
import shutil
import os

DESCRIPTION = """caca - Copy And Convert Audio.

A very simple one-shot parallel audio converter that behaves a lot like cp"""


def detect_utils():
    tools = ['flac', 'lame', 'oggenc']
    for tool in tools:
        if not shutil.which(tool):
            raise FileNotFoundError("'{}' was not found on your system but is required".format(tool))


def handle_path(src, target):
    for entry in os.walk(src):
        for file in entry[2]:
            if os.path.splitext(file)[1].tolower() == ".flac":
                #os.makedirs(
                # encoding magic
                if args.verbose:
                    print("'{}' -> '{}'".format(src, target))
            else:
                dst = target
                #os.makedirs(dst, exist_ok=True)
                if args.verbose:
                    print("'{}' -> '{}'".format(src, target))


def main():
    parser = argparse.ArgumentParser(description=DESCRIPTION)
    parser.add_argument("sources", metavar="SOURCE", nargs='+', help="input path")
    parser.add_argument("target", metavar="TARGET", help="output path")
    parser.add_argument("-v", "--verbose", help="print progress output", action="store_true")
    parser.add_argument("-R", "-r", "--recursive", help="copy directories recursively", action="store_true")
    parser.add_argument("-a", "--archive", help="same as --recursive but preserves attributes", action="store_true")
    args = parser.parse_args()

    detect_utils()

    # Handle case where we have more than one sources but target is not a directory
    if len(args.sources) > 1 and os.path.isfile(args.target):
        raise RuntimeException("target '{}' is not a directory".format(args.target))

    # Handle case where the source is a directory but target is a file
    if os.path.isdir(args.sources[0]) and os.path.isfile(args.target):
        raise RuntimeException("cannot overwrite non-directory '{}' with directory '{}'".format(args.target, args.sources[0]))

    for src in args.sources:
        handle_path(src)

if __name__ == '__main__':
    main()

@@ -74,16 +74,30 @@
 mport os
+%0Aimport shutil
 %0A%0ADESCRI
@@ -218,16 +218,28 @@
  cp%22%22%22%0A%0A
+args = None%0A
 %0Adef det
@@ -456,27 +456,28 @@
 )%0A%0A%0Adef 
-handle_path
+convert_flac
 (src, ta
@@ -491,79 +491,279 @@
     
-for entry in os.walk(src):%0A        for file in entry%5B2%5D:%0A            if
+pass%0A%0Adef copy_raw(src, target):%0A    shutil.copyfile(src, target)%0A%0Adef convert_ogg(src, target):%0A    pass%0A%0Aextensions = %7B%0A    'flac': convert_flac,%0A    'mp3': copy_raw,%0A    'ogg': convert_ogg%0A%7D%0A%0Adef handle_file(src, target):%0A    # get the file type by extension%0A    ext =
  os.
@@ -780,110 +780,277 @@
 ext(
-file
+src
 )%5B1%5D.
-to
 lower()
- == %22.flac%22:%0A                #os.makedirs(%0A                # encoding magic%0A        
+%0A    if ext: ext = ext%5B1:%5D # cut away the '.'%0A%0A    # create target directory%0A    target_dir = target if os.path.isdir(target) else os.path.dirname(target)%0A    if not os.path.exists(target_dir):%0A        os.makedirs(target_dir)%0A%0A%0A    if ext in extensions:%0A
     
@@ -1078,39 +1078,41 @@
             
-       
- 
 print(%22
+%5Bconvert%5D 
 '%7B%7D' -%3E '%7B%7D'
@@ -1134,24 +1134,25 @@
 arget))%0A
+%0A
         
     else
@@ -1147,102 +1147,162 @@
     
-    else:%0A                dst = target%0A                #os.makedirs(dst, exist_ok=True
+extensions%5Bext%5D(src, target)%0A%0A    else:%0A        if args.skip_unknown:%0A            if args.verbose:%0A                print(%22%5Bskip%5D '%7B%7D'%22.format(src)
 )%0A      
     
@@ -1293,24 +1293,32 @@
 src))%0A      
+ 
+ else:%0A
           if
@@ -1307,32 +1307,34 @@
 else:%0A          
+  
 if args.verbose:
@@ -1350,27 +1350,30 @@
         
-    
 print(%22
+%5Bcopy%5D 
 '%7B%7D' -%3E 
@@ -1396,24 +1396,278 @@
 , target))%0A%0A
+            copy_raw(src, target)%0A%0A%0Adef handle_path(src, target):%0A    if os.path.isdir(src):%0A        for entry in os.listdir(src):%0A            handle_path(os.path.join(src, entry), os.path.join(target, entry))%0A    else:%0A        handle_file(src, target)%0A%0A
 %0Adef main():
@@ -2200,24 +2200,176 @@
 tore_true%22)%0A
+    parser.add_argument(%22-s%22, %22--skip-unknown%22, help=%22do not copy unknown file types%22, action=%22store_true%22)%0A%0A    # overwrite args array%0A    global args%0A
     args = p
@@ -2962,16 +2962,29 @@
 path(src
+, args.target
 )%0A%0Aif __

Fix file walking