Skip to content

Commit

Permalink
Add Import Tolerance (#216)
Browse files Browse the repository at this point in the history
* modified init files to ensure that a failing module will not crash the entire system

*  dynamic imports of ccr rules added

*  dynamic imports for filters as well

*  dynamic imports for rules
  • Loading branch information
BazookaMusic authored and Versatilus committed Apr 1, 2018
1 parent 162e7c8 commit f23f8a2
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 19 deletions.
21 changes: 20 additions & 1 deletion caster/apps/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
import glob
import inspect
import os


def is_valid(module):
''' This function attempts to import the applications in order to detect
errors in their implementation . After they are imported, they are garbage collected
when the function returns.'''
try:
_ = __import__(module, globals(), locals())
return True
except Exception as e:
print(
"Ignoring application '{}'. Failed to load with: ".format(module))
print(e)
return False


modules = glob.glob(os.path.dirname(__file__) + "/*.py")
__all__ = [os.path.basename(f)[:-3] for f in modules if not f.endswith('__init__.py')]
# only valid applications will be added to the list
__all__ = [
os.path.basename(f)[:-3] for f in modules if (not f.endswith('__init__.py') and is_valid(os.path.basename(f)[:-3]))
]
48 changes: 32 additions & 16 deletions caster/lib/ccr/__init__.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,32 @@
from bash.bash import Bash
from core.alphabet import Alphabet
from core.nav import Navigation
from core.numbers import Numbers
from core.punctuation import Punctuation
from cpp.cpp import CPP
from csharp.csharp import CSharp
from haxe.haxe import Haxe
from html.html import HTML
from java.java import Java
from javascript.javascript import Javascript
from python.python import Python
from rust.rust import Rust
from sql.sql import SQL
from vhdl.vhdl import VHDL
from prolog.prolog import Prolog
# new modules should be added in the dictionary
# the tupple contains the rules which will be imported

command_sets = {
"bash.bash": ("Bash", ),
"core.alphabet": ("Alphabet", ),
"core.nav": ("Navigation", ),
"core.numbers": ("Numbers", ),
"core.punctuation": ("Punctuation", ),
"cpp.cpp": ("CPP", ),
"csharp.csharp": ("CSharp", ),
"haxe.haxe": ("Haxe", ),
"html.html": ("HTML", ),
"java.java": ("Java", ),
"javascript.javascript": ("Javascript", ),
"python.python": ("Python", ),
"rust.rust": ("Rust", ),
"sql.sql": ("SQL", ),
"prolog.prolog": ("Prolog", ),
"vhdl.vhdl": ("VHDL", ),
}


for module_name,class_name_tup in command_sets.iteritems():
for class_name in class_name_tup:
try:
module = __import__(module_name, globals(), locals(),[class_name]) #attempts to import the class
globals()[class_name]= module #make the name available globally

except Exception as e:
print("Ignoring ccr rule '{}'. Failed to load with: ".format(class_name))
print(e)
21 changes: 20 additions & 1 deletion caster/user/filters/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
import glob
import inspect
import os


def is_valid(module):
''' This function attempts to import the filters in order to detect
errors in their implementation . After they are imported, they are garbage collected
when the function returns.'''
try:
_ = __import__(module, globals(), locals())
return True
except Exception as e:
print("Ignoring filter '{}'. Failed to load with: ".format(module))
print(e)
return False


modules = glob.glob(os.path.dirname(__file__) + "/*.py")
__all__ = [os.path.basename(f)[:-3] for f in modules if not f.endswith('__init__.py')]
# only valid filters will be added to the list
__all__ = [
os.path.basename(f)[:-3] for f in modules
if (not f.endswith('__init__.py') and is_valid(os.path.basename(f)[:-3]))
]
22 changes: 21 additions & 1 deletion caster/user/rules/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
import glob
import inspect
import os


def is_valid(module):
''' This function attempts to import the rules in order to detect
errors in their implementation . After they are imported, they are garbage collected
when the function returns.'''
try:
_ = __import__(module, globals(), locals())
return True
except Exception as e:
print("Ignoring application '{}'. Failed to load with: ".format(module))
print(e)
return False


modules = glob.glob(os.path.dirname(__file__) + "/*.py")
__all__ = [os.path.basename(f)[:-3] for f in modules if not f.endswith('__init__.py')]
# only valid rules will be added to the list
__all__ = [
os.path.basename(f)[:-3]
for f in modules
if (not f.endswith('__init__.py') and is_valid(os.path.basename(f)[:-3]))
]

0 comments on commit f23f8a2

Please sign in to comment.