Skip to content

Commit

Permalink
v1.0.5 - integrates requests-cache
Browse files Browse the repository at this point in the history
change internal storage format
  • Loading branch information
batou9150 committed Jun 16, 2021
1 parent f302f7d commit b826589
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 14 deletions.
31 changes: 21 additions & 10 deletions gitlabui/gitlab.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,19 @@
import sys
import urllib.parse

from requests import request
from requests_cache import CachedSession


class GitlabApi:
def __init__(self, url, token, logger=None):
def __init__(self, url, token, logger=None, cache_expire_after=60):
self.url = url
self.token = token
self.logger = logger
self.db = './projects.json'
self.session = CachedSession(backend='memory', expire_after=cache_expire_after)

def request(self, method, path, **kwargs):
return request(method, self.url + path, headers={'PRIVATE-TOKEN': self.token}, **kwargs)
return self.session.request(method, self.url + path, headers={'PRIVATE-TOKEN': self.token}, **kwargs)

def get(self, path, **kwargs):
return self.request('get', path, **kwargs)
Expand All @@ -40,7 +41,7 @@ def get_projects(self, search=None, opts=None):
if os.path.exists(self.db):
self.logger.info('load from file')
with open(self.db, 'r') as f:
projects = json.load(f)
projects = json.load(f)['projects']
else:
self.logger.info('load from api')
page = 1
Expand All @@ -55,8 +56,7 @@ def get_projects(self, search=None, opts=None):

projects = sorted(projects, key=lambda p: p['path_with_namespace'])

with open(self.db, 'w') as f:
json.dump(projects, f)
self.save(projects)

if search:
projects = [p for p in projects if re.search(search, p['path_with_namespace'], flags=re.IGNORECASE)]
Expand All @@ -70,7 +70,7 @@ def get_projects(self, search=None, opts=None):
if opts and 'sortby' in opts:
sortby = opts['sortby']
is_desc = opts['sortbydirection'] == 'desc' if 'sortbydirection' in opts else False
default_value = "" if is_desc else chr(sys.maxunicode)
default_value = '' if is_desc else chr(sys.maxunicode)
projects = sorted(projects,
key=lambda p: p[sortby] if sortby in p else default_value,
reverse=is_desc)
Expand All @@ -79,15 +79,26 @@ def get_projects(self, search=None, opts=None):

def refresh_tags(self):
projects = [self.get_latest_tag(p) for p in self.get_projects()]
from datetime import datetime
self.save(projects, datetime.now().isoformat(timespec='seconds'))

with open(self.db, 'w') as f:
json.dump(projects, f)
def get_refresh_tags_time(self):
if os.path.exists(self.db):
with open(self.db, 'r') as f:
return json.load(f)['refresh_tags_time']
else:
return None

def reset(self):
self.session.cache.clear()
try:
os.remove(self.db)
except FileNotFoundError:
self.logger.debug("nothing to reset")
self.logger.debug('nothing to reset')

def save(self, projects, refresh_tags_time=None):
with open(self.db, 'w') as f:
json.dump({'refresh_tags_time': refresh_tags_time, 'projects': projects}, f)

def get_latest_tag(self, p):
res = self.get('/projects/' + str(p['id']) + '/repository/tags?order_by=name&search=^v').json()
Expand Down
2 changes: 1 addition & 1 deletion gitlabui/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

{% block menu %}
<a class="btn btn-secondary me-2" href="{{ url_for('refresh_tags') }}" role="button"
data-bs-toggle="tooltip" data-bs-placement="bottom" title="Refresh tags">
data-bs-toggle="tooltip" data-bs-placement="bottom" title="Previous refresh was at {{ refresh_tags_time }}">
<i class="bi bi-arrow-repeat" style="color: white;"></i>
</a>
<a class="btn btn-dark" href="{{ url_for('reset') }}" role="button"
Expand Down
5 changes: 3 additions & 2 deletions gitlabui/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ def index():
@app.route('/tags')
def tags():
projects = api.get_projects(request.args.get('q'), opts=request.args)
refresh_tags_time = api.get_refresh_tags_time()
if request.args.get('format') == 'json':
return {'results': projects}
return {'refresh_tags_time': refresh_tags_time, 'results': projects}
else:
return render_template('index.html', projects=projects)
return render_template('index.html', projects=projects, refresh_tags_time=refresh_tags_time)


@app.route('/reset')
Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@

setup(
name='gitlabui',
version='1.0.4',
version='1.0.5',
author='batou9150',
packages=find_packages(),
include_package_data=True,
zip_safe=False,
install_requires=[
'requests',
'requests-cache',
'flask'
],
description="Flask App over Gitlab Api to browse projects tags and to search into repository files",
Expand Down

0 comments on commit b826589

Please sign in to comment.