Skip to content

Commit

Permalink
implement INI and CSV file parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Tremblay authored and kblomqvist committed Aug 18, 2020
1 parent 2ff0ba2 commit e415418
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions yasha/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,46 @@ def parse_svd(file):
"peripherals": svd.peripherals,
}


def parse_ini(file):
from configparser import ConfigParser
from io import TextIOWrapper
cfg = ConfigParser()
# yasha opens files in binary mode, configparser expects files in text mode
content = file.read().decode(ENCODING)
cfg.read_string(content)
result = dict(cfg)
for section, data in result.items():
result[section] = dict(data)
return result


def parse_csv(file):
from csv import reader, DictReader, Sniffer
from io import TextIOWrapper
from os.path import basename, splitext
assert file.name.endswith('.csv')
name = splitext(basename(file.name))[0] # get the filename without the extension
content = TextIOWrapper(file, encoding='utf-8', errors='replace')
sample = content.read(1024)
content.seek(0)
csv = list()
if Sniffer().has_header(sample):
for row in DictReader(content):
csv.append(dict(row))
else:
for row in reader(content):
csv.append(row)
return {name: csv}


PARSERS = {
'.json': parse_json,
'.yaml': parse_yaml,
'.yml': parse_yaml,
'.toml': parse_toml,
'.xml': parse_xml,
'.svd': parse_svd,
'.ini': parse_ini,
'.csv': parse_csv
}

0 comments on commit e415418

Please sign in to comment.