<commit_before># -*- coding: utf-8 -*-
"""
zincToJson allows to convert native haystack 'zinc'
format to Json when connecting to old Jace devices that
aren't able to generate Json response

As Json is easily handled in Python, it's the prefered syntax for web transactions
So everything is built with Json in mind.

It's a patch to support old devices

@author: CTremblay
"""
import json


def zincToJson(req):
    req = req.split('\n')
    metaInfo = req[0].split(' ')
    keys = req[1].split(',')
    grid = req[2:]

    result = {}
    rows = []
    cols = []
    metaDict = {}

    #Build Meta
    for each in metaInfo:
        if each != '':
            #When parsing histories, hisStart and hisEnd Timestamp are separated by a space...
            #Limiting split of each.split(':',1) because ":" is used in the time portion of datetime
            try :
                metaDict[each.split(':',1)[0].replace('"','')] = each.split(':',1)[1].replace('"','')
            except:
                pass
    #Buil cols
    for each in keys:
        if each != '':
            colsDict = {'name' : each.replace('"','')}
            cols.append(colsDict)

    # Building Rows
    for each in grid:
        if each != '':
            values = each.replace('"','').split(',')
            rowsDict = dict(zip(keys, values))
            rows.append(rowsDict)

    result['meta'] = metaDict
    result['cols'] = cols
    result['rows'] = rows

    return result<commit_msg>zincParser: Replace guts with hszinc module.<commit_after># -*- coding: utf-8 -*-
"""
zincToJson allows to convert native haystack 'zinc'
format to Json when connecting to old Jace devices that
aren't able to generate Json response

As Json is easily handled in Python, it's the prefered syntax for web transactions
So everything is built with Json in mind.

It's a patch to support old devices

@author: CTremblay
"""
import json
import hszinc
import datetime

def zincToJson(req):
    # Parse the zinc text into grids
    grids = hszinc.parse(req)

    # Convert this back to a format that pyhaystack understands.
    # JSON grids have the following structure:
    # // JSON
    # {
    #   "meta": {"ver":"2.0", "projName":"test"},
    #   "cols":[
    #     {"name":"dis", "dis":"Equip Name"},
    #     {"name":"equip"},
    #     {"name":"siteRef"},
    #     {"name":"installed"}
    #   ],
    #   "rows":[
    #     {"dis":"RTU-1", "equip":"m:",
    #       "siteRef":"r:153c-699a HQ", "installed":"d:2005-06-01"},
    #     {"dis":"RTU-2", "equip":"m:",
    #       "siteRef":"r:153c-699a HQ", "installed":"d:999-07-12"}
    #   ]
    # }
    #
    # There is only support for one grid at a time.  Out of pure laziness on my
    # part, this does not convert scalar values to JSON format but rather, the
    # equivalent Python type.
    if len(grids) != 1:
        raise ValueError('Unable to handle result with %d grids' % \
                len(grids))

    grid = grids[0]
    grid_meta = {'ver':'2.0'}
    grid_cols = []
    grid_rows = []
    json_grid = {
            'meta': grid_meta,
            'cols': grid_cols,
            'rows': grid_rows,
    }

    grid_meta.update(grid.metadata)
    def _col_to_json(c):
        (name, meta) = c
        json_col = dict(meta)
        json_col['name'] = name
        return json_col
    grid_cols.extend(map(_col_to_json, grid.column.items()))
    grid_rows.extend(grid)
    return json_grid
{'fraction_non_alphanumeric': 0.14189584552369808, 'fraction_numerical': 0.013165593914569925, 'mean_word_length': 2.4053784860557768, 'pattern_counts': {'":': 19, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5257601', 'n_tokens_mistral': 1116, 'n_tokens_neox': 1057, 'n_words': 333}