Skip to main content

Generate architecture rules for Python projects.

Project description

Sourcery Rules Generator

This is an experimental project. It might become a part of the Sourcery CLI.

Sourcery Rules Generator creates architecture rules for your project.

The generated rules can be used by Sourcery to review your project's architecture.

Currently, the project can create dependency rules.

Usage

You can create Sourcery rules based on a template with the command:

sourcery-rules <TEMPLATE-NAME> create

Supported templates:

For example:

sourcery-rules dependencies create

gif sourcery-rules dependencies create

Create Dependencies Rules

With the dependencies template, you can create rules to check the dependencies:

  • between the packages of your application
  • to external packages.

Let's say your project has an architecture like this:

dependencies overview

You can create rules to ensure:

  • no other package imports api
  • only api imports core
  • only db import SQLAlchemy
  • etc.

Run the command:

sourcery-rules dependencies create

You'll be prompted to provide:

  • a package name
  • the packages that are allowed to import the package above

The 1st parameter is the fully qualified name of a package or module.
It can be a package within your project or an external dependency.

The 2nd parameter is optional.
You have the following possibilities:

  • 0 allowed importer (e.g. for packages like api, cli). Leave this parameter empty.
  • 1 allowed importer. Provide the importer package's fully qualified name.
  • Multiple allowed importers. Provide multiple fully qualified package names separated by a comma ,

=>

2 rules will be generated:

  • 1 for import statements
  • 1 for from ... import statements

Every generated rule allows imports:

  • within the package itself
  • in tests

Dependencies Use Cases

Internal Dependencies Between the Packages of a Project

  • Law of Demeter: Packages should talk only to their "direct neighbors".
  • A mature package shouldn't depend on a less mature package
  • A core package shouldn't depend on a customer-specific package

Thanks to w_t_payne and hbrn for their input in this HackerNews discussion 😃

External Dependencies

  • Gateway pattern: Ensure that only a dedicated package of your software communicates with an external dependency.
  • Ensure that a deprecated library isn't used

This blog post shows a 3-step method of defining dependency rules:

  1. Draw a diagram showing the optimal dependencies between your packages.
  2. Phrase some rules in a human language based on the diagram: Which package should depend on which?
  3. Translate the rules into code with Sourcery Rules Generator.

Create Voldemort Rules

With a "voldemort" template, you can create rules that ensure that a specific name isn't used in your code.

For example:

  • The word annual shouldn't be used, because the preferred term is yearly.
  • The word util shouldn't be used, because it's overly general.

You can create a "voldemort" rule with the command:

sourcery-rules voldemort create

screenshot sourcery-rules voldemort create

You'll be prompted to provide:

  • the name that you want to avoid

=>

5 rules will be generated:

  • function names
  • function arguments
  • class names
  • variable declarations
  • variable assignments

Expensive Loop

Loops often cause performance problems. Especially, if they execute expensive operations: talking to external systems, complex calculations.

sourcery-rules expensive-loop create

Using the Generated Rules

The generated rules can be used by Sourcery to review your project. If you copy the generated rules into your project's .sourcery.yaml, Sourcery will use them automatically.

All the generated rules have the tag architecture. Once you've copied them to your .sourcery.yaml, you can run them with:

sourcery review --enable architecture .

You'll be prompted to provide:

  • the fully qualified name of the function that shouldn't be called in loops

=>

2 rules will be generated:

  • for for loops
  • for while loops

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

sourcery_rules_generator-0.6.0.tar.gz (9.3 kB view details)

Uploaded Source

Built Distribution

sourcery_rules_generator-0.6.0-py3-none-any.whl (11.6 kB view details)

Uploaded Python 3

File details

Details for the file sourcery_rules_generator-0.6.0.tar.gz.

File metadata

  • Download URL: sourcery_rules_generator-0.6.0.tar.gz
  • Upload date:
  • Size: 9.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.2.2 CPython/3.11.0 Linux/5.19.0-31-generic

File hashes

Hashes for sourcery_rules_generator-0.6.0.tar.gz
Algorithm Hash digest
SHA256 66a54daa5b41570a1a37a6e98b65475405b3b656430296d21a5897ecda93da2e
MD5 ed2e0d3a40206f81c8519454b51ef079
BLAKE2b-256 02047ac636c21e54d50b7f478619b2fcfe5c0f90440468f41e610cc737e060e8

See more details on using hashes here.

File details

Details for the file sourcery_rules_generator-0.6.0-py3-none-any.whl.

File metadata

File hashes

Hashes for sourcery_rules_generator-0.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5a63cad75b6c58e9f444ac57898b499933d1d36b1448e210fc6ad391c50091c9
MD5 c962ba73e19a3a6d00103d1284241bc3
BLAKE2b-256 cb5ded3b75974eb01232f26cd7e1c53d0c672233bcb41bf0fee0ba8aff1aaade

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