mirror of
https://github.com/netbox-community/netbox.git
synced 2024-05-10 07:54:54 +00:00
41 lines
1.0 KiB
Python
41 lines
1.0 KiB
Python
from __future__ import unicode_literals
|
|
|
|
import six
|
|
|
|
|
|
def csv_format(data):
|
|
"""
|
|
Encapsulate any data which contains a comma within double quotes.
|
|
"""
|
|
csv = []
|
|
for value in data:
|
|
|
|
# Represent None or False with empty string
|
|
if value in [None, False]:
|
|
csv.append('')
|
|
continue
|
|
|
|
# Force conversion to string first so we can check for any commas
|
|
if not isinstance(value, six.string_types):
|
|
value = '{}'.format(value)
|
|
|
|
# Double-quote the value if it contains a comma
|
|
if ',' in value:
|
|
csv.append('"{}"'.format(value))
|
|
else:
|
|
csv.append('{}'.format(value))
|
|
|
|
return ','.join(csv)
|
|
|
|
|
|
def foreground_color(bg_color):
|
|
"""
|
|
Return the ideal foreground color (black or white) for a given background color in hexadecimal RGB format.
|
|
"""
|
|
bg_color = bg_color.strip('#')
|
|
r, g, b = [int(bg_color[c:c + 2], 16) for c in (0, 2, 4)]
|
|
if r * 0.299 + g * 0.587 + b * 0.114 > 186:
|
|
return '000000'
|
|
else:
|
|
return 'ffffff'
|