@@ -1,20 +1,26 @@ | |||||
#!/usr/bin/python | #!/usr/bin/python | ||||
# -*- coding: utf-8 -*- | # -*- coding: utf-8 -*- | ||||
''' | ''' | ||||
Advanced Text Generator module for a Kai's Text Tools. | |||||
Advanced Text Generator module for a KaiSD Text Tools. | |||||
(c) 2013 Ivan "Kai SD" Korystin | (c) 2013 Ivan "Kai SD" Korystin | ||||
License: GPLv3 | License: GPLv3 | ||||
''' | ''' | ||||
from os.path import join, split, exists | |||||
from os.path import join, exists | |||||
from os import makedirs | from os import makedirs | ||||
class ATG(object): | |||||
class ATG: | |||||
''' | ''' | ||||
Advanced Text Generator is a class, created to generate multiple text files from table data. | |||||
Advanced Text Generator is a class, created to generate multiple | |||||
text files from table data. | |||||
''' | ''' | ||||
def __init__(self, data, template): | def __init__(self, data, template): | ||||
''' | |||||
Constructor. | |||||
data - an instance of the data.Data class (i.e. CSVData) | |||||
template - an instance of the template.Template class (i.e. TemplateV2) | |||||
''' | |||||
self.data = data | self.data = data | ||||
self.template = template | self.template = template | ||||
self.out = template.process(data) | self.out = template.process(data) | ||||
@@ -25,6 +31,9 @@ class ATG(object): | |||||
self.multiple = False | self.multiple = False | ||||
def write_files(self, outputDir='.'): | def write_files(self, outputDir='.'): | ||||
''' | |||||
Write generated files to the given directory. | |||||
''' | |||||
encoding = self.template.encoding | encoding = self.template.encoding | ||||
extension = self.template.extension | extension = self.template.extension | ||||
out = self.out | out = self.out | ||||
@@ -59,4 +68,7 @@ class ATG(object): | |||||
f.close() | f.close() | ||||
def log(self, text): | def log(self, text): | ||||
''' | |||||
Print information | |||||
''' | |||||
pass | pass |
@@ -1,16 +1,18 @@ | |||||
#!/usr/bin/python | #!/usr/bin/python | ||||
# -*- coding: utf-8 -*- | # -*- coding: utf-8 -*- | ||||
''' | ''' | ||||
Advanced Text Replacer module for a Kai's Text Tools. | |||||
Advanced Text Replacer module for a KaiSD Text Tools. | |||||
(c) 2013 Ivan "Kai SD" Korystin | (c) 2013 Ivan "Kai SD" Korystin | ||||
License: GPLv3 | License: GPLv3 | ||||
''' | ''' | ||||
import re | import re | ||||
class ATR(object): | |||||
class ATR: | |||||
''' | ''' | ||||
classdocs | |||||
Advanced Text Replacer - is a class, created to make multiple replacements | |||||
in the content or names of text file. | |||||
It can make plain replacements, or use ATG templates to do something more complex. | |||||
''' | ''' | ||||
def __init__(self, files): | def __init__(self, files): | ||||
@@ -160,4 +162,7 @@ class ATR(object): | |||||
self.replacements = [] | self.replacements = [] | ||||
def log(self, string): | def log(self, string): | ||||
''' | |||||
Print information | |||||
''' | |||||
pass | pass |
@@ -1,7 +1,7 @@ | |||||
#!/usr/bin/python | #!/usr/bin/python | ||||
# -*- coding: utf-8 -*- | # -*- coding: utf-8 -*- | ||||
''' | ''' | ||||
Data module for a Kai's Text Tools. | |||||
Data module for a KaiSD Text Tools. | |||||
(c) 2013 Ivan "Kai SD" Korystin | (c) 2013 Ivan "Kai SD" Korystin | ||||
@@ -10,7 +10,7 @@ License: GPLv3 | |||||
import csv, codecs | import csv, codecs | ||||
class Data(object): | |||||
class Data: | |||||
''' | ''' | ||||
Empty data class. Can be used for a subclassing or procedural data creation. | Empty data class. Can be used for a subclassing or procedural data creation. | ||||
''' | ''' | ||||
@@ -97,6 +97,9 @@ class Data(object): | |||||
r.append('') | r.append('') | ||||
def col_by_key(self, key): | def col_by_key(self, key): | ||||
''' | |||||
Returns a column by header's name | |||||
''' | |||||
cols = [] | cols = [] | ||||
keys = self.keys | keys = self.keys | ||||
rows = self.rows | rows = self.rows | ||||
@@ -109,6 +112,9 @@ class Data(object): | |||||
return tuple(cols) | return tuple(cols) | ||||
def row_by_idx(self, idx): | def row_by_idx(self, idx): | ||||
''' | |||||
Returns a row by index. | |||||
''' | |||||
return tuple(self.rows[idx]) | return tuple(self.rows[idx]) | ||||
class CSVData(Data): | class CSVData(Data): | ||||
@@ -138,6 +144,15 @@ class CSVData(Data): | |||||
return self | return self | ||||
def __init__(self, filename, encoding='utf-8', delimiter=';', quotechar='"', **kwargs): | def __init__(self, filename, encoding='utf-8', delimiter=';', quotechar='"', **kwargs): | ||||
''' | |||||
Constructor. | |||||
filename - CSV table filename | |||||
encoding - CSV table encoding | |||||
delimiter - CSV table delimiter | |||||
quotechar - CSV table quotechar | |||||
transpose=True - transpose the table | |||||
''' | |||||
csvfile = self.Reader(open(filename), encoding=encoding, delimiter=delimiter, quotechar=quotechar) | csvfile = self.Reader(open(filename), encoding=encoding, delimiter=delimiter, quotechar=quotechar) | ||||
sourceData = [] | sourceData = [] | ||||
sourcekeys = None | sourcekeys = None | ||||
@@ -0,0 +1,47 @@ | |||||
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> | |||||
<html><head><title>Python: module atg</title> | |||||
</head><body bgcolor="#f0f0f8"> | |||||
<table width="100%" cellspacing=0 cellpadding=2 border=0 summary="heading"> | |||||
<tr bgcolor="#7799ee"> | |||||
<td valign=bottom> <br> | |||||
<font color="#ffffff" face="helvetica, arial"> <br><big><big><strong>atg</strong></big></big></font></td | |||||
><td align=right valign=bottom | |||||
><font color="#ffffff" face="helvetica, arial"><a href=".">index</a><br><a href="file:///C:/users/kaisd/documents/workbench/programming/ktt/src/atg.py">c:\users\kaisd\documents\workbench\programming\ktt\src\atg.py</a></font></td></tr></table> | |||||
<p><tt>Advanced Text Generator module for a KaiSD Text Tools.<br> | |||||
<br> | |||||
(c) 2013 Ivan "Kai SD" Korystin <br> | |||||
<br> | |||||
License: GPLv3</tt></p> | |||||
<p> | |||||
<table width="100%" cellspacing=0 cellpadding=2 border=0 summary="section"> | |||||
<tr bgcolor="#ee77aa"> | |||||
<td colspan=3 valign=bottom> <br> | |||||
<font color="#ffffff" face="helvetica, arial"><big><strong>Classes</strong></big></font></td></tr> | |||||
<tr><td bgcolor="#ee77aa"><tt> </tt></td><td> </td> | |||||
<td width="100%"><dl> | |||||
<dt><font face="helvetica, arial"><a href="atg.html#ATG">ATG</a> | |||||
</font></dt></dl> | |||||
<p> | |||||
<table width="100%" cellspacing=0 cellpadding=2 border=0 summary="section"> | |||||
<tr bgcolor="#ffc8d8"> | |||||
<td colspan=3 valign=bottom> <br> | |||||
<font color="#000000" face="helvetica, arial"><a name="ATG">class <strong>ATG</strong></a></font></td></tr> | |||||
<tr bgcolor="#ffc8d8"><td rowspan=2><tt> </tt></td> | |||||
<td colspan=2><tt>Advanced Text Generator is a class, created to generate multiple<br> | |||||
text files from table data.<br> </tt></td></tr> | |||||
<tr><td> </td> | |||||
<td width="100%">Methods defined here:<br> | |||||
<dl><dt><a name="ATG-__init__"><strong>__init__</strong></a>(self, data, template)</dt><dd><tt>Constructor.<br> | |||||
data - an instance of the data.Data class (i.e. CSVData)<br> | |||||
template - an instance of the template.Template class (i.e. TemplateV2)</tt></dd></dl> | |||||
<dl><dt><a name="ATG-log"><strong>log</strong></a>(self, text)</dt><dd><tt>Print information</tt></dd></dl> | |||||
<dl><dt><a name="ATG-write_files"><strong>write_files</strong></a>(self, outputDir<font color="#909090">='.'</font>)</dt><dd><tt>Write generated files to the given directory.</tt></dd></dl> | |||||
</td></tr></table></td></tr></table> | |||||
</body></html> |
@@ -0,0 +1,70 @@ | |||||
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> | |||||
<html><head><title>Python: module atr</title> | |||||
</head><body bgcolor="#f0f0f8"> | |||||
<table width="100%" cellspacing=0 cellpadding=2 border=0 summary="heading"> | |||||
<tr bgcolor="#7799ee"> | |||||
<td valign=bottom> <br> | |||||
<font color="#ffffff" face="helvetica, arial"> <br><big><big><strong>atr</strong></big></big></font></td | |||||
><td align=right valign=bottom | |||||
><font color="#ffffff" face="helvetica, arial"><a href=".">index</a><br><a href="file:///C:/users/kaisd/documents/workbench/programming/ktt/src/atr.py">c:\users\kaisd\documents\workbench\programming\ktt\src\atr.py</a></font></td></tr></table> | |||||
<p><tt>Advanced Text Replacer module for a KaiSD Text Tools.<br> | |||||
<br> | |||||
(c) 2013 Ivan "Kai SD" Korystin <br> | |||||
<br> | |||||
License: GPLv3</tt></p> | |||||
<p> | |||||
<table width="100%" cellspacing=0 cellpadding=2 border=0 summary="section"> | |||||
<tr bgcolor="#aa55cc"> | |||||
<td colspan=3 valign=bottom> <br> | |||||
<font color="#ffffff" face="helvetica, arial"><big><strong>Modules</strong></big></font></td></tr> | |||||
<tr><td bgcolor="#aa55cc"><tt> </tt></td><td> </td> | |||||
<td width="100%"><table width="100%" summary="list"><tr><td width="25%" valign=top><a href="re.html">re</a><br> | |||||
</td><td width="25%" valign=top></td><td width="25%" valign=top></td><td width="25%" valign=top></td></tr></table></td></tr></table><p> | |||||
<table width="100%" cellspacing=0 cellpadding=2 border=0 summary="section"> | |||||
<tr bgcolor="#ee77aa"> | |||||
<td colspan=3 valign=bottom> <br> | |||||
<font color="#ffffff" face="helvetica, arial"><big><strong>Classes</strong></big></font></td></tr> | |||||
<tr><td bgcolor="#ee77aa"><tt> </tt></td><td> </td> | |||||
<td width="100%"><dl> | |||||
<dt><font face="helvetica, arial"><a href="atr.html#ATR">ATR</a> | |||||
</font></dt></dl> | |||||
<p> | |||||
<table width="100%" cellspacing=0 cellpadding=2 border=0 summary="section"> | |||||
<tr bgcolor="#ffc8d8"> | |||||
<td colspan=3 valign=bottom> <br> | |||||
<font color="#000000" face="helvetica, arial"><a name="ATR">class <strong>ATR</strong></a></font></td></tr> | |||||
<tr bgcolor="#ffc8d8"><td rowspan=2><tt> </tt></td> | |||||
<td colspan=2><tt>Advanced Text Replacer - is a class, created to make multiple replacements<br> | |||||
in the content or names of text file.<br> | |||||
It can make plain replacements, or use ATG templates to do something more complex.<br> </tt></td></tr> | |||||
<tr><td> </td> | |||||
<td width="100%">Methods defined here:<br> | |||||
<dl><dt><a name="ATR-__init__"><strong>__init__</strong></a>(self, files)</dt><dd><tt>Constructor</tt></dd></dl> | |||||
<dl><dt><a name="ATR-clear_replacements"><strong>clear_replacements</strong></a>(self)</dt><dd><tt>Removes all replacements.</tt></dd></dl> | |||||
<dl><dt><a name="ATR-log"><strong>log</strong></a>(self, string)</dt><dd><tt>Print information</tt></dd></dl> | |||||
<dl><dt><a name="ATR-plain_replace"><strong>plain_replace</strong></a>(self, pattern, string, regexp<font color="#909090">=False</font>)</dt><dd><tt>Replaces the given pattern with string in files.</tt></dd></dl> | |||||
<dl><dt><a name="ATR-replace_in_names"><strong>replace_in_names</strong></a>(self)</dt><dd><tt>Do replacement, but in file names instead of file content. Returns the list of new file names,<br> | |||||
you can use it with writeNewFiles() method.</tt></dd></dl> | |||||
<dl><dt><a name="ATR-templated_replace"><strong>templated_replace</strong></a>(self, pattern, template, data, keyFormat<font color="#909090">='filename'</font>, regexp<font color="#909090">=False</font>)</dt><dd><tt>Replaces the given pattern with data formated by template.<br> | |||||
Valid values for keyFormat:<br> | |||||
filename - take data rows by filename(path ignored), key value of the data row should store the filename.<br> | |||||
fullname - as filename, but with path.<br> | |||||
index - take data rows in order, key value of the data row should store the index. Indexes starts with 0.<br> | |||||
If filename or index cannot be found in data keys, pattern will not be replaced.</tt></dd></dl> | |||||
<dl><dt><a name="ATR-write_in_place"><strong>write_in_place</strong></a>(self)</dt><dd><tt>Do replacement and save the files</tt></dd></dl> | |||||
<dl><dt><a name="ATR-write_new_files"><strong>write_new_files</strong></a>(self, outfiles)</dt><dd><tt>Do replacement, but save to given files instead of the original ones.</tt></dd></dl> | |||||
</td></tr></table></td></tr></table> | |||||
</body></html> |
@@ -0,0 +1,114 @@ | |||||
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> | |||||
<html><head><title>Python: module data</title> | |||||
</head><body bgcolor="#f0f0f8"> | |||||
<table width="100%" cellspacing=0 cellpadding=2 border=0 summary="heading"> | |||||
<tr bgcolor="#7799ee"> | |||||
<td valign=bottom> <br> | |||||
<font color="#ffffff" face="helvetica, arial"> <br><big><big><strong>data</strong></big></big></font></td | |||||
><td align=right valign=bottom | |||||
><font color="#ffffff" face="helvetica, arial"><a href=".">index</a><br><a href="file:///C:/users/kaisd/documents/workbench/programming/ktt/src/data.py">c:\users\kaisd\documents\workbench\programming\ktt\src\data.py</a></font></td></tr></table> | |||||
<p><tt><a href="#Data">Data</a> module for a KaiSD Text Tools.<br> | |||||
<br> | |||||
(c) 2013 Ivan "Kai SD" Korystin <br> | |||||
<br> | |||||
License: GPLv3</tt></p> | |||||
<p> | |||||
<table width="100%" cellspacing=0 cellpadding=2 border=0 summary="section"> | |||||
<tr bgcolor="#aa55cc"> | |||||
<td colspan=3 valign=bottom> <br> | |||||
<font color="#ffffff" face="helvetica, arial"><big><strong>Modules</strong></big></font></td></tr> | |||||
<tr><td bgcolor="#aa55cc"><tt> </tt></td><td> </td> | |||||
<td width="100%"><table width="100%" summary="list"><tr><td width="25%" valign=top><a href="codecs.html">codecs</a><br> | |||||
</td><td width="25%" valign=top><a href="csv.html">csv</a><br> | |||||
</td><td width="25%" valign=top></td><td width="25%" valign=top></td></tr></table></td></tr></table><p> | |||||
<table width="100%" cellspacing=0 cellpadding=2 border=0 summary="section"> | |||||
<tr bgcolor="#ee77aa"> | |||||
<td colspan=3 valign=bottom> <br> | |||||
<font color="#ffffff" face="helvetica, arial"><big><strong>Classes</strong></big></font></td></tr> | |||||
<tr><td bgcolor="#ee77aa"><tt> </tt></td><td> </td> | |||||
<td width="100%"><dl> | |||||
<dt><font face="helvetica, arial"><a href="data.html#Data">Data</a> | |||||
</font></dt><dd> | |||||
<dl> | |||||
<dt><font face="helvetica, arial"><a href="data.html#CSVData">CSVData</a> | |||||
</font></dt></dl> | |||||
</dd> | |||||
</dl> | |||||
<p> | |||||
<table width="100%" cellspacing=0 cellpadding=2 border=0 summary="section"> | |||||
<tr bgcolor="#ffc8d8"> | |||||
<td colspan=3 valign=bottom> <br> | |||||
<font color="#000000" face="helvetica, arial"><a name="CSVData">class <strong>CSVData</strong></a>(<a href="data.html#Data">Data</a>)</font></td></tr> | |||||
<tr bgcolor="#ffc8d8"><td rowspan=2><tt> </tt></td> | |||||
<td colspan=2><tt>Class for reading CSV files.<br> </tt></td></tr> | |||||
<tr><td> </td> | |||||
<td width="100%">Methods defined here:<br> | |||||
<dl><dt><a name="CSVData-__init__"><strong>__init__</strong></a>(self, filename, encoding<font color="#909090">='utf-8'</font>, delimiter<font color="#909090">=';'</font>, quotechar<font color="#909090">='"'</font>, **kwargs)</dt><dd><tt>Constructor.<br> | |||||
<br> | |||||
filename - CSV table filename<br> | |||||
encoding - CSV table encoding<br> | |||||
delimiter - CSV table delimiter<br> | |||||
quotechar - CSV table quotechar<br> | |||||
transpose=True - transpose the table</tt></dd></dl> | |||||
<hr> | |||||
Data and other attributes defined here:<br> | |||||
<dl><dt><strong>Reader</strong> = <class data.Reader></dl> | |||||
<hr> | |||||
Methods inherited from <a href="data.html#Data">Data</a>:<br> | |||||
<dl><dt><a name="CSVData-__getitem__"><strong>__getitem__</strong></a>(self, pair)</dt><dd><tt>Returns a value for given key and row.</tt></dd></dl> | |||||
<dl><dt><a name="CSVData-__repr__"><strong>__repr__</strong></a>(self)</dt></dl> | |||||
<dl><dt><a name="CSVData-__setitem__"><strong>__setitem__</strong></a>(self, pair, value)</dt><dd><tt>Sets a value for given key and row.</tt></dd></dl> | |||||
<dl><dt><a name="CSVData-__str__"><strong>__str__</strong></a>(self)</dt><dd><tt>Returns data as string.</tt></dd></dl> | |||||
<dl><dt><a name="CSVData-add_keys"><strong>add_keys</strong></a>(self, *h)</dt><dd><tt>Adds new keys to the data.</tt></dd></dl> | |||||
<dl><dt><a name="CSVData-add_rows"><strong>add_rows</strong></a>(self, n<font color="#909090">=1</font>)</dt><dd><tt>Adds some empty rows to the data.</tt></dd></dl> | |||||
<dl><dt><a name="CSVData-col_by_key"><strong>col_by_key</strong></a>(self, key)</dt><dd><tt>Returns a column by header's name</tt></dd></dl> | |||||
<dl><dt><a name="CSVData-has_key"><strong>has_key</strong></a>(self, key)</dt><dd><tt>Returns True if given key exists in data</tt></dd></dl> | |||||
<dl><dt><a name="CSVData-row_by_idx"><strong>row_by_idx</strong></a>(self, idx)</dt><dd><tt>Returns a row by index.</tt></dd></dl> | |||||
</td></tr></table> <p> | |||||
<table width="100%" cellspacing=0 cellpadding=2 border=0 summary="section"> | |||||
<tr bgcolor="#ffc8d8"> | |||||
<td colspan=3 valign=bottom> <br> | |||||
<font color="#000000" face="helvetica, arial"><a name="Data">class <strong>Data</strong></a></font></td></tr> | |||||
<tr bgcolor="#ffc8d8"><td rowspan=2><tt> </tt></td> | |||||
<td colspan=2><tt>Empty data class. Can be used for a subclassing or procedural data creation.<br> </tt></td></tr> | |||||
<tr><td> </td> | |||||
<td width="100%">Methods defined here:<br> | |||||
<dl><dt><a name="Data-__getitem__"><strong>__getitem__</strong></a>(self, pair)</dt><dd><tt>Returns a value for given key and row.</tt></dd></dl> | |||||
<dl><dt><a name="Data-__init__"><strong>__init__</strong></a>(self, *args, **kwargs)</dt><dd><tt>Constructor</tt></dd></dl> | |||||
<dl><dt><a name="Data-__repr__"><strong>__repr__</strong></a>(self)</dt></dl> | |||||
<dl><dt><a name="Data-__setitem__"><strong>__setitem__</strong></a>(self, pair, value)</dt><dd><tt>Sets a value for given key and row.</tt></dd></dl> | |||||
<dl><dt><a name="Data-__str__"><strong>__str__</strong></a>(self)</dt><dd><tt>Returns data as string.</tt></dd></dl> | |||||
<dl><dt><a name="Data-add_keys"><strong>add_keys</strong></a>(self, *h)</dt><dd><tt>Adds new keys to the data.</tt></dd></dl> | |||||
<dl><dt><a name="Data-add_rows"><strong>add_rows</strong></a>(self, n<font color="#909090">=1</font>)</dt><dd><tt>Adds some empty rows to the data.</tt></dd></dl> | |||||
<dl><dt><a name="Data-col_by_key"><strong>col_by_key</strong></a>(self, key)</dt><dd><tt>Returns a column by header's name</tt></dd></dl> | |||||
<dl><dt><a name="Data-has_key"><strong>has_key</strong></a>(self, key)</dt><dd><tt>Returns True if given key exists in data</tt></dd></dl> | |||||
<dl><dt><a name="Data-row_by_idx"><strong>row_by_idx</strong></a>(self, idx)</dt><dd><tt>Returns a row by index.</tt></dd></dl> | |||||
</td></tr></table></td></tr></table> | |||||
</body></html> |
@@ -0,0 +1,135 @@ | |||||
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> | |||||
<html><head><title>Python: module template</title> | |||||
</head><body bgcolor="#f0f0f8"> | |||||
<table width="100%" cellspacing=0 cellpadding=2 border=0 summary="heading"> | |||||
<tr bgcolor="#7799ee"> | |||||
<td valign=bottom> <br> | |||||
<font color="#ffffff" face="helvetica, arial"> <br><big><big><strong>template</strong></big></big></font></td | |||||
><td align=right valign=bottom | |||||
><font color="#ffffff" face="helvetica, arial"><a href=".">index</a><br><a href="file:///C:/users/kaisd/documents/workbench/programming/ktt/src/template.py">c:\users\kaisd\documents\workbench\programming\ktt\src\template.py</a></font></td></tr></table> | |||||
<p><tt><a href="#Template">Template</a> module for a KaiSD Text Tools.<br> | |||||
<br> | |||||
(c) 2013 Ivan "Kai SD" Korystin <br> | |||||
<br> | |||||
License: GPLv3</tt></p> | |||||
<p> | |||||
<table width="100%" cellspacing=0 cellpadding=2 border=0 summary="section"> | |||||
<tr bgcolor="#aa55cc"> | |||||
<td colspan=3 valign=bottom> <br> | |||||
<font color="#ffffff" face="helvetica, arial"><big><strong>Modules</strong></big></font></td></tr> | |||||
<tr><td bgcolor="#aa55cc"><tt> </tt></td><td> </td> | |||||
<td width="100%"><table width="100%" summary="list"><tr><td width="25%" valign=top><a href="re.html">re</a><br> | |||||
</td><td width="25%" valign=top></td><td width="25%" valign=top></td><td width="25%" valign=top></td></tr></table></td></tr></table><p> | |||||
<table width="100%" cellspacing=0 cellpadding=2 border=0 summary="section"> | |||||
<tr bgcolor="#ee77aa"> | |||||
<td colspan=3 valign=bottom> <br> | |||||
<font color="#ffffff" face="helvetica, arial"><big><strong>Classes</strong></big></font></td></tr> | |||||
<tr><td bgcolor="#ee77aa"><tt> </tt></td><td> </td> | |||||
<td width="100%"><dl> | |||||
<dt><font face="helvetica, arial"><a href="template.html#Template">Template</a> | |||||
</font></dt><dd> | |||||
<dl> | |||||
<dt><font face="helvetica, arial"><a href="template.html#TemplateV2">TemplateV2</a> | |||||
</font></dt></dl> | |||||
</dd> | |||||
</dl> | |||||
<p> | |||||
<table width="100%" cellspacing=0 cellpadding=2 border=0 summary="section"> | |||||
<tr bgcolor="#ffc8d8"> | |||||
<td colspan=3 valign=bottom> <br> | |||||
<font color="#000000" face="helvetica, arial"><a name="Template">class <strong>Template</strong></a></font></td></tr> | |||||
<tr bgcolor="#ffc8d8"><td rowspan=2><tt> </tt></td> | |||||
<td colspan=2><tt>Empty template class. Generates empty text.<br> </tt></td></tr> | |||||
<tr><td> </td> | |||||
<td width="100%">Methods defined here:<br> | |||||
<dl><dt><a name="Template-log"><strong>log</strong></a>(self, text)</dt><dd><tt>Print information</tt></dd></dl> | |||||
<dl><dt><a name="Template-process"><strong>process</strong></a>(self, data)</dt><dd><tt>Replace this method in subclasses.</tt></dd></dl> | |||||
<dl><dt><a name="Template-warning"><strong>warning</strong></a>(self, text)</dt><dd><tt>Prints a warning</tt></dd></dl> | |||||
</td></tr></table> <p> | |||||
<table width="100%" cellspacing=0 cellpadding=2 border=0 summary="section"> | |||||
<tr bgcolor="#ffc8d8"> | |||||
<td colspan=3 valign=bottom> <br> | |||||
<font color="#000000" face="helvetica, arial"><a name="TemplateV2">class <strong>TemplateV2</strong></a>(<a href="template.html#Template">Template</a>)</font></td></tr> | |||||
<tr bgcolor="#ffc8d8"><td rowspan=2><tt> </tt></td> | |||||
<td colspan=2><tt>Class for reading ATGv2 templates.<br> | |||||
<br> | |||||
ATGv2 template file should be a plain text file, starting with the line<br> | |||||
ATGV2<br> | |||||
followed by the info line:<br> | |||||
[$KeyField$Extension$Prefix$Encoding$]<br> | |||||
where<br> | |||||
KeyField - is a name of a data column, that contains an identifier.<br> | |||||
Extension - is the desired extension for the generated files.<br> | |||||
Prefix - is the desired filename prefix for the generated files<br> | |||||
Encoding - is the desired encoding for the generated files.<br> | |||||
The line may also have some optional keywords before the closing bracket:<br> | |||||
oneFile$ - place all generated text into a single file instead of<br> | |||||
generating a file for each table row.<br> | |||||
After the info line, you can put your text.<br> | |||||
You can use following commands to handle the data:<br> | |||||
* [$Name$], where Name is the column header,<br> | |||||
will be replaced with value from the current row.<br> | |||||
* [$ATGLINDEX$] will be replaced with the number of a current row.<br> | |||||
* [$ATGHEADER$Text$] and [$ATGFOOTER$Text$] will place the given text<br> | |||||
at the begining or at the end of the file. You can't use other<br> | |||||
commands in this text.<br> | |||||
* [$ATGLIST$Name$Text$], where Name is a multi-column header<br> | |||||
(i.e. 'Col' will represent 'Col1', 'Col2', 'Col3' etc)<br> | |||||
will repeat the given text for each non-empty value.<br> | |||||
You can use other commands in Text. Also [$Name$] inside the list<br> | |||||
will be replaced with the value for the current row and column.<br> | |||||
* [$ATGLINDEX$] can be used only inside the ATGLIST text,<br> | |||||
will be replaced with the current column index.<br> | |||||
* [$ATGLISTCUT$Name$Text$] - same as ATGLIST, but the last symbol<br> | |||||
will be removed. Useful for removing unnecessary newlines.<br> | |||||
* [$ATGIF$Name$Value$Text$] will be replaced with the given text<br> | |||||
only if the the given column's value is the same as the given one.<br> | |||||
Will be replaced with the empty text otherwise. You can use other<br> | |||||
commands in Text.<br> | |||||
* [$ATGIFNOT$Name$Value$Text$] - same as ATGIF, but the column's value<br> | |||||
should not be equal to the given one.<br> | |||||
* [$ATGGREATER$Name$Value$Text$] - same as ATGIF, but the value should<br> | |||||
be the number and it should be greater then the given one.<br> | |||||
* [$ATGGREATER$Name$Value$Text$] - same as ATGGREATER, but the value<br> | |||||
should be less then the given one.<br> | |||||
* [$ATGREPLACE$Text1$Text2$] - Will replace Text1 with Text2. Replacements<br> | |||||
will be done after all other commands. You can't use regular expressions or<br> | |||||
other commands in the text.<br> | |||||
* [$ATGPREFIX$Text$] - Will add the given text to the filename prefix.<br> | |||||
You can use other commands in text, but do it carefully.<br> | |||||
* [$ATGSKIP$] - Skip the current row. Use only in combination with the<br> | |||||
ATGIF/ATGIFNOT, or you will generate nothing.<br> | |||||
* [$ATGPREV$Name$], where Name is the column header,<br> | |||||
will be replaced with the with the value of the given header from the<br> | |||||
previous row. ATGSKIP will be used for the first row.<br> </tt></td></tr> | |||||
<tr><td> </td> | |||||
<td width="100%">Methods defined here:<br> | |||||
<dl><dt><a name="TemplateV2-__init__"><strong>__init__</strong></a>(self, filename<font color="#909090">=None</font>, encoding<font color="#909090">='utf-8'</font>, text<font color="#909090">=''</font>)</dt><dd><tt>Constructor.<br> | |||||
<br> | |||||
filename - name of the ATGv2 template file.<br> | |||||
encoding - encoding of the template file.<br> | |||||
text - text to use if no filename has been provided.</tt></dd></dl> | |||||
<dl><dt><a name="TemplateV2-process"><strong>process</strong></a>(self, data)</dt><dd><tt>Generate text for the given data.</tt></dd></dl> | |||||
<hr> | |||||
Static methods defined here:<br> | |||||
<dl><dt><a name="TemplateV2-express"><strong>express</strong></a>(cls, text, **kwargs)</dt></dl> | |||||
<hr> | |||||
Methods inherited from <a href="template.html#Template">Template</a>:<br> | |||||
<dl><dt><a name="TemplateV2-log"><strong>log</strong></a>(self, text)</dt><dd><tt>Print information</tt></dd></dl> | |||||
<dl><dt><a name="TemplateV2-warning"><strong>warning</strong></a>(self, text)</dt><dd><tt>Prints a warning</tt></dd></dl> | |||||
</td></tr></table></td></tr></table> | |||||
</body></html> |
@@ -3,7 +3,7 @@ | |||||
''' | ''' | ||||
Generates files from csv table. | Generates files from csv table. | ||||
Part of Kai's Text Tools | |||||
Part of KaiSD Text Tools | |||||
(c) 2013 Ivan "Kai SD" Korystin | (c) 2013 Ivan "Kai SD" Korystin | ||||
@@ -0,0 +1,10 @@ | |||||
@echo off | |||||
set PYDOC=C:\Python27\Lib\pydoc.py | |||||
set OUTDIR=docs | |||||
cd .. | |||||
for %%I in (atg atr data template) do ( | |||||
%PYDOC% -w %%I | |||||
move %%I.html %OUTDIR% | |||||
) |
@@ -1,7 +1,7 @@ | |||||
#!/usr/bin/python | #!/usr/bin/python | ||||
# -*- coding: utf-8 -*- | # -*- coding: utf-8 -*- | ||||
''' | ''' | ||||
Template module for a Kai's Text Tools. | |||||
Template module for a KaiSD Text Tools. | |||||
(c) 2013 Ivan "Kai SD" Korystin | (c) 2013 Ivan "Kai SD" Korystin | ||||
@@ -9,20 +9,90 @@ License: GPLv3 | |||||
''' | ''' | ||||
import re | import re | ||||
class TemplateV3(object): | |||||
class Template: | |||||
''' | ''' | ||||
Class for reading ATGv3 templates. | |||||
Empty template class. Generates empty text. | |||||
''' | ''' | ||||
pass | |||||
def process(self, data): | |||||
''' | |||||
Replace this method in subclasses. | |||||
''' | |||||
return '' | |||||
def warning(self, text): | |||||
''' | |||||
Prints a warning | |||||
''' | |||||
print text | |||||
def log(self, text): | |||||
''' | |||||
Print information | |||||
''' | |||||
pass | |||||
class TemplateV2(object): | |||||
class TemplateV2(Template): | |||||
''' | ''' | ||||
Class for reading ATGv2 templates. | Class for reading ATGv2 templates. | ||||
ATGv2 template file should be a plain text file, starting with the line | |||||
ATGV2 | |||||
followed by the info line: | |||||
[$KeyField$Extension$Prefix$Encoding$] | |||||
where | |||||
KeyField - is a name of a data column, that contains an identifier. | |||||
Extension - is the desired extension for the generated files. | |||||
Prefix - is the desired filename prefix for the generated files | |||||
Encoding - is the desired encoding for the generated files. | |||||
The line may also have some optional keywords before the closing bracket: | |||||
oneFile$ - place all generated text into a single file instead of | |||||
generating a file for each table row. | |||||
After the info line, you can put your text. | |||||
You can use following commands to handle the data: | |||||
* [$Name$], where Name is the column header, | |||||
will be replaced with value from the current row. | |||||
* [$ATGLINDEX$] will be replaced with the number of a current row. | |||||
* [$ATGHEADER$Text$] and [$ATGFOOTER$Text$] will place the given text | |||||
at the begining or at the end of the file. You can't use other | |||||
commands in this text. | |||||
* [$ATGLIST$Name$Text$], where Name is a multi-column header | |||||
(i.e. 'Col' will represent 'Col1', 'Col2', 'Col3' etc) | |||||
will repeat the given text for each non-empty value. | |||||
You can use other commands in Text. Also [$Name$] inside the list | |||||
will be replaced with the value for the current row and column. | |||||
* [$ATGLINDEX$] can be used only inside the ATGLIST text, | |||||
will be replaced with the current column index. | |||||
* [$ATGLISTCUT$Name$Text$] - same as ATGLIST, but the last symbol | |||||
will be removed. Useful for removing unnecessary newlines. | |||||
* [$ATGIF$Name$Value$Text$] will be replaced with the given text | |||||
only if the the given column's value is the same as the given one. | |||||
Will be replaced with the empty text otherwise. You can use other | |||||
commands in Text. | |||||
* [$ATGIFNOT$Name$Value$Text$] - same as ATGIF, but the column's value | |||||
should not be equal to the given one. | |||||
* [$ATGGREATER$Name$Value$Text$] - same as ATGIF, but the value should | |||||
be the number and it should be greater then the given one. | |||||
* [$ATGGREATER$Name$Value$Text$] - same as ATGGREATER, but the value | |||||
should be less then the given one. | |||||
* [$ATGREPLACE$Text1$Text2$] - Will replace Text1 with Text2. Replacements | |||||
will be done after all other commands. You can't use regular expressions or | |||||
other commands in the text. | |||||
* [$ATGPREFIX$Text$] - Will add the given text to the filename prefix. | |||||
You can use other commands in text, but do it carefully. | |||||
* [$ATGSKIP$] - Skip the current row. Use only in combination with the | |||||
ATGIF/ATGIFNOT, or you will generate nothing. | |||||
* [$ATGPREV$Name$], where Name is the column header, | |||||
will be replaced with the with the value of the given header from the | |||||
previous row. ATGSKIP will be used for the first row. | |||||
''' | ''' | ||||
def __init__(self, filename=None, encoding='utf-8', text=''): | def __init__(self, filename=None, encoding='utf-8', text=''): | ||||
''' | ''' | ||||
Constructor | |||||
Constructor. | |||||
filename - name of the ATGv2 template file. | |||||
encoding - encoding of the template file. | |||||
text - text to use if no filename has been provided. | |||||
''' | ''' | ||||
if filename: | if filename: | ||||
with open(filename, 'r') as templateFile: | with open(filename, 'r') as templateFile: | ||||
@@ -43,10 +113,6 @@ class TemplateV2(object): | |||||
self.oneFile = True | self.oneFile = True | ||||
else: | else: | ||||
self.oneFile = False | self.oneFile = False | ||||
if 'transpose' in keyInfo[4:]: | |||||
self.transpose = True | |||||
else: | |||||
self.transpose = False | |||||
self.text = u'' | self.text = u'' | ||||
else: | else: | ||||
raise BaseException('%s has bad ATGv2 key' % (filename)) | raise BaseException('%s has bad ATGv2 key' % (filename)) | ||||
@@ -56,7 +122,7 @@ class TemplateV2(object): | |||||
else: | else: | ||||
self.text = text | self.text = text | ||||
self.key = u'' | |||||
self.header = u'' | |||||
self.footer = u'' | self.footer = u'' | ||||
self.replacement = {} | self.replacement = {} | ||||
self._data = None | self._data = None | ||||
@@ -138,12 +204,12 @@ class TemplateV2(object): | |||||
def lIndex(index, flow, keytag, number): | def lIndex(index, flow, keytag, number): | ||||
return flow.replace('[$ATGLINDEX$]', str(number)) | return flow.replace('[$ATGLINDEX$]', str(number)) | ||||
def addkey(index, flow, text): | |||||
if self.key.find(text) < 0: | |||||
self.key += text | |||||
key = '[$ATGkey$' + text + '$]' | |||||
def addHeader(index, flow, text): | |||||
if self.header.find(text) < 0: | |||||
self.header += text | |||||
key = '[$ATGHEADER$' + text + '$]' | |||||
return flow.replace(key,'') | return flow.replace(key,'') | ||||
partCommands['ATGkey'] = addkey | |||||
partCommands['ATGHEADER'] = addHeader | |||||
def addFooter(index, flow, text): | def addFooter(index, flow, text): | ||||
if self.footer.find(text) < 0: | if self.footer.find(text) < 0: | ||||
@@ -349,6 +415,9 @@ class TemplateV2(object): | |||||
self.parts = parse(self.text) | self.parts = parse(self.text) | ||||
def process(self, data): | def process(self, data): | ||||
''' | |||||
Generate text for the given data. | |||||
''' | |||||
self._data = data | self._data = data | ||||
multiWords = {} | multiWords = {} | ||||
@@ -394,26 +463,20 @@ class TemplateV2(object): | |||||
out += text | out += text | ||||
else: | else: | ||||
name = self.bonusPrefix + unicode(element) | name = self.bonusPrefix + unicode(element) | ||||
out[name] = text | |||||
out[name] = self.header + text + self.footer | |||||
self.log('Created %s' % (element)) | self.log('Created %s' % (element)) | ||||
if self.oneFile: | if self.oneFile: | ||||
out = self.key + out + self.footer | |||||
out = self.header + out + self.footer | |||||
return out | return out | ||||
def warning(self, text): | |||||
print text | |||||
def log(self, text): | |||||
pass | |||||
@staticmethod | @staticmethod | ||||
def express(cls, text, **kwargs): | def express(cls, text, **kwargs): | ||||
obj = cls() | obj = cls() | ||||
obj.text = text | obj.text = text | ||||
self.keyField = kwargs.get('keyField', 'Index') | |||||
self.extension = kwargs.get('extension', '') | |||||
self.prefix = kwargs.get('prefix', '') | |||||
self.encoding = kwargs.get('encoding', 'utf-8') | |||||
obj.keyField = kwargs.get('keyField', 'Index') | |||||
obj.extension = kwargs.get('extension', '') | |||||
obj.prefix = kwargs.get('prefix', '') | |||||
obj.encoding = kwargs.get('encoding', 'utf-8') | |||||
return obj | return obj |