tempgen.parsers.plaintext
View Source
from tempgen.parsers.parser import AbstractParser class Parser(AbstractParser): def parse(self, path, container, parse_entry, find_matches): with open(path, 'r', encoding='utf-8') as file: text = file.read() matches = find_matches(str(text)) for match in matches: payload = parse_entry(match, path) container[payload['id']] = payload ## add check here for duplicates def replace(self, source_path, target_path, compute_match, replacements, update_external = False): text = '' with open(source_path, 'r', encoding='utf-8') as file: text = file.read() local_to_replace = compute_match(text, {}, replacements, source_path, update_external) for match, value in local_to_replace.items(): text = text.replace(match, value) with open(target_path, 'w', encoding='utf-8') as file: file.write(text)
View Source
class Parser(AbstractParser): def parse(self, path, container, parse_entry, find_matches): with open(path, 'r', encoding='utf-8') as file: text = file.read() matches = find_matches(str(text)) for match in matches: payload = parse_entry(match, path) container[payload['id']] = payload ## add check here for duplicates def replace(self, source_path, target_path, compute_match, replacements, update_external = False): text = '' with open(source_path, 'r', encoding='utf-8') as file: text = file.read() local_to_replace = compute_match(text, {}, replacements, source_path, update_external) for match, value in local_to_replace.items(): text = text.replace(match, value) with open(target_path, 'w', encoding='utf-8') as file: file.write(text)
Helper class that provides a standard way to create an ABC using inheritance.
View Source
def parse(self, path, container, parse_entry, find_matches): with open(path, 'r', encoding='utf-8') as file: text = file.read() matches = find_matches(str(text)) for match in matches: payload = parse_entry(match, path) container[payload['id']] = payload ## add check here for duplicates
Parse file accessible via path property
A general implementation of parse method should include following steps:
- Open file
- Read file data and transform it's meaningful content into string or an iterable of strings
- Call of find_matches function on such strings, resulting in an array of matches
- For each match found one should call parse_entry, resulting in an entry dictionary
- For each entry use entry "id" property as key and payload as value to populate the container provided
Parameters
- path (str): Absolute path to file to be parsed
- container (Dict[str, [Dict[str, Any]]]): Dictionary to be populated with parsed entries, contains key-value pairs with entry id property as key and entry payload dictionary as value
- parse_entry (callable): Function that extracts entry (current implementation uses json parse) from matching string, returns entry payload dictionary
- find_matches (callable): Function that searches the entry string for matches (that is, {{VALID_JSON_OBJECT}} patterns), returns array of matching substrings
#  
def
replace(
self,
source_path,
target_path,
compute_match,
replacements,
update_external=False
):
View Source
def replace(self, source_path, target_path, compute_match, replacements, update_external = False): text = '' with open(source_path, 'r', encoding='utf-8') as file: text = file.read() local_to_replace = compute_match(text, {}, replacements, source_path, update_external) for match, value in local_to_replace.items(): text = text.replace(match, value) with open(target_path, 'w', encoding='utf-8') as file: file.write(text)
Replace file contents
A general implementation of replace method should include following steps:
- Open file accessible via source_path in read mode
- Read file data and transform it so its text content becomes available for editing
- For each string in obtained text call compute_match, it results in an dictionary with {{VALID_JSON_OBJECT}} patterns as keys and computed substitutions as values
- For each match, value in the dictionary, replace match with value in text
- Create file at target_path and write modified text
Parameters
- source_path (str): Absolute path to file to be parsed
- target_path (str): Absolute path to file to be generated
- compute_match (callable):
Function that
- searches the entry string for matches (that is, {{VALID_JSON_OBJECT}} patterns)
- finds entry id in replacements dictionary
- populates to_replace dictionary parameter with "{{VALID_JSON_OBJECT}}" as key and replacement string as value
- if update_external is True, it updates external resources with replacement string
- returns to_replace dictionary
- replacements (Dict[str, str]): Dictionary containing pairs of "{{VALID_JSON_OBJECT}}" keys and their replacements as values
- update_external (bool, optional): Boolean, indicating whether external resources should be updated