"""Casting poems into hexadecimal

Author: Paul Müller (https://pycache.de)
License: CC BY-SA 4.0 (https://creativecommons.org/licenses/by-sa/4.0/)
Version: 1.0 (2018)
"""

rife = """
rife malpractice
newcomer snakist
delicate heights
seemingly blight
notes went amiss
cat made me this
birds there rest
cases won't mess
mutable standard
mystical conduct
scope alteration
world domination
noble changeling
inherited pippin
fathomless print
for Xerox a hint
"""


# Ode to spot
# Felis catus is your taxonomic nomenclature.
# An endothermic quadruped, carnivorous by nature.
# Your visual, olfactory, and auditory senses
# Contribute to your hunting skill and natural defences.
# I find myself intrigued by your subvocal oscillations.
# A singular development of cat communications
# That obviates your basic hedonistic predilection,
# For a rhythmic stroking of your fur to demonstrate affection.
# A tail is quite essential for your acrobatic talents.
# You would not be so agile if you lacked its counterbalance.
# And when not being utilised to aid in locomotion
# It often serves to illustrate the state of your emotions.
# Oh, Spot, the complex levels of behaviour you display
# Connote a fairly well developed cognitive array.
# And though you are not sentient, Spot, and do not comprehend
# I nonetheless consider you a true and valued friend.

spot = """
hexadecimal spot
ecce felis catus
carnivore status
esthesia focused
nature momentous
infrasound words
joyous discourse
altruistic shine
need stroke mine
constituent tail
balance prevails
feeling conveyed
gorgeous display
perceptual array
can't comprehend
such true friend
"""


def poem_to_hex(poem):
    """Convert a string to a hexadecimal poem"""
    poem = poem.strip()
    lines = poem.split("\n")
    hexlines = []
    for ii, line in enumerate(lines):
        hline = []
        for char in line:
            hline.append(hex(ord(char))[2:])
        hexlines.append("{:08X}".format((ii+1)*16)
                        + "    "
                        + " ".join(hline)
                        + "    "
                        + line)
    hexpoem = "\n".join(hexlines)
    # also save a binary file for user convenience
    with open("{}.bin".format(lines[0].replace(" ", "_")), "wb") as fd:
        for line in lines:
            fd.write(line)
    return hexpoem


print(poem_to_hex(rife))
print("")
print(poem_to_hex(spot))
