prthm11 commited on
Commit
7028127
·
verified ·
1 Parent(s): cf01cc2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -92
app.py CHANGED
@@ -1830,37 +1830,23 @@ def similarity_matching(sprites_data: dict, project_folder: str, top_k: int = 1,
1830
  print("matched_indices=========================>",matched_indices)
1831
  for matched_idx in matched_indices:
1832
  matched_image_path = paths_list[matched_idx]
1833
- # matched_folder = os.path.dirname(matched_image_path)
1834
- # matched_filename = os.path.basename(matched_image_path)
1835
- # matched_image_path = paths_list[matched_idx]
1836
- # # Normalize Windows-style backslashes to the platform separator, then normpath
1837
- # matched_image_path = os.path.normpath(str(matched_image_path).replace('\\', os.sep))
1838
- # matched_folder = os.path.dirname(matched_image_path)
1839
- # matched_filename = os.path.basename(matched_image_path)
1840
- mp = str(matched_image_path).replace('\\', os.sep) # convert Windows backslashes -> platform sep
1841
- if not os.path.isabs(mp):
1842
- mp = os.path.normpath(os.path.join(str(BLOCKS_DIR), mp)) # turn relative -> absolute under BLOCKS_DIR
1843
- else:
1844
- mp = os.path.normpath(mp) # already absolute: just normalize
1845
-
1846
- matched_image_path = os.path.abspath(mp) # final absolute path
1847
  matched_folder = os.path.dirname(matched_image_path)
1848
  matched_filename = os.path.basename(matched_image_path)
1849
- print("NORM matched_image_path:", matched_image_path, " matched_folder:", matched_folder)
1850
  try:
1851
  # Normalize many forms so we can compare apples-to-apples
1852
  mf_abs = os.path.normpath(os.path.abspath(matched_folder))
1853
  mf_repr = repr(matched_folder)
1854
  mf_abspath_repr = repr(mf_abs)
1855
-
1856
  # backdrop / sprite base (may be pathlib.Path in prod)
1857
  bb = os.path.normpath(os.path.abspath(str(backdrop_base_path)))
1858
  sb = os.path.normpath(os.path.abspath(str(sprite_base_path)))
1859
-
1860
  # quick membership tests
1861
  starts_with_backdrop = matched_folder.startswith(backdrop_base_path) or mf_abs.startswith(bb)
1862
  starts_with_sprite = matched_folder.startswith(sprite_base_path) or mf_abs.startswith(sb)
1863
-
1864
  # cross-platform commonpath test
1865
  try:
1866
  common_with_backdrop = (os.path.commonpath([mf_abs, bb]) == bb)
@@ -1870,7 +1856,7 @@ def similarity_matching(sprites_data: dict, project_folder: str, top_k: int = 1,
1870
  common_with_sprite = (os.path.commonpath([mf_abs, sb]) == sb)
1871
  except Exception:
1872
  common_with_sprite = False
1873
-
1874
  print("DEBUG matched_idx:", matched_idx)
1875
  print("DEBUG matched_image_path:", matched_image_path)
1876
  print("DEBUG matched_folder (raw):", mf_repr)
@@ -1885,94 +1871,32 @@ def similarity_matching(sprites_data: dict, project_folder: str, top_k: int = 1,
1885
  print("DEBUG sample paths_list entry:", repr(paths_list[matched_idx])[:400])
1886
  except Exception as _e:
1887
  print("DEBUG diagnostics failed:", _e)
1888
-
1889
  # If it's a sprite (under SPRITE_DIR) -> copy sprite assets and read sprite.json
1890
  # if matched_folder.startswith(sprite_base_path) and matched_folder not in copied_sprite_folders:
1891
- # if is_subpath(matched_folder, sprite_base_path) and matched_folder not in copied_sprite_folders:
1892
- # copied_sprite_folders.add(matched_folder)
1893
- # sprite_json_path = os.path.join(matched_folder, "sprite.json")
1894
- # if os.path.exists(sprite_json_path):
1895
- # try:
1896
- # with open(sprite_json_path, "r", encoding="utf-8") as f:
1897
- # sprite_info = json.load(f)
1898
- # project_data.append(sprite_info)
1899
- # except Exception as e:
1900
- # print("Failed to read sprite.json in %s: %s", matched_folder, e)
1901
- # else:
1902
- # print("No sprite.json in %s", matched_folder)
1903
- # # copy non-matching files from the sprite folder (except the matched image and sprite.json)
1904
- # for fname in os.listdir(matched_folder):
1905
- # if fname in (matched_filename, "sprite.json"):
1906
- # continue
1907
- # src = os.path.join(matched_folder, fname)
1908
- # dst = os.path.join(project_folder, fname)
1909
- # if os.path.isfile(src):
1910
- # try:
1911
- # shutil.copy2(src, dst)
1912
- # except Exception as e:
1913
- # print("Failed to copy sprite asset %s: %s", src, e)
1914
  if is_subpath(matched_folder, sprite_base_path) and matched_folder not in copied_sprite_folders:
1915
  copied_sprite_folders.add(matched_folder)
1916
  sprite_json_path = os.path.join(matched_folder, "sprite.json")
1917
-
1918
- # Debug: existence & listing
1919
- try:
1920
- exists = os.path.exists(matched_folder)
1921
- entries = os.listdir(matched_folder) if exists else []
1922
- except Exception as e:
1923
- exists = False
1924
- entries = []
1925
- print(f"DEBUG: cannot list '{matched_folder}': {e}")
1926
-
1927
- print(f"DEBUG: processing sprite folder: {matched_folder} exists={exists} entries_count={len(entries)}")
1928
- if len(entries) > 0:
1929
- print("DEBUG: sample entries:", entries[:20])
1930
-
1931
- # Read sprite.json if present
1932
  if os.path.exists(sprite_json_path):
1933
  try:
1934
  with open(sprite_json_path, "r", encoding="utf-8") as f:
1935
  sprite_info = json.load(f)
1936
  project_data.append(sprite_info)
1937
- print(f"DEBUG: loaded sprite.json from {sprite_json_path}")
1938
  except Exception as e:
1939
- print(f"DEBUG: Failed to read sprite.json in {matched_folder}: {e}")
1940
  else:
1941
- print(f"DEBUG: No sprite.json in {matched_folder}")
1942
-
1943
- # Recursively copy everything under matched_folder (preserve structure) EXCEPT matched image and sprite.json
1944
- dst_base = os.path.join(project_folder, os.path.basename(matched_folder))
1945
- os.makedirs(dst_base, exist_ok=True)
1946
-
1947
- copied_count = 0
1948
- for root, dirs, files in os.walk(matched_folder):
1949
- rel = os.path.relpath(root, matched_folder)
1950
- dest_dir = dst_base if rel == "." else os.path.join(dst_base, rel)
1951
- os.makedirs(dest_dir, exist_ok=True)
1952
- for fname in files:
1953
- if fname in (matched_filename, "sprite.json"):
1954
- continue
1955
- src = os.path.join(root, fname)
1956
- dst = os.path.join(dest_dir, fname)
1957
  try:
1958
  shutil.copy2(src, dst)
1959
- copied_count += 1
1960
  except Exception as e:
1961
- print(f"DEBUG: Failed to copy sprite asset {src} -> {dst}: {e}")
1962
-
1963
- print(f"DEBUG: copied {copied_count} asset files from sprite folder {matched_folder} -> {dst_base}")
1964
-
1965
- # Fallback: if nothing was copied, copy the matched image (at least)
1966
- if copied_count == 0:
1967
- try:
1968
- src_img = os.path.join(matched_folder, matched_filename)
1969
- if os.path.exists(src_img):
1970
- shutil.copy2(src_img, os.path.join(project_folder, matched_filename))
1971
- print(f"DEBUG: fallback copied matched sprite image to project root: {matched_filename}")
1972
- else:
1973
- print(f"DEBUG: fallback: matched image not found at {src_img}")
1974
- except Exception as e:
1975
- print(f"DEBUG: fallback copy failed for {matched_filename}: {e}")
1976
 
1977
  # If it's a backdrop (under BACKDROP_DIR) -> copy backdrop assets and read project.json for stage
1978
  # if matched_folder.startswith(backdrop_base_path) and matched_folder not in copied_backdrop_folders:
 
1830
  print("matched_indices=========================>",matched_indices)
1831
  for matched_idx in matched_indices:
1832
  matched_image_path = paths_list[matched_idx]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1833
  matched_folder = os.path.dirname(matched_image_path)
1834
  matched_filename = os.path.basename(matched_image_path)
1835
+ # --- Minimal diagnostics (drop these in prod) ---
1836
  try:
1837
  # Normalize many forms so we can compare apples-to-apples
1838
  mf_abs = os.path.normpath(os.path.abspath(matched_folder))
1839
  mf_repr = repr(matched_folder)
1840
  mf_abspath_repr = repr(mf_abs)
1841
+
1842
  # backdrop / sprite base (may be pathlib.Path in prod)
1843
  bb = os.path.normpath(os.path.abspath(str(backdrop_base_path)))
1844
  sb = os.path.normpath(os.path.abspath(str(sprite_base_path)))
1845
+
1846
  # quick membership tests
1847
  starts_with_backdrop = matched_folder.startswith(backdrop_base_path) or mf_abs.startswith(bb)
1848
  starts_with_sprite = matched_folder.startswith(sprite_base_path) or mf_abs.startswith(sb)
1849
+
1850
  # cross-platform commonpath test
1851
  try:
1852
  common_with_backdrop = (os.path.commonpath([mf_abs, bb]) == bb)
 
1856
  common_with_sprite = (os.path.commonpath([mf_abs, sb]) == sb)
1857
  except Exception:
1858
  common_with_sprite = False
1859
+
1860
  print("DEBUG matched_idx:", matched_idx)
1861
  print("DEBUG matched_image_path:", matched_image_path)
1862
  print("DEBUG matched_folder (raw):", mf_repr)
 
1871
  print("DEBUG sample paths_list entry:", repr(paths_list[matched_idx])[:400])
1872
  except Exception as _e:
1873
  print("DEBUG diagnostics failed:", _e)
1874
+ # --- end diagnostics ---
1875
  # If it's a sprite (under SPRITE_DIR) -> copy sprite assets and read sprite.json
1876
  # if matched_folder.startswith(sprite_base_path) and matched_folder not in copied_sprite_folders:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1877
  if is_subpath(matched_folder, sprite_base_path) and matched_folder not in copied_sprite_folders:
1878
  copied_sprite_folders.add(matched_folder)
1879
  sprite_json_path = os.path.join(matched_folder, "sprite.json")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1880
  if os.path.exists(sprite_json_path):
1881
  try:
1882
  with open(sprite_json_path, "r", encoding="utf-8") as f:
1883
  sprite_info = json.load(f)
1884
  project_data.append(sprite_info)
 
1885
  except Exception as e:
1886
+ print("Failed to read sprite.json in %s: %s", matched_folder, e)
1887
  else:
1888
+ print("No sprite.json in %s", matched_folder)
1889
+ # copy non-matching files from the sprite folder (except the matched image and sprite.json)
1890
+ for fname in os.listdir(matched_folder):
1891
+ if fname in (matched_filename, "sprite.json"):
1892
+ continue
1893
+ src = os.path.join(matched_folder, fname)
1894
+ dst = os.path.join(project_folder, fname)
1895
+ if os.path.isfile(src):
 
 
 
 
 
 
 
 
1896
  try:
1897
  shutil.copy2(src, dst)
 
1898
  except Exception as e:
1899
+ print("Failed to copy sprite asset %s: %s", src, e)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1900
 
1901
  # If it's a backdrop (under BACKDROP_DIR) -> copy backdrop assets and read project.json for stage
1902
  # if matched_folder.startswith(backdrop_base_path) and matched_folder not in copied_backdrop_folders: