Skip to content

Commit 232b398

Browse files
committed
Refactor io to pass folders as params
1 parent 4005137 commit 232b398

File tree

3 files changed

+42
-36
lines changed

3 files changed

+42
-36
lines changed

src/scheduler/cli.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def build(
7979

8080
kwargs = {}
8181
if objective == 'consistency' or diff:
82-
original_solution = io.import_solution()
82+
original_solution = io.import_solution(session.folders['solution'])
8383
revised_solution = [
8484
item for item in original_solution
8585
if item[0] < len(events)]
@@ -103,8 +103,10 @@ def build(
103103
allocations = defn.allocations(resources)
104104
defn.add_allocations(events, slots, solution, allocations)
105105
logger.debug(convert.schedule_to_text(solution, events, slots))
106-
io.export_solution_and_definition(resources, events, slots, solution)
107-
io.build_output(resources, events, slots, solution)
106+
io.export_solution_and_definition(
107+
resources, events, slots, solution, session.folders['solution'])
108+
io.build_output(
109+
resources, events, slots, solution, session.folders['build'])
108110

109111

110112
@scheduler.command()
@@ -124,18 +126,18 @@ def validate(verbosity, input_dir, solution_dir, reload):
124126
if solution_dir:
125127
session.folders['solution'] = Path(solution_dir)
126128

127-
solution = io.import_solution()
129+
solution = io.import_solution(session.folders['solution'])
128130

129131
if reload:
130132
resources = defn.resources()
131133
events, slots = events_and_slots(resources)
132-
original_solution = io.import_solution()
134+
original_solution = io.import_solution(session.folders['solution'])
133135
solution = [
134136
item for item in original_solution
135137
if item[0] < len(events)]
136138
else:
137-
solution = io.import_solution()
138-
definition = io.import_schedule_definition()
139+
solution = io.import_solution(session.folders['solution'])
140+
definition = io.import_schedule_definition(session.folders['solution'])
139141
events = definition['events']
140142
slots = definition['slots']
141143

@@ -166,14 +168,14 @@ def rebuild(verbosity, solution_dir, build_dir):
166168
if build_dir:
167169
session.folders['build'] = Path(build_dir)
168170

169-
solution = io.import_solution()
170-
definition = io.import_schedule_definition()
171+
solution = io.import_solution(session.folders['solution'])
172+
definition = io.import_schedule_definition(session.folders['solution'])
171173
logger.info('Validating schedule...')
172174
if is_valid_solution(solution, definition['events'], definition['slots']):
173175
logger.info('Imported solution is valid')
174176
io.build_output(
175177
definition['resources'], definition['events'],
176-
definition['slots'], solution)
178+
definition['slots'], solution, session.folders['build'])
177179
else:
178180
for v in solution_violations(
179181
solution, definition['events'], definition['slots']):

src/scheduler/define.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@
66
import daiquiri
77

88
import scheduler.denormalise as dn
9-
from scheduler import io
9+
from scheduler import io, session
1010

1111
logger = daiquiri.getLogger(__name__)
1212

1313

1414
def resources():
15-
resources = io.import_yaml()
16-
resources['events'] = io.import_proposals(resources)
15+
resources = io.import_yaml(session.folders['input'])
16+
resources['events'] = io.import_proposals(
17+
resources, session.folders['input'])
1718
return resources
1819

1920

src/scheduler/io.py

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,17 @@
99
from ruamel.yaml import YAML
1010
from slugify import slugify
1111

12-
from scheduler import session
13-
1412
logger = daiquiri.getLogger(__name__)
1513
yaml = YAML(typ='safe')
1614
yaml.default_flow_style = False
1715

1816

19-
def import_yaml():
17+
def import_yaml(input_folder):
2018
"""Import all yaml files in the given folder into a single resources
2119
dict"""
2220
yaml_resources = {}
2321
yaml_files = [
24-
path for path in session.folders['input'].iterdir()
22+
path for path in input_folder.iterdir()
2523
if path.suffix == '.yml']
2624
for path in yaml_files:
2725
with path.open('r') as file:
@@ -30,10 +28,10 @@ def import_yaml():
3028
return yaml_resources
3129

3230

33-
def import_proposals(resources):
31+
def import_proposals(resources, input_folder):
3432
"""Import the proposals data from a .csv file"""
3533
proposals = []
36-
with Path(session.folders['input'], 'proposals.csv').open('r') as file:
34+
with Path(input_folder, 'proposals.csv').open('r') as file:
3735
reader = csv.DictReader(file)
3836
for row in reader:
3937
if row['session_type'] in resources['event_types']:
@@ -50,9 +48,9 @@ def import_proposals(resources):
5048
return proposals
5149

5250

53-
def import_solution():
51+
def import_solution(solution_folder):
5452
"""Import a previously computed schedule from a .csv file"""
55-
csv_file = Path(session.folders['solution'], 'schedule.csv')
53+
csv_file = Path(solution_folder, 'schedule.csv')
5654
logger.info(f'Importing schedule from {csv_file}')
5755
solution = []
5856
with Path(csv_file).open('r') as file:
@@ -65,20 +63,22 @@ def import_solution():
6563
return solution
6664

6765

68-
def import_schedule_definition():
66+
def import_schedule_definition(solution_folder):
6967
"""Import previously pickled schedule"""
70-
pickle_file = Path(session.folders['solution'], 'scheduler.pickle')
68+
pickle_file = Path(solution_folder, 'scheduler.pickle')
7169
logger.info(
7270
f'Importing resources, events, slots and schedule from {pickle_file}')
7371
with pickle_file.open('rb') as f:
7472
bundle = pickle.load(f)
7573
return bundle
7674

7775

78-
def pickle_solution_and_definition(resources, events, slots, solution):
76+
def pickle_solution_and_definition(
77+
resources, events, slots, solution, solution_folder
78+
):
7979
"""Store the computed solution, the resources dict and the associated
8080
events and slots lists in pickle format"""
81-
pickle_file = Path(session.folders['solution'], 'scheduler.pickle')
81+
pickle_file = Path(solution_folder, 'scheduler.pickle')
8282
logger.info(
8383
f'Pickling resources, events, slots and schedule to {pickle_file}')
8484
bundle = {
@@ -91,9 +91,9 @@ def pickle_solution_and_definition(resources, events, slots, solution):
9191
pickle.dump(bundle, f, pickle.HIGHEST_PROTOCOL)
9292

9393

94-
def export_schedule(solution, events, slots):
94+
def export_schedule(solution, events, slots, solution_folder):
9595
"""Write a human readable .csv file of the computed solution"""
96-
csv_file = Path(session.folders['solution'], 'schedule.csv')
96+
csv_file = Path(solution_folder, 'schedule.csv')
9797
logger.info(f'Exporting schedule to {csv_file}')
9898

9999
schedule = converter.solution_to_schedule(solution, events, slots)
@@ -114,18 +114,21 @@ def export_schedule(solution, events, slots):
114114
writer.writerow(item)
115115

116116

117-
def export_solution_and_definition(resources, events, slots, solution):
118-
session.folders['solution'].mkdir(exist_ok=True)
119-
pickle_solution_and_definition(resources, events, slots, solution)
120-
export_schedule(solution, events, slots)
117+
def export_solution_and_definition(
118+
resources, events, slots, solution, solution_folder
119+
):
120+
solution_folder.mkdir(exist_ok=True)
121+
pickle_solution_and_definition(
122+
resources, events, slots, solution, solution_folder)
123+
export_schedule(solution, events, slots, solution_folder)
121124

122125

123-
def build_output(resources, events, slots, solution):
126+
def build_output(resources, events, slots, solution, build_folder):
124127
"""Create the yaml files required by the conference django-amber based
125128
website for display of the programme"""
126-
logger.info(f'Creating output files in {session.folders["build"]}...')
127-
shutil.rmtree(session.folders['build'], ignore_errors=True)
128-
session.folders['build'].mkdir()
129+
logger.info(f'Creating output files in {build_folder}...')
130+
shutil.rmtree(build_folder, ignore_errors=True)
131+
build_folder.mkdir()
129132

130133
day_format = '%A %-d'
131134
start_format = '%H:%M'
@@ -144,7 +147,7 @@ def build_output(resources, events, slots, solution):
144147
'title': events[item[0]].name,
145148
}
146149

147-
folder = Path(session.folders['build'], day, venue)
150+
folder = Path(build_folder, day, venue)
148151
folder.mkdir(parents=True, exist_ok=True)
149152
with Path(folder, start_time).open('a') as f:
150153
yaml.dump(content, f)

0 commit comments

Comments
 (0)