"Fossies" - the Fresh Open Source Software Archive

Member "http-prompt-2.1.0/http_prompt/contextio.py" (5 Mar 2021, 1124 Bytes) of package /linux/www/http-prompt-2.1.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "contextio.py" see the Fossies "Dox" file reference documentation.

    1 """Serialization and deserialization of a Context object."""
    2 
    3 import io
    4 import os
    5 
    6 from . import xdg
    7 from .context.transform import format_to_http_prompt
    8 from .execution import execute
    9 
   10 
   11 # Don't save these HTTPie options to avoid collision with user config file
   12 EXCLUDED_OPTIONS = ['--style']
   13 
   14 # Filename the current environment context will be saved to
   15 CONTEXT_FILENAME = 'context.hp'
   16 
   17 
   18 def _get_context_filepath():
   19     dir_path = xdg.get_data_dir()
   20     return os.path.join(dir_path, CONTEXT_FILENAME)
   21 
   22 
   23 def load_context(context, file_path=None):
   24     """Load a Context object in place from user data directory."""
   25     if not file_path:
   26         file_path = _get_context_filepath()
   27     if os.path.exists(file_path):
   28         with io.open(file_path, encoding='utf-8') as f:
   29             for line in f:
   30                 execute(line, context)
   31 
   32 
   33 def save_context(context):
   34     """Save a Context object to user data directory."""
   35     file_path = _get_context_filepath()
   36     content = format_to_http_prompt(context, excluded_options=EXCLUDED_OPTIONS)
   37     with io.open(file_path, 'w', encoding='utf-8') as f:
   38         f.write(content)