2019-08-19 11:57:35 +00:00
|
|
|
import logging
|
2019-08-25 17:57:24 +00:00
|
|
|
import os
|
2019-08-19 11:57:35 +00:00
|
|
|
|
2019-08-18 12:11:38 +00:00
|
|
|
USER_AGENT = 'YCast'
|
2019-08-25 17:57:24 +00:00
|
|
|
VAR_PATH = os.path.expanduser("~") + '/.ycast'
|
|
|
|
CACHE_PATH = VAR_PATH + '/cache'
|
2019-08-18 12:11:38 +00:00
|
|
|
|
|
|
|
|
2019-08-18 11:50:38 +00:00
|
|
|
class Directory:
|
2020-10-10 14:36:44 +00:00
|
|
|
def __init__(self, name, item_count, displayname=None):
|
2019-08-18 11:50:38 +00:00
|
|
|
self.name = name
|
|
|
|
self.item_count = item_count
|
2020-10-10 14:36:44 +00:00
|
|
|
if displayname:
|
|
|
|
self.displayname = displayname
|
|
|
|
else:
|
|
|
|
self.displayname = name
|
2019-08-19 11:57:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
def generate_stationid_with_prefix(uid, prefix):
|
|
|
|
if not prefix or len(prefix) != 2:
|
|
|
|
logging.error("Invalid station prefix length (must be 2)")
|
|
|
|
return None
|
|
|
|
if not uid:
|
|
|
|
logging.error("Missing station id for full station id generation")
|
|
|
|
return None
|
|
|
|
return str(prefix) + '_' + str(uid)
|
|
|
|
|
|
|
|
|
|
|
|
def get_stationid_prefix(uid):
|
|
|
|
if len(uid) < 4:
|
|
|
|
logging.error("Could not extract stationid (Invalid station id length)")
|
|
|
|
return None
|
|
|
|
return uid[:2]
|
|
|
|
|
|
|
|
|
|
|
|
def get_stationid_without_prefix(uid):
|
|
|
|
if len(uid) < 4:
|
|
|
|
logging.error("Could not extract stationid (Invalid station id length)")
|
|
|
|
return None
|
|
|
|
return uid[3:]
|
2019-08-25 17:57:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_cache_path(cache_name):
|
|
|
|
cache_path = CACHE_PATH + '/' + cache_name
|
|
|
|
try:
|
|
|
|
os.makedirs(cache_path)
|
|
|
|
except FileExistsError:
|
|
|
|
pass
|
|
|
|
except PermissionError:
|
|
|
|
logging.error("Could not create cache folders (%s) because of access permissions", cache_path)
|
|
|
|
return None
|
|
|
|
return cache_path
|