Skip to content

Commit cc0f7aa

Browse files
committed
chore: Refactor file_item.py for improved logging and error handling
1 parent 97d93c0 commit cc0f7aa

2 files changed

Lines changed: 27 additions & 25 deletions

File tree

struct_module/commands/generate.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,12 @@ def _create_structure(self, args):
4848
elif isinstance(content, str):
4949
file_item = FileItem({"name": name, "content": content})
5050

51-
file_item.apply_template_variables(template_vars)
52-
file_item.process_prompt(args.dry_run)
53-
file_item.create(
54-
args.base_path,
55-
args.dry_run or False,
56-
args.backup_path or None,
57-
args.file_strategy or 'overwrite'
58-
)
51+
file_item.apply_template_variables(template_vars)
52+
file_item.process_prompt(args.dry_run)
53+
54+
file_item.create(
55+
args.base_path,
56+
args.dry_run or False,
57+
args.backup or None,
58+
args.file_strategy or 'overwrite'
59+
)

struct_module/file_item.py

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
class FileItem:
1717
def __init__(self, properties):
18+
self.logger = logging.getLogger(__name__)
1819
self.name = properties.get("name")
1920
self.content = properties.get("content")
2021
self.remote_location = properties.get("file")
@@ -27,18 +28,18 @@ def __init__(self, properties):
2728
)
2829

2930
if not openai_model:
30-
logging.info("OpenAI model not found. Using default model.")
31+
self.logger.info("OpenAI model not found. Using default model.")
3132
self.openai_model = "gpt-3.5-turbo"
3233
else:
33-
logging.debug(f"Using OpenAI model: {openai_model}")
34+
self.logger.debug(f"Using OpenAI model: {openai_model}")
3435
self.openai_model = openai_model
3536

3637
def process_prompt(self, dry_run=False):
3738
if self.user_prompt:
38-
logging.debug(f"Using user prompt: {self.user_prompt}")
39+
self.logger.debug(f"Using user prompt: {self.user_prompt}")
3940

4041
if not openai_api_key:
41-
logging.warning("Skipping processing prompt as OpenAI API key is not set.")
42+
self.logger.warning("Skipping processing prompt as OpenAI API key is not set.")
4243
return
4344

4445
if not self.system_prompt:
@@ -47,7 +48,7 @@ def process_prompt(self, dry_run=False):
4748
system_prompt = self.system_prompt
4849

4950
if dry_run:
50-
logging.info("[DRY RUN] Would generate content using OpenAI API.")
51+
self.logger.info("[DRY RUN] Would generate content using OpenAI API.")
5152
self.content = "[DRY RUN] Generating content using OpenAI"
5253
return
5354

@@ -60,27 +61,27 @@ def process_prompt(self, dry_run=False):
6061
)
6162

6263
self.content = completion.choices[0].message.content
63-
logging.debug(f"Generated content: {self.content}")
64+
self.logger.debug(f"Generated content: {self.content}")
6465

6566
def fetch_content(self):
6667
if self.remote_location:
67-
logging.debug(f"Fetching content from: {self.remote_location}")
68+
self.logger.debug(f"Fetching content from: {self.remote_location}")
6869
response = requests.get(self.remote_location)
69-
logging.debug(f"Response status code: {response.status_code}")
70+
self.logger.debug(f"Response status code: {response.status_code}")
7071
response.raise_for_status()
7172
self.content = response.text
72-
logging.debug(f"Fetched content: {self.content}")
73+
self.logger.debug(f"Fetched content: {self.content}")
7374

7475
def apply_template_variables(self, template_vars):
7576
if self.content and template_vars:
76-
logging.debug(f"Applying template variables: {template_vars}")
77+
self.logger.debug(f"Applying template variables: {template_vars}")
7778
template = Template(self.content)
7879
self.content = template.substitute(template_vars)
7980

8081
def create(self, base_path, dry_run=False, backup_path=None, file_strategy='overwrite'):
8182
file_path = os.path.join(base_path, self.name)
8283
if dry_run:
83-
logging.info(f"[DRY RUN] Would create file: {file_path} with content: \n\n{self.content}")
84+
self.logger.info(f"[DRY RUN] Would create file: {file_path} with content: \n\n{self.content}")
8485
return
8586

8687
# Create the directory if it does not exist
@@ -90,24 +91,24 @@ def create(self, base_path, dry_run=False, backup_path=None, file_strategy='over
9091
if file_strategy == 'backup' and backup_path:
9192
backup_file_path = os.path.join(backup_path, os.path.basename(file_path))
9293
shutil.copy2(file_path, backup_file_path)
93-
logging.info(f"Backed up existing file: {file_path} to {backup_file_path}")
94+
self.logger.info(f"Backed up existing file: {file_path} to {backup_file_path}")
9495
elif file_strategy == 'skip':
95-
logging.info(f"Skipped existing file: {file_path}")
96+
self.logger.info(f"Skipped existing file: {file_path}")
9697
return
9798
elif file_strategy == 'append':
9899
with open(file_path, 'a') as f:
99100
f.write(self.content)
100-
logging.info(f"Appended to existing file: {file_path}")
101+
self.logger.info(f"Appended to existing file: {file_path}")
101102
return
102103
elif file_strategy == 'rename':
103104
new_name = f"{file_path}.{int(time.time())}"
104105
os.rename(file_path, new_name)
105-
logging.info(f"Renamed existing file: {file_path} to {new_name}")
106+
self.logger.info(f"Renamed existing file: {file_path} to {new_name}")
106107

107108
with open(file_path, 'w') as f:
108109
f.write(self.content)
109-
logging.info(f"Created file: {file_path} with content: {self.content}")
110+
self.logger.info(f"Created file: {file_path} with content: {self.content}")
110111

111112
if self.permissions:
112113
os.chmod(file_path, int(self.permissions, 8))
113-
logging.info(f"Set permissions {self.permissions} for file: {file_path}")
114+
self.logger.info(f"Set permissions {self.permissions} for file: {file_path}")

0 commit comments

Comments
 (0)