from pant.pant import ant, project,  ant_attr_map, antarget

    

def pant_task_examples():
    """ Examples of using PAnt to invoke various Ant tasks """
    # alternatively, "antask" variable is already pre-set    
    
    # Create a property using named arguments
    ant.property(name="target.dir", value="test_prop")
    
    # Create a property using positional arguments (this currently only works for a few select tasks)
    ant.property("target.dir", "test_dir")
 
    # Run mkdir task using positional arguments
    ant.mkdir("${target.dir}")
    
    # more complex example using copy task 
    # note how nesting is implemented using nested dicts
    ant.copy( todir="${target.dir}", overwrite="true", 
               fileset=dict( dir=".", includes="*.xml" ), 
               filterchain=dict( expandproperties={} ) )
    
    # We can also build tasks from multiple dicts defined in different places
    # this is useful for code reuse
    xml_fileset={"dir":".", "includes":"*.xml"}
    expand_filter=dict( expandproperties={} )
    ant.copy( todir="${target.dir}", 
               overwrite="true", 
               filterchain=expand_filter, 
               fileset=xml_fileset)
                
    # we can also use lists to represent elements of the same type (multiple filesets)
    prop_fileset={"dir":".", "includes":"*.properties"}
    ant.copy( todir="${target.dir}", 
               overwrite="true", 
               filterchain=expand_filter, 
               fileset=[xml_fileset, prop_fileset])

    
    # Alternatively, we can use suffixes to distinguish 
    # different elements of the same name. 
    # antask ignores part of the name following "_". 
    ant.copy( todir="${target.dir}", 
               overwrite="true", 
               filterchain=expand_filter, 
               fileset_xml=xml_fileset, 
               fileset_prop=prop_fileset)
    
    # Here is how to extend the support for positional arguments  
    # Simply provide a list with Ant attribute names
    ant_attr_map["basename"]=["file", "property"]
    ant.basename("test_dir", "file.name.prop")
    ant.echo("${file.name.prop}")

    # Try manifest task -- it uses "addConfigured" methods
    ant.manifest(file="test_manifest.mf", attribute_1 = dict( name="Built-By", value="pant") )

def create_targets():
    """
    Examples of creating targets from python
    """
    
    # Create a target by specifying a code block
    # arguments match Ant "target" attributes: name, desc (or description), unless, if (you have to use if_), etc
    # You can optionally provide Ant project name if you want to override a target that was
    # created in a certain Ant project.
    antarget(name="target1", code="import pant_examples\ntarget1()", 
                      if_="lib.dir",  desc="Python target1", project_name="python.proj")
                      
    
    # Create a target by simply pointing to a function
    # Function's name is used as target's name and docstring 
    # is used for description (run "ant -p" to see it)
    # 'depends' can list any Ant target, not just the one defined in python  
    antarget(func=target2, depends="init, target1")
    
    # Create a target using a method
    targets=TargetClass()
    # We can pass a function as a positional argument
    # you can override name and description and provide other keyword arguments
    # note how you can pass a function to "depends" (this only works for a single dependency)
    antarget(targets.pclasstarget, name="target3", if_="lib.dir", depends=target2)


def target1():
    ant.echo("Executing target1 function")
    

def target2():
    """Python target 2"""
    ant.echo("Executing target2 function")

# We can also create targets using classes

class TargetClass(object):
    def pclasstarget(self):
        """Target defined in a class"""
        ant.echo("Executing pclasstarget method")

