1
0
mirror of https://github.com/Llewellynvdm/pdflayers.git synced 2024-12-18 06:54:07 +00:00

Restructure single script into package

This commit is contained in:
Simon Segerblom Rex 2019-05-20 11:31:50 +02:00
parent a389c34b0d
commit 84fa57c3b5
4 changed files with 81 additions and 35 deletions

3
.pylintrc Normal file
View File

@ -0,0 +1,3 @@
[MASTER]
good-names=logger,

View File

@ -12,44 +12,12 @@ References:
""" """
import argparse import argparse
import logging import logging
import sys
import pikepdf import pikepdf
from pdflayers import utils
def set_layer_visibility(pdf, layers_to_show): logger = logging.getLogger(__name__)
"""Set visibility of layers."""
try:
ocgs = pdf.root.OCProperties.OCGs
except (AttributeError, KeyError):
logging.error("Unable to locate layers in PDF.")
sys.exit(1)
ocgs_on = []
for ocg in ocgs:
if ocg.Name in layers_to_show:
logging.info("Layer %s will be visible.", ocg.Name)
ocgs_on.append(ocg)
else:
logging.info("Layer %s will be hidden.", ocg.Name)
ocgs_config = pikepdf.Dictionary(
BaseState=pikepdf.Name('/OFF'),
ON=ocgs_on,
Order=ocgs,
)
pdf.root.OCProperties = pikepdf.Dictionary(
D=ocgs_config,
OCGs=ocgs,
)
# Needed for google-chrome (at least):
for ocg in ocgs:
if '/View' in ocg.Usage:
del ocg.Usage.View
if '/Print' in ocg.Usage:
del ocg.Usage.Print
def main(): def main():
@ -72,7 +40,7 @@ def main():
pdf = pikepdf.open(args.input, password=args.password) pdf = pikepdf.open(args.input, password=args.password)
set_layer_visibility(pdf, args.show) utils.set_layer_visibility(pdf, args.show)
pdf.remove_unreferenced_resources() pdf.remove_unreferenced_resources()
save_options = { save_options = {

42
pdflayers/utils.py Normal file
View File

@ -0,0 +1,42 @@
"""Utility functions used in pdflayers."""
import logging
import sys
import pikepdf
logger = logging.getLogger(__name__)
def set_layer_visibility(pdf, layers_to_show):
"""Set visibility of layers."""
try:
ocgs = pdf.root.OCProperties.OCGs
except (AttributeError, KeyError):
logger.error("Unable to locate layers in PDF.")
sys.exit(1)
ocgs_on = []
for ocg in ocgs:
if ocg.Name in layers_to_show:
logger.info("Layer %s will be visible.", ocg.Name)
ocgs_on.append(ocg)
else:
logger.info("Layer %s will be hidden.", ocg.Name)
ocgs_config = pikepdf.Dictionary(
BaseState=pikepdf.Name('/OFF'),
ON=ocgs_on,
Order=ocgs,
)
pdf.root.OCProperties = pikepdf.Dictionary(
D=ocgs_config,
OCGs=ocgs,
)
# Needed for google-chrome (at least):
for ocg in ocgs:
if '/View' in ocg.Usage:
del ocg.Usage.View
if '/Print' in ocg.Usage:
del ocg.Usage.Print

33
setup.py Normal file
View File

@ -0,0 +1,33 @@
#!/usr/bin/env python3
"""Setup."""
import setuptools
def _readme():
with open('README.md', 'r') as file:
return file.read()
setuptools.setup(
name='pdflayers',
version='0.1.0',
author='Simon Segerblom Rex',
description='PDF layer handling.',
long_description=_readme(),
long_description_content_type='text/markdown',
url='https://github.com/SimonSegerblomRex/pdflayers',
license='MIT',
packages=setuptools.find_packages(),
entry_points={
'console_scripts': [
'pdflayers = pdflayers.__main__:main',
],
},
python_requires='>=3.5',
install_requires=[
'pikepdf',
],
classifiers=[
'Python :: 3'
],
)