Skip to content

Commit 3aa2f89

Browse files
Merge pull request #21 from HenestrosaDev/dev
Remove the `--credentials` option
2 parents 24f4e47 + 35dfa09 commit 3aa2f89

File tree

4 files changed

+95
-77
lines changed

4 files changed

+95
-77
lines changed

README.md

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,9 @@
102102
- [Package Installation](#package-installation)
103103
- [Usage](#usage)
104104
- [Running the Program](#running-the-program)
105-
- [Script Flags](#script-flags)
105+
- [Script Arguments](#script-arguments)
106+
- [Positional Arguments](#positional-arguments)
107+
- [Options](#options)
106108
- [Using the Package in Your Project](#using-the-package-in-your-project)
107109
- [Generating a Spreadsheet in Google Sheets](#generating-a-spreadsheet-in-google-sheets)
108110
- [Setting Up a Google Account](#setting-up-a-google-account)
@@ -303,15 +305,15 @@ python path/to/mobile_strings_converter.py *.[SUPPORTED_FILE_TYPE] -f *.[SUPPORT
303305

304306
---
305307

306-
To include the comments of the `.xml`/`.strings` input file in the output file, add the `-p` (also `--print-comments`) flag. Note that it will be ignored for other input file types.
308+
To include the comments of the `.xml`/`.strings` input file in the output file, add the `-p` (or `--print-comments`) option. Note that it will be ignored for other input file types.
307309

308310
```
309311
python path/to/mobile_strings_converter.py *.[SUPPORTED_FILE_TYPE] -f *.[SUPPORTED_FILE_TYPE] -p
310312
```
311313

312314
---
313315

314-
To convert multiple files at once and save them to the specified directory passed in the `-d` flag, use the`-t` flag followed by the desired file type extension (e.g., `.json`). Note that the program will create the directory if it doesn't exist.
316+
To convert multiple files at once and save them in the specified directory specified with the `-d` option, use the`-t` option followed by the desired file type extension (e.g., `.json`). Note that the program will create the directory if it doesn't exist.
315317

316318
```
317319
python path/to/mobile_strings_converter.py *.[SUPPORTED_FILE_TYPE] *.[SUPPORTED_FILE_TYPE] *.[SUPPORTED_FILE_TYPE] -d [DIR_PATH] -t [TARGET_TYPE]
@@ -341,17 +343,27 @@ See the [Generating a Spreadsheet in Google Sheets](#generating-a-spreadsheet-in
341343

342344
---
343345

344-
#### Script Flags
345-
346-
| FLAG | DESCRIPTION |
347-
|:----------------------------|:--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
348-
| `-h` or `--help` | Displays help text for the program. |
349-
| `-f` or `--output-filepath` | Path to save the converted file. Only works if only one input file is provided. The file extension can be chosen from [the list of supported file types](#file-types-supported). |
350-
| `-d` or `--output-dir` | Directory where the converted files will be saved. Compatible with single and multiple input files as well as directories. The specified directory will be created if it does not already exist. |
351-
| `-t` or `--target-type` | Target file type to convert the files (e.g., .pdf, .json). Required if multiple file paths or the `--output-dir` is specified. |
352-
| `-g` or `--google-sheets` | If provided, a Google spreadsheet will be created in your Google account. You must pass the `service_account.json` with the `-c` flag. |
353-
| `-c` or `--credentials` | `service_account.json` filepath. Mandatory if you want to generate a spreadsheet in your Google account. You can learn how to generate it in the [Generating a Spreadsheet in Google Sheets](#generating-a-spreadsheet-in-google-sheets) section. |
354-
| `-p` or `--print-comments` | If provided, the commented strings will be printed in the output file. Only valid for input files of type `.xml` or `.strings`. Otherwise it is ignored. |
346+
#### Script Arguments
347+
348+
A full list of the program command's options are as follows:
349+
350+
##### Positional Arguments
351+
352+
| POSITIONAL ARGUMENT | DESCRIPTION |
353+
|:--------------------|:-----------------------------------------------------------------------------------------------------------------------|
354+
| `input_paths` | Files or directory paths of supported files to convert. See [the list of supported file types](#file-types-supported). |
355+
356+
##### Options
357+
358+
| OPTION | DESCRIPTION |
359+
|:--------------------------------------------------------|:---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
360+
| `-h, --help` | Show the help text and exit. |
361+
| `-v, --version` | Show script version info and exit. |
362+
| `-f FILE_PATH, --output-file FILE_PATH` | File path to save the converted file. Only works if only one input file is provided. See [the list of supported file types](#file-types-supported). |
363+
| `-d DIR_PATH, --output-dir DIR_PATH` | Directory path where the converted files will be saved. Compatible with single and multiple input files as well as directories. The specified directory will be created if it does not already exist. |
364+
| `-t FILE_TYPE, --target-type FILE_TYPE` | Target file type to convert the files. Required when specifying multiple file paths or `--output-dir`. See [the list of supported file types](#file-types-supported). |
365+
| `-g CREDENTIALS_PATH, --google-sheets CREDENTIALS_PATH` | Create a Google spreadsheet with the output in your Google account. You must specify the `service_account.json` path. You can learn how to generate it in the [Generating a Spreadsheet in Google Sheets](#generating-a-spreadsheet-in-google-sheets) section. |
366+
| `-p, --print-comments` | Print commented strings from the input file to the output file. Only valid for `.xml` or `.strings` input file types, otherwise it is ignored. |
355367

356368
<p align="right">(<a href="#top">back to top</a>)</p>
357369

pyproject.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ version = "0.1.4"
44
description = "Convert Android & iOS strings files to any supported file type and vice versa."
55
authors = ["José Carlos López <[email protected]>"]
66
license = "MIT"
7-
classifiers=[
7+
classifiers = [
88
"Intended Audience :: Developers",
99
"Environment :: Console",
1010
"Topic :: Text Processing :: General",
@@ -17,7 +17,7 @@ classifiers=[
1717
"Programming Language :: Python :: 3",
1818
"License :: OSI Approved :: MIT License",
1919
]
20-
keywords=[
20+
keywords = [
2121
"android",
2222
"strings.xml",
2323
"strings",
@@ -33,9 +33,9 @@ keywords=[
3333
"ios",
3434
"Localizable.strings"
3535
]
36-
repository="https://github.com/HenestrosaDev/mobile-strings-converter"
36+
repository = "https://github.com/HenestrosaDev/mobile-strings-converter"
3737
readme = "README.md"
38-
packages = [{include = "mobile_strings_converter", from = "src"}]
38+
packages = [{ include = "mobile_strings_converter", from = "src" }]
3939

4040
[tool.poetry.dependencies]
4141
python = ">=3.8"

src/mobile_strings_converter/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from .converter import convert_strings, to_google_sheets
88

99
# Constants
10-
__version__ = "0.1.4"
10+
__version__ = "0.1.5"
1111

1212
__author__ = "José Carlos López Henestrosa"
1313
__license = "MIT"

src/mobile_strings_converter/__main__.py

Lines changed: 64 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
from console_style import ConsoleStyle
66
from converter import convert_strings, to_google_sheets
7+
from mobile_strings_converter import __version__
78

89

910
def get_filepaths_from_dir(directory, extensions):
@@ -18,65 +19,87 @@ def get_filepaths_from_dir(directory, extensions):
1819

1920

2021
def main():
22+
supported_file_types = [
23+
".csv",
24+
".xlsx",
25+
".ods",
26+
".md",
27+
".json",
28+
".yaml",
29+
".html",
30+
".strings",
31+
".xml",
32+
".pdf",
33+
]
34+
supported_file_types_str = "\n".join(f" - {ext}" for ext in supported_file_types)
35+
2136
parser = argparse.ArgumentParser(
22-
description="Takes input and output files from console."
37+
description="Script to convert Android & iOS string files to any supported "
38+
"file type, and vice versa.\n\n"
39+
f"Supported file types:\n{supported_file_types_str}",
40+
formatter_class=argparse.RawDescriptionHelpFormatter,
2341
)
42+
2443
parser.add_argument(
2544
"input_paths",
2645
type=str,
2746
nargs="+", # Accept one or more values
28-
help="File paths or directories containing supported file types to be "
29-
"converted.",
47+
help="Files or directory paths of supported files to convert. Check the list "
48+
"of the supported file types above.",
49+
)
50+
parser.add_argument(
51+
"-v",
52+
"--version",
53+
action="version",
54+
version=__version__,
55+
help="Show program version info and exit.",
3056
)
3157
parser.add_argument(
3258
"-f",
33-
"--output-filepath",
59+
"--output-file",
3460
required=False,
3561
type=str,
36-
help="Path to save the converted file. Only works if only one input file "
37-
"is provided. See the README for a list of supported file types.",
62+
metavar="FILE_PATH",
63+
help="File path to save the converted file. Only works if only one input file "
64+
"is specified. Check the list of the supported file types above.",
3865
)
3966
parser.add_argument(
4067
"-d",
4168
"--output-dir",
4269
required=False,
4370
type=str,
44-
help="Directory where the converted files will be saved. Compatible with "
45-
"single and multiple input files as well as directories. The specified "
46-
"directory will be created if it does not already exist.",
71+
metavar="DIR_PATH",
72+
help="Directory path to save the converted files. Compatible with single and "
73+
"multiple input files as well as directories. The specified directory will be "
74+
"created if it does not already exist.",
4775
)
4876
parser.add_argument(
4977
"-t",
5078
"--target-type",
5179
type=str,
52-
help="Target file type to convert the files (e.g., .pdf, .json). Required if "
53-
"multiple file paths or the `--output-dir` is specified.",
80+
metavar="FILE_TYPE",
81+
help="Target file type to convert the files. Required when specifying "
82+
"multiple file paths or `--output-dir`. Check the list of the supported file "
83+
"types above.",
5484
)
5585
parser.add_argument(
5686
"-g",
5787
"--google-sheets",
5888
required=False,
59-
action="store_true",
60-
help="If provided, a Google spreadsheet will be created in your Google "
61-
"account. You must pass the `service_account.json` with the -c flag.",
62-
)
63-
parser.add_argument(
64-
"-c",
65-
"--credentials",
66-
required=False,
6789
type=str,
68-
help="`service_account.json` filepath. Mandatory if you want to generate a "
69-
"spreadsheet in your Google account. You can learn how to generate "
70-
"it in the README.",
90+
metavar="CREDENTIALS_PATH",
91+
help="Create a Google spreadsheet with the output in your Google account. "
92+
"You must specify the `service_account.json` path. You can learn how to "
93+
"generate it in the Generating a Spreadsheet in Google Sheets section in the "
94+
"README.",
7195
)
7296
parser.add_argument(
7397
"-p",
7498
"--print-comments",
7599
required=False,
76100
action="store_true",
77-
help="If provided, the commented strings will be printed in the output file. "
78-
"Only valid for input files of type `.xml` or `.strings`. Otherwise it is "
79-
"ignored.",
101+
help="Print commented strings from the input file to the output file. "
102+
"Only valid for `.xml` or `.strings` input file types, otherwise it is ignored.",
80103
)
81104

82105
args = parser.parse_args()
@@ -88,28 +111,15 @@ def main():
88111
f"{ConsoleStyle.END}"
89112
)
90113

91-
allowed_extensions = [
92-
".csv",
93-
".xlsx",
94-
".ods",
95-
".md",
96-
".json",
97-
".yaml",
98-
".html",
99-
".strings",
100-
".xml",
101-
".pdf",
102-
]
103-
104114
input_filepaths = []
105115
output_dir = None
106116

107117
for path in args.input_paths:
108118
if os.path.isdir(path):
109119
# If it's a directory, get all matching files
110-
input_filepaths.extend(get_filepaths_from_dir(path, allowed_extensions))
111-
elif os.path.isfile(path) and path.endswith(tuple(allowed_extensions)):
112-
# If it's a file with an allowed extension, add it to the list
120+
input_filepaths.extend(get_filepaths_from_dir(path, supported_file_types))
121+
elif os.path.isfile(path) and path.endswith(tuple(supported_file_types)):
122+
# If it's a supported file type, add it to the list
113123
input_filepaths.append(Path(path))
114124
else:
115125
print(
@@ -135,23 +145,19 @@ def main():
135145
f"-f or -d flag.{ConsoleStyle.END}"
136146
)
137147

138-
if args.google_sheets and not args.credentials:
139-
raise ValueError(
140-
f"{ConsoleStyle.RED}You need to pass the path of the "
141-
f"`service_account.json` file to generate a Sheet.{ConsoleStyle.END}"
142-
)
143-
elif not args.google_sheets and args.credentials:
144-
raise ValueError(
145-
f"{ConsoleStyle.RED}You need to pass the name of the Sheet to be "
146-
f"generated.{ConsoleStyle.END}"
147-
)
148-
elif args.google_sheets and args.credentials:
149-
for input_filepath in input_filepaths:
150-
to_google_sheets(
151-
args.input_filepath,
152-
sheet_name=Path(input_filepath).stem,
153-
credentials_filepath=Path(args.credentials),
154-
with_comments=args.print_comments,
148+
if credentials_path := args.google_sheets:
149+
if os.path.isfile(credentials_path):
150+
for input_filepath in input_filepaths:
151+
to_google_sheets(
152+
args.input_filepath,
153+
sheet_name=Path(input_filepath).stem,
154+
credentials_filepath=Path(credentials_path),
155+
with_comments=args.print_comments,
156+
)
157+
else:
158+
raise ValueError(
159+
f"{ConsoleStyle.RED}You need to pass the path of the "
160+
f"`service_account.json` file to generate a Sheet.{ConsoleStyle.END}"
155161
)
156162

157163
for input_filepath in input_filepaths:

0 commit comments

Comments
 (0)