def test_write_blowdrycss_settings_dot_py(self):
        settings_file = 'blowdrycss_settings.py'

        # Remove it if it exists.
        if path.isfile(settings_file):
            remove(settings_file)

        # Identical section of code from blowdrycss.py for test purposes.
        write_blowdrycss_settings_dot_py()

        # Import from the current folder.
        try:
            import blowdrycss_settings as settings                          # development case
        except ImportError:
            import blowdrycss.unit_tests.blowdrycss_settings as settings    # python setup.py test

        # test file existence
        self.assertTrue(path.isfile('blowdrycss_settings.py'))

        # test directory and file_type settings
        cwd = getcwd()
        self.assertTrue(
            settings.markdown_directory == path.join(cwd, 'docs', 'markdown'),
            msg=settings.markdown_directory + '\t' + path.join(cwd, 'docs', 'markdown')
        )
        self.assertTrue(settings.project_directory == cwd)
        self.assertTrue(settings.css_directory == path.join(cwd, 'css'))
        self.assertTrue(settings.docs_directory == path.join(cwd, 'docs'))

        self.assertTrue(settings.file_types == ('*.html', ))

        # test accessibility of true settings
        true_settings = [
            settings.timing_enabled, settings.human_readable, settings.minify, settings.media_queries_enabled,
            settings.use_em
        ]
        for true_setting in true_settings:
            self.assertTrue(true_setting)

        # test accessibility of false settings
        false_settings = [settings.markdown_docs, settings.html_docs, settings.rst_docs]
        for false_setting in false_settings:
            self.assertFalse(false_setting)

        # test base, px_to_em, and breakpoints
        self.assertTrue(settings.base == 16)

        self.assertTrue(settings.xxsmall == (settings.px_to_em(0), settings.px_to_em(120)))
        self.assertTrue(settings.xsmall == (settings.px_to_em(121), settings.px_to_em(240)))
        self.assertTrue(settings.small == (settings.px_to_em(241), settings.px_to_em(480)))
        self.assertTrue(settings.medium == (settings.px_to_em(481), settings.px_to_em(720)))
        self.assertTrue(settings.large == (settings.px_to_em(721), settings.px_to_em(1024)))
        self.assertTrue(settings.xlarge == (settings.px_to_em(1025), settings.px_to_em(1366)))
        self.assertTrue(settings.xxlarge == (settings.px_to_em(1367), settings.px_to_em(1920)))
        self.assertTrue(settings.giant == (settings.px_to_em(1921), settings.px_to_em(2560)))
        self.assertTrue(settings.xgiant == (settings.px_to_em(2561), settings.px_to_em(2800)))
        self.assertTrue(settings.xxgiant == (settings.px_to_em(2801), settings.px_to_em(10**6)))

        # Clean up by deleting test settings file.  Can be commented to see what the generated file looks like.
        # Remove it if it exists.
        if path.isfile(settings_file):
            remove(settings_file)
Ejemplo n.º 2
0
def main():
    """ This is the main script.

    **Order of Operations:**

    - Initialize settings.
    - Start performance timer.
    - Generate Markdown documentation files.
    - Generate HTML documentation files. (This location is important since it allows encoded css to be included
      in the documentation files.)
    - Generate reStructuredText documentation files.
    - Define File all file types/extensions to search for in project_directory
    - Get all files associated with defined file_types in project_directory
    - Get set of all defined classes
    - Filter class names only keeping classes that match the defined class encoding.
    - Build a set() of valid css properties. Some classes may be removed during cssutils validation.
    - Output the DRY CSS file. (user command option)
    - Output the Minified DRY CSS file. (user command option)

    **Depending on the settings this script generates the following:**

    - DRY CSS files
        - blowdry.css |sp| |sp| |sp| |sp| |sp| **human readable**
        - blowdry.min.css |sp| **minified**

    - Clashing Alias files (Encoded class selector aliases that are invalid and cannot be used because they clash.)
        - Markdown |sp| |sp| |sp| |sp| |sp| |sp| **Github**
        - HTML |sp| |sp| |sp| |sp| |sp| |sp| |sp| |sp| |sp| **Browser**
        - reStructuredText |sp| **Sphinx**

    - Property Alias File (Encoded class selector aliases that are valid and can be used to construct class selectors.)
        - Markdown |sp| |sp| |sp| |sp| |sp| |sp| **Github**
        - HTML |sp| |sp| |sp| |sp| |sp| |sp| |sp| |sp| |sp| **Browser**
        - reStructuredText |sp| **Sphinx**

    - Temporal Statistics

    **Note:** The default locations of these files can be overridden to suit your needs.

    **Directory assignments**
    ``project_directory`` -- Allows ``blowdrycss`` to know where the HTML project is located. It will only search the files
    in the directory specified here.

.. |sp| raw:: html

     

    """

    # Import settings. Better to ask forgiveness both importing and writing the file.
    # The long name blowdrycss_settings is used since the django uses settings.py and using the same name would
    # cause a name conflict.
    try:
        from settings import blowdrycss_settings as settings
    except ImportError:
        if not path.isfile('blowdrycss_settings.py'):
            write_blowdrycss_settings_dot_py()
        import blowdrycss_settings as settings

    # Performance timer
    if settings.timing_enabled:
        pass

    # Generate Markdown documentation files.
    if settings.markdown_docs:
        markdown_file = GenericFile(                                        # Document forbidden clashing aliases.
                file_directory=settings.markdown_directory,
                file_name='clashing_aliases',
                extension='.md'
        )
        markdown_file.write(str(clashing_alias_markdown))
        markdown_file = GenericFile(                                        # Document allowed property aliases.
                file_directory=settings.markdown_directory,
                file_name='property_aliases',
                extension='.md'
        )
        markdown_file.write(str(property_alias_markdown))

    # Generate HTML documentation files. (This location is important since it allows encoded css to be included
    # in the documentation files.)
    if settings.html_docs:
        html_file = GenericFile(                                            # Document forbidden clashing aliases.
                file_directory=settings.project_directory,
                file_name='clashing_aliases',
                extension='.html'
        )
        html_file.write(str(clashing_alias_html))
        html_file = GenericFile(                                            # Document allowed property aliases.
                file_directory=settings.project_directory,
                file_name='property_aliases',
                extension='.html'
        )
        html_file.write(str(property_alias_html))

    # Generate reStructuredText documentation files.
    if settings.rst_docs:
        print(str(settings.docs_directory))                                              # Python 2 requires str().
        rst_file = GenericFile(file_directory=settings.docs_directory, file_name='clashing_aliases', extension='.rst')
        rst_file.write(str(clashing_alias_rst))
        rst_file = GenericFile(file_directory=settings.docs_directory, file_name='property_aliases', extension='.rst')
        rst_file.write(str(property_alias_rst))

    # Get all files associated with defined file_types in project_directory
    file_finder = FileFinder(project_directory=settings.project_directory, file_types=settings.file_types)

    # Create set of all defined classes
    class_parser = HTMLClassParser(files=file_finder.files)

    # Filter class names. Only keep classes matching the defined class encoding.
    class_property_parser = ClassPropertyParser(class_set=class_parser.class_set)
    # print('\nclass_property_parser.class_set:', class_property_parser.class_set)
    class_set = class_property_parser.class_set.copy()

    # Build a set() of valid css properties. Some classes may be removed during cssutils validation.
    css_builder = CSSBuilder(property_parser=class_property_parser)
    css_text = css_builder.get_css_text()

    # Build Media Queries
    if settings.media_queries_enabled:
        unassigned_class_set = class_set.difference(css_builder.property_parser.class_set)
        css_builder.property_parser.class_set = unassigned_class_set                # Only use unassigned classes
        css_builder.property_parser.removed_class_set = set()                       # Clear set
        media_query_builder = MediaQueryBuilder(property_parser=class_property_parser)
        # print(media_query_builder.property_parser.class_set)
        css_text += bytes(media_query_builder.get_css_text(), 'utf-8')

    # print('CSS Text:')
    # print(css_text)

    # Output the DRY CSS file. (user command option)
    if settings.human_readable:
        css_file = CSSFile(file_directory=settings.css_directory, file_name='blowdry')
        css_file.write(css_text=css_text)
        print(str(settings.css_directory + css_file.file_name) + '.css created.')

    # Output the Minified DRY CSS file. (user command option)
    if settings.minify:
        css_file = CSSFile(file_directory=settings.css_directory, file_name='blowdry')
        css_file.minify(css_text=css_text)
        print(str(settings.css_directory + css_file.file_name) + '.min.css created.')