Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
HenestrosaDev committed Mar 19, 2023
2 parents 415597d + f6f3d8e commit cc8f535
Show file tree
Hide file tree
Showing 14 changed files with 182 additions and 114 deletions.
14 changes: 1 addition & 13 deletions src/mobile_strings_converter/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,7 @@
# @author: José Carlos López Henestrosa

"""Imports for the mobile-strings-converter package."""
from .converter import (
to_android,
to_csv,
to_google_sheets,
to_html,
to_ios,
to_json,
to_md,
to_ods,
to_pdf,
to_xlsx,
to_yaml,
)
from .converter import convert_strings, to_google_sheets

# Constants
__version__ = "0.1.0"
Expand Down
38 changes: 3 additions & 35 deletions src/mobile_strings_converter/__main__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import argparse
from pathlib import Path

import converter as conv
from console_style import ConsoleStyle
from converter import convert_strings, to_google_sheets


def main():
Expand Down Expand Up @@ -70,46 +70,14 @@ def main():
)
return
elif args.google_sheets and args.credentials:
conv.to_google_sheets(
to_google_sheets(
input_filepath,
sheet_name=args.google_sheets,
credentials_filepath=Path(args.credentials),
should_print_comments=args.print_comments,
)

if args.output_filepath:
conversion_functions = {
".csv": conv.to_csv,
".xlsx": conv.to_xlsx,
".ods": conv.to_ods,
".md": conv.to_md,
".json": conv.to_json,
".yaml": conv.to_yaml,
".html": conv.to_html,
".strings": conv.to_ios,
".xml": conv.to_android,
".pdf": conv.to_pdf,
}

output_path = Path(args.output_filepath)

if output_path.suffix in conversion_functions:
conversion_functions[output_path.suffix](
input_filepath, output_path, args.print_comments
)
else:
print(
f"{ConsoleStyle.YELLOW}File type not supported. Feel free to create "
f"an issue here (https://github.com/HenestrosaConH/mobile-strings"
f"-converter/issues) if you want the file type to be supported by the "
f"package.{ConsoleStyle.END}"
)
return

print(
f"{ConsoleStyle.GREEN}Data successfully written to {output_path}"
f"{ConsoleStyle.END}"
)
convert_strings(input_filepath, Path(args.output_path), args.print_comments)


if __name__ == "__main__":
Expand Down
197 changes: 168 additions & 29 deletions src/mobile_strings_converter/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import warnings
from contextlib import redirect_stderr, redirect_stdout
from pathlib import Path
from typing import List

import gspread
import openpyxl
Expand All @@ -15,8 +16,80 @@
from google.oauth2.credentials import Credentials
from lingua import LanguageDetectorBuilder

from .console_style import ConsoleStyle


def convert_strings(
input_filepath: Path, output_filepath: Path, should_print_comments: bool
):
"""
Extracts strings from the input file in either .xml or .strings format and converts
them to the desired output file format. The output file format can be any of the
following:
- Android strings format (*.xml)
- CSV
- HTML
- iOS strings format (*.strings)
- JSON
- MD
- ODS
- PDF
- XLSX
- YAML
:param input_filepath: .strings or .xml file to extract the strings
:type input_filepath: Path
:param output_filepath: Name of the sheet to be generated
:type output_filepath: Path
:param should_print_comments: True if the user wants to print comments from
.strings/.xml to the output file
:type should_print_comments: bool
"""

strings = get_strings(input_filepath, should_print_comments)

if output_filepath:
conversion_functions = {
".csv": to_csv,
".xlsx": to_sheet,
".ods": to_sheet,
".md": to_md,
".json": to_json,
".yaml": to_yaml,
".html": to_html,
".strings": to_ios,
".xml": to_android,
".pdf": to_pdf,
}

if output_filepath.suffix in conversion_functions:
conversion_functions[output_filepath.suffix](strings, output_filepath)

print(
f"{ConsoleStyle.GREEN}Data successfully written to {output_filepath}"
f"{ConsoleStyle.END}"
)
else:
raise ValueError(
f"{ConsoleStyle.YELLOW}File type not supported. Feel free to create "
f"an issue here (https://github.com/HenestrosaConH/mobile-strings"
f"-converter/issues) if you want the file type to be supported by the "
f"package.{ConsoleStyle.END}"
)


def get_strings(input_filepath: Path, should_print_comments: bool):
"""
Creates a Google spreadsheet with the extracted strings from the input filepath
:param input_filepath: .strings or .xml file to extract the strings
:type input_filepath: Path
:param should_print_comments: True if the user wants to print comments from
.strings/.xml to the file
:type should_print_comments: bool
"""

if input_filepath.suffix == ".strings":
if should_print_comments:
pattern = r'"(.*?)"\s*=\s*"((?:[^"\\]|\\.)*)"\s*;'
Expand Down Expand Up @@ -54,6 +127,21 @@ def to_google_sheets(
credentials_filepath: Path,
should_print_comments: bool,
):
"""
Creates a Google spreadsheet with the extracted strings from the input filepath
:param input_filepath: .strings or .xml file to extract the strings
:type input_filepath: Path
:param sheet_name: Name of the sheet to be generated
:type sheet_name: str
:param credentials_filepath: Path to the service_account.json in order to be able
to create the sheet in the user's Google account
:type credentials_filepath: Path
:param should_print_comments: True if the user wants to print comments from
.strings/.xml to the sheet
:type should_print_comments: bool
"""

strings = get_strings(input_filepath, should_print_comments)

# Authenticate with Google Sheets API
Expand All @@ -75,8 +163,15 @@ def to_google_sheets(
sheet.append_row(string)


def to_csv(input_filepath: Path, output_filepath: Path, should_print_comments: bool):
strings = get_strings(input_filepath, should_print_comments)
def to_csv(strings: List[str], output_filepath: Path):
"""
Formats strings to a .csv file
:param strings: Strings extracted from a .strings or .xml file
:type strings: List[str]
:param output_filepath: The path where the generated file will be saved.
:type output_filepath: Path
"""

with open(output_filepath, "w", encoding="utf-8", newline="") as file:
writer = csv.writer(file)
Expand All @@ -90,10 +185,15 @@ def to_csv(input_filepath: Path, output_filepath: Path, should_print_comments: b
writer.writerow([name, value])


def _write_to_sheet(
input_filepath: Path, output_filepath: Path, should_print_comments: bool
):
strings = get_strings(input_filepath, should_print_comments)
def to_sheet(strings: List[str], output_filepath: Path):
"""
Formats strings to a .xlsx / .ods file
:param strings: Strings extracted from a .strings or .xml file
:type strings: List[str]
:param output_filepath: The path where the generated file will be saved.
:type output_filepath: Path
"""

# Create a new workbook
workbook = openpyxl.Workbook()
Expand All @@ -114,16 +214,15 @@ def _write_to_sheet(
workbook.save(output_filepath)


def to_xlsx(input_filepath: Path, output_filepath: Path, should_print_comments: bool):
_write_to_sheet(input_filepath, output_filepath, should_print_comments)
def to_json(strings: List[str], output_filepath: Path):
"""
Formats strings to a .json file

def to_ods(input_filepath: Path, output_filepath: Path, should_print_comments: bool):
_write_to_sheet(input_filepath, output_filepath, should_print_comments)


def to_json(input_filepath: Path, output_filepath: Path, should_print_comments: bool):
strings = get_strings(input_filepath, should_print_comments)
:param strings: Strings extracted from a .strings or .xml file
:type strings: List[str]
:param output_filepath: The path where the generated file will be saved.
:type output_filepath: Path
"""

# Create a list of dictionaries to store the data
data_list = []
Expand All @@ -135,8 +234,15 @@ def to_json(input_filepath: Path, output_filepath: Path, should_print_comments:
json.dump(data_list, file, ensure_ascii=False, indent=2)


def to_yaml(input_filepath: Path, output_filepath: Path, should_print_comments: bool):
strings = get_strings(input_filepath, should_print_comments)
def to_yaml(strings: List[str], output_filepath: Path):
"""
Formats strings to a .yaml file
:param strings: Strings extracted from a .strings or .xml file
:type strings: List[str]
:param output_filepath: The path where the generated file will be saved.
:type output_filepath: Path
"""

# Convert the data to a dictionary
strings_dict = {name: value for name, value in strings}
Expand All @@ -146,8 +252,15 @@ def to_yaml(input_filepath: Path, output_filepath: Path, should_print_comments:
yaml.dump(strings_dict, file, default_flow_style=False, allow_unicode=True)


def to_html(input_filepath: Path, output_filepath: Path, should_print_comments: bool):
strings = get_strings(input_filepath, should_print_comments)
def to_html(strings: List[str], output_filepath: Path):
"""
Formats strings to a .html file
:param strings: Strings extracted from a .strings or .xml file
:type strings: List[str]
:param output_filepath: The path where the generated file will be saved.
:type output_filepath: Path
"""

# Create an HTML file
with open(output_filepath, "w", encoding="utf-8") as file:
Expand All @@ -174,18 +287,30 @@ def to_html(input_filepath: Path, output_filepath: Path, should_print_comments:
file.write("</table>\n")


def to_ios(input_filepath: Path, output_filepath: Path, should_print_comments: bool):
strings = get_strings(input_filepath, should_print_comments)
def to_ios(strings: List[str], output_filepath: Path):
"""
Formats strings to a .strings file
:param strings: Strings extracted from a .strings or .xml file
:type strings: List[str]
:param output_filepath: The path where the generated file will be saved.
:type output_filepath: Path
"""

with open(output_filepath, "w", encoding="utf-8") as file:
for string in strings:
file.write(f'"{string[0]}" = "{string[1]}";\n')


def to_android(
input_filepath: Path, output_filepath: Path, should_print_comments: bool
):
strings = get_strings(input_filepath, should_print_comments)
def to_android(strings: List[str], output_filepath: Path):
"""
Formats strings to a .xml file
:param strings: Strings extracted from a .strings or .xml file
:type strings: List[str]
:param output_filepath: The path where the generated file will be saved.
:type output_filepath: Path
"""

with open(output_filepath, "w", encoding="utf-8") as file:
file.write("<resources>\n")
Expand All @@ -195,8 +320,15 @@ def to_android(
file.write("</resources>")


def to_pdf(input_filepath: Path, output_filepath: Path, should_print_comments: bool):
strings = get_strings(input_filepath, should_print_comments)
def to_pdf(strings: List[str], output_filepath: Path):
"""
Formats strings to a .pdf file
:param strings: Strings extracted from a .strings or .xml file
:type strings: List[str]
:param output_filepath: The path where the generated file will be saved.
:type output_filepath: Path
"""

# Ignore the following warning when adding a font already added:
# UserWarning: Core font or font already added 'dejavusanscondensed': doing nothing
Expand Down Expand Up @@ -329,8 +461,15 @@ def add_font(font_name, size=12):
pdf.output(str(output_filepath))


def to_md(input_filepath: Path, output_filepath: Path, should_print_comments: bool):
strings = get_strings(input_filepath, should_print_comments)
def to_md(strings: List[str], output_filepath: Path):
"""
Formats strings to a .md file
:param strings: Strings extracted from a .strings or .xml file
:type strings: List[str]
:param output_filepath: The path where the generated file will be saved.
:type output_filepath: Path
"""

with open(output_filepath, "w", encoding="utf-8") as f:
# Write each string to the Markdown file in a table format
Expand Down
Loading

0 comments on commit cc8f535

Please sign in to comment.