Commit cf5ef3ad authored by eckhart's avatar eckhart

- DHParser-refactoring

parent 75c51cb9
......@@ -38,13 +38,14 @@ import copy
import os
from typing import Any, Optional, Tuple, List, cast
from DHParser.configuration import get_config_value
from DHParser.preprocess import with_source_mapping, PreprocessorFunc, SourceMapFunc
from DHParser.syntaxtree import Node, RootNode, ZOMBIE_TAG, StrictResultType
from DHParser.transform import TransformationFunc
from DHParser.parse import Grammar
from DHParser.error import adjust_error_locations, is_error, is_fatal, Error
from DHParser.log import log_parsing_history, log_ST, is_logging
from DHParser.toolkit import load_if_file, is_filename, get_config_value
from DHParser.toolkit import load_if_file, is_filename
__all__ = ('CompilerError',
......
......@@ -17,9 +17,8 @@
"""
Module "configuration.py" defines the default configuration for DHParser.
The configuration values can be changed while running via the
DHParser.toolkit.get_config_value() and DHParser.toolkit.get_config_value()-
functions.
The configuration values can be read and changed while running via the
get_config_value() and set_config_value()-functions.
The presets can also be overwritten before(!) spawning any parsing processes by
overwriting the values in the CONFIG_PRESET dictionary.
......@@ -30,10 +29,14 @@ this is desired in the CONFIG_PRESET dictionary right after the start of the
program and before any DHParser-function is invoked.
"""
import threading
from typing import Dict, Hashable, Any
__all__ = ('access_presets',
'finalize_presets',
'THREAD_LOCALS',
'get_config_value',
'set_config_value',
'XML_SERIALIZATION',
'SXPRESSION_SERIALIZATION',
'COMPACT_SERIALIZATION',
......@@ -44,13 +47,14 @@ __all__ = ('access_presets',
########################################################################
#
# multiprocessing-safe preset-handling
# multiprocessing-safe preset- and configuration-handling
#
########################################################################
CONFIG_PRESET = dict() # type: Dict[str, Any]
CONFIG_PRESET['syncfile_path'] = ''
THREAD_LOCALS = threading.local()
def get_syncfile_path(pid: int) -> str:
......@@ -120,6 +124,46 @@ def finalize_presets():
pickle.dump(CONFIG_PRESET, f)
def get_config_value(key: Hashable) -> Any:
"""
Retrieves a configuration value thread-safely.
:param key: the key (an immutable, usually a string)
:return: the value
"""
global THREAD_LOCALS
try:
cfg = THREAD_LOCALS.config
except AttributeError:
THREAD_LOCALS.config = dict()
cfg = THREAD_LOCALS.config
try:
return cfg[key]
except KeyError:
CONFIG_PRESET = access_presets()
value = CONFIG_PRESET[key]
THREAD_LOCALS.config[key] = value
return value
def set_config_value(key: Hashable, value: Any):
"""
Changes a configuration value thread-safely. The configuration
value will be set only for the current thread. In order to
set configuration values for any new thread, add the key and value
to CONFIG_PRESET, before any thread accessing config values is started.
:param key: the key (an immutable, usually a string)
:param value: the value
"""
global THREAD_LOCALS
if THREAD_LOCALS is None:
THREAD_LOCALS = threading.local()
try:
_ = THREAD_LOCALS.config
except AttributeError:
THREAD_LOCALS.config = dict()
THREAD_LOCALS.config[key] = value
########################################################################
#
# parser configuration
......
......@@ -31,6 +31,7 @@ import os
from typing import Callable, Dict, List, Set, Tuple, Sequence, Union, Optional, Any, cast
from DHParser.compile import CompilerError, Compiler, compile_source, visitor_name
from DHParser.configuration import THREAD_LOCALS, get_config_value
from DHParser.error import Error
from DHParser.parse import Grammar, mixin_comment, Forward, RegExp, DropWhitespace, \
NegativeLookahead, Alternative, Series, Option, OneOrMore, ZeroOrMore, Token, \
......@@ -38,7 +39,7 @@ from DHParser.parse import Grammar, mixin_comment, Forward, RegExp, DropWhitespa
from DHParser.preprocess import nil_preprocessor, PreprocessorFunc
from DHParser.syntaxtree import Node, WHITESPACE_PTYPE, TOKEN_PTYPE
from DHParser.toolkit import load_if_file, escape_re, md5, sane_parser_name, re, expand_table, \
THREAD_LOCALS, get_config_value, unrepr, compile_python_object, DHPARSER_PARENTDIR
unrepr, compile_python_object, DHPARSER_PARENTDIR
from DHParser.transform import TransformationFunc, traverse, remove_brackets, \
reduce_single_child, replace_by_single_child, remove_whitespace, remove_empty, \
remove_tokens, flatten, forbid, assert_content
......
......@@ -54,10 +54,11 @@ import html
import os
from typing import List, Tuple, Union, Optional
from DHParser.configuration import THREAD_LOCALS
from DHParser.error import Error
from DHParser.stringview import StringView
from DHParser.syntaxtree import Node, ZOMBIE_TAG
from DHParser.toolkit import is_filename, escape_control_characters, THREAD_LOCALS
from DHParser.toolkit import is_filename, escape_control_characters
__all__ = ('log_dir',
'logging',
......
......@@ -34,14 +34,14 @@ from collections import defaultdict
import copy
from typing import Callable, cast, List, Tuple, Set, Dict, DefaultDict, Union, Optional, Any
from DHParser.configuration import get_config_value
from DHParser.error import Error, linebreaks, line_col
from DHParser.log import is_logging, HistoryRecord
from DHParser.preprocess import BEGIN_TOKEN, END_TOKEN, RX_TOKEN_NAME
from DHParser.stringview import StringView, EMPTY_STRING_VIEW
from DHParser.syntaxtree import Node, FrozenNode, RootNode, WHITESPACE_PTYPE, \
TOKEN_PTYPE, ZOMBIE_TAG, ResultType
from DHParser.toolkit import sane_parser_name, escape_control_characters, get_config_value, \
re, cython
from DHParser.toolkit import sane_parser_name, escape_control_characters, re, cython
__all__ = ('Parser',
......
......@@ -53,9 +53,10 @@ import time
from typing import Callable, Coroutine, Optional, Union, Dict, List, Tuple, Sequence, Set, Any, \
cast
from DHParser.configuration import get_config_value, THREAD_LOCALS
from DHParser.syntaxtree import DHParser_JSONEncoder
from DHParser.log import create_log, append_log
from DHParser.toolkit import get_config_value, THREAD_LOCALS, re
from DHParser.toolkit import re
from DHParser.versionnumber import __version__
......
......@@ -31,10 +31,10 @@ from typing import Callable, cast, Iterator, Sequence, List, AbstractSet, Set, U
Container, Optional, Dict
from DHParser.configuration import SERIALIZATIONS, XML_SERIALIZATION, SXPRESSION_SERIALIZATION, \
COMPACT_SERIALIZATION, JSON_SERIALIZATION, SMART_SERIALIZATION
COMPACT_SERIALIZATION, JSON_SERIALIZATION, SMART_SERIALIZATION, get_config_value
from DHParser.error import Error, ErrorCode, linebreaks, line_col
from DHParser.stringview import StringView
from DHParser.toolkit import get_config_value, re
from DHParser.toolkit import re
__all__ = ('WHITESPACE_PTYPE',
......
......@@ -37,11 +37,12 @@ import os
import sys
from typing import Dict, List, Union, cast
from DHParser.configuration import THREAD_LOCALS, get_config_value
from DHParser.error import Error, is_error, adjust_error_locations
from DHParser.log import log_dir, logging, is_logging, clear_logs, log_parsing_history
from DHParser.parse import UnknownParserError, Parser, Lookahead
from DHParser.syntaxtree import Node, RootNode, parse_tree, flatten_sxpr, ZOMBIE_TAG
from DHParser.toolkit import THREAD_LOCALS, get_config_value, load_if_file, re
from DHParser.toolkit import load_if_file, re
__all__ = ('unit_from_config',
'unit_from_json',
......
......@@ -48,8 +48,6 @@ except ImportError:
cython_optimized = False
import DHParser.shadow_cython as cython
from DHParser.configuration import access_presets
__all__ = ('typing',
'cython',
......@@ -70,10 +68,7 @@ __all__ = ('typing',
'smart_list',
'sane_parser_name',
'DHPARSER_DIR',
'DHPARSER_PARENTDIR',
'THREAD_LOCALS',
'get_config_value',
'set_config_value')
'DHPARSER_PARENTDIR')
#######################################################################
......@@ -85,47 +80,6 @@ __all__ = ('typing',
DHPARSER_DIR = os.path.dirname(os.path.abspath(__file__))
DHPARSER_PARENTDIR = os.path.dirname(DHPARSER_DIR.rstrip('/'))
THREAD_LOCALS = threading.local()
def get_config_value(key: Hashable) -> Any:
"""
Retrieves a configuration value thread-safely.
:param key: the key (an immutable, usually a string)
:return: the value
"""
global THREAD_LOCALS
try:
cfg = THREAD_LOCALS.config
except AttributeError:
THREAD_LOCALS.config = dict()
cfg = THREAD_LOCALS.config
try:
return cfg[key]
except KeyError:
CONFIG_PRESET = access_presets()
value = CONFIG_PRESET[key]
THREAD_LOCALS.config[key] = value
return value
def set_config_value(key: Hashable, value: Any):
"""
Changes a configuration value thread-safely. The configuration
value will be set only for the current thread. In order to
set configuration values for any new thread, add the key and value
to CONFIG_PRESET, before any thread accessing config values is started.
:param key: the key (an immutable, usually a string)
:param value: the value
"""
global THREAD_LOCALS
if THREAD_LOCALS is None:
THREAD_LOCALS = threading.local()
try:
_ = THREAD_LOCALS.config
except AttributeError:
THREAD_LOCALS.config = dict()
THREAD_LOCALS.config[key] = value
# global_id_counter = multiprocessing.Value('Q', 0)
......
......@@ -9,7 +9,7 @@ import sys
LOGGING = False
scriptpath = os.path.dirname(__file__)
dhparserdir = os.path.abspath(os.path.join(scriptpath, '..\..'))
dhparserdir = os.path.abspath(os.path.join(scriptpath, '..', '..'))
if dhparserdir not in sys.path:
sys.path.append(dhparserdir)
......
......@@ -28,7 +28,8 @@ scriptdir = os.path.dirname(os.path.realpath(__file__))
sys.path.extend(['../', './'])
from DHParser.toolkit import compile_python_object, get_config_value, set_config_value, re
from DHParser.configuration import get_config_value, set_config_value
from DHParser.toolkit import compile_python_object, re
from DHParser.preprocess import nil_preprocessor
from DHParser import compile_source
from DHParser.error import has_errors, Error
......
......@@ -24,7 +24,8 @@ from functools import partial
sys.path.extend(['../', './'])
from DHParser.toolkit import compile_python_object, get_config_value, set_config_value
from DHParser.configuration import get_config_value, set_config_value
from DHParser.toolkit import compile_python_object
from DHParser.log import logging, is_logging, log_ST, log_parsing_history
from DHParser.error import Error, is_error
from DHParser.parse import ParserError, Parser, Grammar, Forward, TKN, ZeroOrMore, RE, \
......
......@@ -30,8 +30,9 @@ import time
sys.path.extend(['../', './'])
from DHParser.configuration import THREAD_LOCALS
from DHParser.server import Server, STOP_SERVER_REQUEST, IDENTIFY_REQUEST, SERVER_OFFLINE, asyncio_run
from DHParser.toolkit import concurrent_ident, THREAD_LOCALS
from DHParser.toolkit import concurrent_ident
# from DHParser.configuration import CONFIG_PRESET
# THREAD_LOCALS.LOGGING = 'LOGS'
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment