Skip to content

Commit

Permalink
Merge pull request #28 from debrief/combine
Browse files Browse the repository at this point in the history
Combine
  • Loading branch information
IanMayo authored Nov 7, 2019
2 parents e5adf5a + e8ce330 commit 4cdc5cd
Show file tree
Hide file tree
Showing 14 changed files with 257 additions and 221 deletions.
69 changes: 10 additions & 59 deletions data_highlight/highlighter.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from .support.char_array import CharIndex
from .support.line import Line
from .support.color_picker import hexColorFor, meanColorFor, colorFor
from .highlighter_functionality.export import export as export_from_functionality


class HighlightedFile:
Expand All @@ -17,12 +17,14 @@ def __init__(self, filename: str, number_of_lines=None):
number_of_lines(int) Number of lines that should be showed
to the output, or all line if omitted
"""
self.chars = []
self.filename = filename
self.dict_color = {}
self.number_of_lines = number_of_lines

#
def charsDebug(self):

def chars_debug(self):
"""
Debug method, to check contents of chars
"""
Expand All @@ -46,55 +48,7 @@ def export(self, filename: str):
Args:
filename (str): The name of the destination for the HTML output
"""
fOut = open(filename, "w")

lastHash = ""

for char_index in self.chars:
letter = char_index.letter
thisHash = ""
thisMessage = ""
colors = []
for usage in char_index.usages:
thisHash += usage.tool_field
needsNewLine = thisMessage != ""
colors.append(colorFor(usage.tool_field, self.dict_color))
if needsNewLine:
thisMessage += " // "
thisMessage += usage.tool_field + ", " + usage.message

# do we have anything to shade?
if thisHash != "":
# generate/retrieve a color for this hash
new_color = meanColorFor(colors)
hex_color = hexColorFor(new_color)

# are we already in hash?
if lastHash != "":
# is it the different to this one?
if lastHash != thisHash:
# ok, close the span
fOut.write("</span>")

# start a new span
fOut.write("<span title='" + thisMessage + "' style=\"background-color:" + hex_color + "\"a>")
else:
fOut.write("<span title='" + thisMessage + "' style=\"background-color:" + hex_color + "\">")
elif lastHash != "":
fOut.write("</span>")

# just check if it's newline
if letter == "\n":
fOut.write("<br>")
else:
fOut.write(letter)

lastHash = thisHash

if lastHash != "":
fOut.write("</span>")

fOut.close()
export_from_functionality(filename, self.chars, self.dict_color)

def limited_lines(self):
"""
Expand Down Expand Up @@ -124,21 +78,18 @@ def not_limited_lines(self):
def fill_char_array(self, string_to_char, array_to_lines):
line_ctr = 0
lines = []
# make the char index the correct length
self.chars = [None] * len(string_to_char)

# initialise the char index
charCtr = 0
for char in string_to_char:
# put letter into a struct
charInd = CharIndex(char)
self.chars[charCtr] = charInd
charCtr += 1
char_ind = CharIndex(char)
self.chars.append(char_ind)


for this_line in array_to_lines:
line_length = len(this_line)
newL = Line(str(line_ctr), str(line_ctr + line_length), this_line, self.chars)
lines.append(newL)
new_l = Line(str(line_ctr), str(line_ctr + line_length), this_line, self.chars)
lines.append(new_l)
line_ctr += line_length + 1

return lines
Empty file.
61 changes: 61 additions & 0 deletions data_highlight/highlighter_functionality/export.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
from ..support.color_picker import hex_color_for, mean_color_for, color_for


def export(filename, chars, dict_colors):
"""
To outsource method from high class and make it more readable
:param filename: Name of f_out
:param chars: pointer of char array
:param dict_colors: pointer of dict colors
:return:
"""

f_out = open(filename, "w")

last_hash = ""

for char_index in chars:
letter = char_index.letter
this_hash = ""
this_message = ""
colors = []
for usage in char_index.usages:
this_hash += usage.tool_field
needs_new_line = this_message != ""
colors.append(color_for(usage.tool_field, dict_colors))
if needs_new_line:
this_message += " // "
this_message += usage.tool_field + ", " + usage.message

# do we have anything to shade?
if this_hash != "":
# generate/retrieve a color for this hash
new_color = mean_color_for(colors)
hex_color = hex_color_for(new_color)

# are we already in hash?
if last_hash != "":
# is it the different to this one?
if last_hash != this_hash:
# ok, close the span
f_out.write("</span>")

# start a new span
f_out.write("<span title='" + this_message + "' style=\"background-color:" + hex_color + "\"a>")
else:
f_out.write("<span title='" + this_message + "' style=\"background-color:" + hex_color + "\">")
elif last_hash != "":
f_out.write("</span>")

# just check if it's newline
if letter == "\n":
f_out.write("<br>")
else:
f_out.write(letter)

last_hash = this_hash

if last_hash != "":
f_out.write("</span>")

f_out.close()
1 change: 1 addition & 0 deletions data_highlight/support/char_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class CharIndex:
def __init__(self, letter):
self.letter = letter
self.usages = []

def __str__(self):
message = "[" + self.letter + "]"
for usage in self.usages:
Expand Down
27 changes: 14 additions & 13 deletions data_highlight/support/color_picker.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import random
import colorsys

def colorFor(hashCode,colorDict):

def color_for(hash_code, color_dict):
# do we have it already?
if hashCode in colorDict:
if hash_code in color_dict:
# yes, job done
return colorDict[hashCode]
return color_dict[hash_code]
else:
# no, generate one
hue = random.random()
Expand All @@ -14,27 +15,27 @@ def colorFor(hashCode,colorDict):
r = int(rgb[0] * 255)
g = int(rgb[1] * 255)
b = int(rgb[2] * 255)
newCol = (r, g, b)
new_col = (r, g, b)
# store the new color
colorDict[hashCode] = newCol
return newCol
color_dict[hash_code] = new_col
return new_col


# convert a 3-element rgb structure to a HTML color definition
def hexColorFor(rgb):
opacityShade = 0.3
return 'rgba(%d,%d,%d,%f)' % (rgb[0], rgb[1], rgb[2], opacityShade)
def hex_color_for(rgb):
opacity_shade = 0.3
return 'rgba(%d,%d,%d,%f)' % (rgb[0], rgb[1], rgb[2], opacity_shade)


# find the mean of the provided colors
def meanColorFor(colorArr):
def mean_color_for(color_arr):
r = 0
g = 0
b = 0
for color in colorArr:
for color in color_arr:
r += color[0]
g += color[1]
b += color[2]

arrLen = len(colorArr)
return (int(r / arrLen), int(g / arrLen), int(b / arrLen))
arr_len = len(color_arr)
return int(r / arr_len), int(g / arr_len), int(b / arr_len)
37 changes: 23 additions & 14 deletions data_highlight/support/line.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,42 @@
from re import finditer
from .token import Token
from .char_array import SingleUsage
from .token import Token, SmallToken
from .char_array import SingleUsage

class Line():

class Line:
"""
a line from the file
"""

WHITESPACE_DELIM = "\\S+"

def __init__(self, start, end, text, chars):
self.start = start
self.end = end
self.text = text
self.chars = chars
self.tokens_array = []

def tokens(self, reg_exp=WHITESPACE_DELIM, strip_char=""):

def tokens(self, regExp=WHITESPACE_DELIM, stripChar=""):
tokens = []
for match in finditer(regExp, self.text):
tokenStr = match.group()
for match in finditer(reg_exp, self.text):
token_str = match.group()
# special handling, we may need to strip a leading delimiter
if stripChar != "":
charIndex = tokenStr.find(stripChar)
if charIndex == 0:
tokenStr = tokenStr[1:]
if strip_char != "":
char_index = token_str.find(strip_char)
if char_index == 0:
token_str = token_str[1:]
# and ditch any new whitespace
tokenStr = tokenStr.strip()
token_str = token_str.strip()
token = SmallToken(match.span(), token_str, int(self.start), self.chars)

# the token object expects an array of tokens,
# since it could be a composite object
arr_of_tokens = [token]

self.tokens_array.append(Token(arr_of_tokens))

tokens.append(Token(match.span(), tokenStr, int(self.start), self.chars))
return tokens
return self.tokens_array

def record(self, tool, message):
for i in range(int(self.start), int(self.end)):
Expand Down
44 changes: 29 additions & 15 deletions data_highlight/support/token.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,33 @@
from .char_array import SingleUsage
class Token():
"""
a single token
"""
from .char_array import SingleUsage


def __init__(self, span, text, lineStart, chars):
class SmallToken:
def __init__(self, span, text, line_start, chars):
self.span = span
self.text = text
self.lineStart = lineStart
self.line_start = line_start
self.chars = chars

def __str__(self):
return "[(" + str(self.start()) + "-" + str(self.end()) + ")" + ":\"" + self.text + "\"]"

def start(self):
return self.lineStart + self.span[0]
return self.line_start + self.span[0]

def end(self):
return self.lineStart + self.span[1]
return self.line_start + self.span[1]


class Token:
"""
a single token
"""

def __init__(self, array_of_tokens):
"""
:param array_of_tokens: Arrays Of token
"""
self.children = array_of_tokens

def token_text(self):
return self.children[0].text

def record(self, tool: str, field: str, value: str, units: str = "n/a"):
"""
Expand All @@ -30,6 +40,10 @@ def record(self, tool: str, field: str, value: str, units: str = "n/a"):
"""
tool_field = tool + "/" + field
message = "Value:" + str(value) + " Units:" + str(units)
for i in range(self.start(), self.end()):
usage = SingleUsage(tool_field, message)
self.chars[i].usages.append(usage)

for token in self.children:
start = token.start()
end = token.end()
for i in range(start, end):
usage = SingleUsage(tool_field, message)
token.chars[i].usages.append(usage)
12 changes: 6 additions & 6 deletions data_highlight/file.txt → files/file.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
951212 050000.000 MONDEO_44 @C 269.7 10.0 10
// EVENT 951212 050300.000 BRAVO
// EVENT 951212 050300.000 CHARLIE
951212 050300.000 FORD_11 @C 354.7 22.1 14
951212 050200.000 COROLLA_44 @C 177.9 36.1 15
// EVENT 951212 050300.000 DELTA
951212 050300.000 COROLLA_44 @C 200 60.1 15
// EVENT 951212 050300.000 BRAVO
// EVENT 951212 050300.000 CHARLIE
951212 050300.000 FORD_11 @C 354.7 22.1 14
951212 050200.000 COROLLA_44 @C 177.9 36.1 15
// EVENT 951212 050300.000 DELTA
951212 050300.000 COROLLA_44 @C 200 60.1 15
12 changes: 6 additions & 6 deletions data_highlight/file_comma.txt → files/file_comma.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
951212, 050000.000, MONDEO_44, @C, 269.7, 2.0, 10
//, EVENT, 951212, 050300.000, BRAVO
//, EVENT, 951212, 050300.000, CHARLIE
951212, 050300.000, FORD_11, @C, 354.7, 2.1, 14
951212, 050200.000, COROLLA_44, @C, 177.9, 3.1, 15
//, EVENT, 951212, 050300.000, DELTA
951212, 050300.000, COROLLA_44, @C, 200, 3.1, 15
//, EVENT, 951212, 050300.000, BRAVO
//, EVENT, 951212, 050300.000, CHARLIE
951212, 050300.000, FORD_11, @C, 354.7, 2.1, 14
951212, 050200.000, COROLLA_44, @C, 177.9, 3.1, 15
//, EVENT, 951212, 050300.000, DELTA
951212, 050300.000, COROLLA_44, @C, 200, 3.1, 15
Loading

0 comments on commit 4cdc5cd

Please sign in to comment.