Make converters dynamically loadable in autoimport

Adds 1 restriction to the HHC sub classes, the classname must be the
same as the filename - ToFpdb.py

FulltiltToFpdb.py must contain a hhc class named Fulltilt
EverleafToFpdb.py must contain a hhc class named Everleaf
This commit is contained in:
Worros 2009-03-14 05:00:12 +09:00
parent 875f218379
commit 6ebab06981
4 changed files with 19 additions and 48 deletions

View File

@ -43,6 +43,7 @@ out_path (default '-' = sys.stdout)
follow : whether to tail -f the input follow : whether to tail -f the input
autostart: whether to run the thread (or you can call start() yourself) autostart: whether to run the thread (or you can call start() yourself)
debugging: if False, pass on partially supported game types. If true, have a go and error...""" debugging: if False, pass on partially supported game types. If true, have a go and error..."""
print "DEBUG: XXXXXXXXXXXXXXX"
HandHistoryConverter.__init__(self, in_path, out_path, sitename="Everleaf", follow=follow) HandHistoryConverter.__init__(self, in_path, out_path, sitename="Everleaf", follow=follow)
logging.info("Initialising Everleaf converter class") logging.info("Initialising Everleaf converter class")
self.filetype = "text" self.filetype = "text"

View File

@ -22,9 +22,9 @@ import sys
import logging import logging
from HandHistoryConverter import * from HandHistoryConverter import *
# FullTilt HH Format converter # Fulltilt HH Format converter
class FullTilt(HandHistoryConverter): class Fulltilt(HandHistoryConverter):
# Static regexes # Static regexes
re_GameInfo = re.compile('- (?P<CURRENCY>\$|)?(?P<SB>[.0-9]+)/\$?(?P<BB>[.0-9]+) (Ante \$(?P<ANTE>[.0-9]+) )?- (?P<LIMIT>(No Limit|Pot Limit|Limit))? (?P<GAME>(Hold\'em|Omaha Hi|Razz))') re_GameInfo = re.compile('- (?P<CURRENCY>\$|)?(?P<SB>[.0-9]+)/\$?(?P<BB>[.0-9]+) (Ante \$(?P<ANTE>[.0-9]+) )?- (?P<LIMIT>(No Limit|Pot Limit|Limit))? (?P<GAME>(Hold\'em|Omaha Hi|Razz))')
@ -39,8 +39,8 @@ class FullTilt(HandHistoryConverter):
in_path (default '-' = sys.stdin) in_path (default '-' = sys.stdin)
out_path (default '-' = sys.stdout) out_path (default '-' = sys.stdout)
follow : whether to tail -f the input""" follow : whether to tail -f the input"""
HandHistoryConverter.__init__(self, in_path, out_path, sitename="FullTilt", follow=follow) HandHistoryConverter.__init__(self, in_path, out_path, sitename="Fulltilt", follow=follow)
logging.info("Initialising FullTilt converter class") logging.info("Initialising Fulltilt converter class")
self.filetype = "text" self.filetype = "text"
self.codepage = "cp1252" self.codepage = "cp1252"
if autostart: if autostart:
@ -314,4 +314,4 @@ if __name__ == "__main__":
LOG_FILENAME = './logging.out' LOG_FILENAME = './logging.out'
logging.basicConfig(filename=LOG_FILENAME,level=options.verbosity) logging.basicConfig(filename=LOG_FILENAME,level=options.verbosity)
e = FullTilt(in_path = options.ipath, out_path = options.opath, follow = options.follow) e = Fulltilt(in_path = options.ipath, out_path = options.opath, follow = options.follow)

View File

@ -1,29 +0,0 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright 2009, Carl Gherardi
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
########################################################################
def importName(module_name, name):
"""Import a named object 'name' from module 'module_name'."""
# Recipe 16.3 in the Python Cookbook, 2nd ed. Thanks!!!!
try:
module = __import__(module_name, globals(), locals(), [name])
except:
return None
return(getattr(module, name))

View File

@ -33,8 +33,6 @@ import fpdb_simple
import fpdb_db import fpdb_db
import fpdb_parse_logic import fpdb_parse_logic
import Configuration import Configuration
import EverleafToFpdb
import FulltiltToFpdb
# database interface modules # database interface modules
try: try:
@ -238,21 +236,22 @@ class Importer:
except: except:
out_path = os.path.join(hhdir, "x"+strftime("%d-%m-%y")+os.path.basename(file)) out_path = os.path.join(hhdir, "x"+strftime("%d-%m-%y")+os.path.basename(file))
# someone can just create their own python module for it filter_name = filter.replace("ToFpdb", "")
if filter in ("EverleafToFpdb","Everleaf"):
conv = EverleafToFpdb.Everleaf(in_path = file, out_path = out_path) mod = __import__(filter)
elif filter == "FulltiltToFpdb": obj = getattr(mod, filter_name, None)
conv = FulltiltToFpdb.FullTilt(in_path = file, out_path = out_path) if callable(obj):
conv = obj(in_path = file, out_path = out_path)
if(conv.getStatus()):
(stored, duplicates, partial, errors, ttime) = self.import_fpdb_file(out_path, site)
else:
# conversion didn't work
# TODO: appropriate response?
return (0, 0, 0, 1, 0)
else: else:
print "Unknown filter ", filter print "Unknown filter filter_name:'%s' in filter:'%s'" %(filter_name, filter)
return return
if(conv.getStatus()):
(stored, duplicates, partial, errors, ttime) = self.import_fpdb_file(out_path, site)
else:
# conversion didn't work
# TODO: appropriate response?
return (0, 0, 0, 1, 0)
#This will barf if conv.getStatus != True #This will barf if conv.getStatus != True
return (stored, duplicates, partial, errors, ttime) return (stored, duplicates, partial, errors, ttime)