Skip to content

Commit

Permalink
✨ Feature: Added Document Export.
Browse files Browse the repository at this point in the history
  • Loading branch information
FindMalek committed Mar 9, 2024
1 parent 9571ba5 commit 7c25ec9
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 15 deletions.
40 changes: 33 additions & 7 deletions app/controllers/photoshop_controller.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from photoshop import Session

from app.services.file_service import FileService
from app.services.date_service import DateService
from app.utils.artboard_support import hide_all_layers
from app.utils.cli_utils import show_error_message, show_warning_message, show_info_message

class PhotoshopController:
Expand All @@ -9,23 +11,38 @@ def __init__(self):
self.date_service = DateService()
self.week_date = self.date_service.get_week_date()
self.ps = None
self.document = None

def open_document(self, psd_path):
self.ps = Session()
show_info_message(f"Opened document: {psd_path}")
return self.ps.app.load(psd_path)

def action_document(self, action):
def action_document(self, document=None, layer=None, export_path="", action="save"):
"""
Executes an action on the current Photoshop document.
For example: "save" or "export".
"""

if action == "save":
self.ps.app.activeDocument.save()
show_info_message("Saved changes to document.")
elif action == "export":
self.ps.app.activeDocument.export()
show_info_message("Exported document.")
if(layer is not None):
options = self.ps.PNGSaveOptions()
options.compression = 1
hide_all_layers(document.layers)
layer.visible = True
options.artboard_range = layer.name
export_path = self.file_service.get_exported_path(export_path, layer.name)
document.saveAs(export_path, options=options)
show_info_message("Exported artboard.")
else:
export_path = self.file_service.get_exported_path(export_path, document.name)
options = self.ps.PNGSaveOptions()
options.compression = 1
document.saveAs(export_path, options=options)
show_info_message("Exported document.")

def close_session(self):
show_info_message("Closed Photoshop session.")
Expand Down Expand Up @@ -65,7 +82,6 @@ def find_layer_recursive(layer_set, target_path, current_path=[]):
return None

next_layer_name = target_path[0]

for layer in layer_set.layers:
new_path = current_path + [layer.name]

Expand Down Expand Up @@ -103,17 +119,25 @@ def update_type_artboard(self, file_data, action):
return False

document = self.open_document(file_data['paths']['PSD'])
self.document = document
layer_path = file_data['path_object']['Layers']['Path'].split('/')
target_artboards = file_data['artboards']['Boards']

for artboard in document.layers:
if artboard.name in target_artboards:
self.update_text_in_artboard(artboard, layer_path)
if (action == "export"):
self.action_document(document=document, layer=artboard, export_path=file_data["paths"]["Export"], action=action)

layer_paths = self.extract_layer_paths(file_data['path_object'], "artboard")
self.update_weekdate_layers(document, layer_paths)

self.action_document(action)
if (action == "export"):
for layer in document.layers:
if layer.name == "Main":
self.action_document(document=document, layer=layer, export_path=file_data["paths"]["Export"], action=action)

self.action_document("save")
self.close_session()
return True

Expand All @@ -126,7 +150,9 @@ def update_type_layer(self, file_data, action):
layer_paths = self.extract_layer_paths(file_data['path_object'], "layer")

self.update_weekdate_layers(document, layer_paths)

self.action_document(action)
if (action == "export"):
self.action_document(document=document, export_path=file_data["paths"]["Export"], action=action)

self.action_document("save")
self.close_session()
return True
7 changes: 6 additions & 1 deletion app/services/file_service.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import os
import json

from app.models.client_model import Client

class FileService:
Expand Down Expand Up @@ -89,4 +91,7 @@ def get_client_file(self, name, file_name):

def get_client_files(self, name):
client = self.get_client(name)
return client['files'] if client else None
return client['files'] if client else None

def get_exported_path(self, folder, file_name):
return os.path.join(folder, file_name)
6 changes: 5 additions & 1 deletion app/utils/artboard_support.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,8 @@ def get_artboards(file_data):
"""
if "artboards" in file_data and file_data["artboards"]["Supported"]:
return file_data["artboards"].get("Boards", [])
return []
return []

def hide_all_layers(layers):
for layer in layers:
layer.visible = False
79 changes: 74 additions & 5 deletions cli/commands/export_file.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,85 @@
import typer

from app.services.file_service import FileService
from app.controllers.photoshop_controller import PhotoshopController

def export_file(name: str = typer.Option(..., prompt=True, help="Name of the client."),
from app.utils.artboard_support import supports_artboards
from app.utils.cli_utils import (
show_error_message,
show_success_message,
show_info_message,
show_update_details
)

def export_file(client: str = typer.Option(..., prompt=True, help="Name of the client."),
all_files: bool = typer.Option(False, help="Update all files for the client."),
file: str = typer.Option(None, help="Name of the file to be updated.")):
"""
Update text layers in Photoshop file, and then saves and exports the Photoshop file.
"""
file_service = FileService()
photoshop_controller = PhotoshopController()
# Add logic to update text layers and export artboards based on the arguments
# This is a placeholder for the actual implementation
typer.echo(f"Updated & saved the files for client '{name}'.")

client_data = file_service.get_client(client)
if not client_data:
show_error_message(f"No client found with name '{client}'.")
raise typer.Exit(code=1)

if all_files:
show_info_message(f"Exporting all files for client '{client}' ...")
client_files = file_service.get_client_files(client)

for file in client_files:
file_data = file_service.get_client_file(client, file['name'])
artboards_supported = supports_artboards(file_data)

if artboards_supported:
show_info_message(f"Exporting all artboards in file '{file['name']}' for client '{client}' ...")
show_update_details(client, file_data["name"], file_data['path_object']['Layers']['Path'], file_data['artboards'])

outcome = photoshop_controller.update_type_artboard(file_data, "export")
if outcome:
show_success_message(f"All artboards in file '{file_data['name']}' updated and exported successfully.")
else:
show_error_message(f"File '{file_data['name']}' could not be updated.")

else:
show_info_message(f"Exporting a single layer in file '{file['name']}' for client '{client}' ...")
show_update_details(client, file_data["name"], file_data['path_object'])

outcome = photoshop_controller.update_type_layer(file_data, "export")
if outcome:
show_success_message(f"Layer in file '{file_data['name']}' updated and exported successfully.")
else:
show_error_message(f"File '{file_data['name']}' could not be updated.")

elif file:
file_data = file_service.get_client_file(client, file)
artboards_supported = supports_artboards(file_data)

if artboards_supported:
show_info_message(f"Exporting all artboards in file '{file}' for client '{client}' ...")
show_update_details(client, file_data["name"], file_data['path_object']['Layers']['Path'], file_data['artboards'])

outcome = photoshop_controller.update_type_artboard(file_data, "export")
if outcome:
show_success_message(f"All artboards in file '{file_data['name']}' updated and exported successfully.")
else:
show_error_message(f"File '{file_data['name']}' could not be updated.")

else:
show_info_message(f"Exporting a single layer in file '{file}' for client '{client}' ...")
show_update_details(client, file_data["name"], file_data['path_object'])

outcome = photoshop_controller.update_type_layer(file_data, "export")
if outcome:
show_success_message(f"Layer in file '{file_data['name']}' updated and exported successfully.")
else:
show_error_message(f"File '{file_data['name']}' could not be updated.")

else:
show_error_message("Please specify either '--all-files' or '--file' argument.")
raise typer.Exit(code=1)

if __name__ == "__main__":
typer.run(export_file)
typer.run(export_file)
2 changes: 1 addition & 1 deletion data/client_data.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
"name": "weekly plan",
"paths": {
"PSD": "D:\\Documents\\Graphic Design Packs\\Move U\\Weekly Plan\\Weekly Plan.psb",
"Export": "G:\\My Drive\\Projects\\Move U\\Weekly Plan\\Result",
"Export": "D:\\Documents\\Graphic Design Packs\\Move U\\Weekly Plan\\Result",
"Google Drive": "https://drive.google.com/file/d/1xgWJxo3sJdyOSolphi6NjZzBoFuW0N7y/view"
},
"path_object": {
Expand Down

0 comments on commit 7c25ec9

Please sign in to comment.