Skip to content

internetimagery/pyhike

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

36 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

An adventure through code

Pyhike is an interface for tools that wish to walk live objects for inspection.

Yep. Pretty simple!

This is still wip, as it is being developed alongside another tool making use of it.

Installation

Install via pip. Python 2 and 3 compatible

python -m pip install -U pyhike

Usage

There are two parts to the API.

  • Chart: Visitor interface that you will subclass and provide custom logic on top of
  • TrailBlazer: Traversal class that will act on the visitor
from pyhike import Chart, TrailBlazer

class MyVisitor(Chart):

    def visit_method(self, name, method, class_, trailblazer):
        print("A METHOD!", name, method)

TrailBlazer(MyVisitor()).roam_file("/path/to/somefile.py").hike()
# A METHOD somefile:SomeClass.some_method <some_method at 0x123123123>

Create a visitor (or multiple layers of subclass if it makes design sense) for functionality.

Create a traversal object, and add things to traverse (roam_file, roam_class, roam_directory, etc).

Trigger the traversal (hike).

TrailBlazer

Contains the following methods to add things for traversal. They are all chainable. They can also be used during live traversal to dynamically search new locations (see the visitor methods below).

  • roam_directory(filepath) : Walk directory looking for modules. This does not recurse into non-module directories.
  • roam_file(filepath) : import and traverse a file, given the filepath
  • roam_module(module) : search an already imported module object
  • roam_class(class_) : search an already loaded class object

Finally the hike() method kicks off the traversal, and off we go. Adventure is out there!

Chart

Subclass this for your own adventure! This object will have its methods called for each object type visited along the way.

The methods all recieve an instance of the active traversal object so they can dynamically add more things to check if needed.

Returning True from a method will stop further traversal along that path at that point (eg return true visiting a class will not traverse its methods).

  • visit_directory(name, directorypath, traveler)
  • visit_file(name, filepath, traveler)
  • visit_module(name, module, traveler)
  • visit_class(name, class_, traveler)
  • visit_function(name, func, parent, traveler)
  • visit_method(name, func, class_, traveler)
  • visit_classmethod(name, func, class_, traveler)
  • visit_staticmethod(name, func, class_, traveler)
  • visit_property(name, func, class_, traveler)
  • visit_attribute(name, value, parent, traveler)

There are a few special methods as well for consideration, that may make life a little easier.

  • enter(name) Run before traversing into anything, with the name of the thing it's about to traverse into.
  • leave(name) Run after finishing the traversal. Run with the same name as it was entered.
  • error(errType, errVal, errTrace) Run anytime an error occurrs during traversal. Return True to allow the error to propigate up halting traversal.

Notes

There is no attempt at resolving circular code traversal. This is left up to the user of the visitor to provide. This is to provide more flexability on behalf of the visitor logic. Remember that visitor methods can halt further traversal by returning True.

The traversal happens mostly from the bottom up. It visits first come first serve on a priority basis. So things are visited in this order:

  • Directories
  • Files
  • Modules
  • Classes
  • Functions / Methods
  • Attributes

The name provided to the visitor methods is generated from the path taken to reach the object. eg some_package.some_module:SomeClass.some_method. This means that it does not reflect the name of the objects definition. But instead an import path that would lead you there.

The name splits modules and qualified names by colon.

Errors that occurr during traversal are suppressed, and forwarded to the error visitation method. To allow them to propigate normally, and halt everything return True in the error method.

About

Live code structure traversal

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages