diff --git a/atg.py b/atg.py index 624f3b0..5dc9024 100644 --- a/atg.py +++ b/atg.py @@ -1,20 +1,26 @@ #!/usr/bin/python # -*- 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 License: GPLv3 ''' -from os.path import join, split, exists +from os.path import join, exists 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): + ''' + 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.template = template self.out = template.process(data) @@ -25,6 +31,9 @@ class ATG(object): self.multiple = False def write_files(self, outputDir='.'): + ''' + Write generated files to the given directory. + ''' encoding = self.template.encoding extension = self.template.extension out = self.out @@ -59,4 +68,7 @@ class ATG(object): f.close() def log(self, text): + ''' + Print information + ''' pass \ No newline at end of file diff --git a/atr.py b/atr.py index 36f1ae4..a848ed2 100644 --- a/atr.py +++ b/atr.py @@ -1,16 +1,18 @@ #!/usr/bin/python # -*- 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 License: GPLv3 ''' 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): @@ -160,4 +162,7 @@ class ATR(object): self.replacements = [] def log(self, string): + ''' + Print information + ''' pass \ No newline at end of file diff --git a/data.py b/data.py index 0163062..c5b5cfc 100644 --- a/data.py +++ b/data.py @@ -1,7 +1,7 @@ #!/usr/bin/python # -*- coding: utf-8 -*- ''' -Data module for a Kai's Text Tools. +Data module for a KaiSD Text Tools. (c) 2013 Ivan "Kai SD" Korystin @@ -10,7 +10,7 @@ License: GPLv3 import csv, codecs -class Data(object): +class Data: ''' Empty data class. Can be used for a subclassing or procedural data creation. ''' @@ -97,6 +97,9 @@ class Data(object): r.append('') def col_by_key(self, key): + ''' + Returns a column by header's name + ''' cols = [] keys = self.keys rows = self.rows @@ -109,6 +112,9 @@ class Data(object): return tuple(cols) def row_by_idx(self, idx): + ''' + Returns a row by index. + ''' return tuple(self.rows[idx]) class CSVData(Data): @@ -138,6 +144,15 @@ class CSVData(Data): return self 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) sourceData = [] sourcekeys = None diff --git a/docs/atg.html b/docs/atg.html new file mode 100644 index 0000000..bd42e50 --- /dev/null +++ b/docs/atg.html @@ -0,0 +1,47 @@ + + +Python: module atg + + + + +
 
+ 
atg
index
c:\users\kaisd\documents\workbench\programming\ktt\src\atg.py
+

Advanced Text Generator module for a KaiSD Text Tools.

+(c) 2013 Ivan "Kai SD" Korystin 

+License: GPLv3

+

+ + + + + +
 
+Classes
       
+
ATG +
+

+ + + + + + + +
 
+class ATG
   Advanced Text Generator is a class, created to generate multiple
+text files from table data.
 
 Methods defined here:
+
__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)
+ +
log(self, text)
Print information
+ +
write_files(self, outputDir='.')
Write generated files to the given directory.
+ +

+ \ No newline at end of file diff --git a/docs/atr.html b/docs/atr.html new file mode 100644 index 0000000..e4ec319 --- /dev/null +++ b/docs/atr.html @@ -0,0 +1,70 @@ + + +Python: module atr + + + + +
 
+ 
atr
index
c:\users\kaisd\documents\workbench\programming\ktt\src\atr.py
+

Advanced Text Replacer module for a KaiSD Text Tools.

+(c) 2013 Ivan "Kai SD" Korystin 

+License: GPLv3

+

+ + + + + +
 
+Modules
       
re
+

+ + + + + +
 
+Classes
       
+
ATR +
+

+ + + + + + + +
 
+class ATR
   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.
 
 Methods defined here:
+
__init__(self, files)
Constructor
+ +
clear_replacements(self)
Removes all replacements.
+ +
log(self, string)
Print information
+ +
plain_replace(self, pattern, string, regexp=False)
Replaces the given pattern with string in files.
+ +
replace_in_names(self)
Do replacement, but in file names instead of file content. Returns the list of new file names,
+you can use it with writeNewFiles() method.
+ +
templated_replace(self, pattern, template, data, keyFormat='filename', regexp=False)
Replaces the given pattern with data formated by template.
+Valid values for keyFormat:
+filename - take data rows by filename(path ignored), key value of the data row should store the filename.
+fullname - as filename, but with path.
+index - take data rows in order, key value of the data row should store the index. Indexes starts with 0.
+If filename or index cannot be found in data keys, pattern will not be replaced.
+ +
write_in_place(self)
Do replacement and save the files
+ +
write_new_files(self, outfiles)
Do replacement, but save to given files instead of the original ones.
+ +

+ \ No newline at end of file diff --git a/docs/data.html b/docs/data.html new file mode 100644 index 0000000..6a28322 --- /dev/null +++ b/docs/data.html @@ -0,0 +1,114 @@ + + +Python: module data + + + + +
 
+ 
data
index
c:\users\kaisd\documents\workbench\programming\ktt\src\data.py
+

Data module for a KaiSD Text Tools.

+(c) 2013 Ivan "Kai SD" Korystin 

+License: GPLv3

+

+ + + + + +
 
+Modules
       
codecs
+
csv
+

+ + + + + +
 
+Classes
       
+
Data +
+
+
CSVData +
+
+
+

+ + + + + + + +
 
+class CSVData(Data)
   Class for reading CSV files.
 
 Methods defined here:
+
__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
+ +
+Data and other attributes defined here:
+
Reader = <class data.Reader>
+ +
+Methods inherited from Data:
+
__getitem__(self, pair)
Returns a value for given key and row.
+ +
__repr__(self)
+ +
__setitem__(self, pair, value)
Sets a value for given key and row.
+ +
__str__(self)
Returns data as string.
+ +
add_keys(self, *h)
Adds new keys to the data.
+ +
add_rows(self, n=1)
Adds some empty rows to the data.
+ +
col_by_key(self, key)
Returns a column by header's name
+ +
has_key(self, key)
Returns True if given key exists in data
+ +
row_by_idx(self, idx)
Returns a row by index.
+ +

+ + + + + + + +
 
+class Data
   Empty data class. Can be used for a subclassing or procedural data creation.
 
 Methods defined here:
+
__getitem__(self, pair)
Returns a value for given key and row.
+ +
__init__(self, *args, **kwargs)
Constructor
+ +
__repr__(self)
+ +
__setitem__(self, pair, value)
Sets a value for given key and row.
+ +
__str__(self)
Returns data as string.
+ +
add_keys(self, *h)
Adds new keys to the data.
+ +
add_rows(self, n=1)
Adds some empty rows to the data.
+ +
col_by_key(self, key)
Returns a column by header's name
+ +
has_key(self, key)
Returns True if given key exists in data
+ +
row_by_idx(self, idx)
Returns a row by index.
+ +

+ \ No newline at end of file diff --git a/docs/template.html b/docs/template.html new file mode 100644 index 0000000..7f80dfd --- /dev/null +++ b/docs/template.html @@ -0,0 +1,135 @@ + + +Python: module template + + + + +
 
+ 
template
index
c:\users\kaisd\documents\workbench\programming\ktt\src\template.py
+

Template module for a KaiSD Text Tools.

+(c) 2013 Ivan "Kai SD" Korystin 

+License: GPLv3

+

+ + + + + +
 
+Modules
       
re
+

+ + + + + +
 
+Classes
       
+
Template +
+
+
TemplateV2 +
+
+
+

+ + + + + + + +
 
+class Template
   Empty template class. Generates empty text.
 
 Methods defined here:
+
log(self, text)
Print information
+ +
process(self, data)
Replace this method in subclasses.
+ +
warning(self, text)
Prints a warning
+ +

+ + + + + + + +
 
+class TemplateV2(Template)
   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.
 
 Methods defined here:
+
__init__(self, filename=None, encoding='utf-8', text='')
Constructor.

+filename - name of the ATGv2 template file.
+encoding - encoding of the template file.
+text - text to use if no filename has been provided.
+ +
process(self, data)
Generate text for the given data.
+ +
+Static methods defined here:
+
express(cls, text, **kwargs)
+ +
+Methods inherited from Template:
+
log(self, text)
Print information
+ +
warning(self, text)
Prints a warning
+ +

+ \ No newline at end of file diff --git a/ktt_atgcsv.py b/ktt_atgcsv.py index b026b84..08421d4 100755 --- a/ktt_atgcsv.py +++ b/ktt_atgcsv.py @@ -3,7 +3,7 @@ ''' Generates files from csv table. -Part of Kai's Text Tools +Part of KaiSD Text Tools (c) 2013 Ivan "Kai SD" Korystin diff --git a/scripts/gendoc.cmd b/scripts/gendoc.cmd new file mode 100644 index 0000000..cf7d59d --- /dev/null +++ b/scripts/gendoc.cmd @@ -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% +) diff --git a/template.py b/template.py index f448a47..39e5eb4 100644 --- a/template.py +++ b/template.py @@ -1,7 +1,7 @@ #!/usr/bin/python # -*- coding: utf-8 -*- ''' -Template module for a Kai's Text Tools. +Template module for a KaiSD Text Tools. (c) 2013 Ivan "Kai SD" Korystin @@ -9,20 +9,90 @@ License: GPLv3 ''' 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. + + 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=''): ''' - 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: with open(filename, 'r') as templateFile: @@ -43,10 +113,6 @@ class TemplateV2(object): self.oneFile = True else: self.oneFile = False - if 'transpose' in keyInfo[4:]: - self.transpose = True - else: - self.transpose = False self.text = u'' else: raise BaseException('%s has bad ATGv2 key' % (filename)) @@ -56,7 +122,7 @@ class TemplateV2(object): else: self.text = text - self.key = u'' + self.header = u'' self.footer = u'' self.replacement = {} self._data = None @@ -138,12 +204,12 @@ class TemplateV2(object): def lIndex(index, flow, keytag, 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,'') - partCommands['ATGkey'] = addkey + partCommands['ATGHEADER'] = addHeader def addFooter(index, flow, text): if self.footer.find(text) < 0: @@ -349,6 +415,9 @@ class TemplateV2(object): self.parts = parse(self.text) def process(self, data): + ''' + Generate text for the given data. + ''' self._data = data multiWords = {} @@ -394,26 +463,20 @@ class TemplateV2(object): out += text else: name = self.bonusPrefix + unicode(element) - out[name] = text + out[name] = self.header + text + self.footer self.log('Created %s' % (element)) if self.oneFile: - out = self.key + out + self.footer + out = self.header + out + self.footer return out - def warning(self, text): - print text - - def log(self, text): - pass - @staticmethod def express(cls, text, **kwargs): obj = cls() 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 \ No newline at end of file