Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
UuuNyaa committed May 13, 2021
1 parent 4e96b5a commit b1a126d
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 20 deletions.
32 changes: 20 additions & 12 deletions mmd_uuunyaa_tools/asset_search/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@


class Content:
# pylint: disable=too-few-public-methods

class State(Enum):
FETCHING = 1
CACHED = 2
Expand All @@ -42,7 +44,8 @@ def __init__(
type: str = None,
length: int = 0,
):
self.id = id
# pylint: disable=too-many-arguments,redefined-builtin
self.id = id # pylint: disable=invalid-name
self.state = state
self.filepath = filepath
self.type = type
Expand All @@ -54,6 +57,8 @@ def to_content_id(url: URL) -> str:


class Task:
# pylint: disable=too-few-public-methods

class State(Enum):
QUEUING = 1
RUNNING = 2
Expand All @@ -73,11 +78,11 @@ def __init__(
self,
url: URL,
state: State,
callbacks: List[Callback] = [],
callbacks: List[Callback] = None,
):
self.url = url
self.state = state
self.callbacks = callbacks
self.callbacks = [] if callbacks is None else callbacks
self.future = None
self.content_id = Content.to_content_id(url)
self.fetched_size = 0
Expand Down Expand Up @@ -107,6 +112,8 @@ def async_get_content(self, url: URL, callback: Callback) -> Future:


class ContentCache(CacheABC):
# pylint: disable=too-many-instance-attributes

def __init__(
self,
cache_folder: str,
Expand Down Expand Up @@ -147,8 +154,8 @@ def _load_contents(self):
return

with self._lock:
with open(contents_json_path, 'r') as f:
content_json = json.load(f, object_pairs_hook=OrderedDict)
with open(contents_json_path, 'r') as file:
content_json = json.load(file, object_pairs_hook=OrderedDict)

self._contents = OrderedDict({
key: Content(
Expand Down Expand Up @@ -176,8 +183,8 @@ def _save_contents(self):
'length': value.length
} for key, value in self._contents.items()
}
with open(contents_json_path, 'w') as f:
json.dump(content_json, f)
with open(contents_json_path, 'w') as file:
json.dump(content_json, file)

def _schedule_save_contents(self):
if self._contents_save_timer is not None:
Expand All @@ -190,6 +197,7 @@ def _to_content_filepath(self, content_id: str) -> str:
return os.path.join(self.cache_folder, content_id)

def _fetch(self, task: Task):
# pylint: disable=too-many-statements
with self._lock:
if task.state is not Task.State.QUEUING:
raise ValueError(f'task (={task.url}) is invalid state (={task.state})')
Expand Down Expand Up @@ -235,7 +243,7 @@ def _fetch(self, task: Task):
content.length = content_length
content.type = content_type

except:
except: # pylint: disable=bare-except
traceback.print_exc()
with self._lock:
content.state = Content.State.FAILED
Expand All @@ -260,7 +268,7 @@ def _fetch(self, task: Task):
def _invoke_callback(callback, content):
try:
callback(content)
except:
except: # pylint: disable=bare-except
traceback.print_exc()

def _invoke_callbacks(self, task: Task):
Expand Down Expand Up @@ -321,7 +329,7 @@ def try_get_content(self, url: URL) -> Union[Content, None]:
if os.path.exists(content.file_path):
try:
os.remove(content.file_path)
except:
except: # pylint: disable=bare-except
traceback.print_exc()

excess_cache_size -= content.length
Expand Down Expand Up @@ -366,10 +374,10 @@ def __init__(self):

def delete_cache_object(self):
if self._cache is not None:
with self._cache._lock:
with self._cache._lock: # pylint: disable=protected-access
try:
del self._cache
except:
except: # pylint: disable=bare-except
traceback.print_exc()
self._cache = None

Expand Down
33 changes: 25 additions & 8 deletions mmd_uuunyaa_tools/asset_search/panels.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
from mmd_uuunyaa_tools.asset_search.operators import DeleteDebugAssetJson, ReloadAssetJsons, UpdateAssetJson, UpdateDebugAssetJson
from mmd_uuunyaa_tools.utilities import label_multiline, to_human_friendly_text, to_int32

PREVIEWS: Union[bpy.utils.previews.ImagePreviewCollection, None]


class AssetState(Enum):
INITIALIZED = 0
Expand Down Expand Up @@ -75,7 +77,7 @@ def _on_thumbnail_fetched(self, context, region, update_time, asset, content):
asset_item = search_result.asset_items.add()
asset_item.id = asset.id

global PREVIEWS
global PREVIEWS # pylint: disable=global-statement
if asset.thumbnail_url not in PREVIEWS:
PREVIEWS.load(asset.thumbnail_url, content.filepath, 'IMAGE')

Expand Down Expand Up @@ -142,7 +144,7 @@ class AssetDownload(bpy.types.Operator):

asset_id: bpy.props.StringProperty()

def __on_fetched(self, context, asset, content):
def __on_fetched(self, _, asset, content):
print(f'done: {asset.name}, {asset.id}, {content.state}, {content.id}')

def execute(self, context):
Expand Down Expand Up @@ -302,7 +304,19 @@ def draw_titled_label(layout, title, text, split_factor=0.11):
class AssetSearchQueryTags(bpy.types.UIList):
bl_idname = 'UUUNYAA_UL_mmd_uuunyaa_tools_asset_search_query_tags'

def draw_item(self, context, layout, data, item, icon, active_data, active_propname, index):
def draw_item(
self,
context,
layout,
data,
item,
icon,
active_data,
active_property,
index: int = 0,
flt_flag: int = 0
):
# pylint: disable=too-many-arguments
layout.prop(item, 'enabled', text=item.name, index=index)


Expand All @@ -314,6 +328,8 @@ class AssetSearchPanel(bpy.types.Panel):
bl_category = 'Assets'

def draw(self, context):
# pylint: disable=too-many-locals

search = context.scene.mmd_uuunyaa_tools_asset_search
query = search.query
layout = self.layout
Expand Down Expand Up @@ -343,7 +359,7 @@ def draw(self, context):

asset_items = context.scene.mmd_uuunyaa_tools_asset_search.result.asset_items

global PREVIEWS
global PREVIEWS # pylint: disable=global-statement

grid = layout.grid_flow(row_major=True)
for asset_item in asset_items:
Expand Down Expand Up @@ -376,12 +392,12 @@ def draw(self, context):

@staticmethod
def register():
global PREVIEWS
PREVIEWS = bpy.utils.previews.new()
global PREVIEWS # pylint: disable=global-statement
PREVIEWS = bpy.utils.previews.new() # pylint: disable=assignment-from-no-return

@staticmethod
def unregister():
global PREVIEWS
global PREVIEWS # pylint: disable=global-statement
if PREVIEWS is not None:
bpy.utils.previews.remove(PREVIEWS)

Expand Down Expand Up @@ -434,7 +450,8 @@ def draw(self, context):
box.prop(props, 'repo', text='Repository')
box.prop(props, 'query', text='Query')
box.prop(props, 'output_json', text='Write to')
op = box.operator(UpdateAssetJson.bl_idname, text='Update Assets JSON by query', icon='TRIA_DOWN_BAR')

op = box.operator(UpdateAssetJson.bl_idname, text='Update Assets JSON by query', icon='TRIA_DOWN_BAR') # pylint: disable=invalid-name
op.repo = props.repo
op.query = props.query
op.output_json = props.output_json

0 comments on commit b1a126d

Please sign in to comment.