"""
dploy script is an attempt at creating a clone of GNU stow that will work on
Windows as well as *nix
"""


import sys
assert sys.version_info >= (3, 3), 'Requires Python 3.3 or Greater'
import os
import pathlib

from dploy.util import resolve_abs_path
import dploy.command as command

class Stow():
    def __init__(self, source, dest):
        self.source_input = source
        self.dest_input = dest
        self.source_absolute = resolve_abs_path(source)
        self.dest_absolute = resolve_abs_path(dest)
        self.source = pathlib.Path(self.source_absolute)
        self.dest = pathlib.Path(self.dest_absolute)

        self.commands = []
        self.basic(self.source, self.dest)
        self.execute_commands()

    def execute_commands(self):
        for command in self.commands:
            print(command)
            command.execute()

    def unfold(self, dest):
        children = []

        for child in dest.iterdir():
            child_relative = _get_pathlib_relative_path(child.resolve(), dest.parent)
            children.append(child)

        self.commands.append(command.UnLink(dest))
        self.commands.append(command.MakeDirectory(dest))
        self.collect_commands(children, dest, is_unfolding=True)


    def basic(self, source, dest):
        assert source.is_dir()
        assert source.is_absolute()
        assert dest.is_absolute()

        src_files = []

        for src_file in source.iterdir():
            src_files.append(src_file)

        self.collect_commands(src_files, dest)

    def collect_commands(self, sources, dest, is_unfolding=False):
        for source in sources:
            dest_path = dest / pathlib.Path(source.name)
            source_relative = _get_pathlib_relative_path(source,
                                                         dest_path.parent)
            if dest_path.exists():
                if _is_pathlib_same_file(dest_path, source): #TODO should we also check if this is a symlink
                    if is_unfolding:
                        self.commands.append(command.SymbolicLink(source_relative, dest_path))
                    else:
                        msg = "Link: Already Linked {dest} => {source}"
                        print(msg.format(source=source, dest=dest_path))
                elif dest_path.is_dir() and source.is_dir:
                    if dest_path.is_symlink():
                        self.unfold(dest_path)
                    self.basic(source, dest_path)

                else:
                    msg = "Abort: {file} Already Exists"
                    print(msg.format(file=dest_path))
                    sys.exit(1)
            elif not dest_path.parent.exists():
                    msg = "Abort: {dest} Not Found"
                    print(msg.format(dest=dest_path))
                    sys.exit(1)
            else:
                self.commands.append(command.SymbolicLink(source_relative, dest_path))



def stow(source, dest):
    """
    sub command stow
    """

    Stow(source, dest)


def link(source, dest):
    """
    sub command link
    """
    source_absolute = resolve_abs_path(source)
    dest_absolute = resolve_abs_path(dest)

    _link_absolute_paths(pathlib.Path(source_absolute),
                        pathlib.Path(dest_absolute))


def _link_absolute_paths(source, dest):
    assert source.is_absolute()
    assert dest.is_absolute()
    assert source.exists()

    src_file_relative = _get_pathlib_relative_path(source,
                                                   dest.parent)
    try:
        dest.symlink_to(src_file_relative)
        msg = "Link: {dest} => {source}"
        print(msg.format(source=src_file_relative, dest=dest))

    except FileExistsError:
        if _is_pathlib_same_file(dest, source):
            msg = "Link: Already Linked {dest} => {source}"
            print(msg.format(source=src_file_relative, dest=dest))

        else:
            msg = "Abort: {file} Already Exists"
            print(msg.format(file=dest))
            sys.exit(1)

    except FileNotFoundError:
        msg = "Abort: {dest} Not Found"
        print(msg.format(dest=dest))
        sys.exit(1)


def _is_pathlib_same_file(file1, file2):
    # Note python 3.5 supports pathlib.Path.samefile(file)
    return file1.resolve() == file2.resolve()


def _get_pathlib_relative_path(path, start_at):
    return pathlib.Path(os.path.relpath(path.__str__(), start_at.__str__()))

@@ -150,17 +150,17 @@
 (3, 3), 
-'
+%22
 Requires
@@ -185,9 +185,9 @@
 ater
-'
+%22
 %0Aimp
@@ -338,29 +338,24 @@
 t):%0A        
-self.
 source_input
@@ -368,29 +368,24 @@
 rce%0A        
-self.
 dest_input =
@@ -390,24 +390,25 @@
  = dest%0A
+%0A
         
 self.sou
@@ -395,29 +395,24 @@
 st%0A%0A        
-self.
 source_absol
@@ -436,24 +436,30 @@
 _path(source
+_input
 )%0A        se
@@ -452,29 +452,24 @@
 ut)%0A        
-self.
 dest_absolut
@@ -489,25 +489,32 @@
 bs_path(dest
-)
+_input)%0A
 %0A        sel
@@ -511,26 +511,29 @@
        s
-elf.s
 ource
+_pathlib
  = pathl
@@ -540,21 +540,16 @@
 ib.Path(
-self.
 source_a
@@ -565,25 +565,28 @@
         
-self.dest
+dest_pathlib
  = pathl
@@ -593,21 +593,16 @@
 ib.Path(
-self.
 dest_abs
@@ -660,29 +660,35 @@
 ic(s
-elf.source, self.dest
+ource_pathlib, dest_pathlib
 )%0A  
@@ -714,24 +714,25 @@
 commands()%0A%0A
+%0A
     def exec
@@ -843,24 +843,25 @@
 .execute()%0A%0A
+%0A
     def unfo
@@ -1521,24 +1521,25 @@
 les, dest)%0A%0A
+%0A
     def coll
@@ -1920,56 +1920,8 @@
 ce):
- #TODO should we also check if this is a symlink
 %0A   
@@ -4157,11 +4157,12 @@
  # N
-ote
+OTE:
  pyt

Change some Stow() member vars to non-member vars