# Path: autoexec/autoexec_rules_manager.py
# class AutoexecRulesManager:
# 
#     def get_all_rules(self):
#         result = []
# 
#         f = utils.config_file.get_field("autoexec")
#         if not f:
#             return result
# 
#         json_arr = json.loads(f)
# 
#         for rule in json_arr:
#             result.append(autoexec.autoexec.AutoexecRule.from_json(rule).__dict__)
# 
#         return result
# 
#     def add_rule(self, rule):
#         if self.get_by_id(rule.id) is not None:
#             self.replace(rule.id, rule)
#             return 0
# 
#         if type(rule) is not autoexec.autoexec.AutoexecRule:
#             raise TypeError("value must be instance if AutoexecRule")
# 
#         rules = self.get_all_rules()
#         rules.append(rule.__dict__)
#         _json = json.dumps(rules)
#         utils.config_file.set_field(AUTOEXEC, _json)
# 
#     def get_by_id(self, id):
#         rules = self.get_all_rules()
#         for rule in rules:
#             if rule['id'] == id:
#                 return rule
# 
#     def remove(self, id):
#         rule = self.get_by_id(id)
#         if rule is None:
#             raise Exception("No such rule")
#         rules = self.get_all_rules()
#         rules.remove(rule)
#         _json = json.dumps(rules)
#         utils.config_file.set_field(AUTOEXEC, _json)
# 
#     def replace(self, id, rule):
#         self.remove(id)
#         self.add_rule(rule)
# 
# Path: utils/time_utils.py
# def parse_time(time):
#     if type(time) == int:
#         return time
#     elif type(time) == str:
#         rtime = 0
#         r = re.findall("(\d+)s", time)
#         for o in r:
#             rtime += int(o)
# 
#         r = re.findall("(\d+)m", time)
#         for o in r:
#             rtime += int(o) * 60
# 
#         r = re.findall("(\d+)h", time)
#         for o in r:
#             rtime += int(o) * 3600
# 
#         r = re.findall("(\d+)d", time)
#         for o in r:
#             rtime += int(o) * 3600 * 24
#         return rtime
# 
# Path: autoexec/autoexec_action_type.py
# class AutoexecActionType():
#     ACTION_HC_CONSOLE = 1
#     ACTION_MESSAGE_SEND = 2
# 
# Path: autoexec/autoexec.py
# class AutoexecRule:
#     id = None
#     ticks = None
#     time_each = None
#     time_when = None
#     action = None
#     action_value = None
# 
#     def from_json(d):
#         s = AutoexecRule(force=True)
#         s.__dict__.update(d)
#         return s
# 
#     def __init__(self, time_each=None, time_when=None, action_type=None, force=False):
#         if not force:
# 
#             if not time_each:
#                 time_each = None
# 
#             if not time_when:
#                 time_when = None
# 
#             print("%s%s" % (time_each, time_when))
# 
#             if time_each == None and time_when == None:
#                 raise AttributeError("You must specify time when it action will be go")
# 
#             if time_each != None and time_when != None:
#                 raise AttributeError("You can't specify each and when parameters in the same time")
# 
#             if time_when is not None:
#                 self.time_when = time_utils.parse_time(time_when)
# 
#             if time_each is not None:
#                 self.time_each = time_utils.parse_time(time_each)
# 
#         pass


from optparse import OptionParser
from autoexec.autoexec_rules_manager import AutoexecRulesManager
from utils.time_utils import parse_time
from autoexec.autoexec_action_type import AutoexecActionType
from autoexec.autoexec import AutoexecRule
import bot_header
    parser.add_option("-A", "--action-value", dest="action_value", help="the value of action (USE /s instead of spaces) (in case of action type 'HC_CONSOLE' it's command to execute in console)")
    parser.add_option("-#", "--id", dest="id", help="use specific id (not required because it will be generated automatically if not defined)")
    parser.add_option("-?", "--?", default=None, action="callback", callback=help, help="show this help message")

    o, a = parser.parse_args(c.split())
    o = o.__dict__
    type = o['type']

    if str.isnumeric(o['time']):
        time = int(o['time'])
    else:
        time = parse_time(o['time'])

    a_type = get_action_type(o['action'])
    a_type_val = o['action_value']
    id = o['id']

    a = AutoexecRule(force=True)
    if type == 'each':
        a.time_each = time
    elif type == 'when':
        a.time_when = time
    else:
        raise Exception("Invalid rule type (-t/--type=) each|when")

    if a_type is not None:
        a.action = a_type
    else:
        raise Exception("Invalid action type (-a/--action-type) HC_CONSOLE")


# Path: autoexec/autoexec_rules_manager.py
# class AutoexecRulesManager:
# 
#     def get_all_rules(self):
#         result = []
# 
#         f = utils.config_file.get_field("autoexec")
#         if not f:
#             return result
# 
#         json_arr = json.loads(f)
# 
#         for rule in json_arr:
#             result.append(autoexec.autoexec.AutoexecRule.from_json(rule).__dict__)
# 
#         return result
# 
#     def add_rule(self, rule):
#         if self.get_by_id(rule.id) is not None:
#             self.replace(rule.id, rule)
#             return 0
# 
#         if type(rule) is not autoexec.autoexec.AutoexecRule:
#             raise TypeError("value must be instance if AutoexecRule")
# 
#         rules = self.get_all_rules()
#         rules.append(rule.__dict__)
#         _json = json.dumps(rules)
#         utils.config_file.set_field(AUTOEXEC, _json)
# 
#     def get_by_id(self, id):
#         rules = self.get_all_rules()
#         for rule in rules:
#             if rule['id'] == id:
#                 return rule
# 
#     def remove(self, id):
#         rule = self.get_by_id(id)
#         if rule is None:
#             raise Exception("No such rule")
#         rules = self.get_all_rules()
#         rules.remove(rule)
#         _json = json.dumps(rules)
#         utils.config_file.set_field(AUTOEXEC, _json)
# 
#     def replace(self, id, rule):
#         self.remove(id)
#         self.add_rule(rule)
# 
# Path: utils/time_utils.py
# def parse_time(time):
#     if type(time) == int:
#         return time
#     elif type(time) == str:
#         rtime = 0
#         r = re.findall("(\d+)s", time)
#         for o in r:
#             rtime += int(o)
# 
#         r = re.findall("(\d+)m", time)
#         for o in r:
#             rtime += int(o) * 60
# 
#         r = re.findall("(\d+)h", time)
#         for o in r:
#             rtime += int(o) * 3600
# 
#         r = re.findall("(\d+)d", time)
#         for o in r:
#             rtime += int(o) * 3600 * 24
#         return rtime
# 
# Path: autoexec/autoexec_action_type.py
# class AutoexecActionType():
#     ACTION_HC_CONSOLE = 1
#     ACTION_MESSAGE_SEND = 2
# 
# Path: autoexec/autoexec.py
# class AutoexecRule:
#     id = None
#     ticks = None
#     time_each = None
#     time_when = None
#     action = None
#     action_value = None
# 
#     def from_json(d):
#         s = AutoexecRule(force=True)
#         s.__dict__.update(d)
#         return s
# 
#     def __init__(self, time_each=None, time_when=None, action_type=None, force=False):
#         if not force:
# 
#             if not time_each:
#                 time_each = None
# 
#             if not time_when:
#                 time_when = None
# 
#             print("%s%s" % (time_each, time_when))
# 
#             if time_each == None and time_when == None:
#                 raise AttributeError("You must specify time when it action will be go")
# 
#             if time_each != None and time_when != None:
#                 raise AttributeError("You can't specify each and when parameters in the same time")
# 
#             if time_when is not None:
#                 self.time_when = time_utils.parse_time(time_when)
# 
#             if time_each is not None:
#                 self.time_each = time_utils.parse_time(time_each)
# 
#         pass


# Path: console/aexec/new.py
from optparse import OptionParser
from autoexec.autoexec_rules_manager import AutoexecRulesManager
from utils.time_utils import parse_time
from autoexec.autoexec_action_type import AutoexecActionType
from autoexec.autoexec import AutoexecRule
import bot_header

    parser.add_option("-A", "--action-value", dest="action_value", help="the value of action (USE /s instead of spaces) (in case of action type 'HC_CONSOLE' it's command to execute in console)")
    parser.add_option("-#", "--id", dest="id", help="use specific id (not required because it will be generated automatically if not defined)")
    parser.add_option("-?", "--?", default=None, action="callback", callback=help, help="show this help message")

    o, a = parser.parse_args(c.split())
    o = o.__dict__
    type = o['type']

    if str.isnumeric(o['time']):
        time = int(o['time'])
    else:
        time = parse_time(o['time'])

    a_type = get_action_type(o['action'])
    a_type_val = o['action_value']
    id = o['id']

    a = AutoexecRule(force=True)
    if type == 'each':
        a.time_each = time
    elif type == 'when':
        a.time_when = time
    else:
        raise Exception("Invalid rule type (-t/--type=) each|when")

    if a_type is not None:
        a.action = a_type
    else:
        raise Exception("Invalid action type (-a/--action-type) HC_CONSOLE")


    mgr = AutoexecRulesManager()