Exemplo n.º 1
0
    def parse(self, input):

        if input in self.libs:
            lib = glob.glob(
                os.path.join(JINJA2MODERN_HOME, self.libs[input]['src']))

            if not lib:
                raise ParserError('%s lib not found' % input)

            try:
                from jinja2modern.default_settings import JINJA2MODERN_JS_LIBS_PATH
                out_path = os.path.join(JINJA2MODERN_MEDIA_PATH,
                                        JINJA2MODERN_JS_LIBS_PATH)
            except ImportError:
                out_path = os.path.join(JINJA2MODERN_MEDIA_PATH, self.out_dir,
                                        'libs')

            out_file = os.path.split(lib[0])[1]
            out_path_file = os.path.join(out_path, out_file)

            if self.needUpdate(lib, out_path_file):
                create_dir_if_not_exist(out_path)
                shutil.copy(lib[0], out_path_file)

            if 'template' in self.libs[input]:
                return self.render(template=self.libs[input]['template'],
                                   file_link=((JINJA2MODERN_JS_LIBS_PATH
                                               or self.out_dir + '/libs') + ''
                                              '/' + out_file))
            else:
                return self.render(file_link=(
                    (JINJA2MODERN_JS_LIBS_PATH or self.out_dir + '/libs') + ''
                    '/' + out_file))

        else:
            out_relative_path, out_file = os.path.split(input)
            if out_relative_path:
                out_path = os.path.join(JINJA2MODERN_HOME,
                                        JINJA2MODERN_MEDIA_PATH,
                                        out_relative_path)
            else:
                out_path = os.path.join(JINJA2MODERN_HOME,
                                        JINJA2MODERN_MEDIA_PATH, self.out_dir)

            out_path_file = os.path.join(out_path, out_file)

            input = os.path.join(JINJA2MODERN_HOME, input)

            if self.needUpdate([input], out_path_file):
                create_dir_if_not_exist(out_path)
                shutil.copy(input, out_path_file)

            return self.render(file_link=(out_relative_path or self.out_dir) +
                               '/' + out_file)
Exemplo n.º 2
0
    def parse(self, input):

        if input in self.libs:
            lib = glob.glob(os.path.join(JINJA2MODERN_HOME, self.libs[input]['src']))

            if not lib:
                raise ParserError('%s lib not found' % input)

            try:
                from settings import JINJA2MODERN_JS_LIBS_PATH
                out_path = os.path.join(JINJA2MODERN_MEDIA_PATH, JINJA2MODERN_JS_LIBS_PATH)
            except ImportError:
                out_path = os.path.join(JINJA2MODERN_MEDIA_PATH, self.out_dir, 'libs')

            out_file = os.path.split(lib[0])[1]
            out_path_file = os.path.join(out_path, out_file)

            if self.needUpdate(lib, out_path_file):
                create_dir_if_not_exist(out_path)
                shutil.copy(lib[0], out_path_file)

            if 'template' in self.libs[input]:
                return self.render(
                    template = self.libs[input]['template'],
                    file_link = (JINJA2MODERN_JS_LIBS_PATH or self.out_dir + '/libs') + '/' + out_file
                )
            else:
                return self.render(
                    file_link = (JINJA2MODERN_JS_LIBS_PATH or self.out_dir + '/libs') + '/' + out_file
                )

        else:
            out_relative_path, out_file = os.path.split(input)
            if out_relative_path:
                out_path = os.path.join(JINJA2MODERN_HOME, JINJA2MODERN_MEDIA_PATH, out_relative_path)
            else:
                out_path = os.path.join(JINJA2MODERN_HOME, JINJA2MODERN_MEDIA_PATH, self.out_dir)

            out_path_file = os.path.join(out_path, out_file)

            input = os.path.join(JINJA2MODERN_HOME, input)

            if self.needUpdate([input], out_path_file):
                create_dir_if_not_exist(out_path)
                shutil.copy(input, out_path_file)

            return self.render(
                file_link = (out_relative_path or self.out_dir) + '/' + out_file
            )
Exemplo n.º 3
0
    def parse(self, input_str, out_file):

        #single file {% tag "some_file" %}
        if not input_str and out_file:
            files = [out_file]

            if self.out_extension:
                out_file = os.path.splitext(os.path.basename(out_file))[0] + '.' + self.out_extension
            else:
                out_file = os.path.basename(out_file)

        else:
            files = strip_list(input_str.split('\n'))

        files = [os.path.join(JINJA2MODERN_HOME, f) for f in files]

        if out_file:
            out_relative_path, out_file = os.path.split(out_file)
            if out_relative_path:
                out_path = os.path.join(JINJA2MODERN_HOME, JINJA2MODERN_MEDIA_PATH, out_relative_path)
            else:
                out_path = os.path.join(JINJA2MODERN_HOME, JINJA2MODERN_MEDIA_PATH, self.out_dir)
            out_path_file = os.path.join(out_path, out_file)
            create_dir_if_not_exist(out_path)

        #single file
        if len(files) == 1 and self.parser_single:

            # no out_file for single file conversion in self (example uglify tag in single file mode)
            if not out_file:
                render = False

                if self.out_extension:
                    out_path_file = os.path.splitext(files[0])[0] + '.' + self.out_extension
                else:
                    out_path_file = files[0]

                out_path, out_file = os.path.split(files[0])

            else:
                render = True

            if self.needUpdate(files, out_path_file):

                args = shlex.split('{bin} {options}'.format(
                    bin = self.parser_bin,
                    options = self.parser_single.format(
                        in_file = files[0],
                        out_path = out_path,
                        out_file = out_file,
                        out_path_file = out_path_file
                    )
                ))

                self.compile(args)

            if render:
                return self.render((out_relative_path or self.out_dir) + '/' + out_file)
            else:
                return ''

        #multiple
        elif len(files) > 1 and self.parser_multiple and out_file:
            if self.needUpdate(files, out_path_file):
                args = shlex.split('{bin} {options}'.format(
                    bin = self.parser_bin,
                    options = self.parser_multiple.format(
                        in_files = ' '.join(files),
                        out_path = out_path,
                        out_path_file = out_path_file,
                        out_file = out_file,
                    )
                ))

                self.compile(args)
            return self.render( (out_relative_path or self.out_dir) + '/' + out_file)

        else:
            raise ParserError("Not enough parameters")
Exemplo n.º 4
0
	def parse(self, input_str, out_file):

		# single file {% tag "some_file" %}
		if not input_str and out_file:
			files = [out_file]

			if self.out_extension:
				out_file = (os.path.splitext(os.path.basename(out_file))[0] + ''
					'.' + self.out_extension)
			else:
				out_file = os.path.basename(out_file)

		else:
			files = strip_list(input_str.split('\n'))

		files = [os.path.join(JINJA2MODERN_HOME, f) for f in files]

		if out_file:
			out_relative_path, out_file = os.path.split(out_file)
			if out_relative_path:
				out_path = os.path.join(JINJA2MODERN_HOME,
					JINJA2MODERN_MEDIA_PATH, out_relative_path)
			else:
				out_path = os.path.join(JINJA2MODERN_HOME, JINJA2MODERN_MEDIA_PATH,
					self.out_dir)
			out_path_file = os.path.join(out_path, out_file)
			create_dir_if_not_exist(out_path)
		else:
			out_path_file = ''
			out_path = ''
			out_relative_path = ''

		# single file
		if len(files) == 1 and self.parser_single:

			# no out_file for single file conversion in self (example uglify tag
			# 	in single file mode)
			if not out_file:
				render = False

				if self.out_extension:
					out_path_file = os.path.splitext(files[0])[0] + '.' + self.out_extension
				else:
					out_path_file = files[0]

				out_path, out_file = os.path.split(files[0])

			else:
				render = True

			if self.needUpdate(files, out_path_file):

				args = shlex.split('{bin} {options}'.format(
					bin=self.parser_bin,
					options=self.parser_single.format(
						in_file=files[0],
						out_path=out_path,
						out_file=out_file,
						out_path_file=out_path_file
					)
				))

				self.compile(args)

			if render:
				return self.render((out_relative_path or self.out_dir) + '/' + out_file)
			else:
				return ''

		# multiple
		elif len(files) > 1 and self.parser_multiple and out_file:
			if self.needUpdate(files, out_path_file):
				args = shlex.split('{bin} {options}'.format(
					bin=self.parser_bin,
					options=self.parser_multiple.format(
						in_files=' '.join(files),
						out_path=out_path,
						out_path_file=out_path_file,
						out_file=out_file,
					)
				))

				self.compile(args)
			return self.render((out_relative_path or self.out_dir) + '/' + out_file)

		else:
			raise ParserError("Not enough parameters")