Skip to content

Commit

Permalink
refactor container.py and scripts/folder.py + test
Browse files Browse the repository at this point in the history
  • Loading branch information
jdum committed Oct 27, 2023
1 parent 4722d04 commit 666bc4e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 19 deletions.
12 changes: 6 additions & 6 deletions odfdo/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,10 +242,10 @@ def dump(part_path, content):
is_folder = False
pure_path = PurePath(folder, part_path)
path = Path(pure_path)
path.parent.mkdir(parents=False, exist_ok=True)
if is_folder:
path.mkdir(parents=False, exist_ok=True)
path.mkdir(parents=True, exist_ok=True)
else:
path.parent.mkdir(parents=True, exist_ok=True)
path.write_bytes(content)
path.chmod(0o666)

Expand Down Expand Up @@ -364,12 +364,12 @@ def _save_target(self, target):
target = target.split(".folder", 1)[0]
return target

def _save_as_zip(self, packaging, target, backup):
def _save_as_zip(self, target, backup):
if isinstance(target, str) and backup:
self.__do_backup(target)
self.__save_zip(target)

def _save_as_folder(self, packaging, target, backup):
def _save_as_folder(self, target, backup):
if not isinstance(target, str):
raise TypeError(
f"Saving in folder format requires a folder name, not '{target}'"
Expand Down Expand Up @@ -410,7 +410,7 @@ def save(self, target=None, packaging=None, backup=False):
self.get_part(path)
target = self._save_target(target)
if packaging == "folder":
self._save_as_folder(packaging, target, backup)
self._save_as_folder(target, backup)
else:
# default:
self._save_as_zip(packaging, target, backup)
self._save_as_zip(target, backup)
31 changes: 18 additions & 13 deletions odfdo/scripts/folder.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#!/usr/bin/env python
# Copyright 2018-2023 Jérôme Dumonteil
# Copyright (c) 2009-2013 Ars Aperta, Itaapy, Pierlis, Talend.
#
Expand All @@ -23,32 +22,38 @@
import os
import sys
from optparse import OptionParser

from pathlib import Path
from odfdo import Document, __version__


def main():
usage = "%prog <input>"
description = "Convert standard ODF File to folder, and reverse."
usage = "%prog <input file_or_folder>"
description = "Convert standard ODF file to folder, and reverse."
parser = OptionParser(usage, version=__version__, description=description)

# Parse !
options, args = parser.parse_args()

# Go !
if len(args) != 1:
try:
convert_folder(args[0])
except Exception as e:
parser.print_help()
sys.exit(0)
print()
print(repr(e))
sys.exit(1)


if os.path.isfile(args[0]):
def convert_folder(path):
path = Path(path)
if path.is_file():
out_packaging = "folder"
elif os.path.isdir(args[0]):
elif path.is_dir():
out_packaging = "zip"
else:
raise ValueError("no File or folder ?")
doc = Document(args[0])
doc.save(packaging=out_packaging, pretty=True)
raise ValueError(f"Not a file or folder: {path}")
document = Document(path)
document.save(packaging=out_packaging, pretty=True)


if __name__ == "__main__":
if __name__ == "__main__": # pragma: no cover
main()

0 comments on commit 666bc4e

Please sign in to comment.