From c84d7b6684e0c3745ff9b089c228e01e1b3378bd Mon Sep 17 00:00:00 2001 From: gabriel becker Date: Thu, 8 Sep 2022 12:44:48 -0300 Subject: [PATCH] Make it a cli app with parameters. --- data/default.kmz | Bin 0 -> 1916 bytes requirements.txt | 3 ++- run.sh | 8 ++++++ src/__main__.py | 67 +++++++++++++++++++++++++++++++++++++---------- 4 files changed, 63 insertions(+), 15 deletions(-) create mode 100644 data/default.kmz create mode 100644 run.sh diff --git a/data/default.kmz b/data/default.kmz new file mode 100644 index 0000000000000000000000000000000000000000..541863f40b53e4524d2d24f692960db63d70f1bd GIT binary patch literal 1916 zcmZ`)c~la57Dmm~me(_@krvG*(-C(w6_;Gev;=X@T{Kcb%>~?WInAW0mFas!sevz7 zcq5#bsi89F_R?Gk+;T}YLqtq+0|}L}IdjfyX3l)~{La1ioZr2FefRsI?s5lC02CD! z0fpweK7j9_uwO^UBMf8Fs4+Y`zTEi7H*7B4d6N^flQY!qT(5&J)j@kzH>kgszg&45 zEL#F%XF<*@HI&?lRjH=u_dIPB>LM_26Z3JW=(LRX^YV*DY8>PUG9y~S}w5VOmGv7mv_>eqCPKTWiAb*0F zO|fph$i13UghhPd6Nh|%jea~-q1kj0Moe8QG}6`Rdr$spig&hi>x|{fj7ZDax9&LV z^up@=_VX0GGUH;uWfQ$?H0Y&ON1A(M{#x9Q<*{QIG2@L-*0AErO`dsQp?Sm(%j$Qh zg`er%Ud@Rwv3pgYM_N$QydZ*h!i*^B)Y$p)(kY$bsGe6iwMZrf8_N5%=%MG3m4SCE z=pOU@tM!hu^hdNYw<*W===pckNd|DAx68aax=O*tzPvm~KC&wp|CBsJ_HQn`b~Aju zau8xr?qxy7I3!Sl=B@ERJJN#34vkF@)*HO_tCe9omEF{G)pJPdbezvKQ8Tm>^Ibhr z@o|Opbu1{JZ?TANdLc8U(al;s8MosHGLuU}`U0H6CB+Wfl<`AsSUxq} z=3=aJe%MZ}uCuD}g-~lg^zkjU`z3mHGb1O`JZI0&;_C3On${+S7|cV_-iv3H&lFfG z%9*7561@*Rg#`tlnXKpwrS?#F1;sjxqs$kw008YM008~JG*{lIc{Dm4 z8I@!djfjskFfmMsL-LP&N#b%!K1PIv&24QaD9N%;ogeHxqRoX=jLUdc$g4f>y>jzl zkr;>-$^kRvG980}#jj~e+?+@lj472$bBiXZp%53B>t*IjniBPsJA1or6w2zZM07U4%D4A@SxK0poIk;I1o`^fa_~>j;<)_>s_U8s{b`!^&$;DN?y%mzsn`Vi}8{1lfCaqHDO~u#|M7kT!^zNHp^E z{qRg0yuK5Kdn8{r$fubKgfGdHQAM4c4vNXiKziSkQbd*~6E6*!TG0X?Rxj7C;J(@= zxY?z4qd7+4NwU+$xb{p;+Wn@Fi++>O84MGlfsMLXr>RyuhXWsueXypllDfNw5?ER#dG$8g-aW`XR)AmMiDn5%KA>w2=Rg^sZCN#c-=mUzdG9BiqL>cM7Yc%q-&SaBT`t1= z-FC+#Qe8i!SUfCqVUcI(p{XSO!4%C-SOj#i;H0}CjH|j%MrNYfZ7{>ECJBBD9`?!2 z?I`t-%Z4jo2_n?ZU)8%5?GGb|r`fQUr%sdPx{SJ1+eRtsrC%5XXD$H7Y|Ag6oEQ3t?u<|-%{p_hXVC08E zYNPlgU*cS~)J0V7KWy4DiUd+fesdho?tmVK)i%nmH=}PmCnL7;KC6evaks6N)7JxP zzH}Lt-yPBy*bc<-#G{-Dzvok}v#G5u<*s{bxHTgFSBvo{p|`zs?ewdsGcw((SzDDZ zRxRaI-f<{!w6q^I5RBfJvhHNBnB?WMQdUs&|^c57%Gt`)aasa^0{)4*9$esZF v;d$T7#5d3TPyE{f|9j~Bh2Iq94{-Mt=YRAB>Mnoq+ZWmWNZ!xu|LXn;Ja?mt literal 0 HcmV?d00001 diff --git a/requirements.txt b/requirements.txt index 2f5b8d3..aba78b2 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,3 @@ pandas -openpyxl \ No newline at end of file +openpyxl +click \ No newline at end of file diff --git a/run.sh b/run.sh new file mode 100644 index 0000000..313f4f8 --- /dev/null +++ b/run.sh @@ -0,0 +1,8 @@ +source /home/gabriel/dev/Python/Environments/xlsx2kmz/bin/activate +python3 /home/gabriel/dev/Python/xlsx2kmz/src/__main__.py \ + -i data/housing.xlsx \ + -o testoutput.kml \ + -b data/Acoomodations.kmz \ + -n "Price,Who,Includes Bills" \ + -d "Url,Min Stay,Who,Price,Notes" \ + -l Location \ No newline at end of file diff --git a/src/__main__.py b/src/__main__.py index cf02b6b..df77161 100644 --- a/src/__main__.py +++ b/src/__main__.py @@ -1,3 +1,4 @@ +from calendar import prmonth import os import zipfile import numpy as np @@ -5,18 +6,40 @@ import pandas as pd from pathlib import Path import xml.etree.ElementTree as xmltree from xml.dom import minidom +import click + + +class Config: + name_columns = ['Price', 'Includes Bills'] + description_columns = ['Url', 'Min Stay', 'Notes'] + location_column = 'Location' + + @staticmethod + def set_name_columns(name_columns): + if isinstance(name_columns, str): + name_columns = name_columns.split(',') + Config.name_columns = name_columns + + @staticmethod + def set_description_columns(description_columns): + if isinstance(description_columns, str): + description_columns = description_columns.split(',') + Config.description_columns = description_columns + + @staticmethod + def set_location_column(location_column): + assert isinstance(location_column, str) + Config.location_column = location_column def load_kmz(kmz_file_path): - print(Path(kmz_file_path).resolve()) - print(Path(kmz_file_path).resolve().exists()) file_to_extract = 'doc.kml' xml = None try: with zipfile.ZipFile(kmz_file_path) as z: xml = xmltree.fromstring(z.read(file_to_extract)) except: - print("Invalid file") + click.echo("Invalid file") return xml @@ -41,13 +64,11 @@ def append_placemarks_to_xml(xml, placemarks): def map_dit(dict_list): items = list() - name_columns = ['Price', 'Includes Bills'] - description_columns = ['Url', 'Min Satay', 'Notes'] - location_column = 'Location' + for item in dict_list: - name = ' '.join([str(item[x]) for x in name_columns]) - description = '\n'.join([str(item[x]) for x in description_columns if item[x] is not np.nan]) - location = parse_location(item[location_column]) + name = ' '.join([str(item[x]) for x in Config.name_columns]) + description = '\n'.join([str(item[x]) for x in Config.description_columns if item[x] is not np.nan]) + location = parse_location(item[Config.location_column]) items.append(dict(name=name, description=description, location=location)) return items @@ -74,24 +95,42 @@ def map_dict_element_to_placemark_xml(element: dict): return xml_element -def main(): - dict_elements = map_dit(load_xlsx('data/housing.xlsx')) +def process(xlsx_path, base_kmz_path, output_path): + dict_elements = map_dit(load_xlsx(xlsx_path)) placemarks = [map_dict_element_to_placemark_xml(dict_element) for dict_element in dict_elements] - kmz_xml = load_kmz('data/Acoomodations.kmz') + kmz_xml = load_kmz(base_kmz_path) kmz_xml = remove_all_current_placemarkers(kmz_xml) kmz_xml = append_placemarks_to_xml(kmz_xml, placemarks) xmltree.register_namespace("", 'http://www.opengis.net/kml/2.2') tree = xmltree.ElementTree(kmz_xml) - with open ('testoutput.kml', "w") as file: + with open (output_path, "w") as file: rough_string = xmltree.tostring(kmz_xml, 'utf-8') reparsed = minidom.parseString(rough_string) content = reparsed.toprettyxml(indent="\t") file.write(content) + +import click + +@click.command() +@click.option("-i", "--xlsx-path", prompt="XLSX containign data to convert to kml.") +@click.option("-o", "--output-path", default='output.kml', prompt="Output fiel path.", help="Where to save output.") +@click.option("-b", "--kmz-path", default='data/default.kmz', help="Base kmz file that provides templating.") +@click.option("-n", "--name", default='name', prompt='Columns to be used as name.') +@click.option("-d", "--description", default='description', prompt='Columns to be used as description.') +@click.option("-l", "--location", default='locaiton', prompt='Columns to be used as location.') +def convert_xlsx_to_kml(xlsx_path, kmz_path, output_path, name, description, location): + Config.set_name_columns(name) + Config.set_description_columns(description) + Config.set_location_column(location) + + process( + xlsx_path, kmz_path, output_path + ) if __name__ == '__main__': - main() \ No newline at end of file + convert_xlsx_to_kml()