2020-06-01 17:24:22 +00:00
#!/usr/bin/env python3
#
# usage:
# add_style.py <file_name> <tags>
2022-02-04 17:30:18 +00:00
#
# where <tags> is a space-separated list of tags to apply
# for that style (1 minimum). Creates a new file to
# chrome/ folder and updates tags.csv and docs/tagmap.json
#
# OR
#
# add_style.py --update-only
#
# updates docs/tagmap.json based on tags.csv without creating anything
#
2020-06-01 17:24:22 +00:00
# OR
#
2022-02-04 17:30:18 +00:00
# add_style.py --list
2020-06-01 17:24:22 +00:00
#
2022-02-04 17:30:18 +00:00
# shows a list of unique tags used in tags.csv
2020-06-01 17:24:22 +00:00
import sys , os
def filterEmpty ( list ) :
for i in range ( 2 , len ( list ) ) :
if list [ i ] == " " :
del list [ i ]
def createJSON ( tagmap , filename , args , onlyupdate ) :
charBuffer = " { \n "
map_last = len ( tagmap ) - 1
n_line = 0
for line in tagmap :
tokens = line . rsplit ( " , " )
filterEmpty ( tokens )
if len ( tokens ) < 2 :
continue
charBuffer + = " \" {} \" :[ " . format ( tokens [ 0 ] )
for t in range ( 1 , len ( tokens ) ) :
# if len(tokens[t]) > 0:
charBuffer + = " \" {} \" " . format ( tokens [ t ] )
if t < len ( tokens ) - 1 :
charBuffer + = " , "
if ( n_line < map_last ) :
charBuffer + = " ], \n "
else :
if onlyupdate :
charBuffer + = " ] \n } "
break
else :
charBuffer + = " ], \n "
n_line + = 1
if not ( onlyupdate ) :
charBuffer + = " \" {} \" :[ " . format ( filename )
for t in range ( 0 , len ( args ) ) :
charBuffer + = " \" {} \" " . format ( args [ t ] )
if t < len ( args ) - 1 :
charBuffer + = " , "
charBuffer + = " ] \n } "
with open ( " html_resources/tagmap.json " , " w " ) as json :
print ( charBuffer , file = json )
print ( " wrote JSON \n " )
def searchFile ( tagmap , name ) :
for line in tagmap :
if name in line :
return True
return False
2020-07-21 10:16:17 +00:00
def createNewFile ( name , folder ) :
filesuffix = " {} / {} " . format ( folder , name )
text = " /* Source file https://github.com/MrOtherGuy/firefox-csshacks/tree/master/ {} made available under Mozilla Public License v. 2.0 \n See the above repository for updates as well as full license text. */ \n " . format ( filesuffix )
2020-06-01 17:24:22 +00:00
2020-07-21 10:16:17 +00:00
if os . path . isfile ( filesuffix ) :
confirm = input ( " File {} already exists! proceed (will overwrite) ? [y/N] " . format ( filesuffix ) )
2020-06-01 17:24:22 +00:00
if confirm != " y " :
print ( " Aborted " )
return False
2020-07-21 10:16:17 +00:00
with open ( filesuffix , " w " ) as css :
2020-06-01 17:24:22 +00:00
print ( text , file = css )
2020-07-21 10:16:17 +00:00
print ( " Created file: {} " . format ( filesuffix ) )
2020-06-01 17:24:22 +00:00
return True
2022-02-04 17:30:18 +00:00
class TaskMode :
def __init__ ( self , args ) :
self . show_help = len ( args ) < 2 or ( args [ 1 ] == " -h " )
if self . show_help :
self . update_only = False
self . list_tags = False
self . normal = False
else :
self . update_only = ( args [ 1 ] == " --update-only " ) or ( args [ 1 ] == " -update-only " ) or ( args [ 1 ] == " -u " )
self . list_tags = ( args [ 1 ] == " --list " ) or ( args [ 1 ] == " -list " ) or ( args [ 1 ] == " -l " )
self . normal = not self . update_only and ( not self . list_tags )
self . min_arg_length = ( 3 if self . normal else 2 )
def printCurrentTags ( filecontent ) :
tags = [ ] ;
for line in filecontent :
tokens = line . rsplit ( " , " )
filterEmpty ( tokens )
if len ( tokens ) < 2 :
continue
for t in range ( 1 , len ( tokens ) ) :
if tags . count ( tokens [ t ] ) == 0 :
tags . append ( tokens [ t ] )
tags . sort ( )
print ( " \n " . join ( tags ) )
2020-07-21 10:16:17 +00:00
2022-02-04 17:30:18 +00:00
2020-06-01 17:24:22 +00:00
if __name__ == " __main__ " :
2022-02-04 17:30:18 +00:00
runmode = TaskMode ( sys . argv )
2020-07-21 10:16:17 +00:00
2022-02-04 17:30:18 +00:00
if runmode . show_help or len ( sys . argv ) < runmode . min_arg_length :
2020-06-01 17:24:22 +00:00
print ( " usage: add_file.py <filename> <list_of_tags> " )
exit ( )
args = [ ]
for i in range ( 2 , ( len ( sys . argv ) ) ) :
args . append ( sys . argv [ i ] )
name = sys . argv [ 1 ]
2022-02-04 17:30:18 +00:00
if not ( runmode . update_only ) and not ( name . endswith ( " .css " ) ) :
2020-07-21 10:16:17 +00:00
name + = " .css "
# For the moment files in content/ are not tagged, but the script can still create the css files
folder = " content " if ( " -content " in sys . argv ) else " chrome "
2022-02-04 17:30:18 +00:00
if ( folder == " content " ) and not ( runmode . update_only ) :
2020-07-21 10:16:17 +00:00
createNewFile ( name , folder )
print ( " Done " )
exit ( 0 )
tagfile = open ( " tags.csv " )
2020-06-01 17:24:22 +00:00
2020-07-21 10:16:17 +00:00
tagmap = tagfile . read ( ) . lstrip ( ) . splitlines ( )
tagfile . close ( )
2022-02-04 17:30:18 +00:00
if runmode . normal and searchFile ( tagmap , name ) :
2020-06-01 17:24:22 +00:00
print ( name + " exist already " )
else :
2022-02-04 17:30:18 +00:00
if runmode . normal :
2020-07-21 10:16:17 +00:00
exists = createNewFile ( name , folder )
tagfile = open ( " tags.csv " , " a " )
tagfile . write ( name + " , " + " , " . join ( args ) + " \n " )
tagfile . close ( )
2022-02-04 17:30:18 +00:00
createJSON ( tagmap , name , args , runmode . update_only )
elif runmode . update_only :
2020-06-01 17:24:22 +00:00
print ( " Only update json " )
2022-02-04 17:30:18 +00:00
createJSON ( tagmap , name , args , runmode . update_only )
elif runmode . list_tags :
print ( " listing tags... " )
printCurrentTags ( tagmap )
else :
print ( " this shouldn ' t happen! " )
# print("Done")
2020-06-01 17:24:22 +00:00
exit ( 0 )