Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Jun 3, 2024
1 parent 864feed commit 6d3422b
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 81 deletions.
35 changes: 11 additions & 24 deletions osm_fieldwork/CSVDump.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,9 @@
import csv
import logging
import os
import re
import sys
from datetime import datetime

import pandas as pd
from geojson import Feature, FeatureCollection, Point, dump

from osm_fieldwork.convert import Convert
Expand All @@ -38,9 +36,7 @@


class CSVDump(Convert):
"""
A class to parse the CSV files from ODK Central.
"""
"""A class to parse the CSV files from ODK Central."""

def __init__(
self,
Expand All @@ -67,8 +63,7 @@ def lastSaved(
self,
keyword: str,
) -> str:
"""
Get the last saved value for a question.
"""Get the last saved value for a question.
Args:
keyword (str): The keyword to search for
Expand All @@ -86,8 +81,7 @@ def updateSaved(
keyword: str,
value: str,
) -> bool:
"""
Update the last saved value for a question.
"""Update the last saved value for a question.
Args:
keyword (str): The keyword to search for
Expand All @@ -107,8 +101,7 @@ def createOSM(
self,
filespec: str,
):
"""
Create an OSM XML output files.
"""Create an OSM XML output files.
Args:
filespec (str): The output file name
Expand All @@ -121,8 +114,7 @@ def writeOSM(
self,
feature: dict,
):
"""
Write a feature to an OSM XML output file.
"""Write a feature to an OSM XML output file.
Args:
feature (dict): The OSM feature to write to
Expand All @@ -147,8 +139,7 @@ def createGeoJson(
self,
filespec: str = "tmp.geojson",
):
"""
Create a GeoJson output file.
"""Create a GeoJson output file.
Args:
filespec (str): The output file name
Expand All @@ -160,8 +151,7 @@ def writeGeoJson(
self,
feature: dict,
):
"""
Write a feature to a GeoJson output file.
"""Write a feature to a GeoJson output file.
Args:
feature (dict): The OSM feature to write to
Expand All @@ -172,9 +162,7 @@ def writeGeoJson(
self.features.append(feature)

def finishGeoJson(self):
"""
Write the GeoJson FeatureCollection to the output file and close it.
"""
"""Write the GeoJson FeatureCollection to the output file and close it."""
features = list()
for item in self.features:
if len(item["attrs"]["lon"]) == 0 or len(item["attrs"]["lat"]) == 0:
Expand All @@ -194,8 +182,7 @@ def parse(
filespec: str,
data: str = None,
) -> list:
"""
Parse the CSV file from ODK Central and convert it to a data structure.
"""Parse the CSV file from ODK Central and convert it to a data structure.
Args:
filespec (str): The file to parse.
Expand Down Expand Up @@ -270,8 +257,7 @@ def basename(
self,
line: str,
) -> str:
"""
Extract the basename of a path after the last -.
"""Extract the basename of a path after the last -.
Args:
line (str): The path from the json file entry
Expand All @@ -285,6 +271,7 @@ def basename(
base = tmp[len(tmp) - 1]
return base


def main():
"""Run conversion directly from the terminal."""
parser = argparse.ArgumentParser(description="convert CSV from ODK Central to OSM XML")
Expand Down
62 changes: 25 additions & 37 deletions osm_fieldwork/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,20 @@

import argparse
import logging
import re
import sys

import pandas as pd
import re

from osm_fieldwork.xlsforms import xlsforms_path
from osm_fieldwork.yamlfile import YamlFile

# Instantiate logger
log = logging.getLogger(__name__)


def escape(value: str) -> str:
"""
Escape characters like embedded quotes in text fields.
"""Escape characters like embedded quotes in text fields.
Args:
value (str):The string to modify
Expand All @@ -44,9 +45,9 @@ def escape(value: str) -> str:
tmp = value.replace("&", " and ")
return tmp.replace("'", "'")


class Convert(YamlFile):
"""
A class to apply a YAML config file and convert ODK to OSM.
"""A class to apply a YAML config file and convert ODK to OSM.
Returns:
(Convert): An instance of this object
Expand Down Expand Up @@ -99,8 +100,7 @@ def privateData(
self,
keyword: str,
) -> bool:
"""
Search he private data category for a keyword.
"""Search he private data category for a keyword.
Args:
keyword (str): The keyword to search for
Expand All @@ -114,8 +114,7 @@ def convertData(
self,
keyword: str,
) -> bool:
"""
Search the convert data category for a keyword.
"""Search the convert data category for a keyword.
Args:
keyword (str): The keyword to search for
Expand All @@ -129,8 +128,7 @@ def ignoreData(
self,
keyword: str,
) -> bool:
"""
Search the convert data category for a ketyword.
"""Search the convert data category for a ketyword.
Args:
keyword (str): The keyword to search for
Expand All @@ -144,8 +142,7 @@ def getKeyword(
self,
value: str,
) -> str:
"""
Get the keyword for a value from the yaml file.
"""Get the keyword for a value from the yaml file.
Args:
value (str): The value to find the keyword for
Expand All @@ -164,8 +161,7 @@ def getValues(
self,
keyword: str = None,
) -> str:
"""
Get the values for a primary key.
"""Get the values for a primary key.
Args:
keyword (str): The keyword to get the value of
Expand All @@ -184,8 +180,7 @@ def convertEntry(
tag: str,
value: str,
) -> list:
"""
Convert a tag and value from the ODK represention to an OSM one.
"""Convert a tag and value from the ODK represention to an OSM one.
Args:
tag (str): The tag from the ODK XML file
Expand Down Expand Up @@ -237,8 +232,7 @@ def convertValue(
tag: str,
value: str,
) -> list:
"""
Convert a single tag value.
"""Convert a single tag value.
Args:
tag (str): The tag from the ODK XML file
Expand Down Expand Up @@ -281,8 +275,7 @@ def convertTag(
self,
tag: str,
) -> str:
"""
Convert a single tag.
"""Convert a single tag.
Args:
tag (str): The tag from the ODK XML file
Expand Down Expand Up @@ -314,8 +307,7 @@ def convertMultiple(
self,
value: str,
) -> list:
"""
Convert a multiple tags from a select_multiple question..
"""Convert a multiple tags from a select_multiple question..
Args:
value (str): The tags from the ODK XML file
Expand All @@ -324,13 +316,13 @@ def convertMultiple(
(list): The new tags
"""
tags = list()
for tag in value.split(' '):
for tag in value.split(" "):
low = tag.lower()
if self.convertData(low):
newtag = self.convert[low]
# tags.append({newtag}: {value})
if newtag.find('=') > 0:
tmp = newtag.split('=')
if newtag.find("=") > 0:
tmp = newtag.split("=")
tags.append({tmp[0]: tmp[1]})
else:
tags.append({low: "yes"})
Expand All @@ -349,10 +341,10 @@ def parseXLS(
defaults = self.entries["default"]
i = 0
while i < len(self.entries):
if type(self.entries['type'][i]) == float:
self.types[self.entries['name'][i]] = None
if type(self.entries["type"][i]) == float:
self.types[self.entries["name"][i]] = None
else:
self.types[self.entries['name'][i]] = self.entries['type'][i].split(' ')[0]
self.types[self.entries["name"][i]] = self.entries["type"][i].split(" ")[0]
i += 1
total = len(names)
i = 0
Expand All @@ -372,8 +364,7 @@ def createEntry(
self,
entry: dict,
) -> dict:
"""
Create the feature data structure.
"""Create the feature data structure.
Args:
entry (dict): The feature data
Expand Down Expand Up @@ -449,9 +440,7 @@ def createEntry(
return feature

def dump(self):
"""
Dump internal data structures, for debugging purposes only.
"""
"""Dump internal data structures, for debugging purposes only."""
print("YAML file: %s" % self.filespec)
print("Convert section")
for key, val in self.convert.items():
Expand All @@ -472,9 +461,7 @@ def dump(self):
# this way than using pytest,
#
def main():
"""
This main function lets this class be run standalone by a bash script.
"""
"""This main function lets this class be run standalone by a bash script."""
parser = argparse.ArgumentParser(description="Read and parse a YAML file")

parser.add_argument("-v", "--verbose", action="store_true", help="verbose output")
Expand Down Expand Up @@ -536,6 +523,7 @@ def main():
for i in entry:
print("XX: %r" % i)


if __name__ == "__main__":
"""This is just a hook so this file can be run standlone during development."""
main()
Loading

0 comments on commit 6d3422b

Please sign in to comment.