Ejemplo n.º 1
0
    def __init__(self, input_file, **kwargs):

        # Determine the format
        self._format = kwargs.pop('format', 'remark')

        self._reveal_dir = None
        if self._format == 'reveal':
            self._reveal_dir = os.getenv(
                'REVEAL_JS_DIR',
                os.path.join(os.getenv('HOME'), 'projects', 'reveal.js'))

            if not os.path.exists(self._reveal_dir):
                print 'ERROR: Attempted to output in Reveal.js format, but Reveal.js directory was not found, set the REAVEL_JS_DIR enviornmental variable or clone the repository into your ~/projects directory.'
                sys.exit()

        # Create the Factory and Warehouse
        self.factory = Factory()
        self.warehouse = SlideSetWarehouse(format=self._format)

        # Set the location of PresentationBuilder directory
        self._source_dir = os.path.abspath(
            os.path.join(
                os.path.split(inspect.getfile(self.__class__))[0], '..'))

        # Extract input/output file names
        f, ext = os.path.splitext(input_file)
        self._input_file = input_file
        self._output_file = f + '.html'

        blaster_dir = os.path.abspath(
            os.path.dirname(os.path.dirname(__file__)))
        # Register the objects to be created
        self.factory.loadPlugins([blaster_dir], 'slidesets', "IS_PRESENTATION")
        self.factory.loadPlugins([blaster_dir], 'slides', "IS_PRESENTATION")
        self.factory.loadPlugins([blaster_dir], 'images', "IS_PRESENTATION")

        # Build the Parser object
        self.parser = Parser(self.factory,
                             self.warehouse,
                             check_for_type=False)

        # Create SlideSet objects via the Parser object by parsing the input file
        print colorText('Parsing input...', 'CYAN')
        default_params = HitNode(hitnode=hit.parse('', ''))
        err = self.parser.parse(input_file, default_params)
        if err:
            sys.exit(err)
        print ''

        # Store the top-level 'presentation' level parameters

        pres = self.parser.root.find('presentation')
        self._params = {}
        if pres is not None:
            for child in pres.children():
                self._params[child.path()] = child.param()

        # Extract CSS style
        self._style()

        # Build the slides
        self.warehouse.execute()
Ejemplo n.º 2
0
class PresentationBuilder(object):

    ##
    # Constructor
    # @param input_file The name of the input file to read
    def __init__(self, input_file, **kwargs):

        # Determine the format
        self._format = kwargs.pop('format', 'remark')

        self._reveal_dir = None
        if self._format == 'reveal':
            self._reveal_dir = os.getenv(
                'REVEAL_JS_DIR',
                os.path.join(os.getenv('HOME'), 'projects', 'reveal.js'))

            if not os.path.exists(self._reveal_dir):
                print 'ERROR: Attempted to output in Reveal.js format, but Reveal.js directory was not found, set the REAVEL_JS_DIR enviornmental variable or clone the repository into your ~/projects directory.'
                sys.exit()

        # Create the Factory and Warehouse
        self.factory = Factory()
        self.warehouse = SlideSetWarehouse(format=self._format)

        # Set the location of PresentationBuilder directory
        self._source_dir = os.path.abspath(
            os.path.join(
                os.path.split(inspect.getfile(self.__class__))[0], '..'))

        # Extract input/output file names
        f, ext = os.path.splitext(input_file)
        self._input_file = input_file
        self._output_file = f + '.html'

        blaster_dir = os.path.abspath(
            os.path.dirname(os.path.dirname(__file__)))
        # Register the objects to be created
        self.factory.loadPlugins([blaster_dir], 'slidesets', "IS_PRESENTATION")
        self.factory.loadPlugins([blaster_dir], 'slides', "IS_PRESENTATION")
        self.factory.loadPlugins([blaster_dir], 'images', "IS_PRESENTATION")

        # Build the Parser object
        self.parser = Parser(self.factory,
                             self.warehouse,
                             check_for_type=False)

        # Create SlideSet objects via the Parser object by parsing the input file
        print colorText('Parsing input...', 'CYAN')
        default_params = HitNode(hitnode=hit.parse('', ''))
        err = self.parser.parse(input_file, default_params)
        if err:
            sys.exit(err)
        print ''

        # Store the top-level 'presentation' level parameters

        pres = self.parser.root.find('presentation')
        self._params = {}
        if pres is not None:
            for child in pres.children():
                self._params[child.path()] = child.param()

        # Extract CSS style
        self._style()

        # Build the slides
        self.warehouse.execute()

    ##
    # Write the html file containing the presentation (public)
    def write(self):
        fid = open(self._output_file, 'w')
        fid.write(self._template('top'))
        fid.write(self.warehouse.markdown())
        fid.write(self._template('bottom'))

    ##
    # Extracts the html templates that build the Remark slideshow (private)
    # @param name The name of the html file to extract
    def _template(self, name):

        # Open and read the template html
        folder, filename = os.path.split(inspect.getfile(self.__class__))

        fid = open(
            os.path.join(folder, '..', 'templates',
                         self._format + '_' + name + '.html'), 'r')
        data = fid.read()
        fid.close()

        # Update the paths for Reveal.js contents
        if self._format == 'reveal':
            data = data.replace('../..', self._reveal_dir)

        # Inject CSS into the html header material
        if name == 'top':

            if self._format == 'remark':
                data = self._injectCSS(data)

            if 'title' in self._params:
                data = re.sub(r'\<title\>.*?\</title\>',
                              '<title>' + self._params['title'] + '</title>',
                              data)

        # Insert the code format
        if name == 'bottom' and self._format == 'remark' and 'code' in self._params:
            data = re.sub(r"(highlightStyle:.*?\,)",
                          "highlightStyle: '" + self._params['code'] + "',",
                          data)

        # Return the html
        return data

    ##
    # Get the CSS style information
    def _style(self):

        # If style if defined, extract it
        if 'style' in self._params:
            style = self._params['style']

            # Check if the style is a readable file, if not assume it is a name
            # of a file in the styles directory
            if not os.path.exists(style):
                style = os.path.join(self._source_dir, 'styles',
                                     style + '.css')

            # Display a message if the style file doesn't exists and do nothinbg
            if not os.path.exists(style):
                raise Exception('Style sheet file "' + style +
                                '" does not exists')

            # Read the CSS and store the css in a dict() within the warehouse (needed for Reveal format)
            else:
                fid = open(style)
                css = fid.read()
                fid.close()
                css = re.sub(r'/\*(.*)\*/', '', css)
                match = re.findall(r'(.*?)\{(.*?)\}', css, re.S)
                style = OrderedDict()
                for m in match:
                    style[m[0].strip()] = m[1].replace('\n', '').strip()

                self.warehouse.css = style

    ##
    # Inserts additional CSS commands into the html output
    # @param data The raw html to inject the css code into
    # @see _template
    def _injectCSS(self, data):

        # Locate the point at which the custom css should be injected
        idx = data.find('</style>')

        # Write the intial portion of the html
        output = data[0:idx - 1] + '\n'

        # Inject the style
        for key, value in self.warehouse.css.iteritems():
            output += ' ' * 6 + key + ' { ' + value + ' }\n\n'

        # Look for the CSS block in the input file, do nothing if it is not found
        node = self.parser.root.find('CSS')
        if node:

            # Write the custom CSS code
            for name, block in node.children.iteritems():
                output += ' ' * 6 + '.' + name + ' {\n'
                for key, value in block.params.iteritems():
                    output += ' ' * 8 + key + ': ' + str(value) + ';\n'
                output += ' ' * 6 + '}\n'

        # Write the remaining html and return it
        output += ' ' * 4
        output += data[idx:]
        return output
Ejemplo n.º 3
0
 def __init__(self):
     self.factory = Factory()
Ejemplo n.º 4
0
class ClusterLauncher:
    def __init__(self):
        self.factory = Factory()

    def parseJobsFile(self, template_dir, job_file):
        jobs = []
        # We expect the job list to be named "job_list"
        filename = template_dir + job_file

        try:
            data = ParseGetPot.readInputFile(filename)
        except:  # ParseGetPot class
            print "Parse Error: " + filename
            return jobs

        # We expect our root node to be called "Jobs"
        if 'Jobs' in data.children:
            jobs_node = data.children['Jobs']

            # Get the active line
            active_jobs = None
            if 'active' in jobs_node.params:
                active_jobs = jobs_node.params['active'].split(' ')

            for jobname, job_node in jobs_node.children.iteritems():
                # Make sure this job is active
                if active_jobs != None and not jobname in active_jobs:
                    continue

                # First retrieve the type so we can get the valid params
                if 'type' not in job_node.params:
                    print "Type missing in " + filename
                    sys.exit(1)

                params = self.factory.validParams(job_node.params['type'])

                params['job_name'] = jobname

                # Now update all the base level keys
                params_parsed = set()
                params_ignored = set()
                for key, value in job_node.params.iteritems():
                    params_parsed.add(key)
                    if key in params:
                        if params.type(key) == list:
                            params[key] = value.split(' ')
                        else:
                            if re.match('".*"', value):  # Strip quotes
                                params[key] = value[1:-1]
                            else:
                                params[key] = value
                    else:
                        params_ignored.add(key)

                # Make sure that all required parameters are supplied
                required_params_missing = params.required_keys(
                ) - params_parsed
                if len(required_params_missing):
                    print 'Required Missing Parameter(s): ', required_params_missing
                    sys.exit(1)
                if len(params_ignored):
                    print 'Ignored Parameter(s): ', params_ignored

                jobs.append(params)
        return jobs

    def createAndLaunchJob(self, template_dir, job_file, specs, options):
        next_dir = getNextDirName(specs['job_name'], os.listdir('.'))
        os.mkdir(template_dir + next_dir)

        # Log it
        if options.message:
            f = open(template_dir + 'jobs.log', 'a')
            f.write(next_dir.ljust(20) + ': ' + options.message + '\n')
            f.close()

        saved_cwd = os.getcwd()
        os.chdir(template_dir + next_dir)

        # Turn the remaining work over to the Job instance
        # To keep everything consistent we'll also append our serial number to our job name
        specs['job_name'] = next_dir
        job_instance = self.factory.create(specs['type'], specs['job_name'],
                                           specs)

        # Copy files
        job_instance.copyFiles(job_file)

        # Prepare the Job Script
        job_instance.prepareJobScript()

        # Launch it!
        job_instance.launch()

        os.chdir(saved_cwd)

    def registerJobType(self, type, name):
        self.factory.register(type, name)

    ### Parameter Dump ###
    def printDump(self):
        self.factory.printDump("Jobs")
        sys.exit(0)

    def run(self, template_dir, job_file, options):
        jobs = self.parseJobsFile(template_dir, job_file)

        for job in jobs:
            self.createAndLaunchJob(template_dir, job_file, job, options)
Ejemplo n.º 5
0
 def __init__(self):
     self.factory = Factory()
Ejemplo n.º 6
0
class ClusterLauncher:
    def __init__(self):
        self.factory = Factory()

    def parseJobsFile(self, template_dir, job_file):
        jobs = []
        # We expect the job list to be named "job_list"
        filename = template_dir + job_file

        try:
            data = ParseGetPot.readInputFile(filename)
        except:  # ParseGetPot class
            print "Parse Error: " + filename
            return jobs

        # We expect our root node to be called "Jobs"
        if "Jobs" in data.children:
            jobs_node = data.children["Jobs"]

            # Get the active line
            active_jobs = None
            if "active" in jobs_node.params:
                active_jobs = jobs_node.params["active"].split(" ")

            for jobname, job_node in jobs_node.children.iteritems():
                # Make sure this job is active
                if active_jobs != None and not jobname in active_jobs:
                    continue

                # First retrieve the type so we can get the valid params
                if "type" not in job_node.params:
                    print "Type missing in " + filename
                    sys.exit(1)

                params = self.factory.validParams(job_node.params["type"])

                params["job_name"] = jobname

                # Now update all the base level keys
                params_parsed = set()
                params_ignored = set()
                for key, value in job_node.params.iteritems():
                    params_parsed.add(key)
                    if key in params:
                        if params.type(key) == list:
                            params[key] = value.split(" ")
                        else:
                            if re.match('".*"', value):  # Strip quotes
                                params[key] = value[1:-1]
                            else:
                                params[key] = value
                    else:
                        params_ignored.add(key)

                # Make sure that all required parameters are supplied
                required_params_missing = params.required_keys() - params_parsed
                if len(required_params_missing):
                    print "Required Missing Parameter(s): ", required_params_missing
                    sys.exit(1)
                if len(params_ignored):
                    print "Ignored Parameter(s): ", params_ignored

                jobs.append(params)
        return jobs

    def createAndLaunchJob(self, template_dir, job_file, specs, options):
        next_dir = getNextDirName(specs["job_name"], os.listdir("."))
        os.mkdir(template_dir + next_dir)

        # Log it
        if options.message:
            f = open(template_dir + "jobs.log", "a")
            f.write(next_dir.ljust(20) + ": " + options.message + "\n")
            f.close()

        saved_cwd = os.getcwd()
        os.chdir(template_dir + next_dir)

        # Turn the remaining work over to the Job instance
        # To keep everything consistent we'll also append our serial number to our job name
        specs["job_name"] = next_dir
        job_instance = self.factory.create(specs["type"], specs["job_name"], specs)

        # Copy files
        job_instance.copyFiles(job_file)

        # Prepare the Job Script
        job_instance.prepareJobScript()

        # Launch it!
        job_instance.launch()

        os.chdir(saved_cwd)

    def registerJobType(self, type, name):
        self.factory.register(type, name)

    ### Parameter Dump ###
    def printDump(self):
        self.factory.printDump("Jobs")
        sys.exit(0)

    def run(self, template_dir, job_file, options):
        jobs = self.parseJobsFile(template_dir, job_file)

        for job in jobs:
            self.createAndLaunchJob(template_dir, job_file, job, options)