Modules:¶
Currently Qlik-Script-Tools has a few different modules.
QVD¶
Tools for manipulating qvd files.
-
class
qvstools.qvd.QVD(qvdfile, tablename=False, prefix=False)¶ Take a qvd file and make a python class with its xml header info.
Parameters: - qvdfile – filepath of qvdfile to create block for.
- tablename – replaces the name of the table in the qvd with the string given.
- prefix – specifies a prefix to be used when aliasing fieldnames in table, i.e. if prefix = ‘XX’, FieldName will be aliased as XX_FieldName in the load statement.
Blocks¶
Module for creating and combining blocks.
-
class
qvstools.blocks.Block(name, description, blocktype, blocktext, replacelist=[])¶ Container for a block of qlik script.
Parameters: - name – the name for your block.
- description – longer description of your block
- blocktype – currently not much used, but useful for grouping blocks within a library.
- blocktext – string containing the actual text of your block.
- replacelist – list of strings to use as replacements for @0,@1...
-
write(pathname, replacelist=[], mode='a')¶ Write block to pathname.
Parameters: - replacelist – list of strings to use as replacements for @0,@1...
- mode – same as for normal write() method. ‘w’ for write, ‘a’ for append.
-
class
qvstools.blocks.BlockLibrary(name, load_defaults=False)¶ Bundles together several blocks and provide methods for working with them.
A blocklibrary brings together several blocks as a dict within self.blocks. This makes it easy to combine several blocks to write more complex scripts. :param load_defaults: if True will load a default set of blocks.
-
add_directory_block(directory)¶ Create blocks for all .xml files in directory.
-
add_directory_qvd(directory)¶ Create blocks for all .qvd files in directory.
-
add_qvd_block(qvd, blockname='', tablename=False, prefix=False)¶ Read in a qvd file using QVD() and create a block containing a simple load statement for it.
Example Output:
TableName: Load ID as _KEY_ID //(optional) , A as TA_A , B as TA_B , C as TA_C From [Filepath.qvd] (qvd);
-
add_text_block(name, description, blocktype, pathname)¶ Create a Block and adds it to the library.
Parameters: - name – name of the block. Needn’t be the same as pathname.
- description – description of the block.
- blocktype – currently not much used, but useful for grouping blocks within a library.
- pathname – filepath of text file (qvs/txt) to turn into a block.
-
add_xml_block(filepath)¶ Create a block from an xml file and add it to the library.
-
block_to_xml(blockname, directory='.')¶ Takes the name of a block as a string and writes an xml file containing both the block text and its metadata (name, description, type, replacelist).
Parameters: - blockname – name of block to write to xml.
- directory – by default this method writes to the current directory, but you can specify on here.
-
static
find_referenced_files(block)¶ Scan the block with a regular expression that matches qvds, return a list of the qvds it references.
-
remove_block(blockname)¶ Remove block from library.
-
static
split_block_tabs(block)¶ Split the input block at each ///$tab line and return a list of blocks.
The returned list will not be added to the blocklibray. To add it just to a simple for loop:
for sub_block in myblocklibrary.split_block_tabs(block): myblocklibrary[sub_block.name] = sub_block
The reason for this behavior is that the block libray dict is unordered, but it is useful to preserve the order the blocks were split into. Note that this method takes a block, not the name of a block in the library. This is so that the method can be called as a staticmethod independently of a blocklibray object. Used for example by
subbify.
-
write_block(blockname, outputfile, replacelist=[], mode='a')¶ Call write() method of block.
Parameters: blockname – name of block to write.
-
static
write_tab(tabname, pathname, mode='a')¶ Write a single ///$tab to the file specified by pathname.
-
PRJ¶
Module for dealing with prj files.
-
class
qvstools.prj.PRJ(directory)¶ Class for holding the component parts of a prj folder, and methods for doing useful stuff with them.
-
find_replace_elements(search_path, replace, object_id=False, object_type=False)¶ Takes an xpath query and searches through self.xmlfiles to find and replace, then writes back to the source files.
Qlik xml files seem not to use attributes so we assume that the user always wants to replace the .text of an element.
Parameters: - search_path – xpath query.
- object_id – object id of object to limit find and replace to. Leave as default to search all objects.
- object_type – object type of objects to limit find and replace to. Leave as default to search all object types.
Examples: Find and replace all the fonts in a project with calibri:
prj = PRJ('App-prj') prj.find_replace_elements(".//FontName",'Calibri')
-
rescale_layout(new_x, new_y, sheets='all', global_rescale=False)¶ Takes the layout and scales it to a new resolution.
Uses the maximum x and y coordinates of objects in the sheet and rescales all of the layout coordinates to match the resoltion given. For best results add a text box in the lower rightmost corner of your sheet to mark the current bounds of the sheet. :param new_x: New x resolution to scale to in pixels. :param new_y: New y resolution to scale to in pixels. :param sheets: List of sheet IDs e.g. [‘SH01’,’SH02’] to rescale. If left as the default, all sheets will be rescaled. :param global_rescale: Set this to True to use the max x and y coords of any sheet to generate the scaling factors for all sheets. The default behaviour is to rescale each sheet based on it’s own max x and y coordinates.
-
write_xml_all()¶ Write the content of the prj object back to their original file sources.
-
-
class
qvstools.prj.QVObject(object_file)¶ Class for a qlikview object. Holds the xml tree as well as it’s type, name etc.
-
write(path=False)¶ Write the object back to an xml file.
-
-
qvstools.prj.replace_fonts_commandline()¶ Command line tool registered by setuptools as QVReplaceFonts. Takes a qvw file and switches all the fonts to the one specified.
Usage:
> QVReplaceFonts "PrjDirectory-prj" "Comic Sans"
Log¶
Tools for reading logfiles and working out dependencies.
-
class
qvstools.log.LogFile(textfile)¶ Parses a qlikview logfile and creates an object with it’s info more easliy accessible.
-
get_file_lines()¶ Returns a list of the files found by tag_file_lines.
-
parse_logfile(textfile)¶ “Parses the logfile and dict for each line.
Each line is turned into a dict with the following keys:
{ 'date':'date of execution', 'time':'time of execution', 'op':'operation number of the line if present', 'text':'the remainder of the line' }
-
-
qvstools.log.build_dependency_graph(path, depth=100, remap_paths=False, basenames_only=False, blacklist=[])¶ Recursively looks through log files to build a dependency graph. Steps:
- Start with the logfile, and find all the files it generates.
- For the qvd files, find the creator doc for those files, and see if that file has a logfile.
- If it does, see step 1.
- Once all your logfiles have been searched through, write out the dependency graph in a structured format.
The dependency graph is returned in the following format:
{ 'depth':'depth of recursion', 'maxfiles': 'maximum number of files scanned', 'qvw': [list of qvds scanned or named as creatordoc] 'qvd': [list of each qvd file found], 'otherfiles': [list ofeach non qvd data file found] 'creatordocs': [list of tuples (qvd,creatordoc) for each qvd file found], 'userdocs':[list of tuples (userdoc,qvd) for each qvd file found], 'triplets':[list of tuples (userdoc,qvd,creatordoc) for each qvd file found] }Parameters: - depth – The maximum number of logfiles to scan.
- remap_paths – A dict of paths to replace, where key is the path to replace and value is it’s relacement.
This is useful for remapping drive names where different users have relaoded different qvws, resulting in mixed drivenames and filepaths. :param basenames_only: Output will filepaths will be given as basenamees only. :param blacklist: A list of logfiles not to touch. can use wildcard * to blacklist any files beginning with that string. Otherwise should end in ‘.qvw.log’.
-
qvstools.log.decode_log_with_detected(textfile)¶ Uses
detect_encodingto get encoding and returns a unicode string of the text file
-
qvstools.log.detect_log_encoding(textfile, encodings)¶ Tries to open textfile with a variety of encodings and returns the one that works
-
qvstools.log.generate_graphviz(deps_graph, style=1)¶ Takes a logfile and returns a graphviz.it compatible graph of it’s dependencies.
Currently working on a couple of different styles.A better structure for this graph can be found at http://graphviz.it/#/gallery/tree.gv
-
qvstools.log.get_referenced_files(logfile, search='*')¶ An improved function for grabbing logfiles referenced in log files.
Takes a full logfile and returns a list of files used by searching for text within load ... from and store ... into statements. Currently not compatible with directory statements.
Parameters: - logfile – logfile to search
- search – name of search to use. One of from_qvd, from_any, or into_any. Use ‘*’ to match all of these.
Subbify¶
Auto-generates a subbified qvd loader / extractor based on a script input.
Steps¶
- Take input script:
- Split into tabs.
- Wrap each tab in subbify blocks.
- Save out resulting script into cloned prj directory based on a template file.
-
qvstools.subbify.subbify(filepath, open_after=True, reload_after='s')¶ Base method for subbify. Takes a filepath and generates a subbified qvw file.
Parameters: - filepath – should point to a qvs file. possibly in future a qvw file if I can figure out how to generate the prj files for it.
- open_after – opens the generated file in qlikview if True.
- reload_after – ‘s’ loads sub table only (default), ‘f’ loads sub table and runs all subs.
-
qvstools.subbify.subbify_comandline()¶ Command line version of subbify.
This is installed as a command line tool by setuptools as QVSubbify.
Usage:
QVSubbify "MyQlikViewScript.qvs"
Text¶
Tools for helping with text files, encodings etc.
-
qvstools.text.print_progress(iteration, total, prefix='', suffix='', decimals=2, barLength=30)¶ Call in a loop to create terminal progress bar @params:
iterations - Required : current iteration (Int) total - Required : total iterations (Int) prefix - Optional : prefix string (Str) suffix - Optional : suffix string (Str)