def FindInvalidCSSVariables(json_string, input_file, git_runner=RunGit): style_generator = CSSStyleGenerator() style_generator.AddJSONToModel(json_string, in_file=input_file) context = style_generator.in_file_to_context[input_file] if (not context or 'prefix' not in context): raise KeyError('This tool only works on files with a CSS prefix.') css_prefix = '--' + context['prefix'] + '-' valid_names = style_generator.GetCSSVarNames() found_names_list = git_runner([ 'grep', '-ho', '\\%s[a-z-]*' % css_prefix, '--', '*.css', '*.html', '*.js' ]).splitlines() found_names = set() for name in found_names_list: rgb_suffix = '-rgb' if name.endswith(rgb_suffix): name = name[:-len(rgb_suffix)] found_names.add(name) return { 'unspecified': found_names.difference(valid_names), 'unused': valid_names.difference(found_names), 'css_prefix': css_prefix, }
def main(): parser = argparse.ArgumentParser( description='Generate style variables from JSON5 color file.') parser.add_argument('--generator', choices=['CSS'], required=True, help='type of file to generate') parser.add_argument('--out-file', help='file to write output to') parser.add_argument('targets', nargs='+', help='source json5 color files') args = parser.parse_args() if args.generator == 'CSS': style_generator = CSSStyleGenerator() for t in args.targets: style_generator.AddJSONFileToModel(t) if args.out_file: with open(args.out_file, 'w') as f: f.write(style_generator.Render()) else: print(style_generator.Render()) return 0
def get_css_var_names_for_contents(contents_function): style_generator = CSSStyleGenerator() for f in files: file_contents = contents_function(f) if len(file_contents) == 0: continue style_generator.AddJSONToModel('\n'.join(file_contents), in_file=f.LocalPath()) return style_generator.GetCSSVarNames()
class CSSStyleGeneratorTest(unittest.TestCase): def setUp(self): self.generator = CSSStyleGenerator() def assertEqualToFile(self, value, filename): with open(filename) as f: self.assertEqual(value, f.read()) def testColorTestJSON(self): self.generator.AddJSONFileToModel('colors_test.json5') self.assertEqualToFile(self.generator.Render(), 'colors_test_expected.css')
class CSSStyleGeneratorTest(unittest.TestCase): def setUp(self): self.generator = CSSStyleGenerator() def assertEqualToFile(self, value, filename): with open(filename) as f: contents = f.read() self.assertEqual( value, contents, '\n>>>>>\n%s<<<<<\n\ndoes not match\n\n>>>>>\n%s<<<<<' % (value, contents)) def testColorTestJSON(self): self.generator.AddJSONFileToModel('colors_test.json5') self.assertEqualToFile(self.generator.Render(), 'colors_test_expected.css')
class CSSStyleGeneratorTest(unittest.TestCase, BaseStyleGeneratorTest): def setUp(self): self.generator = CSSStyleGenerator() self.generator.AddJSONFileToModel('colors_test_palette.json5') self.generator.AddJSONFileToModel('colors_test.json5') self.expected_output_file = 'colors_test_expected.css' def testCustomDarkModeSelector(self): expected_file_name = 'colors_test_custom_dark_toggle_expected.css' self.generator.generator_options = { 'dark_mode_selector': 'html[dark]:not(body)' } self.assertEqualToFile(self.generator.Render(), expected_file_name) def testCustomDarkModeSelector(self): expected_file_name = 'colors_test_dark_only_expected.css' self.generator.generate_single_mode = Modes.DARK self.assertEqualToFile(self.generator.Render(), expected_file_name)
def setUp(self): self.generator = CSSStyleGenerator() self.generator.AddJSONFileToModel('colors_test_palette.json5') self.generator.AddJSONFileToModel('colors_test.json5')
def setUp(self): self.generator = CSSStyleGenerator() self.generator.AddJSONFileToModel('colors_test_palette.json5') self.generator.AddJSONFileToModel('colors_test.json5') self.expected_output_file = 'colors_test_expected.css'
def setUp(self): self.generator = CSSStyleGenerator()