Skip to content

Commit

Permalink
Merge pull request #4 from os-climate/apply-substitutions
Browse files Browse the repository at this point in the history
Chore: Make template substitutions [skip ci]
  • Loading branch information
ModeSevenIndustrialSolutions authored Jan 19, 2024
2 parents 3770ad3 + 03956cc commit 0fe80d0
Show file tree
Hide file tree
Showing 11 changed files with 186 additions and 36 deletions.
26 changes: 13 additions & 13 deletions CONTRIBUTING.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
Contributing
============

Welcome to ``osc-data-extractor`` contributor's guide.
Welcome to ``osc-transformer-based-extractor`` contributor's guide.

This document focuses on getting any potential contributor familiarized
with the development processes, but `other kinds of contributions`_ are also
Expand All @@ -46,7 +46,7 @@ guidelines.
Issue Reports
=============

If you experience bugs or general issues with ``osc-data-extractor``, please have a look
If you experience bugs or general issues with ``osc-transformer-based-extractor``, please have a look
on the `issue tracker`_. If you don't see anything useful there, please feel
free to fire an issue report.

Expand All @@ -65,10 +65,10 @@ you help us to identify the root cause of the issue.
Documentation Improvements
==========================

You can help improve ``osc-data-extractor`` docs by making them more readable and coherent, or
You can help improve ``osc-transformer-based-extractor`` docs by making them more readable and coherent, or
by adding missing information and correcting mistakes.

``osc-data-extractor`` documentation uses Sphinx_ as its main documentation compiler.
``osc-transformer-based-extractor`` documentation uses Sphinx_ as its main documentation compiler.
This means that the docs are kept in the same repository as the project code, and
that any documentation update is done in the same way was a code contribution.

Expand All @@ -80,7 +80,7 @@ that any documentation update is done in the same way was a code contribution.

.. tip::
Please notice that the `GitHub web interface`_ provides a quick way of
propose changes in ``osc-data-extractor``'s files. While this mechanism can
propose changes in ``osc-transformer-based-extractor``'s files. While this mechanism can
be tricky for normal code contributions, it works perfectly fine for
contributing to the docs, and can be quite handy.

Expand Down Expand Up @@ -131,8 +131,8 @@ This can easily be done via either |virtualenv|_::

or Miniconda_::

conda create -n osc-data-extractor python=3 six virtualenv pytest pytest-cov
conda activate osc-data-extractor
conda create -n osc-transformer-based-extractor python=3 six virtualenv pytest pytest-cov
conda activate osc-transformer-based-extractor

Clone the repository
--------------------
Expand All @@ -142,8 +142,8 @@ Clone the repository
page. This creates a copy of the code under your account on |the repository service|.
#. Clone this copy to your local disk::

git clone [email protected]:YourLogin/osc-data-extractor.git
cd osc-data-extractor
git clone [email protected]:YourLogin/osc-transformer-based-extractor.git
cd osc-transformer-based-extractor

#. You should run::

Expand All @@ -158,7 +158,7 @@ Clone the repository
pip install pre-commit
pre-commit install

``osc-data-extractor`` comes with a lot of hooks configured to automatically help the
``osc-transformer-based-extractor`` comes with a lot of hooks configured to automatically help the
developer to check the code being written.

Implement your changes
Expand Down Expand Up @@ -285,7 +285,7 @@ Releases

If you are part of the group of maintainers and have correct user permissions
on PyPI_, the following steps can be used to release a new version for
``osc-data-extractor``:
``osc-transformer-based-extractor``:

#. Make sure all unit tests are successful.
#. Tag the current commit on the main branch with a release tag, e.g., ``v1.2.3``.
Expand Down Expand Up @@ -314,8 +314,8 @@ on PyPI_, the following steps can be used to release a new version for
.. |the repository service| replace:: GitHub
.. |contribute button| replace:: "Create pull request"

.. _repository: https://github.com/<USERNAME>/osc-data-extractor
.. _issue tracker: https://github.com/<USERNAME>/osc-data-extractor/issues
.. _repository: https://github.com/<USERNAME>/osc-transformer-based-extractor
.. _issue tracker: https://github.com/<USERNAME>/osc-transformer-based-extractor/issues
.. <-- end -->
Expand Down
12 changes: 6 additions & 6 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@

.. image:: https://img.shields.io/badge/GitHub-100000?logo=github&logoColor=white
:alt: Source code on GitHub
:target: https://github.com/ModeSevenIndustrialSolutions/osc-data-extractor
:target: https://github.com/ModeSevenIndustrialSolutions/osc-transformer-based-extractor

.. image:: https://img.shields.io/pypi/v/osc-data-extractor.svg
.. image:: https://img.shields.io/pypi/v/osc-transformer-based-extractor.svg
:alt: PyPI package
:target: https://pypi.org/project/osc-data-extractor/
:target: https://pypi.org/project/osc-transformer-based-extractor/

.. image:: https://api.cirrus-ci.com/github/os-climate/osc-data-extractor.svg?branch=main
.. image:: https://api.cirrus-ci.com/github/os-climate/osc-transformer-based-extractor.svg?branch=main
:alt: Built Status
:target: https://cirrus-ci.com/github/os-climate/osc-data-extractor
:target: https://cirrus-ci.com/github/os-climate/osc-transformer-based-extractor

.. image:: https://img.shields.io/badge/PDM-Project-purple
:alt: Built using PDM
Expand All @@ -30,7 +30,7 @@


==================
osc-data-extractor
osc-transformer-based-extractor
==================

OS-Climate Data Extraction Tool
Expand Down
10 changes: 5 additions & 5 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
from sphinx import apidoc

output_dir = os.path.join(__location__, "api")
module_dir = os.path.join(__location__, "../src/osc_data_extractor")
module_dir = os.path.join(__location__, "../src/osc_transformer_based_extractor")
try:
shutil.rmtree(output_dir)
except FileNotFoundError:
Expand Down Expand Up @@ -87,7 +87,7 @@
master_doc = "index"

# General information about the project.
project = "osc-data-extractor"
project = "osc-transformer-based-extractor"
copyright = "2023, Matthew Watkins"

# The version info for the project you're documenting, acts as replacement for
Expand All @@ -99,7 +99,7 @@
# If you don’t need the separation provided between version and release,
# just set them both to the same value.
try:
from osc_data_extractor import __version__ as version
from osc_transformer_based_extractor import __version__ as version
except ImportError:
version = ""

Expand Down Expand Up @@ -229,7 +229,7 @@
# html_file_suffix = None

# Output file base name for HTML help builder.
htmlhelp_basename = "osc-data-extractor-doc"
htmlhelp_basename = "osc-transformer-based-extractor-doc"


# -- Options for LaTeX output ------------------------------------------------
Expand All @@ -246,7 +246,7 @@
# Grouping the document tree into LaTeX files. List of tuples
# (source start file, target name, title, author, documentclass [howto/manual]).
latex_documents = [
("index", "user_guide.tex", "osc-data-extractor Documentation", "Matthew Watkins", "manual")
("index", "user_guide.tex", "osc-transformer-based-extractor Documentation", "Matthew Watkins", "manual")
]

# The name of an image file (relative to this directory) to place at the top of
Expand Down
4 changes: 2 additions & 2 deletions docs/index.rst
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
==================
osc-data-extractor
osc-transformer-based-extractor
==================

This is the documentation of **osc-data-extractor**.
This is the documentation of **osc-transformer-based-extractor**.

.. note::

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ build-backend = "pdm.backend"
license-files = ["LICENSES.txt"]

[project.scripts]
osc-transformer-based-extractor = "osc_data_extractor.skeleton:run"
osc-transformer-based-extractor = "osc_transformer_based_extractor.skeleton:run"

[project.optional-dependencies]
dev = [
Expand Down
24 changes: 24 additions & 0 deletions scripts/rename-tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/bin/bash

#set -x

REPO_NAME=$(basename "$(git rev-parse --show-toplevel)")
echo "Repository name: $REPO_NAME"

if [ $# -ne 1 ]; then
echo "Usage: $0 [test folder]"; exit 1
elif [ ! -d "$1" ]; then
echo "Error: specified target was not a folder"; exit 1
else
# Target specified was a folder
TARGET="$1"
fi

for TEST in $(find "$TARGET" -type f -name '*_test.py' | xargs -0); do
echo "Processing: $TEST"
FILE_PATH=$(dirname "$TEST")
FILE_NAME=$(basename "$TEST")
STRIPPED="${FILE_NAME//_test.py/.py}"
echo " git mv \"${TEST}\" $FILE_PATH/test_\"${STRIPPED%%}\""
git mv "${TEST}" "$FILE_PATH"/test_"${STRIPPED%%}"
done
127 changes: 127 additions & 0 deletions scripts/template-to-repo.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
#!/bin/bash

# set -x

### Shared functions

# Renames files/folders containing template name
rename_object() {
if [ $# -ne 1 ]; then
echo "Function requires an argumeent: rename_object [filesystem object]"; exit 1
else
FS_OBJECT="$1"
fi
# Function take a filesystem object as a single argument
FS_OBJECT="$1"
OBJECT_PATH=$(dirname "$FS_OBJECT")
OBJECT_NAME=$(basename "$FS_OBJECT")

# Check if filesystem object contains template name
if [[ ! "$OBJECT_NAME" == *"$TEMPLATE_NAME"* ]]; then
# Nothing to do; abort early
return
else
NEW_NAME="${OBJECT_NAME//$TEMPLATE_NAME/$REPO_NAME}"
fi
if [[ ! "$OBJECT_NAME" == *"$ALT_TEMPLATE_NAME"* ]]; then
# Nothing to do; abort early
return
else
NEW_NAME="${OBJECT_NAME//$ALT_TEMPLATE_NAME/$ALT_REPO_NAME}"
fi

# Perform the renaming operation
if [ -d "$FS_OBJECT" ]; then
echo "Renaming folder: $FS_OBJECT"
elif [ -f "$FS_OBJECT" ]; then
echo "Renaming file: $FS_OBJECT"
elif [ -L "$FS_OBJECT" ]; then
echo "Renaming symlink: $FS_OBJECT"
fi
git mv "$OBJECT_PATH/$OBJECT_NAME" "$OBJECT_PATH/$NEW_NAME"
}

# Checks file content for template name and replaces matching strings
file_content_substitution() {
if [ $# -ne 1 ]; then
echo "Function requires an argument: file_content_substitution [filename]"; exit 1
else
FILENAME="$1"
fi
COUNT=0
if (grep "$TEMPLATE_NAME" "$FILENAME" > /dev/null 2>&1); then
MATCHES=$(grep -c "$TEMPLATE_NAME" "$FILENAME")
if [ "$MATCHES" -eq 1 ]; then
echo "1 content substitution required: $FILENAME (dashes)"
COUNT=$((COUNT++))
else
echo "$MATCHES content substitutions required: $FILENAME (dashes)"
COUNT=$((COUNT+MATCHES))
fi
sed -i "s/$TEMPLATE_NAME/$REPO_NAME/g" "$FILENAME"
fi
if (grep "$ALT_TEMPLATE_NAME" "$FILENAME" > /dev/null 2>&1); then
MATCHES=$(grep -c "$ALT_TEMPLATE_NAME" "$FILENAME")
if [ "$MATCHES" -eq 1 ]; then
echo "1 content substitution required: $FILENAME (underscores)"
COUNT=$((COUNT++))
else
echo "$MATCHES content substitutions required: $FILENAME (underscores)"
COUNT=$((COUNT+MATCHES))
fi
sed -i "s/$ALT_TEMPLATE_NAME/$ALT_REPO_NAME/g" "$FILENAME"
fi
if [[ "$COUNT" != "0" ]] && [[ "$COUNT" = "1" ]]; then
echo "$COUNT substitution made in file: $FILENAME"
elif [[ "$COUNT" != "0" ]] && [[ "$COUNT" -gt "1" ]]; then
echo "$COUNT substitutions made in file: $FILENAME"
fi
}

### Main script entry point

TEMPLATE_NAME=osc-transformer-based-extractor
ALT_TEMPLATE_NAME="${TEMPLATE_NAME//-/_}"

if ! (git rev-parse --show-toplevel > /dev/null); then
echo "Error: this folder is not part of a GIT repository"; exit 1
fi

REPO_DIR=$(git rev-parse --show-toplevel)
REPO_NAME=$(basename "$REPO_DIR")
ALT_REPO_NAME="${REPO_NAME//-/_}"

if [ "$TEMPLATE_NAME" == "$REPO_NAME" ]; then
echo "WARNING: template name matches repository name"
else
echo "Template name: $TEMPLATE_NAME"
echo "Alternate name: $ALT_TEMPLATE_NAME"
echo "Repository name: $REPO_NAME"
echo "Alternate name: $ALT_REPO_NAME"
fi

# Change to top-level of GIT repository
CURRENT_DIR=$(pwd)
if [ "$REPO_DIR" != "$CURRENT_DIR" ]; then
echo "Changing directory to: $REPO_DIR"
if ! (cd "$REPO_DIR"); then
echo "Could not change directory!"; exit 1
fi
fi

echo "Processing repository contents..."

# Rename directories first, as they affect file paths afterwards
for FS_OBJECT in $(find -- * -type d | xargs -0); do
rename_object "$FS_OBJECT"
if [ -f "$FS_OBJECT" ]; then
file_content_substitution "$FS_OBJECT"
fi
done

for FS_OBJECT in $(find -- * -type f | xargs -0); do
rename_object "$FS_OBJECT"
if [ -f "$FS_OBJECT" ]; then
file_content_substitution "$FS_OBJECT"
fi
done
2 changes: 1 addition & 1 deletion src/osc_data_extractor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

try:
# Change here if project is renamed and does not equal the package name
dist_name = "osc-data-extractor"
dist_name = "osc-transformer-based-extractor"
__version__ = version(dist_name)
except PackageNotFoundError: # pragma: no cover
__version__ = "unknown"
Expand Down
10 changes: 5 additions & 5 deletions src/osc_data_extractor/skeleton.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
``[options.entry_points]`` section in ``setup.cfg``::
console_scripts =
fibonacci = osc_data_extractor.skeleton:run
fibonacci = osc_transformer_based_extractor.skeleton:run
Then run ``pip install .`` (or ``pip install -e .`` for editable mode)
which will install the command ``fibonacci`` inside your current environment.
Expand All @@ -24,7 +24,7 @@
import logging
import sys

from osc_data_extractor import __version__
from osc_transformer_based_extractor import __version__

__author__ = "Matthew Watkins"
__copyright__ = "Matthew Watkins"
Expand All @@ -36,7 +36,7 @@
# ---- Python API ----
# The functions defined in this section can be imported by users in their
# Python scripts/interactive interpreter, e.g. via
# `from osc_data_extractor.skeleton import fib`,
# `from osc_transformer_based_extractor.skeleton import fib`,
# when using this Python module as a library.


Expand Down Expand Up @@ -76,7 +76,7 @@ def parse_args(args):
parser.add_argument(
"--version",
action="version",
version=f"osc-data-extractor {__version__}",
version=f"osc-transformer-based-extractor {__version__}",
)
parser.add_argument(dest="n", help="n-th Fibonacci number", type=int, metavar="INT")
parser.add_argument(
Expand Down Expand Up @@ -142,6 +142,6 @@ def run():
# After installing your project with pip, users can also run your Python
# modules as scripts via the ``-m`` flag, as defined in PEP 338::
#
# python -m osc_data_extractor.skeleton 42
# python -m osc_transformer_based_extractor.skeleton 42
#
run()
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""
Dummy conftest.py for osc_data_extractor.
Dummy conftest.py for osc_transformer_based_extractor.
If you don't know what this is for, just leave it empty.
Read more about conftest.py under:
Expand Down
Loading

0 comments on commit 0fe80d0

Please sign in to comment.