Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

python: add support for formats #436

Merged
merged 6 commits into from
Aug 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,7 @@
path = web/documentserver-example/java-spring/src/main/resources/assets/document-templates
url = https://github.com/ONLYOFFICE/document-templates
branch = main/en
[submodule "web/documentserver-example/python/assets/document-formats"]
path = web/documentserver-example/python/assets/document-formats
url = https://github.com/ONLYOFFICE/document-formats
branch = master
1 change: 1 addition & 0 deletions web/documentserver-example/python/assets/document-formats
Submodule document-formats added at 6e38b1
Original file line number Diff line number Diff line change
Expand Up @@ -117,78 +117,6 @@ def conversion_timeout(self) -> int:
return int(timeout)
return 120 * 1000

def fillable_file_extensions(self) -> list[str]:
return [
'.docx',
'.oform'
]

def viewable_file_extensions(self) -> list[str]:
return [
'.djvu',
'.oxps',
'.pdf',
'.xps'
]

def editable_file_extensions(self) -> list[str]:
return [
'.csv', '.docm', '.docx',
'.docxf', '.dotm', '.dotx',
'.epub', '.fb2', '.html',
'.odp', '.ods', '.odt',
'.otp', '.ots', '.ott',
'.potm', '.potx', '.ppsm',
'.ppsx', '.pptm', '.pptx',
'.rtf', '.txt', '.xlsm',
'.xlsx', '.xltm', '.xltx'
]

def convertible_file_extensions(self) -> list[str]:
return [
'.doc', '.dot', '.dps', '.dpt',
'.epub', '.et', '.ett', '.fb2',
'.fodp', '.fods', '.fodt', '.htm',
'.html', '.mht', '.mhtml', '.odp',
'.ods', '.odt', '.otp', '.ots',
'.ott', '.pot', '.pps', '.ppt',
'.rtf', '.stw', '.sxc', '.sxi',
'.sxw', '.wps', '.wpt', '.xls',
'.xlsb', '.xlt', '.xml'
]

def spreadsheet_file_extensions(self) -> list[str]:
return [
'.xls', '.xlsx',
'.xlsm', '.xlsb',
'.xlt', '.xltx',
'.xltm', '.ods',
'.fods', '.ots',
'.csv'
]

def presentation_file_extensions(self) -> list[str]:
return [
'.pps', '.ppsx',
'.ppsm', '.ppt',
'.pptx', '.pptm',
'.pot', '.potx',
'.potm', '.odp',
'.fodp', '.otp'
]

def document_file_extensions(self) -> list[str]:
return [
'.doc', '.docx', '.docm',
'.dot', '.dotx', '.dotm',
'.odt', '.fodt', '.ott',
'.rtf', '.txt', '.html',
'.htm', '.mht', '.xml',
'.pdf', '.djvu', '.fb2',
'.epub', '.xps', '.oxps',
'.oform'
]

def languages(self) -> dict[str, str]:
return {
'en': 'English',
Expand Down
17 changes: 17 additions & 0 deletions web/documentserver-example/python/src/format/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#
# (c) Copyright Ascensio System SIA 2023
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

from .format import *
141 changes: 141 additions & 0 deletions web/documentserver-example/python/src/format/format.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
#
# (c) Copyright Ascensio System SIA 2023
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

from pathlib import Path
from msgspec.json import decode
from msgspec import Struct
from src.memoize import memoize

class Format(Struct):
name: str
type: str
actions: list[str]
convert: list[str]
mime: list[str]

def extension(self) -> str:
return f'.{self.name}'

class FormatManager():
def fillable_extensions(self) -> list[str]:
formats = self.fillable()
mapped = map(lambda format: format.extension(), formats)
return list(mapped)

def fillable(self) -> list[Format]:
formats = self.all()
filtered = filter(lambda format: 'fill' in format.actions, formats)
return list(filtered)

def viewable_extensions(self) -> list[str]:
formats = self.viewable()
mapped = map(lambda format: format.extension(), formats)
return list(mapped)

def viewable(self) -> list[Format]:
formats = self.all()
filtered = filter(lambda format: 'view' in format.actions, formats)
return list(filtered)

def editable_extensions(self) -> list[str]:
formats = self.editable()
mapped = map(lambda format: format.extension(), formats)
return list(mapped)

def editable(self) -> list[Format]:
formats = self.all()
filtered = filter(
lambda format: (
'edit' in format.actions or
'lossy-edit' in format.actions
),
formats
)
return list(filtered)

def convertible_extensions(self) -> list[str]:
formats = self.convertible()
mapped = map(lambda format: format.extension(), formats)
return list(mapped)

def convertible(self) -> list[Format]:
formats = self.all()
filtered = filter(
lambda format: (
format.type == 'cell' and 'xlsx' in format.convert or
format.type == 'slide' and 'pptx' in format.convert or
format.type == 'word' and 'docx' in format.convert
),
formats
)
return list(filtered)

def spreadsheet_extensions(self) -> list[str]:
formats = self.spreadsheets()
mapped = map(lambda format: format.extension(), formats)
return list(mapped)

def spreadsheets(self) -> list[Format]:
formats = self.all()
filtered = filter(lambda format: format.type == 'cell', formats)
return list(filtered)

def presentation_extensions(self) -> list[str]:
formats = self.presentations()
mapped = map(lambda format: format.extension(), formats)
return list(mapped)

def presentations(self) -> list[Format]:
formats = self.all()
filtered = filter(lambda format: format.type == 'slide', formats)
return list(filtered)

def document_extensions(self) -> list[str]:
formats = self.documents()
mapped = map(lambda format: format.extension(), formats)
return list(mapped)

def documents(self) -> list[Format]:
formats = self.all()
filtered = filter(lambda format: format.type == 'word', formats)
return list(filtered)

def all_extensions(self) -> list[str]:
formats = self.all()
mapped = map(lambda format: format.extension(), formats)
return list(mapped)

@memoize
def all(self) -> list[Format]:
path = self.__file()
with open(path, 'r', encoding='utf-8') as file:
contents = file.read()
return decode(contents, type=list[Format])

def __file(self) -> Path:
directory = self.__directory()
return directory.joinpath('onlyoffice-docs-formats.json')

def __directory(self) -> Path:
current_file = Path(__file__)
directory = current_file.joinpath(
'..',
'..',
'..',
'assets',
'document-formats'
)
return directory.resolve()
92 changes: 92 additions & 0 deletions web/documentserver-example/python/src/format/format_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#
# (c) Copyright Ascensio System SIA 2023
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

from __future__ import annotations
from unittest import TestCase
from msgspec.json import decode
from . import Format, FormatManager

class FormatTests(TestCase):
json = \
'''
{
"name": "djvu",
"type": "word",
"actions": ["view"],
"convert": ["bmp", "gif", "jpg", "pdf", "pdfa", "png"],
"mime": ["image/vnd.djvu"]
}
'''

def test_generates_extension(self):
form = decode(self.json, type=Format)
self.assertEqual(form.extension(), '.djvu')

class FormatManagerAllTests(TestCase):
def test_loads(self):
format_manager = FormatManager()
formats = format_manager.all()
empty = len(formats) == 0
self.assertFalse(empty)

class FormatManagerDocumentsTests(TestCase):
def test_loads(self):
format_manager = FormatManager()
formats = format_manager.documents()
empty = len(formats) == 0
self.assertFalse(empty)

class FormatManagerPresentationsTests(TestCase):
def test_loads(self):
format_manager = FormatManager()
formats = format_manager.presentations()
empty = len(formats) == 0
self.assertFalse(empty)

class FormatManagerSpreadsheetsTests(TestCase):
def test_loads(self):
format_manager = FormatManager()
formats = format_manager.spreadsheets()
empty = len(formats) == 0
self.assertFalse(empty)

class FormatManagerConvertibleTests(TestCase):
def test_loads(self):
format_manager = FormatManager()
formats = format_manager.convertible()
empty = len(formats) == 0
self.assertFalse(empty)

class FormatManagerEditableTests(TestCase):
def test_loads(self):
format_manager = FormatManager()
formats = format_manager.editable()
empty = len(formats) == 0
self.assertFalse(empty)

class FormatManagerViewableTests(TestCase):
def test_loads(self):
format_manager = FormatManager()
formats = format_manager.viewable()
empty = len(formats) == 0
self.assertFalse(empty)

class FormatManagerFillableTests(TestCase):
def test_loads(self):
format_manager = FormatManager()
formats = format_manager.fillable()
empty = len(formats) == 0
self.assertFalse(empty)
17 changes: 17 additions & 0 deletions web/documentserver-example/python/src/memoize/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#
# (c) Copyright Ascensio System SIA 2023
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

from .memoize import *
26 changes: 26 additions & 0 deletions web/documentserver-example/python/src/memoize/memoize.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#
# (c) Copyright Ascensio System SIA 2023
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

def memoize(func):
def wrapper(*args, **kwargs):
if wrapper.called:
return wrapper.result
wrapper.called = True
wrapper.result = func(*args, **kwargs)
return wrapper.result
wrapper.called = False
wrapper.result = None
return wrapper
Loading
Loading