Skip to main content

OCM: Object Command Mapping

I often need to call command line software in Python scripts, usually through subprocess, for example:

import subprocess


subprocess.run(['ls','-l','/Users/dev'])

The following points of using subprocess make me feel inconvenient:

  1. Need to determine the order of the parameters to determine the order of the list
  2. No parameter verification
  3. It is not convenient to get parameters, for example: ['ls','-l','/Users/dev'], I want to get the directory

In response to the above pain points, I implemented OCM. OCM is named after ORM. When we use Python for database query, we often need to do this:

cursor.execute('SELECT xxx FROM xxx WHERE xxx')
print(cursor.fetchone()[1])

Very inconvenient, you can use ORM to query like this:

xxx = XXX.objects.filter(xxx=xxx).first()
print(xxx.xxx)

So for OCM, what I want to achieve is to run a command like this:

ls = LsCommand(is_long=True, directory='.')
ls()

Installation

pip install python-ocm

Philosophy

Like click, OCM abstracts command-line parameters into two types:

  1. Option, can be specified by -l, --long, -o foo.txt, etc.
  2. Argument, can only pass the value directly, no key

For example: in ls -l /Users/dev:

  1. -l is Option (here there is no value after -l, it is called flag)
  2. /Users/dev is Argument

Usage

from ocm import Command, Option, Argument



class LS(Command):
    is_long = Option('-l', name='is_long', is_flag=True, required=False)
    directory = Argument(name='directory', required=True)

    class Meta:
        exe ='ls'


ls = LS(is_long=True, directory='/Users/dev')
print(ls.is_long)
print(ls.directory)
returncode, stdout = ls()
print(returncode)
print(stdout)

Parameter Type

OCM built-in parameter types:

  1. StringParamType
  2. IntegerParamType
  3. FloatParamType
  4. ChoicesParamType

Instructions:

from ocm import Command, Option, Argument, IntegerParamType


class Head(Command):
    number = Option('-n', name='number', param_type=IntegerParamType(), required=False)
    file = Argument(name='file')

    class Meta:
        exe ='head'


head = Head(number=10, file='ocm.py')
returncode, stdout = head()

Custom parameter type:

from ocm import ParamType


class MyParamType(ParamType):
    def convert(self, value, param, ctx):
        pass

    def show(self, value):
        pass

The convert method is responsible for converting the incoming data into the correct data:

  1. value, the incoming data
  2. param, the parameter object
  3. ctx, all other incoming parameters

The show method is responsible for converting the data into a string to run on the command line:

  1. value, the incoming data, generally the result of conversion by the convert method

Callback

The callback function can perform additional verification and conversion based on the data passed in by the user.

from ocm import Command, Option, Argument, IntegerParamType


def add_one(value, param, ctx):
    if value is None:
        return None
    return value + 1


class Head(Command):
    number = Option(
        '-n', name='number', param_type=IntegerParamType(), required=False, callback=add_one
    )
    file = Argument(name='file')

    class Meta:
        exe ='head'


head = Head(number=10, file='ocm.py')
returncode, stdout = head()

API

Option

  1. __init__(self, key, is_flag=False, default=None, param_type=None, required=None, callback=None, multiple=False)

parameter:

  1. key, the key of the Option when splicing into a command line, for example, -l

  2. name, the name of the parameter

  3. is_flag, whether this parameter is flag, see the description in the concept section, the default is False

  4. default, the default value of the parameter

  5. param_type, the type of the parameter, see the description of the parameter type

  6. required, is it necessary?

  7. callback, some parameters need to be converted according to the input value, you can customize the callback function, see the description of the callback function section

  8. multiple, whether this parameter will be passed in multiple, for example, ls -l /Users/dev /Users/dev/Downloads/, directory parameter is passed twice

  9. process_value(self, value, ctx)

Verify value according to the rules, and return the verified result:

  1. value, the value to be verified
  2. ctx, the value of other parameters passed in at the same time, used for the verification of the parameter

Argument

  1. __init__(self, is_flag=False, default=None, param_type=None, required=None, callback=None, multiple=False)

parameter:

  1. name, the name of the parameter

  2. default, the default value of the parameter

  3. param_type, the type of the parameter, see the description of the parameter type

  4. required, is it necessary?

  5. callback, some parameters need to be converted according to the input value, you can customize the callback function, see the description of the callback function section

  6. multiple, whether this parameter will be passed in multiple, for example, ls -l /Users/dev /Users/dev/Downloads/, directory parameter is passed twice

  7. process_value(self, value, ctx)

Verify value according to the rules, and return the verified result:

  1. value, the value to be verified
  2. ctx, the value of other parameters passed in at the same time, used for the verification of the parameter

Metadata

Release files for python-ocm 0.0.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for python-ocm 0.0.5
File Size Uploaded Provenance Actions
python-ocm-0.0.5.tar.gz 6.4 kB Without provenance Details for python-ocm-0.0.5.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for python-ocm 0.0.5
File Interpreter ABI Platform Actions
python_ocm-0.0.5-py3-none-any.whl Python 3 none any Details for python_ocm-0.0.5-py3-none-any.whl

Total release size: 12.9 kB

Release files / python-ocm-0.0.5.tar.gz

Download URL python-ocm-0.0.5.tar.gz
Size 6.4 kB
Tags Source
SHA-256 checksum
How to use checksums
7c91afb5bd81be6ac93268771f10e9d11543523e04f4e661e97d65a36627493b
BLAKE2b-256 checksum
How to use checksums
13d58b4f8416b708b4eb3a9655d1dcef18aa08bcb14e964fe8ee34c1a1ec4c10
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/4.4.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.7.10

Release files / python_ocm-0.0.5-py3-none-any.whl

Download URL python_ocm-0.0.5-py3-none-any.whl
Size 6.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1ecb3fa0874a340ce26e0265b8f50845423748f917d47ffa4b82c806e13d543f
BLAKE2b-256 checksum
How to use checksums
d829922648f41e3020539116a96eb71bcd458d54e7191227ccc6ff7bee1a7e0a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/4.4.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.7.10

Security for python-ocm 0.0.5

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page