Changeset a438158 in flowtimer


Ignore:
Timestamp:
08/23/24 17:38:20 (9 months ago)
Author:
Enrico Schwass <ennoausberlin@…>
Branches:
guix
Children:
0ec1bff
Parents:
bd94da0
Message:

more steps towards a full schedule implementation with json representation

Location:
flowtimer
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • flowtimer/Phase.py

    rbd94da0 ra438158  
    5151
    5252    def is_sequence(self):
    53         return False 
     53        return False
    5454
    5555    def abort(self):
  • flowtimer/RecurringPhaseSequence.py

    rbd94da0 ra438158  
    7373
    7474    def finished(self):
    75         return (self.passes_left < 1) and (not self.upcoming_phases_in_pass())
     75        return ((self.passes_left < 1) and
     76                (not self.upcoming_phases_in_pass() and
     77                 self.current_phase.finished()))
    7678
    7779    def abort(self):
     
    8890            return
    8991        else:
     92            print("Sequence finished")
    9093            if self.passes_left == 0:
    9194                self.abort()
  • flowtimer/Schedule.py

    rbd94da0 ra438158  
    11import json
     2from flowtimer.Phase import Phase
     3from flowtimer.RecurringPhaseSequence import RecurringPhaseSequence
    24
    35"""
    46I represent a Schedule consisting of blocks. Blocks can be a single phase or
    5 a sequence of phases. 
     7a sequence of phases.
    68"""
    79
     
    911class Schedule:
    1012
    11     def __init__(self, block_list):
    12         assert block_list is not []
    13         self.block_list = block_list
    14         self.current_block = block_list[0]
     13    def __init__(self, title, blocks):
     14        assert blocks is not []
     15        self.title = title
     16        self.blocks = blocks
     17        self.current_block = blocks[0]
    1518        self.state = "initial"
     19
     20    @classmethod
     21    def default_json_string(cls):
     22        return json.dumps(
     23            {"title": "Default",
     24                "blocks": [
     25                    {"type": "Phase",
     26                     "title": "MorningHuddle",
     27                     "initial_ticks": 900
     28                     },
     29                    {"type": "Sequence",
     30                     "title": "AM",
     31                     "sequence": [
     32                         {
     33                             "title": "Tasking",
     34                             "initial_ticks": 120
     35                         },
     36                         {
     37                             "title": "Working",
     38                             "initial_ticks": 5400
     39                         },
     40                         {
     41                             "title": "Syncing",
     42                             "initial_ticks": 300
     43                         },
     44                         {
     45                             "title": "Break",
     46                             "initial_ticks": 600
     47                         }
     48                     ],
     49                     "initial_repetitions": 2
     50                     }
     51                ]
     52             }
     53        )
     54
     55    @classmethod
     56    def from_json(cls, a_json_string):
     57        def custom_object_hook(d):
     58            if 'title' in d and 'blocks' in d:
     59                return Schedule(d['title'], d['blocks'])
     60            if 'title' in d and 'initial_ticks' in d:
     61                return Phase(d['title'], d['initial_ticks'])
     62            if 'sequence' in d and 'initial_repetitions' in d:
     63                return RecurringPhaseSequence(d["title"], d['sequence'], d['initial_repetitions'])
     64            print("Wrong format")
     65            return d
     66        return json.loads(a_json_string, object_hook=custom_object_hook)
     67
     68    @classmethod
     69    def default(cls):
     70        return cls.from_json(cls.default_json_string())
    1671
    1772    def to_json(self):
     
    3691
    3792    def finished(self):
    38         if (self.current_block.finished()) and (self.block_list[-1] == self.current_block):
     93        if (self.current_block.finished()) and (self.blocks[-1] == self.current_block):
    3994            self.state = "finished"
    4095            return True
     
    4398
    4499    def skip(self):
    45         if self.current_block.is_sequence:
     100        if self.current_block.is_sequence():
    46101            self.current_block.skip()
    47         else:
    48             if self.current_block_is_final():
    49                 self.abort()
    50             else:
    51                 index = self.block_list.index(self.current_block)
    52                 self.current_block = self.block_list[index+1]
     102            print("Skip the next phase in sequence")
     103            return
     104        if self.current_block_is_final():
     105            print("Time over")
     106            self.abort()
     107            return
     108        print("Jump to next block")
     109        index = self.blocks.index(self.current_block)
     110        self.current_block = self.blocks[index+1]
     111        return
    53112
    54113    def total_ticks_left(self):
     
    57116
    58117    def upcoming_blocks(self):
    59         index = self.block_list.index(self.current_block)
    60         if index < len(self.block_list):
    61             return self.block_list[index+1:]
     118        index = self.blocks.index(self.current_block)
     119        if index < len(self.blocks):
     120            return self.blocks[index+1:]
    62121        return []
    63122
    64123    def current_block_is_final(self):
    65         index = self.block_list.index(self.current_block)
    66         return index == (len(self.block_list) - 1)
     124        index = self.blocks.index(self.current_block)
     125        return index == (len(self.blocks) - 1)
    67126
    68     def tick(self, duration):
     127    def tick(self, ticks):
    69128        if not self.finished():
    70             self.current_block.tick(duration)
     129            self.current_block.tick(ticks)
    71130            if self.current_block.finished():
    72131                if self.current_block_is_final():
  • flowtimer/configs/default.json

    rbd94da0 ra438158  
    11{ "title": "Default",
    2   "schedule": [
     2  "blocks": [
    33      {"type": "Phase",
    44       "title": "MorningHuddle",
  • flowtimer/main.py

    rbd94da0 ra438158  
    33from tkinter import filedialog
    44from Schedule import Schedule
    5 from RecurringPhaseSequence import RecurringPhaseSequence
    65from PIL import Image, ImageTk
    76from pathlib import Path
     
    162161    def skip(self):
    163162        self.schedule.skip()
     163        if self.schedule.finished():
     164            print("finished")
    164165
    165166    def toggle_tick(self):
     
    207208
    208209    def change_config(self, json_string):
    209         self.schedule = Schedule([RecurringPhaseSequence.from_json(json_string)])
     210        self.schedule = Schedule.from_json(json_string)
    210211
    211212    def load_config(self, file_name=Path(__file__).parent / 'configs' / 'default.json'):
Note: See TracChangeset for help on using the changeset viewer.