<commit_before>import unittest

from jsonschema import ValidationError

import manuallabour.core.common as common
from manuallabour.core.graph import Graph
from manuallabour.core.stores import LocalMemoryStore

from manuallabour.core.schedule import *

def schedule_example(store):
    store.add_blob('imb','foo.png')
    store.add_blob('fb','../t.tmp')
    store.add_blob('imb2','b.png')

    store.add_obj(common.Object(obj_id='ta',name='Tool A'))
    store.add_obj(common.Object(
        obj_id='pa',
        name='Part A',
        images=[dict(blob_id='imb2',alt="boo",extension=".png")]
    ))
    store.add_obj(common.Object(obj_id='ra',name='Result A'))

    store.add_step(common.Step(
        step_id='a',
        title="First",
        description="Whack {{part(a)}} with {{tool(a)}} to get {{result(a)}}",
        duration=dict(minutes=15),
        images = {'t_imag' : dict(blob_id='imb',extension='.png',alt='Foo')},
        tools={'a' : dict(obj_id='ta')},
        parts={'a' : dict(obj_id='pa',quantity=2)},
        results={'a' : dict(obj_id='ra',created=True)}
    ))
    store.add_step(common.Step(
        step_id='b',
        title="Second",
        description="Use all {{tool(a)}} to fix {{part(a)}} to {{part(b)}}",
        duration=dict(minutes=15),
        files = {'t_imag' : dict(blob_id='fb',filename='test.tmp')},
        tools={'a' : dict(obj_id='ta',quantity=3)},
        parts={
            'b' : dict(obj_id='ra'),
            'a' : dict( obj_id='pa',optional=True)
        }
    ))
    store.add_step(common.Step(
        step_id='c',
        title="Third",
        description="Add {{part(a)}}",
        duration=dict(minutes=15),
        parts={'a' : dict(obj_id='pa',quantity=3)}
    ))
    store.add_step(common.Step(
        step_id='d',
        title="Third",
        description="Add {{part(a)}}",
        parts={'a' : dict(obj_id='pa',quantity=3)}
    ))


class TestScheduleStep(unittest.TestCase):
    def test_init(self):
        self.assertRaises(
            ValidationError,
            lambda: ScheduleStep(step_id='a')
        )
        s = ScheduleStep(step_id='9',step_idx=0)
        self.assertEqual(s.step_id,'9')
        self.assertEqual(s.step_nr,1)

        self.assertRaises(
            ValueError,
            lambda: ScheduleStep(
                step_id='a'
                ,step_idx=0,
                start=dict(hours=2)
            )
        )
        s=ScheduleStep(
            step_id='rs9',
            step_idx=0,
            start=dict(minutes=4),
            stop=dict(minutes=24)
        )
        self.assertEqual(s.start.total_seconds(),240)
    def test_dereference(self):
        store = LocalMemoryStore()
        schedule_example(store)

        step = ScheduleStep(
            step_id='a',
            step_idx=0,
            start=dict(hours=2),
            stop=dict(hours=2,minutes=15),
        )
        step_dict = step.dereference(store)
        print step_dict<commit_msg>cosmetics<commit_after>import unittest

from jsonschema import ValidationError

import manuallabour.core.common as common
from manuallabour.core.graph import Graph
from manuallabour.core.stores import LocalMemoryStore

from manuallabour.core.schedule import *

def schedule_example(store):
    store.add_blob('imb','foo.png')
    store.add_blob('fb','../t.tmp')
    store.add_blob('imb2','b.png')

    store.add_obj(common.Object(obj_id='ta',name='Tool A'))
    store.add_obj(common.Object(
        obj_id='pa',
        name='Part A',
        images=[dict(blob_id='imb2',alt="boo",extension=".png")]
    ))
    store.add_obj(common.Object(obj_id='ra',name='Result A'))

    store.add_step(common.Step(
        step_id='a',
        title="First",
        description="Whack {{part(a)}} with {{tool(a)}} to get {{result(a)}}",
        duration=dict(minutes=15),
        images = {'t_imag' : dict(blob_id='imb',extension='.png',alt='Foo')},
        tools={'a' : dict(obj_id='ta')},
        parts={'a' : dict(obj_id='pa',quantity=2)},
        results={'a' : dict(obj_id='ra',created=True)}
    ))
    store.add_step(common.Step(
        step_id='b',
        title="Second",
        description="Use all {{tool(a)}} to fix {{part(a)}} to {{part(b)}}",
        duration=dict(minutes=15),
        files = {'t_imag' : dict(blob_id='fb',filename='test.tmp')},
        tools={'a' : dict(obj_id='ta',quantity=3)},
        parts={
            'b' : dict(obj_id='ra'),
            'a' : dict( obj_id='pa',optional=True)
        }
    ))
    store.add_step(common.Step(
        step_id='c',
        title="Third",
        description="Add {{part(a)}}",
        duration=dict(minutes=15),
        parts={'a' : dict(obj_id='pa',quantity=3)}
    ))
    store.add_step(common.Step(
        step_id='d',
        title="Third",
        description="Add {{part(a)}}",
        parts={'a' : dict(obj_id='pa',quantity=3)}
    ))


class TestScheduleStep(unittest.TestCase):
    def test_init(self):
        self.assertRaises(
            ValidationError,
            lambda: ScheduleStep(step_id='a')
        )
        s = ScheduleStep(step_id='9',step_idx=0)
        self.assertEqual(s.step_id,'9')
        self.assertEqual(s.step_nr,1)

        self.assertRaises(
            ValueError,
            lambda: ScheduleStep(
                step_id='a'
                ,step_idx=0,
                start=dict(hours=2)
            )
        )
        s=ScheduleStep(
            step_id='rs9',
            step_idx=0,
            start=dict(minutes=4),
            stop=dict(minutes=24)
        )
        self.assertEqual(s.start.total_seconds(),240)
    def test_dereference(self):
        store = LocalMemoryStore()
        schedule_example(store)

        step = ScheduleStep(
            step_id='a',
            step_idx=0,
            start=dict(hours=2),
            stop=dict(hours=2,minutes=15),
        )
        step_dict = step.dereference(store)
{'fraction_non_alphanumeric': 0.1659877800407332, 'fraction_numerical': 0.010522742701968771, 'mean_word_length': 2.4321490972626676, 'pattern_counts': {'":': 0, '<': 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': '6019000', 'n_tokens_mistral': 1981, 'n_tokens_neox': 1923, 'n_words': 263}