Skip to content

Commit

Permalink
Inital Windows support
Browse files Browse the repository at this point in the history
  • Loading branch information
nhi-vanye committed Dec 2, 2023
1 parent 2e5fb04 commit 694cf80
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 29 deletions.
4 changes: 2 additions & 2 deletions randeli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

configobj.DEFAULTSECT = "global"

TOPDIR = str(pathlib.PosixPath(randeli.__file__).parent)
TOPDIR = str(pathlib.Path(randeli.__file__).parent)
CFG = os.path.join(
click.get_app_dir("randeli", force_posix=True),
'config.ini')
Expand Down Expand Up @@ -194,7 +194,7 @@ def stderr_info_filter(record):

ctx.ensure_object(dict)

ctx.obj['global.top'] = str(pathlib.PosixPath(randeli.__file__).parent)
ctx.obj['global.top'] = str(pathlib.Path(randeli.__file__).parent)

cfg_path = pathlib.Path(cfg)

Expand Down
2 changes: 1 addition & 1 deletion randeli/cmds/augment.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def augment_pdf(ctx):
from randeli.cmds.handlers.augment import PDFEventHandler
from randeli.librandeli.backend import Apryse as BACKEND

font_map = pathlib.PosixPath( ctx.obj['policy.font-map-file'] )
font_map = pathlib.Path( ctx.obj['policy.font-map-file'] )

if not font_map.exists:
LOGGER.fatal(f"Could not open font-map file {str(font_map)}")
Expand Down
14 changes: 9 additions & 5 deletions randeli/cmds/bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ def cli(ctx, download, force):

ctx.ensure_object(dict)

cfg_path = pathlib.PosixPath(ctx.obj['global.cfg'])
cfg_path = pathlib.Path(ctx.obj['global.cfg'])

ocrdir = pathlib.PosixPath(ctx.obj['global.top'],'ocr')
ocrdir = pathlib.Path(ctx.obj['global.top'],'ocr')
ocrlibdir = ocrdir / "Lib"

if force or ( not cfg_path.exists() or ( cfg_path.exists() and cfg_path.stat().st_size == 0 ) ):
Expand Down Expand Up @@ -92,6 +92,8 @@ def cli(ctx, download, force):
config.filename = cfg_path


cfg_path.parent.mkdir(parents=True)

config.write()

else:
Expand All @@ -105,6 +107,7 @@ def cli(ctx, download, force):
subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'apryse_sdk', '--extra-index-url=https://pypi.apryse.com']) # nosec: B603

urls = {
"Windows" : "https://www.pdftron.com/downloads/OCRModuleWindows.zip",
"Darwin" : "https://www.pdftron.com/downloads/OCRModuleMac.zip",
"Linux" : "https://www.pdftron.com/downloads/OCRModuleLinux.tar.gz",
}
Expand Down Expand Up @@ -142,11 +145,12 @@ def cli(ctx, download, force):
zip.extractall(path=str(ocrlibdir.parent)) # nosec: B202

# reset permissions on module
module = pathlib.PosixPath(ocrlibdir / "OCRModule")
module.chmod(0o755)
module = pathlib.Path(ocrlibdir / "OCRModule")
if platform.system() != "Windows":
module.chmod(0o755)

write_config_value_to_file("ocr.libdir", str(ocrlibdir), str(cfg_path))

else:
click.error(f"Unsupported platform '{platform.system()}, please consider filing a bug.")
click.echo(f"Unsupported platform '{platform.system()}, please consider filing a bug.")
LOGGER.debug(f"Unsupported platform '{platform.system()}, please consider filing a bug.")
2 changes: 1 addition & 1 deletion randeli/cmds/inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def beginSectionCB(msg:randeli.librandeli.notify.BeginPage):
LOGGER.success(f"file = {msg.page}")

def elementCB(msg:randeli.librandeli.notify.Element):
LOGGER.success(f"<{msg.ele_type_str}>")
LOGGER.info(f"<{msg.ele_type_str}>")
for child in msg.element.contents:
LOGGER.debug(f"text={child}")

Expand Down
22 changes: 11 additions & 11 deletions randeli/cmds/map-fonts.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@

from .config import write_config_value_to_file

darwin_font_paths=["/Library/Fonts/", "/System/Library/Fonts/", f'{os.environ.get("HOME", "")}/Library/Fonts']
linux_font_paths=["/usr/share/fonts/opentype", "/usr/share/fonts/truetype", "/usr/share/fonts/type1"]
default_font_paths = None
system_font_paths = {
"Darwin" : ["/Library/Fonts/", "/System/Library/Fonts/", f'{os.environ.get("HOME", "")}/Library/Fonts'],
"Linux" : ["/usr/share/fonts/opentype", "/usr/share/fonts/truetype", "/usr/share/fonts/type1"],
"Windows" : [ "c:/Windows/fonts" ]
}

default_font_paths = system_font_paths[ platform.system() ]

if platform.system() == "Darwin":
default_font_paths = darwin_font_paths
elif platform.system() == "Linux":
default_font_paths = linux_font_paths

def print_long_help_and_exit(ctx, param, value):

Expand Down Expand Up @@ -111,9 +111,9 @@ def cli(ctx, font_file, font_dir, fallback_font, cm_alias, update_config, alias,
for root, dirs, files in os.walk(dir_):
for filename in files:

path_to_font = pathlib.PurePosixPath(root, filename)
path_to_font = pathlib.PurePath(root, filename)

if path_to_font.suffix in [".ttf", ".otf", ".ttc" ]:
if path_to_font.suffix.lower() in [".ttf", ".otf", ".ttc" ]:
try:
font_list = [ ttLib.TTFont(str(path_to_font)) ]
except ttLib.TTLibFileIsCollectionError:
Expand Down Expand Up @@ -175,7 +175,7 @@ def _print_fonts():
yield f" {style}\n"

if echo is True:
click.echo_via_pager(_print_fonts())
click.echo(_print_fonts())

with open( font_file, "w") as out:

Expand All @@ -185,7 +185,7 @@ def _print_fonts():

if update_config is True:

abs_font_file = pathlib.PosixPath(font_file).absolute()
abs_font_file = pathlib.Path(font_file).absolute()

click.echo( f"Updated font-map in configuration file to {write_config_value_to_file( 'policy.font-map-file', str(abs_font_file), ctx.obj['global.cfg'])}" )

Expand Down
8 changes: 4 additions & 4 deletions randeli/librandeli/backend/apryse.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

import math
import tempfile
from pathlib import PosixPath
from pathlib import Path

import apryse_sdk as APRYSE

Expand Down Expand Up @@ -567,12 +567,12 @@ def _process_element_as_ocr(el, file) -> str:

else:

base = PosixPath(self.read_file).name
base = Path(self.read_file).name

if out_dir:
base = PosixPath(out_dir, base)
base = Path(out_dir, base)
if out_filename:
base = PosixPath(out_filename)
base = Path(out_filename)

png = f"{base}.{msg.page_number}-{msg.ele_idx}.png"

Expand Down
10 changes: 5 additions & 5 deletions randeli/librandeli/backend/base.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from pathlib import PosixPath
import pathlib

import EventNotifier

Expand Down Expand Up @@ -31,13 +31,13 @@ def processPage(self, reader, writer, builder, current_page):

def saveDocument(self, filename="", in_dir=""):

self.save_file = PosixPath(self.read_file).name
self.save_file = pathlib.Path(self.read_file).name

if in_dir:
self.save_file = PosixPath(in_dir, self.save_file)
self.save_file = pathlib.Path(in_dir, self.save_file)

if filename:
self.save_file = PosixPath(filename)
self.save_file = pathlib.Path(filename)

if self.save_file == self.read_file:
raise Exception("Output file cannot be the same as the input filename")
Expand Down Expand Up @@ -68,7 +68,7 @@ def read_file(self):

@read_file.setter
def read_file(self, value):
self._read_file = PosixPath(value)
self._read_file = pathlib.Path(value)

@property
def save_file(self):
Expand Down

0 comments on commit 694cf80

Please sign in to comment.