pdfcrowd-cli 1.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pdfcrowd_cli/__init__.py +24 -0
- pdfcrowd_cli/__main__.py +28 -0
- pdfcrowd_cli/adapter.py +143 -0
- pdfcrowd_cli/catalog.py +46 -0
- pdfcrowd_cli/command-spec.json +16717 -0
- pdfcrowd_cli/entrypoints.py +228 -0
- pdfcrowd_cli/help.py +385 -0
- pdfcrowd_cli/parser.py +189 -0
- pdfcrowd_cli/state.py +368 -0
- pdfcrowd_cli/windows.py +173 -0
- pdfcrowd_cli-1.0.0.dist-info/LICENSE +22 -0
- pdfcrowd_cli-1.0.0.dist-info/METADATA +112 -0
- pdfcrowd_cli-1.0.0.dist-info/RECORD +16 -0
- pdfcrowd_cli-1.0.0.dist-info/WHEEL +5 -0
- pdfcrowd_cli-1.0.0.dist-info/entry_points.txt +10 -0
- pdfcrowd_cli-1.0.0.dist-info/top_level.txt +1 -0
pdfcrowd_cli/__init__.py
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# Copyright (C) 2009-2026 pdfcrowd.com
|
|
2
|
+
#
|
|
3
|
+
# Permission is hereby granted, free of charge, to any person
|
|
4
|
+
# obtaining a copy of this software and associated documentation
|
|
5
|
+
# files (the "Software"), to deal in the Software without
|
|
6
|
+
# restriction, including without limitation the rights to use,
|
|
7
|
+
# copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
+
# copies of the Software, and to permit persons to whom the
|
|
9
|
+
# Software is furnished to do so, subject to the following
|
|
10
|
+
# conditions:
|
|
11
|
+
#
|
|
12
|
+
# The above copyright notice and this permission notice shall be
|
|
13
|
+
# included in all copies or substantial portions of the Software.
|
|
14
|
+
#
|
|
15
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
16
|
+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
|
17
|
+
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
18
|
+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|
19
|
+
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|
20
|
+
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
21
|
+
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
22
|
+
# OTHER DEALINGS IN THE SOFTWARE.
|
|
23
|
+
|
|
24
|
+
"""PDFCrowd command-line application, independent of SDK imports."""
|
pdfcrowd_cli/__main__.py
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# Copyright (C) 2009-2026 pdfcrowd.com
|
|
2
|
+
#
|
|
3
|
+
# Permission is hereby granted, free of charge, to any person
|
|
4
|
+
# obtaining a copy of this software and associated documentation
|
|
5
|
+
# files (the "Software"), to deal in the Software without
|
|
6
|
+
# restriction, including without limitation the rights to use,
|
|
7
|
+
# copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
+
# copies of the Software, and to permit persons to whom the
|
|
9
|
+
# Software is furnished to do so, subject to the following
|
|
10
|
+
# conditions:
|
|
11
|
+
#
|
|
12
|
+
# The above copyright notice and this permission notice shall be
|
|
13
|
+
# included in all copies or substantial portions of the Software.
|
|
14
|
+
#
|
|
15
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
16
|
+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
|
17
|
+
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
18
|
+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|
19
|
+
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|
20
|
+
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
21
|
+
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
22
|
+
# OTHER DEALINGS IN THE SOFTWARE.
|
|
23
|
+
|
|
24
|
+
import sys
|
|
25
|
+
|
|
26
|
+
from .entrypoints import cli_main
|
|
27
|
+
|
|
28
|
+
sys.exit(cli_main())
|
pdfcrowd_cli/adapter.py
ADDED
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
# Copyright (C) 2009-2026 pdfcrowd.com
|
|
2
|
+
#
|
|
3
|
+
# Permission is hereby granted, free of charge, to any person
|
|
4
|
+
# obtaining a copy of this software and associated documentation
|
|
5
|
+
# files (the "Software"), to deal in the Software without
|
|
6
|
+
# restriction, including without limitation the rights to use,
|
|
7
|
+
# copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
+
# copies of the Software, and to permit persons to whom the
|
|
9
|
+
# Software is furnished to do so, subject to the following
|
|
10
|
+
# conditions:
|
|
11
|
+
#
|
|
12
|
+
# The above copyright notice and this permission notice shall be
|
|
13
|
+
# included in all copies or substantial portions of the Software.
|
|
14
|
+
#
|
|
15
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
16
|
+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
|
17
|
+
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
18
|
+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|
19
|
+
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|
20
|
+
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
21
|
+
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
22
|
+
# OTHER DEALINGS IN THE SOFTWARE.
|
|
23
|
+
|
|
24
|
+
"""Invoke exact SDK bindings, with no parsing or saved-state discovery."""
|
|
25
|
+
|
|
26
|
+
import os
|
|
27
|
+
import re
|
|
28
|
+
import sys
|
|
29
|
+
import tempfile
|
|
30
|
+
from pathlib import Path
|
|
31
|
+
|
|
32
|
+
from .catalog import CLIError
|
|
33
|
+
from .parser import option_value
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def validate_sources(converter, sources):
|
|
37
|
+
if not sources:
|
|
38
|
+
raise CLIError('An input source is required. '
|
|
39
|
+
f'Run {converter["name"]} --help for examples.')
|
|
40
|
+
if converter['name'] == 'pdf2pdf':
|
|
41
|
+
if any(not Path(source).is_file() for source in sources):
|
|
42
|
+
raise CLIError('PDF manipulation requires existing input files.')
|
|
43
|
+
elif len(sources) != 1:
|
|
44
|
+
raise CLIError('This converter requires exactly one input source.')
|
|
45
|
+
elif sources[0] != '-' and not re.match(r'(?i)^https?://', sources[0]):
|
|
46
|
+
if not Path(sources[0]).is_file():
|
|
47
|
+
raise CLIError('Input must be an HTTP(S) URL, an existing file, '
|
|
48
|
+
"or '-' for stdin.")
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def service_failure(error):
|
|
52
|
+
status = str(error.getStatusCode())
|
|
53
|
+
code = 3 if status in ('401', '403') else 2 if status == '470' else 4
|
|
54
|
+
return CLIError(str(error), code, prefix='PDFCrowd Error')
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def sdk_module(catalog):
|
|
58
|
+
import pdfcrowd
|
|
59
|
+
version = tuple(int(part) for part in pdfcrowd.__version__.split('.'))
|
|
60
|
+
release = catalog['release']
|
|
61
|
+
minimum = tuple(int(part) for part in release['sdk_minimum'].split('.'))
|
|
62
|
+
maximum = tuple(int(part) for part in release['sdk_maximum'].split('.'))
|
|
63
|
+
if not minimum <= version < maximum:
|
|
64
|
+
raise CLIError('The installed PDFCrowd SDK is incompatible with this '
|
|
65
|
+
'CLI. Reinstall pdfcrowd-cli with its dependencies.', 4)
|
|
66
|
+
return pdfcrowd
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def convert(converter, settings, credentials, sources, destination, catalog,
|
|
70
|
+
output_info=(), invocation_options=()):
|
|
71
|
+
validate_sources(converter, sources)
|
|
72
|
+
# Resolve local values before opening output or contacting the service.
|
|
73
|
+
# Invocation setters also override saved setters sharing the same fields.
|
|
74
|
+
options = sorted(converter['options'], key=lambda option:
|
|
75
|
+
option['name'] in invocation_options)
|
|
76
|
+
calls = []
|
|
77
|
+
for option in options:
|
|
78
|
+
if option['name'] not in settings:
|
|
79
|
+
continue
|
|
80
|
+
values = settings[option['name']]
|
|
81
|
+
values = values if option['repeatable'] else [values]
|
|
82
|
+
for value in values:
|
|
83
|
+
calls.append((option['sdk_method'], option_value(
|
|
84
|
+
option, value, converter['name'])))
|
|
85
|
+
sdk = sdk_module(catalog)
|
|
86
|
+
client = getattr(sdk, converter['sdk_class'])(*credentials)
|
|
87
|
+
|
|
88
|
+
def perform(output):
|
|
89
|
+
client.setClientUserAgent('pdfcrowd_cli_client/1.0.0 (https://pdfcrowd.com)')
|
|
90
|
+
for method, arguments in calls:
|
|
91
|
+
getattr(client, method)(*arguments)
|
|
92
|
+
bindings = converter['input']
|
|
93
|
+
if converter['name'] == 'pdf2pdf':
|
|
94
|
+
for source in sources:
|
|
95
|
+
getattr(client, bindings['add_file'])(source)
|
|
96
|
+
output.write(getattr(client, bindings['convert'])())
|
|
97
|
+
else:
|
|
98
|
+
source = sources[0]
|
|
99
|
+
if source == '-':
|
|
100
|
+
if not sys.stdin.buffer.peek(1):
|
|
101
|
+
raise CLIError('Input from stdin must not be empty.')
|
|
102
|
+
method, argument = bindings['stream'], sys.stdin.buffer
|
|
103
|
+
elif re.match(r'(?i)^https?://', source):
|
|
104
|
+
method, argument = bindings['url'], source
|
|
105
|
+
else:
|
|
106
|
+
method, argument = bindings['file'], source
|
|
107
|
+
# The generated SDK has matching streaming-output variants.
|
|
108
|
+
getattr(client, method + 'ToStream')(argument, output)
|
|
109
|
+
|
|
110
|
+
temporary = None
|
|
111
|
+
try:
|
|
112
|
+
if destination == '-':
|
|
113
|
+
if os.name == 'nt':
|
|
114
|
+
import msvcrt
|
|
115
|
+
msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
|
|
116
|
+
perform(sys.stdout.buffer)
|
|
117
|
+
sys.stdout.buffer.flush()
|
|
118
|
+
else:
|
|
119
|
+
target = Path(destination)
|
|
120
|
+
with tempfile.NamedTemporaryFile(
|
|
121
|
+
mode='wb', dir=target.parent, prefix=f'.{target.name}.',
|
|
122
|
+
delete=False) as output:
|
|
123
|
+
temporary = output.name
|
|
124
|
+
perform(output)
|
|
125
|
+
os.replace(temporary, target)
|
|
126
|
+
temporary = None
|
|
127
|
+
except sdk.Error as error:
|
|
128
|
+
raise service_failure(error) from None
|
|
129
|
+
except BrokenPipeError:
|
|
130
|
+
raise
|
|
131
|
+
except (OSError, ValueError):
|
|
132
|
+
raise CLIError('Could not read input or write output. '
|
|
133
|
+
'Check file paths and access permissions.', 4) from None
|
|
134
|
+
finally:
|
|
135
|
+
if temporary is not None:
|
|
136
|
+
try:
|
|
137
|
+
os.unlink(temporary)
|
|
138
|
+
except OSError:
|
|
139
|
+
pass
|
|
140
|
+
|
|
141
|
+
for option in output_info:
|
|
142
|
+
value = getattr(client, option['sdk_method'])()
|
|
143
|
+
print(f'{option["label"]}: {value}', file=sys.stderr)
|
pdfcrowd_cli/catalog.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
# Copyright (C) 2009-2026 pdfcrowd.com
|
|
2
|
+
#
|
|
3
|
+
# Permission is hereby granted, free of charge, to any person
|
|
4
|
+
# obtaining a copy of this software and associated documentation
|
|
5
|
+
# files (the "Software"), to deal in the Software without
|
|
6
|
+
# restriction, including without limitation the rights to use,
|
|
7
|
+
# copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
+
# copies of the Software, and to permit persons to whom the
|
|
9
|
+
# Software is furnished to do so, subject to the following
|
|
10
|
+
# conditions:
|
|
11
|
+
#
|
|
12
|
+
# The above copyright notice and this permission notice shall be
|
|
13
|
+
# included in all copies or substantial portions of the Software.
|
|
14
|
+
#
|
|
15
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
16
|
+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
|
17
|
+
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
18
|
+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|
19
|
+
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|
20
|
+
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
21
|
+
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
22
|
+
# OTHER DEALINGS IN THE SOFTWARE.
|
|
23
|
+
|
|
24
|
+
"""Load the build-generated command contract without importing the SDK."""
|
|
25
|
+
|
|
26
|
+
import json
|
|
27
|
+
import pkgutil
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def command_options(converter, catalog):
|
|
31
|
+
return catalog['command_options'] + converter.get('output_info', [])
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
class CLIError(Exception):
|
|
35
|
+
def __init__(self, message, code=2, prefix='PDFCrowd'):
|
|
36
|
+
super().__init__(message)
|
|
37
|
+
self.code = code
|
|
38
|
+
self.prefix = prefix
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def load_catalog():
|
|
42
|
+
catalog = json.loads(pkgutil.get_data('pdfcrowd_cli', 'command-spec.json'))
|
|
43
|
+
if catalog.get('schema_version') != 1:
|
|
44
|
+
raise CLIError('The CLI help data is incompatible with this '
|
|
45
|
+
'installation. Reinstall pdfcrowd-cli.')
|
|
46
|
+
return catalog
|