Skip to content

Commit

Permalink
gfptar: new environment variables: GFPTAR_ORJSON, GFPTAR_UJSON
Browse files Browse the repository at this point in the history
  • Loading branch information
takuya-isbs committed Oct 8, 2024
1 parent efc60e0 commit e1d3014
Showing 1 changed file with 41 additions and 10 deletions.
51 changes: 41 additions & 10 deletions gftool/gfptar/gfptar
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
# (required: python3-pip)
# pip3 install --user docopt schema

# Envrionment variables:
# GFPTAR_ORJSON=True : Use orjson instead of json
# GFPTAR_UJSON=True : Use ujson instead of json

# Coding style check:
# flake8 ./gfptar

Expand Down Expand Up @@ -42,7 +46,6 @@ import string
import random
from decimal import Decimal, ROUND_DOWN
import sqlite3
import json
import tempfile
import multiprocessing
import queue
Expand All @@ -56,7 +59,39 @@ from docopt import docopt
from schema import Schema, Use, Or


# library
def str2bool(s):
return s.upper() in ['TRUE', '1', 'ON', 'ENABLE', 'ENABLED']


USE_ORJSON = str2bool(os.getenv('GFPTAR_ORJSON', 'False'))
USE_UJSON = str2bool(os.getenv('GFPTAR_UJSON', 'False'))

try:
if USE_ORJSON:
import orjson as json

def _dumps(obj): # compact
return json.dumps(obj).decode()

json_dumps = _dumps
else:
raise ImportError()
except ImportError:
try:
if USE_UJSON:
import ujson as json
json_dumps = json.dumps # compact
else:
raise ImportError()
except ImportError:
import json

def _dumps_compact(obj):
return json.dumps(obj, separators=(',', ':'))

json_dumps = _dumps_compact


def format_seconds(seconds, minhour=False):
if minhour is False:
return f"{seconds:.0f}s"
Expand Down Expand Up @@ -176,7 +211,7 @@ class DBObj:
class JsonObj(DBObj):
@classmethod
def dumps(cls, obj, for_dict):
return json.dumps(obj, separators=(',', ':'))
return json_dumps(obj)

@classmethod
def loads(cls, key, txt, for_dict):
Expand Down Expand Up @@ -224,7 +259,7 @@ class FileAttr1(DBObj):
@classmethod
def dumps(cls, obj, for_dict):
array = [obj.mode, obj.mtime, obj.user, obj.group]
return json.dumps(array, separators=(',', ':'))
return json_dumps(array)

@classmethod
def loads(cls, key, txt, for_dict):
Expand All @@ -246,7 +281,7 @@ class FileAttr2(DBObj):
def dumps(cls, obj, for_dict):
array = [obj.mode, obj.mtime, obj.user, obj.group,
obj.size, obj.linkname, obj.ftype]
return json.dumps(array, separators=(',', ':'))
return json_dumps(array)

@classmethod
def loads(cls, key, txt, for_dict):
Expand Down Expand Up @@ -861,7 +896,7 @@ class GfURLEntry(DBObj):
# save path to key when using dict, so don't save path to value
if not for_dict:
array.append(obj.path) # [7]
return json.dumps(array, separators=(',', ':'))
return json_dumps(array)

@classmethod
def loads(cls, key, txt, for_dict):
Expand Down Expand Up @@ -1392,10 +1427,6 @@ class GfURL(metaclass=abc.ABCMeta):
return True


def str2bool(s):
return s.upper() in ['TRUE', '1', 'ON', 'ENABLE', 'ENABLED']


USE_GFMKDIR_PLUS = str2bool(os.getenv('GFMKDIR_PLUS', 'True'))
USE_GFCHMOD_PLUS = str2bool(os.getenv('GFCHMOD_PLUS', 'True'))
USE_GFREG_PLUS = str2bool(os.getenv('GFREG_PLUS', 'True'))
Expand Down

0 comments on commit e1d3014

Please sign in to comment.