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:

usage: mvdef [-h] -m [MV ...] [-d] [-e] [-c] [-a] [-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 of 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 target only class definitions   bool (False)
• all_defs   whether to target both class and funcdefs  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
  -a, --all-defs
  -v, --verbose

cpdef has the same flags as mvdef, but only copies (i.e. makes no changes to src).

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

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

How it works

The structure of a command

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.

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.2.tar.gz (413.9 kB view hashes)

Uploaded Source

Built Distribution

mvdef-0.9.2-py3-none-any.whl (403.9 kB view hashes)

Uploaded Python 3

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