Skip to content

Commit

Permalink
save(): auto detect gzip
Browse files Browse the repository at this point in the history
  • Loading branch information
nomadrat committed Aug 6, 2022
1 parent 505b7e8 commit 20a4939
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 15 deletions.
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from setuptools import setup, find_packages
from pathlib import Path

VERSION = '0.1.3'
VERSION = '0.1.4'
DESCRIPTION = 'Create advanced sitemaps easily'

this_directory = Path(__file__).parent
Expand All @@ -19,7 +19,7 @@
long_description_content_type='text/markdown',
packages=find_packages(),
install_requires=[],
url='https://github.com/abstractkitchen/sitemapa',
url='https://abstractkitchen.com/blog/sitemaps-for-devs/',
keywords=['python', 'sitemap', ''],
classifiers=[
"Development Status :: 5 - Production/Stable",
Expand Down
15 changes: 2 additions & 13 deletions sitemapa/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,10 @@ def add_url_items(url_node, item_type, items):

class SitemapBase:
def __init__(self, **kwargs):
compress = kwargs.get("compress", False) # if True — gzip compression will be applied
max_urls = kwargs.get("max_urls", 50000)

self.urls = {}
self.max_urls = max_urls
self.compress = compress
self.options = kwargs

# These parameters will be detected automatically
Expand All @@ -84,26 +82,17 @@ def create_tree(self):
raise NotImplementedError

def save(self, save_as, **kwargs):
compress = kwargs.get("compress", None)

if compress:
self.compress = compress

compress = ".xml.gz" in save_as
root = self.create_tree()
sitemap_name = save_as.split("/")[-1]
dest_path = "/".join(save_as.split("/")[:-1])

sitemap_name = f"{sitemap_name}.xml"
if self.compress:
sitemap_name = f"{sitemap_name}.gz"

save_as = f"{dest_path}/{sitemap_name}"

# create sitemap path if not existed
if not os.path.exists(f"{dest_path}/"):
os.makedirs(f"{dest_path}/")

if not self.compress:
if not compress:
tree = cElementTree.ElementTree(root)
tree.write(save_as, encoding='utf-8', xml_declaration=True)
else:
Expand Down

0 comments on commit 20a4939

Please sign in to comment.