## About PACGEN
The primary goal of this module is to provide a programatic way to generate Proxy Auto Config (PAC) files on the fly.  Secondarily, this library also seeks to provide a means to validate any PAC files it through unit tests.

## Installation
PACGEN can be installed with pypi: 
<br />
```
pip install pypacgen
```

Once installed, we can import our module and generate a basic .pac file
```
from pypacgen import *

...

# Create the file name and version
f = PACFile("example.pac", "1.0")

# Add a default/fall-through case
f.set_default_return(PACReturn("DIRECT"))

# Add a block to direct certain domains to our first proxy
f.add_block(
    PACBlock(
        PACReturn("PROXY", "socks.proxy.com", 8090),
        [
            ShExpMatchHost(host="*.test.com"),
            ShExpMatchHost(host="*.org.*"),
            ShExpMatchHost(host="org.*")
        ]
        )
    )
)

# Add a second block to direct various CIDR ranges to a second proxy
f.add_block(
    PACBlock(
        PACReturn("PROXY", "socks.otherproxy.com", 8080),
        [
            IsInNet(net='52.6.51.0', mask='255.255.255.0'),
            IsInNet(net='52.20.84.0', mask='255.255.255.0'),
            IsInNet(net='1.2.0.0', mask='255.255.0.0')
        ]
    )
)

# Return the PAC as text
f.render() 
```

Currently, PACGEN supports the following PAC functions:
- DNSDomainIs
- IsInNet
- IsPlainHostName
- MonthRange
- TimeRange
- ShExpMatchHost
- ShExpMatchUrl
- WeekdayRange

## Modifying templates
PACGEN templates are based on jinja2 templates.  To customize the templates, you must first copy the template from pypacgen/templates into your own project directory:

```
cp lib/pypacgen/templates/pac-file.template ./pacman-pac-file.template
```

We can then update the template with our own formatting, for example:
```
{% if file_name is defined and file_name %}/**
 * File: {{ file_name }}
 * Version: {{ file_version }}
 * ~~~ Generated by PAC-man ~~~
 * 
 */
 
{% endif %}{% if file_documentation is defined and file_documentation %}// {{ file_documentation }}
{% endif %}function FindProxyForURL(url, host) {
    {% filter indent(width=4) %}{{ file_body }}{% endfilter %}

    // This is the default case
    return {{ file_default }};
}
```

and then extend the specific class using our new:

```
import pypacgen

class PACFile(pypacgen.PACFile):
    template_filename = "pacman-pac-file.template"
```

## TODO

- Add support for obfuscation and minifying
- Add in-module support for parsing and validation
- Add support for pre-defined variables and constants