# Path: pym2149/main.py
# def main_lc2txt(args = sys.argv[1:]):
#     'Render a Lurlene song to logging.'
#     from . import txt
#     initlogging()
#     config, di = boot(ConfigName('inpath', '--section', name = 'txt', args = args))
#     with di:
#         di.add(loadcontext)
#         di.add(LurleneBridge)
#         txt.configure(di)
#         di.add(SimpleChipTimer)
#         di.add(LogicalBundle)
#         di.add(Player)
#         di.all(Started)
#         di(MainThread).sleep()
# 
# def main_lc2wav(args = sys.argv[1:]):
#     'Render a Lurlene song to WAV.'
#     from . import out
#     initlogging()
#     config, di = boot(ConfigName('inpath', '--section', 'outpath', args = args))
#     with di:
#         di.add(loadcontext)
#         di.add(LurleneBridge)
#         out.configure(di)
#         di.add(ChipTimer)
#         di.add(LogicalBundle)
#         di.add(Player)
#         di.all(Started)
#         di(MainThread).sleep()
# 
# Path: pym2149/power.py
# def batterypower():
#     try:
#         from lagoon import upower
#     except ImportError:
#         return # Run all tests.
#     def states():
#         for line in upower.__show_info('/org/freedesktop/UPower/devices/battery_C173').splitlines():
#             words = wordpattern.findall(line)
#             if 2 == len(words) and 'state:' == words[0]:
#                 yield words[1]
#     states = list(states())
#     if states:
#         state, = states
#         return statetobatterypower[state]


from .main import main_lc2txt, main_lc2wav
from .power import batterypower
from base64 import a85encode
from lagoon import sox
from lurlene.util import threadlocals
from pathlib import Path
from PIL import Image, ImageChops
from tempfile import NamedTemporaryFile
from unittest import TestCase
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with pym2149.  If not, see <http://www.gnu.org/licenses/>.


project = Path(__file__).parent.parent
expecteddir = project / 'expected'
actualdir = project / 'actual'
pngsuffix = '.png'

def test_expected():
    if batterypower():
        return
    for path in expecteddir.glob('**/*'):
        if not path.is_dir():
            yield (_comparepng if path.name.endswith(pngsuffix) else _comparetxt), path

def _comparetxt(path):
    relpath = path.relative_to(expecteddir)
    actualpath = actualdir / relpath
    actualpath.parent.mkdir(parents = True, exist_ok = True)
    configpath = project / relpath.parent / f"{relpath.name}.arid"
    if configpath.exists():
        with configpath.open() as f:
            config = ['--config', f.read()]
    else:
        config = []
    with open(actualpath, 'w') as stream, threadlocals(stream = stream):

# Path: pym2149/main.py
# def main_lc2txt(args = sys.argv[1:]):
#     'Render a Lurlene song to logging.'
#     from . import txt
#     initlogging()
#     config, di = boot(ConfigName('inpath', '--section', name = 'txt', args = args))
#     with di:
#         di.add(loadcontext)
#         di.add(LurleneBridge)
#         txt.configure(di)
#         di.add(SimpleChipTimer)
#         di.add(LogicalBundle)
#         di.add(Player)
#         di.all(Started)
#         di(MainThread).sleep()
# 
# def main_lc2wav(args = sys.argv[1:]):
#     'Render a Lurlene song to WAV.'
#     from . import out
#     initlogging()
#     config, di = boot(ConfigName('inpath', '--section', 'outpath', args = args))
#     with di:
#         di.add(loadcontext)
#         di.add(LurleneBridge)
#         out.configure(di)
#         di.add(ChipTimer)
#         di.add(LogicalBundle)
#         di.add(Player)
#         di.all(Started)
#         di(MainThread).sleep()
# 
# Path: pym2149/power.py
# def batterypower():
#     try:
#         from lagoon import upower
#     except ImportError:
#         return # Run all tests.
#     def states():
#         for line in upower.__show_info('/org/freedesktop/UPower/devices/battery_C173').splitlines():
#             words = wordpattern.findall(line)
#             if 2 == len(words) and 'state:' == words[0]:
#                 yield words[1]
#     states = list(states())
#     if states:
#         state, = states
#         return statetobatterypower[state]


# Path: pym2149/test_expected.py
from .main import main_lc2txt, main_lc2wav
from .power import batterypower
from base64 import a85encode
from lagoon import sox
from lurlene.util import threadlocals
from pathlib import Path
from PIL import Image, ImageChops
from tempfile import NamedTemporaryFile
from unittest import TestCase

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with pym2149.  If not, see <http://www.gnu.org/licenses/>.


project = Path(__file__).parent.parent
expecteddir = project / 'expected'
actualdir = project / 'actual'
pngsuffix = '.png'

def test_expected():
    if batterypower():
        return
    for path in expecteddir.glob('**/*'):
        if not path.is_dir():
            yield (_comparepng if path.name.endswith(pngsuffix) else _comparetxt), path

def _comparetxt(path):
    relpath = path.relative_to(expecteddir)
    actualpath = actualdir / relpath
    actualpath.parent.mkdir(parents = True, exist_ok = True)
    configpath = project / relpath.parent / f"{relpath.name}.arid"
    if configpath.exists():
        with configpath.open() as f:
            config = ['--config', f.read()]
    else:
        config = []
    with open(actualpath, 'w') as stream, threadlocals(stream = stream):

        main_lc2txt(['--ignore-settings', *config,