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

Update newdoc: Fix the optional formatting and drop Python2 #30

Merged
merged 7 commits into from
Sep 25, 2020
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
2 changes: 1 addition & 1 deletion .tito/packages/python-newdoc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.4.3-1 newdoc/
1.5.1-1 newdoc/
8 changes: 5 additions & 3 deletions newdoc/README.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
###########################
README: The `newdoc` script
###########################
########################################
README: The `newdoc` script (DEPRECATED)
########################################

**DEPRECATED:** This version of `newdoc` is now deprecated and will no longer receive any significant updates. Please uninstall this version and migrate to the current version: https://github.com/redhat-documentation/newdoc.

This script is used for generating empty module and assembly files when writing Red Hat or Fedora documentation in AsciiDoc. The generated files follow template guidelines set up by the Modular Documentation initiative: https://redhat-documentation.github.io/modular-docs/.

Expand Down
40 changes: 15 additions & 25 deletions newdoc/newdoc.spec
Original file line number Diff line number Diff line change
@@ -1,28 +1,20 @@
%global srcname newdoc

Name: python-%{srcname}
Version: 1.4.3
Release: 1%{?dist}
Version: 1.5.1
Release: 1
Summary: A script to generate assembly and module AsciiDoc files from templates

License: GPLv3+
URL: https://pypi.python.org/pypi/%{srcname}
Source0: %pypi_source

BuildArch: noarch
BuildRequires: python2-devel python3-devel
BuildRequires: python3-devel

%description
A script to generate assembly and module AsciiDoc files from templates

%package -n python2-%{srcname}
Summary: %{summary}
%{?python_provide:%python_provide python2-%{srcname}}

%description -n python2-%{srcname}
A script to generate assembly and module AsciiDoc files from templates


%package -n python3-%{srcname}
Summary: %{summary}
%{?python_provide:%python_provide python3-%{srcname}}
Expand All @@ -35,35 +27,33 @@ A script to generate assembly and module AsciiDoc files from templates
%autosetup -n %{srcname}-%{version}

%build
%py2_build
%py3_build

%install
# Must do the python2 install first because the scripts in /usr/bin are
# overwritten with every setup.py install, and in general we want the
# python3 version to be the default.
# If, however, we're installing separate executables for python2 and python3,
# the order needs to be reversed so the unversioned executable is the python2 one.
%py2_install
%py3_install

%check
%{__python2} setup.py test
%{__python3} setup.py test

# Note that there is no %%files section for the unversioned python module if we are building for several python runtimes
%files -n python2-%{srcname}
%license LICENSE
%doc README.rst
%{python2_sitelib}/*

%files -n python3-%{srcname}
%license LICENSE
%doc README.rst
%{python3_sitelib}/*
%{_bindir}/newdoc

%changelog
* Fri Sep 25 2020 Marek Suchánek <[email protected]> 1.5.1-1
- Announce the deprecation of this version and the migration to the new one
([email protected])

* Tue Jun 23 2020 Marek Suchánek <[email protected]> 1.5.0-1
- Remove the remaining Python 2 code ([email protected])
- Align the Optional formatting with the IBM Style Guide; #29
([email protected])
- Remove Python 2 packaging ([email protected])
- Update outdated version information ([email protected])
- Clarify newdoc install instructions ([email protected])

* Mon Oct 07 2019 Marek Suchánek <[email protected]> 1.4.3-1
- Fix a reference to the renamed readme in the RPM spec ([email protected])
- Updated the changelog ([email protected])
Expand Down
47 changes: 11 additions & 36 deletions newdoc/newdoc/newdoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,11 @@
import argparse
from string import Template

NEWDOC_VERSION = "1.3.2"

# Record whether we're running under Python 2 or 3
PYVERSION = sys.version_info.major
NEWDOC_VERSION = "1.5.1"
DEPRECATION = "DEPRECATED:\nThis version of `newdoc` is now deprecated and will no longer receive any significant updates.\nPlease uninstall this version and migrate to the current version:\nhttps://github.com/redhat-documentation/newdoc"

# The configparser module is called ConfigParser in Python2
if PYVERSION == 2:
import ConfigParser as cp
else:
import configparser as cp

# Force Python 2 to use the UTF-8 encoding. Otherwise, loading a template
# containing Unicode characters fails.
if PYVERSION == 2:
reload(sys)
sys.setdefaultencoding('utf8')
import configparser as cp
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not part of the changes, but you could easily import the three things instead of the module, I think.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean ConfigParser, MissingSectionHeaderError, and NoOptionError? Sure, importing them directly could somewhat optimize the performance if I called the objects in a loop. But since they aren't in a performance-sensitive place, I prefer to keep the namespace to make it clear where the objects come from.


# The directory where the script is located
SCRIPT_HOME_DIR = os.path.dirname(__file__)
Expand Down Expand Up @@ -92,7 +81,7 @@ def get_config():

for k in options.keys():
# The configparser library is different in Python 2 and 3.
# This si the only 2/3-compatible way I've found for optional keys.
# This is the only 2/3-compatible way I've found for optional keys.
try:
options[k] = config.get("newdoc", k)
except cp.NoOptionError:
Comment on lines 82 to 87
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe you want to somehow resolve this too? The comment seems to indicate you could do this "better". But then again, the code already works, so it does not matter much.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're absolutely right. It could be improved, but there's no reason to do it since the current code works.

Expand Down Expand Up @@ -204,12 +193,6 @@ def write_file(out_file, module_content):
performing necessary checks
"""

# In Python 2, the `input` function is called `raw_input`
if PYVERSION == 2:
compatible_input = raw_input
else:
compatible_input = input

# Check if the file exists; abort if so
if os.path.exists(out_file):
print('File already exists: "{}"'.format(out_file))
Expand All @@ -218,7 +201,7 @@ def write_file(out_file, module_content):
decision = None

while not decision:
response = compatible_input("Overwrite it? [yes/no] ").lower()
response = input("Overwrite it? [yes/no] ").lower()

if response in ["yes", "y"]:
print("Overwriting.")
Expand All @@ -232,13 +215,8 @@ def write_file(out_file, module_content):
# Write the file
with open(out_file, "w") as f:
f.write(module_content)
# In Python 2, the UTF-8 encoding has to be specified explicitly
if PYVERSION == 2:
with io.open(out_file, mode="w", encoding="utf-8") as f:
f.write(module_content)
else:
with open(out_file, "w") as f:
f.write(module_content)
with open(out_file, "w") as f:
f.write(module_content)

print("File successfully generated.")
print("To include this file from an assembly, use:")
Expand Down Expand Up @@ -271,13 +249,8 @@ def create_module(title, doc_type, options, delete_comments):
print("Error: Template file not found: '{}'".format(template_file))
exit(1)

# In Python 2, the UTF-8 encoding has to be specified explicitly
if PYVERSION == 2:
with io.open(template_file, mode="r", encoding="utf-8") as f:
template = f.read()
else:
with open(template_file, "r") as f:
template = f.read()
with open(template_file, "r") as f:
template = f.read()

# Prepare the content of the new module
module_content = Template(template).substitute(module_title=title,
Expand All @@ -296,6 +269,8 @@ def main():
"""
Main, executable procedure of the script
"""
print(DEPRECATION, "\n")

# Build a command-line options parser
parser = argparse.ArgumentParser()

Expand Down
2 changes: 1 addition & 1 deletion newdoc/newdoc/templates/proc_title.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ This paragraph is the procedure module introduction: a short description of the

.Verification steps

. (Optional) Provide the user with verification method(s) for the procedure, such as expected output or commands that can be used to check for success or failure.
. Optional: Provide the user with verification method(s) for the procedure, such as expected output or commands that can be used to check for success or failure.
. Use an unnumbered bullet (*) if the procedure includes only one step.

.Additional resources
Expand Down
2 changes: 1 addition & 1 deletion newdoc/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

setuptools.setup(
name="newdoc",
version="1.4.3",
version="1.5.1",
license="GPLv3+",
author="Marek Suchánek",
author_email="[email protected]",
Expand Down