Welcome to Finchan’s documentation!

finchan

https://img.shields.io/pypi/v/finchan.svg https://img.shields.io/travis/qytz/finchan.svg Documentation Status Updates

Finchan event process framework with python.

Features

Do whatever you want with it.

  • Event-driven
  • Support live trace and backtrack test
  • Easy extensionable
  • Support Python 3.6+
  • Does not support Python 2.x

Installation

Stable release

To install finchan, run this command in your terminal:

$ pip install finchan

This is the preferred method to install finchan, as it will always install the most recent stable release.

If you don’t have pip installed, this Python installation guide can guide you through the process.

From sources

The sources for finchan can be downloaded from the Github repo.

You can either clone the public repository:

$ git clone git://github.com/qytz/finchan

Or download the tarball:

$ curl  -OL https://github.com/qytz/finchan/tarball/master

Once you have a copy of the source, you can install it with:

$ python setup.py install

Usage

steps
  • optional, write your own EventSource extension.
  • write your own event subscriber extension.
  • configure the running with configure file.
  • run finchan with finchan -c finchan.yml -vvvvv

The example EventSource extension and Subscriber extension can be found in finchan/exts/ and finchan_exts.

system architecture

Finchan is a event system framework, its main modules are Event, EventSource, Dispatcher, Subscribers, env and ExtManager.

concept

The Event is the input manner for finchan.

The EventSource is the original input for the system, it simulate the real world’s things to Event as input.

The Dispatcher is the engine of the system, it manage all EventSource, all event Subscriber, and dispath the events generated by EventSource or Subscriber, call the Subscriber’s callback.

The Subscriber is event subscriber, it subscriber event with a callback function, when the event occurs, the Dispatcher dispatch the event and call the callback function. The Subscriber can do anything with the callback function, include generate new events.

The env is global environment for the system, you can access it just by from finchan.env import env, you can access environment variable setted by other modules, or set your own environment variable.

the ExtManager manage all extensions for finchan.

Interfaces

kvstore

kvstore is persistent storage for finchan, finchan framework just specified the interface for kvstore, the implemention is done by extensions.

EventSource

The EventSource defines the interface to implent an EventSource, finchan system include a TimerSource, if you need other EventSource, you should write an extension for finchan, or check finchan_exts first.

Write an extension for finchan

refer Extend finchan.

APIS

finchan.event

Event implentation

Event

class finchan.event.Event(env, name, dt=None, expire=0, event_id=None, **kwargs)[source]

Event class

Parameters:
  • name – name of the event
  • dt – occur datetime
  • expire – expire in second
  • event_id – id of the event
  • **kwargs – special kwargs for the event, can get by kwargs attribute

can compare less/greater than with other event object by time, equal by event id.

id

ID of the event

name

Name of the event

timestamp

Occur time of the event, in POSIX timestamp format

expire

expire expire in seconds after event occur time (timestamp attribute), 0 is no expire.

kwargs

Parameters of the event, a dict

SysEvents:

class finchan.event.SysEvents[source]

system defined events

enum of events that finchan system generate/reserved:

  • SYSTEM_STARTED = ‘system_event.system_started’
  • SYSTEM_EXITING = ‘system_event.system_will_exit’

finchan.interface.event_source

finchan.interface.kvstore

kvstore Interface, inspired by redis-py.

AbsKvStore

class finchan.interface.kvstore.AbsKvStore(*args, **kwargs)[source]

KV store interface

init the kvstore connection

set(name, value)[source]

Set the value at key name to value.

get(name)[source]

Return the value at key name, or None if the key doesn’t exist.

mset(*args, **kwargs)[source]

Sets key/values based on a mapping. Mapping can be supplied as a single dictionary argument or as kwargs.

mget(keys, *args)[source]

Returns a list of values ordered identically to keys.

setnx(name, value)[source]

Set the value of key name to value if key doesn’t exist.

delete(name)[source]

Delete the key and its value.

hset(name, key, value)[source]

Set key to value within hash name.

Returns 1 if HSET created a new field, otherwise 0.

hget(name, key)[source]

Return the value of key within the hash name.

hmset(name, mapping)[source]

Set key to value within hash name for each corresponding key and value from the mapping dict.

hmget(name, keys, *args)[source]

Returns a list of values ordered identically to keys.

hgetall(name)[source]

Return a Python dict of the hash’s name/value pairs.

hsetnx(name, key, value)[source]

Set key to value within hash name if key does not exist.

Returns 1 if HSETNX created a field, otherwise 0.

hdel(name, *keys)[source]

Delete keys from hash name.

hexists(name, key)[source]

Returns a boolean indicating if key exists within hash name.

hkeys(name)[source]

Return the list of keys within hash name.

hvals(name)[source]

Return the list of values within hash name.

hlen(name)[source]

Return the number of elements in hash name.

lpop(name)[source]

Remove and return the first item of the list name.

lpush(name, value)[source]

Push values onto the head of the list name.

rpop(name)[source]

Remove and return the last item of the list name.

rpush(name, *value)[source]

Push values onto the tail of the list name.

lset(name, index, value)[source]

Set position of list name to value.

lrange(name, start, end)[source]

Return a slice of the list name between position start and end.

start and end can be negative numbers just like Python slicing notation.

ltrim(name, start, end)[source]

Trim the list name, removing all values not within the slice between start and end.

start and end can be negative numbers just like Python slicing notation.

llen(name)[source]

Return the length of the list name

finchan.env

runtime Environment

the env object is a global singleton object, other modules/extensions can access by just import it and can access/set attributes to env .

Env

class finchan.env.Env(*args, **kwargs)[source]

Global environment

now

current datetime, wraps for dispatcher.now

log is not permitted

Return type:datetime
dispatcher

the dispatcher manager object

Return type:Dispatcher
ext_manager

the extension manager object

Return type:ExtManager
set_dispatcher(dispatcher)[source]

set dispatcher object

Return type:bool
set_ext_manager(ext_manager)[source]

set extension manager object

Return type:bool
get_ext_options(ext_name)[source]

get options for extension named ext in configure file

Return type:Dict[~KT, ~VT]
run()[source]

wraps dispatcher’s run

load_exts(*exts)[source]

load all the extensions in *exts list

Return type:None

env

A global Env object.

finchan.dispatcher

Event Dispatcher

get events from event source and dispatch evnets to the subscriber.

Dispatcher

BackTrackDispatcher

class finchan.dispatcher.BackTrackDispatcher(env)[source]

Dispatcher that dispatch backtrack events. the event sources generate events and put them into event queue, the main thread get events from event queue and dispatch them.

Parameters:env – global envirument
now

datetime of current logic time

run()[source]

dispatch the event loop to run.

LiveDispatcher

finchan.exts

Extension manager

class finchan.exts.ExtManager(env, ext_paths='', **kwargs)[source]

Extension manager for finchan.

An finchan extension is an importable Python module that has a function with the signature:

def load_finchan_ext(env):
    # Do setup

This function is called after your extension is imported. *args and **kwargs is passed from configure file’s config.live_track_exts or config.backtrack_exts ‘s extension module name section depend on the run mode.

You can also optionally define an unload_finchan_ext() function, which will be called if the user unloads the extension.

You can put your extension modules anywhere you want, as long as they can be imported by Python’s standard import mechanism. However, to make it easy to write extensions, you can also put your extensions in a configured path config.ext_paths. This directory is added to sys.path automatically.

load_exts(exts)[source]

load exts for finchan:

Parameters:exts – exts dict, key is extension name, value is extension kwargs
cleanup()[source]

Cleanup all extensions

setup()[source]

Initialize all extensions

finchan.exts.timer_source

Extend finchan

An finchan extension is an importable Python module that has a function with the signature:

def load_finchan_ext(*args, **kwargs):
    # Do setup

This function is called after your extension is imported. *args and **kwargs is passed from configure file’s config.live_track_exts or config.backtrack_exts ‘s extension module name section depend on the run mode.

You can also optionally define an unload_finchan_ext() function, which will be called if the user unloads the extension.

You can put your extension modules anywhere you want, as long as they can be imported by Python’s standard import mechanism. However, to make it easy to write extensions, you can also put your extensions in a configured path config.ext_path. This directory is added to sys.path automatically.

the finchan has two built-in extension module, finchan.exts.timer_source and finchan.exts.timer_callback for test

Contributing

Contributions are welcome, and they are greatly appreciated! Every little bit helps, and credit will always be given.

You can contribute in many ways:

Types of Contributions

Report Bugs

Report bugs at https://github.com/qytz/finchan/issues.

If you are reporting a bug, please include:

  • Your operating system name and version.
  • Any details about your local setup that might be helpful in troubleshooting.
  • Detailed steps to reproduce the bug.

Fix Bugs

Look through the GitHub issues for bugs. Anything tagged with “bug” and “help wanted” is open to whoever wants to implement it.

Implement Features

Look through the GitHub issues for features. Anything tagged with “enhancement” and “help wanted” is open to whoever wants to implement it.

Write Documentation

finchan could always use more documentation, whether as part of the official finchan docs, in docstrings, or even on the web in blog posts, articles, and such.

Submit Feedback

The best way to send feedback is to file an issue at https://github.com/qytz/finchan/issues.

If you are proposing a feature:

  • Explain in detail how it would work.
  • Keep the scope as narrow as possible, to make it easier to implement.
  • Remember that this is a volunteer-driven project, and that contributions are welcome :)

Get Started!

Ready to contribute? Here’s how to set up finchan for local development.

  1. Fork the finchan repo on GitHub.

  2. Clone your fork locally:

    $ git clone git@github.com:your_name_here/finchan.git
    
  3. Install your local copy into a virtualenv. Assuming you have virtualenvwrapper installed, this is how you set up your fork for local development:

    $ mkvirtualenv finchan
    $ cd finchan/
    $ python setup.py develop
    
  4. Create a branch for local development:

    $ git checkout -b name-of-your-bugfix-or-feature
    

    Now you can make your changes locally.

  5. When you’re done making changes, check that your changes pass flake8 and the tests, including testing other Python versions with tox:

    $ flake8 finchan tests
    $ python setup.py test or py.test
    $ tox
    

    To get flake8 and tox, just pip install them into your virtualenv.

  6. Commit your changes and push your branch to GitHub:

    $ git add .
    $ git commit -m "Your detailed description of your changes."
    $ git push origin name-of-your-bugfix-or-feature
    
  7. Submit a pull request through the GitHub website.

Pull Request Guidelines

Before you submit a pull request, check that it meets these guidelines:

  1. The pull request should include tests.
  2. If the pull request adds functionality, the docs should be updated. Put your new functionality into a function with a docstring, and add the feature to the list in README.rst.
  3. The pull request should work for Python 2.6, 2.7, 3.3, 3.4 and 3.5, and for PyPy. Check https://travis-ci.org/qytz/finchan/pull_requests and make sure that the tests pass for all supported Python versions.

Tips

To run a subset of tests:

$ python -m unittest tests.test_finchan

AUTHORS

Finchan is written and maintained by qytz and various contributors:

Development Lead

Patches and Suggestions

None yet. Why not be the first?

History

0.2.0 (2018-11-15)

  • optimized exit check mechanism, can exit quickly now.
  • exts load interface has no parameters now.
  • support multiple splited configure files.
  • support extension groups.

0.1.3 (2018-10-27)

  • remove global env object, you can reference by event.env.
  • fix finchan.exts.timer_souce schedule format for month/year/week.

0.1.2 (2018-10-01)

  • Fix pypi package.
  • Support docker run.
  • Add SYSTEM_EXITING event.

0.1.1 (2018-09-22)

  • First release on PyPI.

Indices and tables