Skip to content

Commit

Permalink
add create dir if not exists
Browse files Browse the repository at this point in the history
  • Loading branch information
suraw00t committed Jul 6, 2023
1 parent e9bf4d4 commit d6069b9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 15 deletions.
15 changes: 9 additions & 6 deletions pysitemap/format_processors/text.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
import asyncio
from aiofile import AIOFile, Reader, Writer
import logging
import os


class TextWriter():
def __init__(self, filename: str):
self.filename = filename

class TextWriter:
def __init__(self, filepath: str):
self.filepath = filepath
self.dir, self.filename = os.path.split(self.filepath)

async def write(self, urls):
async with AIOFile(self.filename, 'w') as aiodf:
if self.dir and not os.path.exists(self.dir):
os.makedirs(self.dir)

async with AIOFile(self.filepath, "w") as aiodf:
writer = Writer(aiodf)

for url in urls:
await writer("{}\n".format(url))
await aiodf.fsync()

22 changes: 13 additions & 9 deletions pysitemap/format_processors/xml.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,30 @@
import asyncio
from aiofile import AIOFile, Reader, Writer
import logging
import os


class XMLWriter():
def __init__(self, filename: str):
self.filename = filename

class XMLWriter:
def __init__(self, filepath: str):
self.filepath = filepath
self.dir, self.filename = os.path.split(self.filepath)

async def write(self, urls):
async with AIOFile(self.filename, 'w') as aiodf:
if self.dir and not os.path.exists(self.dir):
os.makedirs(self.dir)

async with AIOFile(self.filepath, "w") as aiodf:
writer = Writer(aiodf)
await writer('<?xml version="1.0" encoding="utf-8"?>\n')
await writer(
'<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"'
' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"'
' xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">\n')
' xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">\n'
)
await aiodf.fsync()
for url in urls:
await writer('<url><loc>{}</loc></url>\n'.format(url))
await writer("<url><loc>{}</loc></url>\n".format(url))
await aiodf.fsync()

await writer('</urlset>')
await writer("</urlset>")
await aiodf.fsync()

0 comments on commit d6069b9

Please sign in to comment.