addheader


Nameaddheader JSON
Version 0.3.2 PyPI version JSON
download
home_page
SummaryA command to manage a header section for a source code tree
upload_time2022-12-12 21:17:20
maintainer
docs_urlNone
authorThe IDAES Project
requires_python>=3.7
license
keywords software engineering text processing utilities
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # addheader - add headers to files
This repository contains a single command to manage a header section, 
e.g. copyright, for a source code tree.

Using UNIX glob patterns, addheader modifies an entire tree of
source code at once. The program replaces existing headers with
an updated version, and places the header after any shell magic
at the top of the file.

As of version 0.3.0, Jupyter notebooks can also be handled.
See Usage -> Adding headers to Jupyter Notebooks.

## Installation

_addheader_ is written in Python and can be simply installed from the PyPI package:

```
pip install addheader
```

If you want Jupyter Notebook support, add "jupyter" in square brackets after the name of the package
(use the quotes unless you know your shell doesn't need them):

```
pip install 'addheader[jupyter]'
```

## Usage

Use the command `addheader`. Invoking`addheader -h` shows a detailed help message
for the command arguments and options. Below are some examples and comments on usage.

### Basic usage

If you have the header file in "copyright.txt", and your source tree is a Python
package located at "./mypackage",
then you would invoke the program like this:
```shell
adddheader mypackage --text copyright.txt
```
By default, the header will not be added to "__init__.py" files.

### Additional actions

If you want to see which files would be changed without modifying them, add
`-n` or `--dry-run` to the command line arguments.
If this argument is given, any arguments related to modifying or removing headers will be ignored.

If you want to remove existing headers instead of adding or updating them,
add `-r` or `--remove` to the command line arguments.

### Specifying file patterns

You can customize the files that
are modified with the `-p` or `--pattern` argument, which takes a UNIX glob-style pattern and can be
repeated as many times as you like. To help exclude files, if the '~' is the first letter of the pattern,
then the rest of the pattern is used to exclude (not include) files. So, for example, if you provide the
following source code tree:
```
mypackage
   __init__.py
   foo.py
   bar.py
   tests/
       __init__.py
       test_foo.py
       test_bar.py
```
The following commands would match the following lists of files:

* `addheader mypackage -t header.txt -p *.py`  
mypackage/{__init__.py, foo.py, bar.py}, mypackage/tests/{__init__.py, test_foo.py, test_bar.py}
* `addheader mypackage -t header.txt -p *.py -p ~__init__.py`  
mypackage/{foo.py, bar.py}, mypackage/tests/{test_foo.py, test_bar.py}
* `addheader mypackage -t header.txt -p *.py -p ~__init__.py -p ~test_*.py`  
mypackage/{foo.py, bar.py}

### Header delimiters

The header itself is, by default, delimited by a line of 78 '#' characters. While _detecting_ an existing
header, the program will look for any separator of 10 or more '#' characters.
For example, if you have a file that looks like this:
```python
##########
my header with 10
hashes above and below
##########
hello
```

and a header text file containing simply "Hello, world!", then the modified
header will be:
```python
##############################################################################
# Hello, world!
##############################################################################
hello
```

The comment character and separator character, as well as the width of the
separator, can be modified with command-line options. For example, to add
a C/C++ style comment as a header, use these options:

```shell
addheader mypackage --comment "//" --sep "=" --sep-len 40 -t myheader.txt
```

This will insert a header that looks like this:
```
//========================================
// my text goes here
//========================================
```

Keep in mind that subsequent operations on files with this header, including
`--remove`, will need the same `--comment` and `--sep`
arguments so that the header can be properly identified. For example,
running `addheader mypackage --remove` after the above command will not
remove anything, and `addheader mypackage -t myheader.txt` will insert a 
second header (using the default comment character  and separator). 

You can control whether the final line has a newline character appended with the `--final-linesep` command-line option (or the `final_linesep` configuration option). This is True by default for text files, but False for Jupyter notebooks. The logic is that Jupyter notebook headers are in their own cell -- and also, this avoids spurious modifications by the Black code reformatter.

> To avoid
passing command-line arguments every time, 
> use the configuration file.
> See the "Configuration" section for more details.

### Adding headers to Jupyter notebooks

Starting in version 0.3.0, you can add headers to Jupyter Notebooks as well.

> To enable Jupyter notebooks, you must
> install the 'jupyter' optional dependencies, e.g.,
> `pip install addheader[jupyter]`.

To enable this, add a `-j {suffix}` or `--jupyter {suffix}` argument to the command-line, or
similarly add a `jupyter: {suffix}` argument in the configuration file.
The `{suffix}` indicates an alternate file suffix to use for identifying
whether a file is a Jupyter Notebook, where the default is ".ipynb".
In the configuration file, use `jupyter: true` to use the default.
On the command-line, omit the value to use the default.

To set the Jupyter notebook format version, add `--notebook-version {value}` to the command-line or,  equivalently, `notebook_version: {value}` to the configuration file.
Values can be from 1 to 4. The default value is 4.

The file pattern arguments (see *Specifying file patterns*, above) are still honored,
but if Jupyter notebooks are enabled, the pattern `*{suffix}` will be automatically added
 to the patterns to match. Thus, by default `*.ipynb` will be added to the files to match.

If there is no existing header, the Jupyter notebook header will be inserted as the first 'cell', i.e. the first
item, in the notebook. An existing header will be found anywhere in the notebook (by its `header` tag, see below).

Currently the header cell is of type "code", with every line of the cell
commented (using a 'markdown' cell is another possibility, but the code cell is friendler to the Jupyterbook machinery, and also retains the header in exported versions of the notebook without markdown cells).
The content of the header is the same as for text files.
Two, optionally three, tags will be added to the cell metadata:
* `header` - Indicates this is the header cell, so it can be modified or removed later.
* `hide-cell` - If you build documentation with Jupyterbook, this will hide the cell in the generated documentation behind a toggle button (see https://jupyterbook.org/interactive/hiding.html).

Just as for text files, Jupyter notebook headers can be updated or removed.


For reference, below is the form of the generated Jupyter notebook cell JSON (with the 'id' field):

```json
   {
      "id": "1234567890abcdef1234567890abcdef",
      "cell_type": "code",
      "metadata": {
        "tags": [
          "header",
          "hide-cell"
        ]
      },
      "source": [
        "# Copyright info\n",
        "# is placed here.\n"
      ],
      "outputs": []
    }
```

### Configuration
To avoid passing commandline arguments every time, you can create a configuration
file that simply lists them as key/value pairs (using the long-option name as
the key). By default, the program will look for a file `addheader.cfg` in the
current directory, but this can also be specified on the command-line with 
`-c/--config`. For example:

```shell
addheader  # looks for addheader.cfg, ok if not present
addheader -c myoptions.conf  # uses myoptions.conf, fails if not present
```

The configuration file is in YAML format. For example:

```yaml
text: myheader.txt
pattern:
   - "*.py"
   - "~__init__.py"
# C/Java style comment block
sep: "-"
comment: "//"
sep-len: 40
# Verbosity as a number instead of -vv
verbose: 2
```

Command-line arguments will override configuration arguments, even if the
configuration file is explicitly provided with `-c/--config`.
The "action" arguments, `-r/--remove` and `-n/--dry-run`, will be
ignored in the configuration file.

### Using in tests

To test your package for files missing headers, use the following formula:
```python
import os
import mypackage
from addheader.add import FileFinder, detect_files

def test_headers():
    root = os.path.dirname(mypackage.__file__)
    # modify patterns to match the files that should have headers
    ff = FileFinder(root, glob_pat=["*.py", "~__init__.py"])
    has_header, missing_header = detect_files(ff)
    assert len(missing_header) == 0
```

## Credits
The _addheader_ program was developed for use in the [IDAES](www.idaes.org) project and is maintained in the
IDAES organization in Github at https://github.com/IDAES/addheader . The primary author
and maintainer is Dan Gunter (dkgunter at lbl dot gov).

## License
Please see the COPYRIGHT.md and LICENSE.md files in the repository for
limitations on use and distribution of this software.

            

Raw data

            {
    "_id": null,
    "home_page": "",
    "name": "addheader",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.7",
    "maintainer_email": "",
    "keywords": "software engineering,text processing,utilities",
    "author": "The IDAES Project",
    "author_email": "Dan Gunter <dkgunter@lbl.gov>",
    "download_url": "https://files.pythonhosted.org/packages/c7/d2/d86428f50c0930eab8b1326345f126df14b0375beaabef8b96eacc33c233/addheader-0.3.2.tar.gz",
    "platform": null,
    "description": "# addheader - add headers to files\r\nThis repository contains a single command to manage a header section, \r\ne.g. copyright, for a source code tree.\r\n\r\nUsing UNIX glob patterns, addheader modifies an entire tree of\r\nsource code at once. The program replaces existing headers with\r\nan updated version, and places the header after any shell magic\r\nat the top of the file.\r\n\r\nAs of version 0.3.0, Jupyter notebooks can also be handled.\r\nSee Usage -> Adding headers to Jupyter Notebooks.\r\n\r\n## Installation\r\n\r\n_addheader_ is written in Python and can be simply installed from the PyPI package:\r\n\r\n```\r\npip install addheader\r\n```\r\n\r\nIf you want Jupyter Notebook support, add \"jupyter\" in square brackets after the name of the package\r\n(use the quotes unless you know your shell doesn't need them):\r\n\r\n```\r\npip install 'addheader[jupyter]'\r\n```\r\n\r\n## Usage\r\n\r\nUse the command `addheader`. Invoking`addheader -h` shows a detailed help message\r\nfor the command arguments and options. Below are some examples and comments on usage.\r\n\r\n### Basic usage\r\n\r\nIf you have the header file in \"copyright.txt\", and your source tree is a Python\r\npackage located at \"./mypackage\",\r\nthen you would invoke the program like this:\r\n```shell\r\nadddheader mypackage --text copyright.txt\r\n```\r\nBy default, the header will not be added to \"__init__.py\" files.\r\n\r\n### Additional actions\r\n\r\nIf you want to see which files would be changed without modifying them, add\r\n`-n` or `--dry-run` to the command line arguments.\r\nIf this argument is given, any arguments related to modifying or removing headers will be ignored.\r\n\r\nIf you want to remove existing headers instead of adding or updating them,\r\nadd `-r` or `--remove` to the command line arguments.\r\n\r\n### Specifying file patterns\r\n\r\nYou can customize the files that\r\nare modified with the `-p` or `--pattern` argument, which takes a UNIX glob-style pattern and can be\r\nrepeated as many times as you like. To help exclude files, if the '~' is the first letter of the pattern,\r\nthen the rest of the pattern is used to exclude (not include) files. So, for example, if you provide the\r\nfollowing source code tree:\r\n```\r\nmypackage\r\n   __init__.py\r\n   foo.py\r\n   bar.py\r\n   tests/\r\n       __init__.py\r\n       test_foo.py\r\n       test_bar.py\r\n```\r\nThe following commands would match the following lists of files:\r\n\r\n* `addheader mypackage -t header.txt -p *.py`  \r\nmypackage/{__init__.py, foo.py, bar.py}, mypackage/tests/{__init__.py, test_foo.py, test_bar.py}\r\n* `addheader mypackage -t header.txt -p *.py -p ~__init__.py`  \r\nmypackage/{foo.py, bar.py}, mypackage/tests/{test_foo.py, test_bar.py}\r\n* `addheader mypackage -t header.txt -p *.py -p ~__init__.py -p ~test_*.py`  \r\nmypackage/{foo.py, bar.py}\r\n\r\n### Header delimiters\r\n\r\nThe header itself is, by default, delimited by a line of 78 '#' characters. While _detecting_ an existing\r\nheader, the program will look for any separator of 10 or more '#' characters.\r\nFor example, if you have a file that looks like this:\r\n```python\r\n##########\r\nmy header with 10\r\nhashes above and below\r\n##########\r\nhello\r\n```\r\n\r\nand a header text file containing simply \"Hello, world!\", then the modified\r\nheader will be:\r\n```python\r\n##############################################################################\r\n# Hello, world!\r\n##############################################################################\r\nhello\r\n```\r\n\r\nThe comment character and separator character, as well as the width of the\r\nseparator, can be modified with command-line options. For example, to add\r\na C/C++ style comment as a header, use these options:\r\n\r\n```shell\r\naddheader mypackage --comment \"//\" --sep \"=\" --sep-len 40 -t myheader.txt\r\n```\r\n\r\nThis will insert a header that looks like this:\r\n```\r\n//========================================\r\n// my text goes here\r\n//========================================\r\n```\r\n\r\nKeep in mind that subsequent operations on files with this header, including\r\n`--remove`, will need the same `--comment` and `--sep`\r\narguments so that the header can be properly identified. For example,\r\nrunning `addheader mypackage --remove` after the above command will not\r\nremove anything, and `addheader mypackage -t myheader.txt` will insert a \r\nsecond header (using the default comment character  and separator). \r\n\r\nYou can control whether the final line has a newline character appended with the `--final-linesep` command-line option (or the `final_linesep` configuration option). This is True by default for text files, but False for Jupyter notebooks. The logic is that Jupyter notebook headers are in their own cell -- and also, this avoids spurious modifications by the Black code reformatter.\r\n\r\n> To avoid\r\npassing command-line arguments every time, \r\n> use the configuration file.\r\n> See the \"Configuration\" section for more details.\r\n\r\n### Adding headers to Jupyter notebooks\r\n\r\nStarting in version 0.3.0, you can add headers to Jupyter Notebooks as well.\r\n\r\n> To enable Jupyter notebooks, you must\r\n> install the 'jupyter' optional dependencies, e.g.,\r\n> `pip install addheader[jupyter]`.\r\n\r\nTo enable this, add a `-j {suffix}` or `--jupyter {suffix}` argument to the command-line, or\r\nsimilarly add a `jupyter: {suffix}` argument in the configuration file.\r\nThe `{suffix}` indicates an alternate file suffix to use for identifying\r\nwhether a file is a Jupyter Notebook, where the default is \".ipynb\".\r\nIn the configuration file, use `jupyter: true` to use the default.\r\nOn the command-line, omit the value to use the default.\r\n\r\nTo set the Jupyter notebook format version, add `--notebook-version {value}` to the command-line or,  equivalently, `notebook_version: {value}` to the configuration file.\r\nValues can be from 1 to 4. The default value is 4.\r\n\r\nThe file pattern arguments (see *Specifying file patterns*, above) are still honored,\r\nbut if Jupyter notebooks are enabled, the pattern `*{suffix}` will be automatically added\r\n to the patterns to match. Thus, by default `*.ipynb` will be added to the files to match.\r\n\r\nIf there is no existing header, the Jupyter notebook header will be inserted as the first 'cell', i.e. the first\r\nitem, in the notebook. An existing header will be found anywhere in the notebook (by its `header` tag, see below).\r\n\r\nCurrently the header cell is of type \"code\", with every line of the cell\r\ncommented (using a 'markdown' cell is another possibility, but the code cell is friendler to the Jupyterbook machinery, and also retains the header in exported versions of the notebook without markdown cells).\r\nThe content of the header is the same as for text files.\r\nTwo, optionally three, tags will be added to the cell metadata:\r\n* `header` - Indicates this is the header cell, so it can be modified or removed later.\r\n* `hide-cell` - If you build documentation with Jupyterbook, this will hide the cell in the generated documentation behind a toggle button (see https://jupyterbook.org/interactive/hiding.html).\r\n\r\nJust as for text files, Jupyter notebook headers can be updated or removed.\r\n\r\n\r\nFor reference, below is the form of the generated Jupyter notebook cell JSON (with the 'id' field):\r\n\r\n```json\r\n   {\r\n      \"id\": \"1234567890abcdef1234567890abcdef\",\r\n      \"cell_type\": \"code\",\r\n      \"metadata\": {\r\n        \"tags\": [\r\n          \"header\",\r\n          \"hide-cell\"\r\n        ]\r\n      },\r\n      \"source\": [\r\n        \"# Copyright info\\n\",\r\n        \"# is placed here.\\n\"\r\n      ],\r\n      \"outputs\": []\r\n    }\r\n```\r\n\r\n### Configuration\r\nTo avoid passing commandline arguments every time, you can create a configuration\r\nfile that simply lists them as key/value pairs (using the long-option name as\r\nthe key). By default, the program will look for a file `addheader.cfg` in the\r\ncurrent directory, but this can also be specified on the command-line with \r\n`-c/--config`. For example:\r\n\r\n```shell\r\naddheader  # looks for addheader.cfg, ok if not present\r\naddheader -c myoptions.conf  # uses myoptions.conf, fails if not present\r\n```\r\n\r\nThe configuration file is in YAML format. For example:\r\n\r\n```yaml\r\ntext: myheader.txt\r\npattern:\r\n   - \"*.py\"\r\n   - \"~__init__.py\"\r\n# C/Java style comment block\r\nsep: \"-\"\r\ncomment: \"//\"\r\nsep-len: 40\r\n# Verbosity as a number instead of -vv\r\nverbose: 2\r\n```\r\n\r\nCommand-line arguments will override configuration arguments, even if the\r\nconfiguration file is explicitly provided with `-c/--config`.\r\nThe \"action\" arguments, `-r/--remove` and `-n/--dry-run`, will be\r\nignored in the configuration file.\r\n\r\n### Using in tests\r\n\r\nTo test your package for files missing headers, use the following formula:\r\n```python\r\nimport os\r\nimport mypackage\r\nfrom addheader.add import FileFinder, detect_files\r\n\r\ndef test_headers():\r\n    root = os.path.dirname(mypackage.__file__)\r\n    # modify patterns to match the files that should have headers\r\n    ff = FileFinder(root, glob_pat=[\"*.py\", \"~__init__.py\"])\r\n    has_header, missing_header = detect_files(ff)\r\n    assert len(missing_header) == 0\r\n```\r\n\r\n## Credits\r\nThe _addheader_ program was developed for use in the [IDAES](www.idaes.org) project and is maintained in the\r\nIDAES organization in Github at https://github.com/IDAES/addheader . The primary author\r\nand maintainer is Dan Gunter (dkgunter at lbl dot gov).\r\n\r\n## License\r\nPlease see the COPYRIGHT.md and LICENSE.md files in the repository for\r\nlimitations on use and distribution of this software.\r\n",
    "bugtrack_url": null,
    "license": "",
    "summary": "A command to manage a header section for a source code tree",
    "version": "0.3.2",
    "split_keywords": [
        "software engineering",
        "text processing",
        "utilities"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "md5": "f845b3b011fa84378d7f88f5342eb06d",
                "sha256": "f506ef3f1de55fcb7aa0d9ba6189c8e8e469b118ca7c5a34fc0d73a23dbf10ca"
            },
            "downloads": -1,
            "filename": "addheader-0.3.2-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "f845b3b011fa84378d7f88f5342eb06d",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.7",
            "size": 17199,
            "upload_time": "2022-12-12T21:17:18",
            "upload_time_iso_8601": "2022-12-12T21:17:18.029096Z",
            "url": "https://files.pythonhosted.org/packages/e5/0f/d947d8e2dc4cab4c9ce4b2b48af88189b1c4884f126ccd2593a454014c70/addheader-0.3.2-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "md5": "886062acdb5c68dd9f5b171837c34692",
                "sha256": "d71587d14596e942b008e58d7624c4977a2737d6aab79c2f9b46cb90ec38b811"
            },
            "downloads": -1,
            "filename": "addheader-0.3.2.tar.gz",
            "has_sig": false,
            "md5_digest": "886062acdb5c68dd9f5b171837c34692",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.7",
            "size": 18901,
            "upload_time": "2022-12-12T21:17:20",
            "upload_time_iso_8601": "2022-12-12T21:17:20.209169Z",
            "url": "https://files.pythonhosted.org/packages/c7/d2/d86428f50c0930eab8b1326345f126df14b0375beaabef8b96eacc33c233/addheader-0.3.2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2022-12-12 21:17:20",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "lcname": "addheader"
}
        
Elapsed time: 0.19864s