Exemple #1
0
    def go(self):
        """Go
        """
        lines = read_text_safe(THEME_FILE).split('\r\n')
        num_theme = 0
        theme = ''

        for line in lines:
            if line.endswith(': {'):
                theme = line[:-3].strip()
                num_theme += 1
                print(f'{num_theme}) {theme}')
                continue

            if 'base64' not in line:
                continue

            items = line.split(': ')
            figure = items[0].strip().lower()
            data = items[1].strip("'")
            pos = data.find('base64,')
            if pos < 0:
                print(f'error at {figure}')
                continue

            data = data[pos + 7:]
            buffer = decodebytes(data.encode('ascii'))
            print(buffer[:50])
            filename = os.path.join(BASE, 'theme', theme, f'{figure}.png')
            write_text_safe(filename, buffer)
Exemple #2
0
 def import_file(match: Any) -> str:
     """@import {common.js}
     """
     source = match.group(1)
     filename = os.path.join(JS_FOLDER, source)
     data = read_text_safe(filename) or ''
     if source.endswith('.js'):
         data = re.sub(r'["\']use strict["\'];?', '', data)
     return data
Exemple #3
0
    def analyse_file(self, filename: str):
        """Analyse a file
        """
        text = read_text_safe(filename)
        rematch = self.re_globals.search(text)
        if not rematch:
            return
        globs = rematch.group(1).strip()
        if not globs:
            return

        globs = [item for item in self.re_split.split(globs) if item]

        # 1) check globals alphabetical order
        alphas = sorted(globs, key=lambda x: x.split(':')[0].lower())
        if globs != alphas:
            for glob, alpha in zip(globs, alphas):
                if glob != alpha:
                    print(f'{filename}: order: {glob} vs {alpha}')
                    break

        # 2) detect unused globals
        unused = []
        data = text[rematch.end():]
        for glob in globs:
            glob = glob.split(':')[0]
            rematch = re.findall(rf'\b{glob}\b', data)
            if not rematch:
                unused.append(glob)

        if unused:
            print(f"{filename}: unused: {', '.join(unused)}")

        # 3) check function doc
        funcs = self.re_function.findall(data)
        for doc, name, args, content in funcs:
            has_return = bool(re.search(r'\n    return ', content))
            doc_return = '@returns' in doc
            if has_return is not doc_return:
                print(f'{filename}: return: {name}')

            doc_param = len(re.findall('@param', doc))
            num_param = len(self.re_args.findall(args))
            if doc_param != num_param:
                print(f'{filename}: args: {name}: {doc_param} vs {num_param}')
Exemple #4
0
    def create_index(self):
        """Create the new index.html
        """
        base = os.path.join(LOCAL, 'index_base.html')
        base_time = os.path.getmtime(base)
        index = os.path.join(LOCAL, 'index.html')
        index_time = os.path.getmtime(index) if os.path.isfile(index) else 0
        change = 0
        if base_time >= index_time:
            change += 1

        # 1) minimise JS
        for js_output, js_files in JS_FILES.items():
            all_js = os.path.join(JS_FOLDER, f'{js_output}.js')
            all_min_js = os.path.join(JS_FOLDER, f'{js_output}_.js')
            # common/engine changed => need to update, even though we're not using those files
            js_dates = [
                os.path.abspath(f"{JS_FOLDER}{js_file.strip(':')}.js")
                for js_file in js_files
            ]
            js_names = [
                os.path.abspath(f'{JS_FOLDER}{js_file}.js')
                for js_file in js_files if js_file[0] != ':'
            ]

            if js_output == 'all':
                # script_js = os.path.join(JS_FOLDER, 'script.js')
                extras = []
            else:
                extras = []

            # skip?
            update = True
            if os.path.isfile(all_min_js) and os.path.isfile(all_js):
                all_time = os.path.getmtime(all_min_js)
                update = False
                for js_date in js_dates + extras:
                    update |= os.path.isfile(
                        js_date) and os.path.getmtime(js_date) >= all_time

            if not update:
                print('J', end='')
                continue

            datas = []
            for js_name in js_names:
                print(js_name)
                script_data = read_text_safe(js_name)
                if not script_data:
                    continue

                # process the script.js
                if js_name.endswith('script.js'):
                    script_data = re.sub('@import {(.*?)}', self.import_file,
                                         script_data)
                    script_data = re.sub('// BEGIN.*?// END',
                                         '',
                                         script_data,
                                         flags=re.S)

                    if self.no_debug:
                        script_data = re.sub('// <<.*?// >>',
                                             '',
                                             script_data,
                                             flags=re.S)

                    # use HOST
                    print(f'host={self.host}')
                    if self.host != '/':
                        script_data = script_data.replace(
                            "HOST = '/',", f"HOST = '{self.host}',")

                datas.append(script_data)

            data = '\n'.join(datas)

            if '4d' in js_output:
                data = self.compress_3d(data)

            write_text_safe(all_js, data)
            self.compress_js(all_js)
            print('j', end='')
            change += 1

        # 2) minimise CSS
        all_css = os.path.join(CSS_FOLDER, 'all.css')
        all_min_css = os.path.join(CSS_FOLDER, 'all_.css')
        css_names = [
            os.path.abspath(f'{CSS_FOLDER}{css_file}.css')
            for css_file in CSS_FILES
        ]

        update = True
        if os.path.isfile(all_min_css) and os.path.isfile(all_css):
            all_time = os.path.getmtime(all_min_css)
            update = False
            for css_name in css_names:
                update |= os.path.isfile(
                    css_name) and os.path.getmtime(css_name) >= all_time

        if update:
            datas = []
            for css_name in css_names:
                datas.append(read_text_safe(css_name) or '')

            data = '\n'.join(datas)
            write_text_safe(all_css, data)
            css_data = css_minify(data)
            write_text_safe(all_min_css, css_data)

            print('c', end='')
            change += 1
        else:
            css_data = read_text_safe(all_min_css) or ''
            print('C', end='')

        if not change:
            print('X', end='')
            return

        # 3) remove BEGIN ... END
        html = read_text_safe(base)
        html = re.sub('<!-- BEGIN -->.*?<!-- END -->', '', html, flags=re.S)
        html = re.sub('// BEGIN.*?// END', '', html, flags=re.S)

        # use the HOST
        if self.host != '/':
            replaces = {
                'href="/': f'href="{self.host}',
                'src="/': f'src="{self.host}',
            }
            for key, value in replaces.items():
                html = html.replace(key, value)

        # 4) create the new index.html
        if not self.no_process:
            all_min_js = os.path.join(JS_FOLDER, 'all_.js')
            js_data = read_text_safe(all_min_js) or ''
            replaces = {
                '<!-- {SCRIPT} -->': f'<script>{js_data}</script>',
                '<!-- {STYLE} -->': f'<style>{css_data}</style>',
            }
            for key, value in replaces.items():
                html = html.replace(key, value)

            html = re.sub('<!-- .*? -->', '', html, flags=re.S)

        html = re.sub(r'\n\s+', '\n', html)
        filename = os.path.join(LOCAL, 'index.html')
        write_text_safe(filename, html)