mirror of
https://github.com/autistic-symposium/web3-starter-py.git
synced 2025-05-17 14:10:21 -04:00
💾
This commit is contained in:
parent
7cb7a479f6
commit
69bb4175f1
124 changed files with 20 additions and 15 deletions
54
web2_projects/magic-pen/src/utils.py
Normal file
54
web2_projects/magic-pen/src/utils.py
Normal file
|
@ -0,0 +1,54 @@
|
|||
# -*- coding: utf8 -*-
|
||||
|
||||
import os
|
||||
import sys
|
||||
import logging as l
|
||||
|
||||
from src.settings import BOUNDARY_LIMIT_ENC, BOUNDARY_LIMIT_DEC
|
||||
|
||||
|
||||
def parse_input_stream(filename):
|
||||
"""
|
||||
Loads a file with the input stream with
|
||||
encoded commands.
|
||||
|
||||
Returns:
|
||||
filename -- location of the file in disk.
|
||||
"""
|
||||
try:
|
||||
with open(filename, 'r') as f:
|
||||
lines = f.readlines()
|
||||
|
||||
for input_stream in lines:
|
||||
if input_stream[0] != '#':
|
||||
l.debug('Input stream:\n{}'.format(input_stream))
|
||||
return input_stream.strip()
|
||||
|
||||
except (KeyError, OSError, TypeError) as e:
|
||||
l.error('Could not open input stream file {0}: {1}.'.format(filename, e))
|
||||
sys.exit(0)
|
||||
|
||||
|
||||
def get_boundary_limits(lim_base):
|
||||
"""
|
||||
Extract boundary limits from env file, returning two integers
|
||||
representing these limits, either in the encoded or
|
||||
the decode base.
|
||||
|
||||
Arguments:
|
||||
lim_base {string}: 'enc' for the encoded limit
|
||||
representation or 'dec' for the encoded limit
|
||||
representation.
|
||||
"""
|
||||
try:
|
||||
if lim_base == 'enc':
|
||||
enc_range = tuple(BOUNDARY_LIMIT_ENC.split(', '))
|
||||
return int(enc_range[0]), int(enc_range[1])
|
||||
|
||||
elif lim_base == 'dec':
|
||||
dec_range = tuple(BOUNDARY_LIMIT_DEC.split(', '))
|
||||
return int(dec_range[0], 16), int(dec_range[1], 16)
|
||||
|
||||
except (KeyError, ValueError, AttributeError) as e:
|
||||
l.error('Could not extract boundary limits from .env file: {}'.format(e))
|
||||
return -1
|
Loading…
Add table
Add a link
Reference in a new issue