Exemplo n.º 1
0
def load_machine_from_source(machine):
    """ loads a machine object from the given source
    """
    # We have to generate a new name for each machine
    module_id = uuid.uuid1()
    try:
        loaded = imp.new_module("machine%s" % module_id)
        exec machine in loaded.__dict__
    except Exception as e:
        raise MachinehubException("Unable to load machine %s" % e.message)
    try:
        result = check_machine(loaded)
        return result
    except MachinehubException as e:  # re-raise with file name
        raise MachinehubException("machine: %s" % str(e))
Exemplo n.º 2
0
 def __init__(self, text):
     self.doc = None
     self.inputs = None
     doc_lines = []
     inputs_lines = []
     pattern = re.compile("^\[([a-z_]{2,50})\]")
     current_lines = []
     for line in text.splitlines():
         line = line.strip()
         if not line or line[0] == '#':
             continue
         m = pattern.match(line)
         if m:
             group = m.group(1)
             current_lines = []
             if group == 'doc':
                 doc_lines = current_lines
             elif group == 'inputs':
                 inputs_lines = current_lines
             else:
                 raise MachinehubException("MachineParser: Unrecognized field '%s'" % group)
         else:
             current_lines.append(line)
     self.doc = DocMachine(doc_lines)
     self.inputs = InputsMachine(inputs_lines).inputs
Exemplo n.º 3
0
 def __getattr__(self, name):
     if name in self._sections:
         return ConfigSection(name, "\n".join(self._sections[name]))
     else:
         if self._allowed_fields and name in self._allowed_fields:
             return ""
         else:
             raise MachinehubException(
                 "ConfigParser: Unrecognized field '%s'" % name)
Exemplo n.º 4
0
 def __init__(self, name, text):
     self.name = name
     self._sections = {}
     for line in text.splitlines():
         key, value = line.split(':', 1)
         value = value.replace(' ', '') if value.startswith(' ') else value
         if self._allowed_fields and key not in self._allowed_fields:
             raise MachinehubException(
                 "%s config: Unrecognized field '%s'" % (self.name, key))
         self._sections[key] = value
Exemplo n.º 5
0
def check_machine(machine_file):
    """ Check the integrity of a given machine
    """
    try:
        a = machine_file.__dict__['machinebuilder']
        if inspect.isfunction(a) and a.__doc__:
            machine_info = MachineParser(a.__doc__)
            return a, machine_info.doc, machine_info.inputs
        else:
            raise MachinehubException('machinebuilder must be a funcion with docstring')
    except Exception as e:
        logger.info(e.message)
        raise NotMachineHub()
Exemplo n.º 6
0
def load_machine(machine_path):
    """ loads a machine from the given file
    """
    # Check if precompiled exist, delete it
    if os.path.exists(machine_path + "c"):
        os.unlink(machine_path + "c")

    if not os.path.exists(machine_path):
        raise MachinehubException("%s not found!" % machine_path)

    # We have to generate a new name for each machine
    module_id = uuid.uuid1()
    try:
        loaded = imp.load_source("machine%s" % module_id, machine_path)
    except Exception:
        import traceback
        trace = traceback.format_exc().split('\n')
        raise MachinehubException("Unable to load machine in %s\n%s" % (machine_path,
                                                                        '\n'.join(trace[3:])))
    try:
        result = check_machine(loaded)
        return result
    except MachinehubException as e:  # re-raise with file name
        raise MachinehubException("%s: %s" % (machine_path, str(e)))
Exemplo n.º 7
0
def save(resource, dest, extensions=None, pattern_extensions=None):
    if not resource:
        return None
    elif not (extensions and pattern_extensions) and (extensions or pattern_extensions):
        allowed = False
        if extensions:
            allowed = allowed_file(resource.filename, extensions)
        elif pattern_extensions:
            allowed = pattern_allowed_file(resource.filename, pattern_extensions)
        if allowed:
            filename = secure_filename(resource.filename)
            file_path = os.path.join(dest, filename)
            resource.save(file_path)
            return file_path
    else:
        raise MachinehubException('Define only one parameter "extensions" or "pattern_extensions"')
Exemplo n.º 8
0
 def __init__(self, text, allowed_fields=None):
     self._sections = {}
     self._allowed_fields = allowed_fields or []
     pattern = re.compile("^\[([a-z_]{2,50})\]")
     current_lines = []
     for line in text.splitlines():
         line = line.strip()
         if not line or line[0] == '#':
             continue
         m = pattern.match(line)
         if m:
             group = m.group(1)
             if self._allowed_fields and group not in self._allowed_fields:
                 raise MachinehubException(
                     "ConfigParser: Unrecognized field '%s'" % group)
             current_lines = []
             self._sections[group] = current_lines
         else:
             current_lines.append(line)
Exemplo n.º 9
0
 def __init__(self, lines):
     self.images = []
     title = []
     description = []
     pattern = re.compile("^\-([a-z_]{2,50})\-")
     for line in lines:
         m = pattern.match(line)
         if m:
             group = m.group(1)
             current_lines = []
             if group == 'title':
                 title = current_lines
             elif group == 'description':
                 description = current_lines
             elif group == 'images_url':
                 self.images = current_lines
             else:
                 raise MachinehubException("SectionParser: Unrecognized field '%s'" % group)
         else:
             current_lines.append(line)
     self.title = '\n'.join(title)
     self.description = '\n'.join(description)