prthm11 commited on
Commit
21a47a8
Β·
verified Β·
1 Parent(s): 6bfa9fd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +187 -57
app.py CHANGED
@@ -1932,104 +1932,234 @@ def similarity_matching(sprites_data: dict, project_folder: str, top_k: int = 1,
1932
  # print(f" ⚠ No project.json in {matched_folder}")
1933
 
1934
  # print("---")
1935
-
 
 
 
 
 
 
 
 
 
 
 
 
1936
  for matched_idx in matched_indices:
1937
  matched_image_path = paths_list[matched_idx]
1938
- matched_folder = os.path.dirname(matched_image_path)
1939
- matched_filename = os.path.basename(matched_image_path)
1940
-
 
1941
  print(f"Processing matched image: {matched_image_path}")
1942
- print(f" - Folder: {matched_folder}")
1943
- print(f" - Sprite path: {sprite_base_path}")
1944
- print(f" - Backdrop path: {backdrop_base_path}")
1945
  print(f" - Filename: {matched_filename}")
1946
-
 
 
 
 
 
 
 
 
1947
  # If it's a sprite (under SPRITE_DIR) -> copy sprite assets and read sprite.json
1948
- if matched_folder.startswith(sprite_base_path) and matched_folder not in copied_sprite_folders:
1949
- print(f"Processing SPRITE folder: {matched_folder}")
1950
- copied_sprite_folders.add(matched_folder)
1951
- sprite_json_path = os.path.join(matched_folder, "sprite.json")
1952
- print("sprite_json_path----------------------->",sprite_json_path)
1953
- print("copied sprite folder----------------------->",copied_sprite_folders)
1954
- if os.path.exists(sprite_json_path):
1955
  try:
1956
- with open(sprite_json_path, "r", encoding="utf-8") as f:
1957
  sprite_info = json.load(f)
1958
  project_data.append(sprite_info)
1959
- print(f" βœ“ Successfully read sprite.json from {matched_folder}")
1960
  except Exception as e:
1961
- print(f" βœ— Failed to read sprite.json in {matched_folder}: {e}")
1962
  else:
1963
- print(f" ⚠ No sprite.json in {matched_folder}")
1964
-
1965
  # copy non-matching files from the sprite folder (except the matched image and sprite.json)
1966
- sprite_files = os.listdir(matched_folder)
1967
- print(f" Files in sprite folder: {sprite_files}")
1968
- for fname in sprite_files:
 
 
 
 
 
 
1969
  if fname in (matched_filename, "sprite.json"):
1970
  print(f" Skipping {fname} (matched image or sprite.json)")
1971
  continue
1972
- src = os.path.join(matched_folder, fname)
1973
- dst = os.path.join(project_folder, fname)
1974
- if os.path.isfile(src):
1975
  try:
1976
- shutil.copy2(src, dst)
1977
- print(f" βœ“ Copied sprite asset: {src} -> {dst}")
1978
  except Exception as e:
1979
- print(f" βœ— Failed to copy sprite asset {src}: {e}")
1980
  else:
1981
  print(f" Skipping {fname} (not a file)")
1982
-
1983
  # If it's a backdrop (under BACKDROP_DIR) -> copy backdrop assets and read project.json for stage
1984
- if matched_folder.startswith(backdrop_base_path) and matched_folder not in copied_backdrop_folders:
1985
- print(f"Processing BACKDROP folder: {matched_folder}")
1986
- copied_backdrop_folders.add(matched_folder)
1987
- print("backdrop_base_path----------------------->",backdrop_base_path)
1988
- print("copied backdrop folder----------------------->",copied_backdrop_folders)
1989
  # copy matched backdrop image
1990
- backdrop_dst = os.path.join(project_folder, matched_filename)
 
1991
  try:
1992
- shutil.copy2(matched_image_path, backdrop_dst)
1993
- print(f" βœ“ Copied matched backdrop image: {matched_image_path} -> {backdrop_dst}")
1994
  except Exception as e:
1995
- print(f" βœ— Failed to copy matched backdrop image {matched_image_path}: {e}")
1996
-
1997
  # copy other files from folder (skip project.json and matched image)
1998
- backdrop_files = os.listdir(matched_folder)
1999
- print(f" Files in backdrop folder: {backdrop_files}")
2000
- for fname in backdrop_files:
 
 
 
 
 
 
2001
  if fname in (matched_filename, "project.json"):
2002
  print(f" Skipping {fname} (matched image or project.json)")
2003
  continue
2004
- src = os.path.join(matched_folder, fname)
2005
- dst = os.path.join(project_folder, fname)
2006
- if os.path.isfile(src):
2007
  try:
2008
- shutil.copy2(src, dst)
2009
- print(f" βœ“ Copied backdrop asset: {src} -> {dst}")
2010
  except Exception as e:
2011
- print(f" βœ— Failed to copy backdrop asset {src}: {e}")
2012
  else:
2013
  print(f" Skipping {fname} (not a file)")
2014
-
2015
  # read project.json to extract Stage/targets
2016
- pj = os.path.join(matched_folder, "project.json")
2017
- if os.path.exists(pj):
2018
  try:
2019
- with open(pj, "r", encoding="utf-8") as f:
2020
  bd_json = json.load(f)
2021
  stage_count = 0
2022
  for tgt in bd_json.get("targets", []):
2023
  if tgt.get("isStage"):
2024
  backdrop_data.append(tgt)
2025
  stage_count += 1
2026
- print(f" βœ“ Successfully read project.json from {matched_folder}, found {stage_count} stage(s)")
2027
  except Exception as e:
2028
- print(f" βœ— Failed to read project.json in {matched_folder}: {e}")
2029
  else:
2030
- print(f" ⚠ No project.json in {matched_folder}")
2031
-
2032
  print("---")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2033
 
2034
  # --- Merge into final Scratch project.json (identical logic to before)
2035
  final_project = {
 
1932
  # print(f" ⚠ No project.json in {matched_folder}")
1933
 
1934
  # print("---")
1935
+ # add at top of file
1936
+ from pathlib import Path
1937
+
1938
+ # ensure base paths are normalized once before the loop
1939
+ sprite_base_p = Path(sprite_base_path).resolve(strict=False)
1940
+ backdrop_base_p = Path(backdrop_base_path).resolve(strict=False)
1941
+ project_folder_p = Path(project_folder)
1942
+ project_folder_p.mkdir(parents=True, exist_ok=True)
1943
+
1944
+ # convert copied sets to use resolved strings
1945
+ copied_sprite_folders = set()
1946
+ copied_backdrop_folders = set()
1947
+
1948
  for matched_idx in matched_indices:
1949
  matched_image_path = paths_list[matched_idx]
1950
+ matched_path_p = Path(matched_image_path)
1951
+ matched_folder_p = matched_path_p.parent.resolve(strict=False)
1952
+ matched_filename = matched_path_p.name
1953
+
1954
  print(f"Processing matched image: {matched_image_path}")
1955
+ print(f" - Folder: {matched_folder_p}") # now resolved
1956
+ print(f" - Sprite path: {sprite_base_p}")
1957
+ print(f" - Backdrop path: {backdrop_base_p}")
1958
  print(f" - Filename: {matched_filename}")
1959
+
1960
+ # helper: robust subpath check
1961
+ def is_subpath(child: Path, parent: Path) -> bool:
1962
+ try:
1963
+ child.relative_to(parent)
1964
+ return True
1965
+ except Exception:
1966
+ return False
1967
+
1968
  # If it's a sprite (under SPRITE_DIR) -> copy sprite assets and read sprite.json
1969
+ if is_subpath(matched_folder_p, sprite_base_p) and str(matched_folder_p) not in copied_sprite_folders:
1970
+ print(f"Processing SPRITE folder: {matched_folder_p}")
1971
+ copied_sprite_folders.add(str(matched_folder_p))
1972
+ sprite_json_path = matched_folder_p / "sprite.json"
1973
+ print("sprite_json_path----------------------->", sprite_json_path)
1974
+ print("copied sprite folder----------------------->", copied_sprite_folders)
1975
+ if sprite_json_path.exists():
1976
  try:
1977
+ with sprite_json_path.open("r", encoding="utf-8") as f:
1978
  sprite_info = json.load(f)
1979
  project_data.append(sprite_info)
1980
+ print(f" βœ“ Successfully read sprite.json from {matched_folder_p}")
1981
  except Exception as e:
1982
+ print(f" βœ— Failed to read sprite.json in {matched_folder_p}: {repr(e)}")
1983
  else:
1984
+ print(f" ⚠ No sprite.json in {matched_folder_p}")
1985
+
1986
  # copy non-matching files from the sprite folder (except the matched image and sprite.json)
1987
+ try:
1988
+ sprite_files = list(matched_folder_p.iterdir())
1989
+ except Exception as e:
1990
+ sprite_files = []
1991
+ print(f" βœ— Failed to list files in {matched_folder_p}: {repr(e)}")
1992
+
1993
+ print(f" Files in sprite folder: {[p.name for p in sprite_files]}")
1994
+ for p in sprite_files:
1995
+ fname = p.name
1996
  if fname in (matched_filename, "sprite.json"):
1997
  print(f" Skipping {fname} (matched image or sprite.json)")
1998
  continue
1999
+ if p.is_file():
2000
+ dst = project_folder_p / fname
 
2001
  try:
2002
+ shutil.copy2(str(p), str(dst))
2003
+ print(f" βœ“ Copied sprite asset: {p} -> {dst}")
2004
  except Exception as e:
2005
+ print(f" βœ— Failed to copy sprite asset {p}: {repr(e)}")
2006
  else:
2007
  print(f" Skipping {fname} (not a file)")
2008
+
2009
  # If it's a backdrop (under BACKDROP_DIR) -> copy backdrop assets and read project.json for stage
2010
+ if is_subpath(matched_folder_p, backdrop_base_p) and str(matched_folder_p) not in copied_backdrop_folders:
2011
+ print(f"Processing BACKDROP folder: {matched_folder_p}")
2012
+ copied_backdrop_folders.add(str(matched_folder_p))
2013
+ print("backdrop_base_path----------------------->", backdrop_base_p)
2014
+ print("copied backdrop folder----------------------->", copied_backdrop_folders)
2015
  # copy matched backdrop image
2016
+ backdrop_src = matched_folder_p / matched_filename
2017
+ backdrop_dst = project_folder_p / matched_filename
2018
  try:
2019
+ shutil.copy2(str(backdrop_src), str(backdrop_dst))
2020
+ print(f" βœ“ Copied matched backdrop image: {backdrop_src} -> {backdrop_dst}")
2021
  except Exception as e:
2022
+ print(f" βœ— Failed to copy matched backdrop image {backdrop_src}: {repr(e)}")
2023
+
2024
  # copy other files from folder (skip project.json and matched image)
2025
+ try:
2026
+ backdrop_files = list(matched_folder_p.iterdir())
2027
+ except Exception as e:
2028
+ backdrop_files = []
2029
+ print(f" βœ— Failed to list files in {matched_folder_p}: {repr(e)}")
2030
+
2031
+ print(f" Files in backdrop folder: {[p.name for p in backdrop_files]}")
2032
+ for p in backdrop_files:
2033
+ fname = p.name
2034
  if fname in (matched_filename, "project.json"):
2035
  print(f" Skipping {fname} (matched image or project.json)")
2036
  continue
2037
+ if p.is_file():
2038
+ dst = project_folder_p / fname
 
2039
  try:
2040
+ shutil.copy2(str(p), str(dst))
2041
+ print(f" βœ“ Copied backdrop asset: {p} -> {dst}")
2042
  except Exception as e:
2043
+ print(f" βœ— Failed to copy backdrop asset {p}: {repr(e)}")
2044
  else:
2045
  print(f" Skipping {fname} (not a file)")
2046
+
2047
  # read project.json to extract Stage/targets
2048
+ pj = matched_folder_p / "project.json"
2049
+ if pj.exists():
2050
  try:
2051
+ with pj.open("r", encoding="utf-8") as f:
2052
  bd_json = json.load(f)
2053
  stage_count = 0
2054
  for tgt in bd_json.get("targets", []):
2055
  if tgt.get("isStage"):
2056
  backdrop_data.append(tgt)
2057
  stage_count += 1
2058
+ print(f" βœ“ Successfully read project.json from {matched_folder_p}, found {stage_count} stage(s)")
2059
  except Exception as e:
2060
+ print(f" βœ— Failed to read project.json in {matched_folder_p}: {repr(e)}")
2061
  else:
2062
+ print(f" ⚠ No project.json in {matched_folder_p}")
2063
+
2064
  print("---")
2065
+
2066
+ # for matched_idx in matched_indices:
2067
+ # matched_image_path = paths_list[matched_idx]
2068
+ # matched_folder = os.path.dirname(matched_image_path)
2069
+ # matched_filename = os.path.basename(matched_image_path)
2070
+
2071
+ # print(f"Processing matched image: {matched_image_path}")
2072
+ # print(f" - Folder: {matched_folder}")
2073
+ # print(f" - Sprite path: {sprite_base_path}")
2074
+ # print(f" - Backdrop path: {backdrop_base_path}")
2075
+ # print(f" - Filename: {matched_filename}")
2076
+
2077
+ # # If it's a sprite (under SPRITE_DIR) -> copy sprite assets and read sprite.json
2078
+ # if matched_folder.startswith(sprite_base_path) and matched_folder not in copied_sprite_folders:
2079
+ # print(f"Processing SPRITE folder: {matched_folder}")
2080
+ # copied_sprite_folders.add(matched_folder)
2081
+ # sprite_json_path = os.path.join(matched_folder, "sprite.json")
2082
+ # print("sprite_json_path----------------------->",sprite_json_path)
2083
+ # print("copied sprite folder----------------------->",copied_sprite_folders)
2084
+ # if os.path.exists(sprite_json_path):
2085
+ # try:
2086
+ # with open(sprite_json_path, "r", encoding="utf-8") as f:
2087
+ # sprite_info = json.load(f)
2088
+ # project_data.append(sprite_info)
2089
+ # print(f" βœ“ Successfully read sprite.json from {matched_folder}")
2090
+ # except Exception as e:
2091
+ # print(f" βœ— Failed to read sprite.json in {matched_folder}: {e}")
2092
+ # else:
2093
+ # print(f" ⚠ No sprite.json in {matched_folder}")
2094
+
2095
+ # # copy non-matching files from the sprite folder (except the matched image and sprite.json)
2096
+ # sprite_files = os.listdir(matched_folder)
2097
+ # print(f" Files in sprite folder: {sprite_files}")
2098
+ # for fname in sprite_files:
2099
+ # if fname in (matched_filename, "sprite.json"):
2100
+ # print(f" Skipping {fname} (matched image or sprite.json)")
2101
+ # continue
2102
+ # src = os.path.join(matched_folder, fname)
2103
+ # dst = os.path.join(project_folder, fname)
2104
+ # if os.path.isfile(src):
2105
+ # try:
2106
+ # shutil.copy2(src, dst)
2107
+ # print(f" βœ“ Copied sprite asset: {src} -> {dst}")
2108
+ # except Exception as e:
2109
+ # print(f" βœ— Failed to copy sprite asset {src}: {e}")
2110
+ # else:
2111
+ # print(f" Skipping {fname} (not a file)")
2112
+
2113
+ # # If it's a backdrop (under BACKDROP_DIR) -> copy backdrop assets and read project.json for stage
2114
+ # if matched_folder.startswith(backdrop_base_path) and matched_folder not in copied_backdrop_folders:
2115
+ # print(f"Processing BACKDROP folder: {matched_folder}")
2116
+ # copied_backdrop_folders.add(matched_folder)
2117
+ # print("backdrop_base_path----------------------->",backdrop_base_path)
2118
+ # print("copied backdrop folder----------------------->",copied_backdrop_folders)
2119
+ # # copy matched backdrop image
2120
+ # backdrop_dst = os.path.join(project_folder, matched_filename)
2121
+ # try:
2122
+ # shutil.copy2(matched_image_path, backdrop_dst)
2123
+ # print(f" βœ“ Copied matched backdrop image: {matched_image_path} -> {backdrop_dst}")
2124
+ # except Exception as e:
2125
+ # print(f" βœ— Failed to copy matched backdrop image {matched_image_path}: {e}")
2126
+
2127
+ # # copy other files from folder (skip project.json and matched image)
2128
+ # backdrop_files = os.listdir(matched_folder)
2129
+ # print(f" Files in backdrop folder: {backdrop_files}")
2130
+ # for fname in backdrop_files:
2131
+ # if fname in (matched_filename, "project.json"):
2132
+ # print(f" Skipping {fname} (matched image or project.json)")
2133
+ # continue
2134
+ # src = os.path.join(matched_folder, fname)
2135
+ # dst = os.path.join(project_folder, fname)
2136
+ # if os.path.isfile(src):
2137
+ # try:
2138
+ # shutil.copy2(src, dst)
2139
+ # print(f" βœ“ Copied backdrop asset: {src} -> {dst}")
2140
+ # except Exception as e:
2141
+ # print(f" βœ— Failed to copy backdrop asset {src}: {e}")
2142
+ # else:
2143
+ # print(f" Skipping {fname} (not a file)")
2144
+
2145
+ # # read project.json to extract Stage/targets
2146
+ # pj = os.path.join(matched_folder, "project.json")
2147
+ # if os.path.exists(pj):
2148
+ # try:
2149
+ # with open(pj, "r", encoding="utf-8") as f:
2150
+ # bd_json = json.load(f)
2151
+ # stage_count = 0
2152
+ # for tgt in bd_json.get("targets", []):
2153
+ # if tgt.get("isStage"):
2154
+ # backdrop_data.append(tgt)
2155
+ # stage_count += 1
2156
+ # print(f" βœ“ Successfully read project.json from {matched_folder}, found {stage_count} stage(s)")
2157
+ # except Exception as e:
2158
+ # print(f" βœ— Failed to read project.json in {matched_folder}: {e}")
2159
+ # else:
2160
+ # print(f" ⚠ No project.json in {matched_folder}")
2161
+
2162
+ # print("---")
2163
 
2164
  # --- Merge into final Scratch project.json (identical logic to before)
2165
  final_project = {