Coverage for sources/agentsmgr/commands/operations.py: 10%
34 statements
« prev ^ index » next coverage.py v7.10.7, created at 2025-10-13 00:43 +0000
« prev ^ index » next coverage.py v7.10.7, created at 2025-10-13 00:43 +0000
1# vim: set filetype=python fileencoding=utf-8:
2# -*- coding: utf-8 -*-
4#============================================================================#
5# #
6# Licensed under the Apache License, Version 2.0 (the "License"); #
7# you may not use this file except in compliance with the License. #
8# You may obtain a copy of the License at #
9# #
10# http://www.apache.org/licenses/LICENSE-2.0 #
11# #
12# Unless required by applicable law or agreed to in writing, software #
13# distributed under the License is distributed on an "AS IS" BASIS, #
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
15# See the License for the specific language governing permissions and #
16# limitations under the License. #
17# #
18#============================================================================#
21''' Core operations for content generation and directory population.
23 This module provides functions for orchestrating content generation,
24 including directory population and file writing operations with
25 simulation support.
26'''
29from . import __
30from . import generator as _generator
33def populate_directory(
34 generator: _generator.ContentGenerator,
35 target: __.Path,
36 simulate: bool = False
37) -> tuple[ int, int ]:
38 ''' Generates all content items to target directory.
40 Orchestrates content generation for all coders and item types
41 configured in generator. Returns tuple of (items_attempted,
42 items_written).
43 '''
44 items_attempted = 0
45 items_written = 0
46 for coder_name in generator.configuration[ 'coders' ]:
47 for item_type in ( 'commands', 'agents' ):
48 attempted, written = produce_coder_item_type(
49 generator, coder_name, item_type, target, simulate )
50 items_attempted += attempted
51 items_written += written
52 return ( items_attempted, items_written )
55def produce_coder_item_type(
56 generator: _generator.ContentGenerator,
57 coder: str,
58 item_type: str,
59 target: __.Path,
60 simulate: bool
61) -> tuple[ int, int ]:
62 ''' Produces items of specific type for a coder.
64 Generates all items (commands or agents) for specified coder by
65 iterating through configuration files. Returns tuple of
66 (items_attempted, items_written).
67 '''
68 items_attempted = 0
69 items_written = 0
70 if generator.mode == 'nowhere':
71 return ( items_attempted, items_written )
72 configuration_directory = (
73 generator.location / 'configurations' / item_type )
74 if not configuration_directory.exists( ):
75 return ( items_attempted, items_written )
76 for configuration_file in configuration_directory.glob( '*.toml' ):
77 items_attempted += 1
78 result = generator.render_single_item(
79 item_type, configuration_file.stem, coder, target )
80 if update_content( result.content, result.location, simulate ):
81 items_written += 1
82 return ( items_attempted, items_written )
85def update_content(
86 content: str, location: __.Path, simulate: bool = False
87) -> bool:
88 ''' Updates content file, creating directories as needed.
90 Writes content to specified location, creating parent directories
91 if necessary. In simulation mode, no actual writing occurs.
92 Returns True if file was written, False if simulated.
93 '''
94 if simulate: return False
95 try: location.parent.mkdir( parents = True, exist_ok = True )
96 except ( OSError, IOError ) as exception:
97 raise __.FileOperationFailure(
98 location.parent, "create directory" ) from exception
99 try: location.write_text( content, encoding = 'utf-8' )
100 except ( OSError, IOError ) as exception:
101 raise __.FileOperationFailure(
102 location, "update content" ) from exception
103 return True