Skip to main content

Package providing command line tools to move/copy function/classes and their associated import statements between files.

Project description

mvdef

Documentation CI Status Coverage Checked with mypy Code style: black

Package providing command line tools to move/copy function/classes and their associated import statements between files

Read The Docs

Requires

  • Python 3.10+

Installation

pip install mvdef

Usage

mvdef

Moves functions named by -m/--mv and their associated imports from src to dst, or just previews the changes as a diff if passed -d/--dry-run.

usage: mvdef [-h] -m [MV ...] [-d] [-e] [-c] [-f] [-v] src dst

  Move function definitions from one file to another, moving/copying
  any necessary associated import statements along with them.

  Option     Description                                Type        Default
  —————————— —————————————————————————————————————————— ——————————— ———————
• src        source file to take definitions from       Path        -
• dst        destination file (may not exist)           Path        -
• mv         names to move from the source file         list[str]   -
• dry_run    whether to only preview the change diffs   bool        False
• escalate   whether to raise an error upon failure     bool        False
• cls_defs   whether to use only class definitions      bool        False
• func_defs  whether to use only function definitions   bool        False
• verbose    whether to log anything                    bool        False

positional arguments:
  src
  dst

options:
  -h, --help            show this help message and exit
  -m [MV ...], --mv [MV ...]
  -d, --dry-run
  -e, --escalate
  -c, --cls-defs
  -f, --func-defs
  -v, --verbose

cpdef

Copies functions named by -m/--mv and their associated imports from src to dst, or just previews the changes as a diff if passed -d/--dry-run.

Has the same flags and signature as mvdef, but never changes src.

usage: cpdef [-h] -m [MV ...] [-d] [-e] [-c] [-f] [-v] src dst

  Copy function definitions from one file to another, and any necessary
  associated import statements along with them.

  Option     Description                                Type        Default
  —————————— —————————————————————————————————————————— ——————————— ———————
• src        source file to copy definitions from       Path        -
• dst        destination file (may not exist)           Path        -
• mv         names to copy from the source file         list[str]   -
• dry_run    whether to only preview the change diffs   bool        False
• escalate   whether to raise an error upon failure     bool        False
• cls_defs   whether to use only class definitions      bool        False
• func_defs  whether to use only function definitions   bool        False
• verbose    whether to log anything                    bool        False

positional arguments:
  src
  dst

options:
  -h, --help            show this help message and exit
  -m [MV ...], --mv [MV ...]
  -d, --dry-run
  -e, --escalate
  -c, --cls-defs
  -f, --func-defs
  -v, --verbose

lsdef

Has a similar signature, but no dst (it operates on just one file) and the mv argument is replaced by match, which can specify regular expressions (default * matches any name).

usage: lsdef [-h] [-m [MATCH ...]] [-d] [-l] [-e] [-c] [-f] [-v] src

  List function definitions in a given file.

  Option     Description                                Type        Default
  —————————— —————————————————————————————————————————— ——————————— ———————
• src        source file to list definitions from       Path        -
• match      name regex to list from the source file    list[str]   ['*']
• dry_run    whether to print the __all__ diff          bool        False
• list       whether to print the list of names         bool        False
• escalate   whether to raise an error upon failure     bool        False
• cls_defs   whether to use only class definitions      bool        False
• func_defs  whether to use only function definitions   bool        False
• verbose    whether to log anything                    bool        False

positional arguments:
  src

options:
  -h, --help            show this help message and exit
  -m [MATCH ...], --match [MATCH ...]
  -d, --dry-run
  -l, --list
  -e, --escalate
  -c, --cls-defs
  -f, --func-defs
  -v, --verbose

How it works

The structure of a mvdef invocation

When you call mvdef foo.py bar.py -d -c -m A, equivalent to:

mvdef foo.py bar.py --dry-run --cls-defs --mv A

You're requesting to show the file diffs it'd take (--dry-run) to move the class definition (--cls-defs) named A (--mv A) from foo.py (the src, first positional argument) to bar.py (the dst, second positional argument).

Parsing the request

The request to move a definition is stored on a dataclass mvdef.transfer.MvDef immediately upon invoking the program command.

Upon creation, this class stores 2 attributes src_diff and dst_diff (both are mvdef.diff.Differ objects) which will coordinate the creation of patches, or 'diffs'. These start their life with empty agendas (mvdef.agenda.Agenda).

Next, the main MvDef class calls its check() method, which returns an exception (or raises it if escalate is True), preventing further work if the source file does not contain a class named A as requested.

If the source file has the required definitions to fulfil the request, then the MvDef.diffs() method gets called next, populates the empty agendas on the Differ objects for the 2 files, then produces the diffs they imply.

If the dry run setting is not used, the source and destination files are overwritten with the changes, instead of just displaying the diffs.

lsdef approach

lsdef is similar, but instead of making a src_diff it makes a src_manifest (mvdef.manifest.Manifest object), and there is no dst file to handle.

Error handling is the same as above.


mvdef is available from PyPI, and the code is on GitHub

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mvdef-0.9.4.tar.gz (411.2 kB view details)

Uploaded Source

Built Distribution

mvdef-0.9.4-py3-none-any.whl (410.1 kB view details)

Uploaded Python 3

File details

Details for the file mvdef-0.9.4.tar.gz.

File metadata

  • Download URL: mvdef-0.9.4.tar.gz
  • Upload date:
  • Size: 411.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: pdm/2.12.4 CPython/3.10.6

File hashes

Hashes for mvdef-0.9.4.tar.gz
Algorithm Hash digest
SHA256 1f7ed65276eecb6a448aace12932dfd74b2bd72e1c95f03a30a529fac985d25f
MD5 f020e569a4c8e114151d386357a5021c
BLAKE2b-256 742afd7d63bdd6e375e6d42dfba5717dbd167edcea69085a0e3a91b169f0cf3a

See more details on using hashes here.

File details

Details for the file mvdef-0.9.4-py3-none-any.whl.

File metadata

  • Download URL: mvdef-0.9.4-py3-none-any.whl
  • Upload date:
  • Size: 410.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: pdm/2.12.4 CPython/3.10.6

File hashes

Hashes for mvdef-0.9.4-py3-none-any.whl
Algorithm Hash digest
SHA256 e17fe8ad1a182da281585e62577b127037d59c22757134169b52011cd73df048
MD5 f96594188f9e3c27a123360e52d37e36
BLAKE2b-256 d9d998817d1d0eba6ff97bba0c1d035dcb3cd69b25307d3adae6d1249c1ca131

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page