Skip to content

Commit

Permalink
Python packing scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
octopoulos committed Apr 16, 2020
1 parent 0674346 commit 41a1e9e
Show file tree
Hide file tree
Showing 8 changed files with 998 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,21 @@
*.log
/package-lock.json

*.jar
*+.js
*.mp3
*.pyc
*.svg
all*.css
all*.js
archive/
crash.json
crosstable.json
data*.json
enginerating.json
export/
gamelist.json
index.html
live*.json
model/
schedule.json
Expand Down
3 changes: 2 additions & 1 deletion TODO
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
TODO
=====
DONE CREATED DESCRIPTION
2020-04-16 : 2020-04-13 : make a Python script to compress the css / js and embed the necessary ones in the html
2020-04-15 : 2020-04-15 : optimise graphs.js
2020-04-15 : 2020-04-13 : add 3d rendering options: shadow, texture quality, output, resolution
2020-04-15 : 2020-04-15 : replace all localStorage code
Expand All @@ -23,6 +24,7 @@ DONE CREATED DESCRIPTION
2020-04-12 : 2020-04-12 : correct table height (no internal vertical scroll)
2020-04-12 : 2020-04-12 : remove lodash, because slower + takes more space
2020-04-12 : 2020-04-12 : jshint + squash bugs, will be easier to debug
~ : 2020-04-15 : remove redundant hhmm and hhmmss functions
~ : 2020-04-13 : X_SETTINGS = much better settings system than what's now
- : 2020-04-13 : make JS generate some of the live.html elements (ex: options)
- : 2020-04-13 : smooth diagonal table scrolling, with mouse + touch
Expand All @@ -37,7 +39,6 @@ DONE CREATED DESCRIPTION
~ : 2020-04-13 : merge B&W functions
~ : 2020-04-12 : remove all `var` = sources of bugs
~ : 2020-04-13 : remove as much jquery as possible
- : 2020-04-13 : make a Python script to compress the css / js and embed the necessary ones in the html
- : 2020-04-13 : remove all click triggers (they're hacks)
- : 2020-04-13 : accelerate table generation, too many memory allocs + expensive selectors with jQuery
- : 2020-04-14 : chessboard drag doesn't work with touch, only mouse
Expand Down
21 changes: 21 additions & 0 deletions index_base.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<!-- BEGIN -->
<script src="js/common.js"></script>
<script src="js/engine.js"></script>
<script src="js/global.js"></script>
<script src="dist/js/themes.js"></script>
<script src="js/3d.js"></script>
<script src="js/board.js"></script>
<script src="js/script.js"></script>
<link href="css/common.css" rel="stylesheet">
<!-- END -->
<!-- {SCRIPT} -->
<!-- {STYLE} -->
</head>
<body>
<vert></vert>
</body>
</html>
46 changes: 46 additions & 0 deletions script/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# coding: utf-8
# @author octopoulo <[email protected]>
# @version 2020-04-13

"""
Main
"""

from argparse import ArgumentParser
import os
from time import time

from download_json import download_json
from sync import Sync


def main():
"""Main
"""
start = time()

parser = ArgumentParser(description='Sync', prog='python __main__.py')
add = parser.add_argument

add('--console-debug', nargs='?', default='INFO', help='console debug level',
choices=['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'])
add('--download', action='store_true', help='download JSON files')
add('--host', nargs='?', default='/', help='host, ex: /seriv/')
add('--no-debug', nargs='?', default=0, const=1, type=int, help="remove debug code from the javascript")
add('--no-process', nargs='?', default=0, const=1, type=int, help="don't process the images")

args = parser.parse_args()
args_dict = vars(args)

if args.download:
download_json()
else:
sync = Sync(**args_dict)
sync.synchronise()

end = time()
print(f'\nELAPSED: {end-start:.3f} seconds')


if __name__ == '__main__':
main()
82 changes: 82 additions & 0 deletions script/common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# coding: utf-8
# @author octopoulo <[email protected]>
# @version 2020-04-13

"""
Common functions
"""

import errno
import logging
import os


def makedirs_safe(folder: str) -> bool:
"""Create a folder recursively + handle errors
:return: True if the folder has been created or existed already, False otherwise
"""
if not folder:
return True

try:
os.makedirs(folder)
return True
except Exception as e:
if isinstance(e, OSError) and e.errno == errno.EEXIST:
return True
logging.error({'status': 'makedirs_safe__error', 'error': e, 'folder': folder})
return False


def read_text_safe(filename: str, locked: bool=False, want_bytes: bool=False) -> str or bytes or None:
"""Read the content of a file and convert it to utf-8
"""
if not filename or not os.path.isfile(filename):
return None

try:
open_func = open # locked_open if locked else open
with open_func(filename, 'rb') as file:
data = file.read()
return data if want_bytes else data.decode('utf-8-sig')
except OSError as e:
logging.error({'status': 'read_text_safe__error', 'error': e, 'filename': filename})
return None


def utc_time() -> float:
"""Utility to return the utc timestamp
"""
return datetime.now(tz=timezone.utc).timestamp()


def write_text_safe(
filename: str,
data: str or bytes,
mode: str='wb',
locked: bool=False,
convert_newlines: bool=False, # convert \n to \r\n on windows
) -> bool:
"""Save text or binary to a file
"""
if not filename or '?' in filename:
return False

# windows support
if convert_newlines and isinstance(data, str) and system() == 'Windows' and '\r\n' not in data:
data = data.replace('\n', '\r\n')

# save
path = os.path.dirname(filename)
if not makedirs_safe(path):
return False

try:
open_func = locked_open if locked else open
with open_func(filename, mode) as file:
if data:
file.write(data.encode('utf-8') if isinstance(data, str) else data)
return True
except OSError as e:
logging.error({'status': 'write_text_safe__error', 'error': e, 'filename': filename})
return False
Loading

0 comments on commit 41a1e9e

Please sign in to comment.