Skip to content

Commit

Permalink
Move dogpile cache and URLShortener to services
Browse files Browse the repository at this point in the history
I can see why this was originally in db because it does touch the
filesystem but it is pretty unrelated to the rest of the module and is
only used by services.
  • Loading branch information
ryneeverett committed Aug 28, 2024
1 parent f5846fd commit 3d5642d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 35 deletions.
32 changes: 0 additions & 32 deletions bugwarrior/db.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import json
import os
import re
import subprocess

import requests
import dogpile.cache
from taskw import TaskWarriorShellout
from taskw.exceptions import TaskwarriorError

Expand All @@ -15,35 +12,6 @@
log = logging.getLogger(__name__)


DOGPILE_CACHE_PATH = os.path.expanduser(''.join([
os.getenv('XDG_CACHE_HOME', '~/.cache'), '/dagd-py3.dbm']))

if not os.path.isdir(os.path.dirname(DOGPILE_CACHE_PATH)):
os.makedirs(os.path.dirname(DOGPILE_CACHE_PATH))
CACHE_REGION = dogpile.cache.make_region().configure(
"dogpile.cache.dbm",
arguments=dict(filename=DOGPILE_CACHE_PATH),
)


class URLShortener:
_instance = None

def __new__(cls, *args, **kwargs):
if not cls._instance:
cls._instance = super().__new__(
cls, *args, **kwargs
)
return cls._instance

@CACHE_REGION.cache_on_arguments()
def shorten(self, url):
if not url:
return ''
base = 'https://da.gd/s'
return requests.get(base, params=dict(url=url)).text.strip()


class NotFound(Exception):
pass

Expand Down
31 changes: 30 additions & 1 deletion bugwarrior/services/__init__.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,49 @@
import abc
import copy
import os
import re

from dateutil.parser import parse as parse_date
from dateutil.tz import tzlocal
import dogpile.cache
from jinja2 import Template
import pytz
import requests

from taskw.task import Task

from bugwarrior.config import schema, secrets
from bugwarrior.db import URLShortener

import logging
log = logging.getLogger(__name__)

DOGPILE_CACHE_PATH = os.path.expanduser(''.join([
os.getenv('XDG_CACHE_HOME', '~/.cache'), '/dagd-py3.dbm']))

if not os.path.isdir(os.path.dirname(DOGPILE_CACHE_PATH)):
os.makedirs(os.path.dirname(DOGPILE_CACHE_PATH))
CACHE_REGION = dogpile.cache.make_region().configure(
"dogpile.cache.dbm",
arguments=dict(filename=DOGPILE_CACHE_PATH),
)

class URLShortener:
_instance = None

def __new__(cls, *args, **kwargs):
if not cls._instance:
cls._instance = super().__new__(
cls, *args, **kwargs
)
return cls._instance

@CACHE_REGION.cache_on_arguments()
def shorten(self, url):
if not url:
return ''
base = 'https://da.gd/s'
return requests.get(base, params=dict(url=url)).text.strip()


def get_processed_url(main_config: schema.MainSectionConfig, url: str):
""" Returns a URL with conditional processing.
Expand Down
3 changes: 1 addition & 2 deletions bugwarrior/services/taiga.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
import typing_extensions

from bugwarrior import config
from bugwarrior.db import CACHE_REGION as cache
from bugwarrior.services import IssueService, Issue, ServiceClient
from bugwarrior.services import IssueService, Issue, ServiceClient, CACHE_REGION as cache

import logging
log = logging.getLogger(__name__)
Expand Down

0 comments on commit 3d5642d

Please sign in to comment.