Skip to content

Commit

Permalink
[FIX] Code reuse improved
Browse files Browse the repository at this point in the history
  • Loading branch information
kumanik committed Jun 1, 2021
1 parent 590a67f commit b78227e
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 233 deletions.
11 changes: 4 additions & 7 deletions esim-cloud-backend/libAPI/admin.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
from libAPI.lib_utils import handle_uploaded_libs
from django.contrib import admin, messages
from django.contrib.auth import get_user_model
from libAPI.models import LibraryComponent, \
Library, \
LibrarySet, \
FavouriteComponent, \
delete_uploaded_files, \
save_libs
from .forms import LibrarySetForm
FavouriteComponent
from libAPI.forms import LibrarySetForm
from inline_actions.admin import InlineActionsMixin
from inline_actions.admin import InlineActionsModelAdminMixin
from django.shortcuts import redirect
Expand Down Expand Up @@ -95,9 +94,7 @@ def save_model(self, request, obj, form, change):
settings.BASE_DIR,
'kicad-symbols',
obj.user.username + '-' + obj.name)

save_libs(obj, path, files) # defined in ./models.py
delete_uploaded_files(files, path)
handle_uploaded_libs(obj, path, files) # defined in ./lib_utils.py
return redirect('/api/admin/libAPI/libraryset/' + str(obj.id))


Expand Down
125 changes: 125 additions & 0 deletions esim-cloud-backend/libAPI/lib_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
from libAPI.helper.main import generate_svg_and_save_to_folder
from libAPI.models import Library, LibraryComponent, ComponentAlternate
import os
import glob


def save_uploaded_files(files, path):
for f in files:
filepath = os.path.join(path, f._name)
with open(filepath, 'wb') as dest:
for chunk in f.chunks():
dest.write(chunk)


def handle_uploaded_libs(library_set, path, files):
if not os.path.isdir(path):
os.mkdir(path)
save_uploaded_files(files, path)
filenames = []
for f in files:
filenames.append(f._name)
save_libs(filenames, path, library_set)
for f in files:
os.remove(path + '/' + f._name)


def save_libs(files, path, library_set):
for f in files:
if '.dcm' in f:
flag = 0
for f1 in files:
if f1[:-4] == f[:-4] and '.lib' in f1:
flag = 1
if flag == 0:
raise FileNotFoundError(
f'.lib file for {f} does not exist')
if '.lib' in f:
lib_output_location = os.path.join(path, 'symbol-svgs')
lib_location = os.path.join(path, f)
component_details = generate_svg_and_save_to_folder(
lib_location,
lib_output_location
)
try:
library = Library.objects.get(
library_name=f, library_set=library_set)
except Library.DoesNotExist:
library = Library(
library_name=f,
library_set=library_set
)
library.save()

library_svg_folder = os.path.join(
lib_output_location, f[:-4])
thumbnails = glob.glob(library_svg_folder + '/*_thumbnail.svg')

for component_svg in glob.glob(library_svg_folder + '/*-1-A.svg'):
thumbnail_path = component_svg[:-4] + '_thumbnail.svg'
if thumbnail_path not in thumbnails:
raise FileNotFoundError(
f'Thumbnail does not exist for {component_svg}')

# Get Component name
component_svg = os.path.split(component_svg)[-1]

# Get Corresponding Details
svg_desc = component_details[component_svg[:-4]]

# Seed DB
try:
component = LibraryComponent.objects.get(
name=svg_desc['name'],
svg_path=os.path.join(
library_svg_folder, component_svg)[6:],
thumbnail_path=thumbnail_path,
symbol_prefix=svg_desc['symbol_prefix'],
full_name=svg_desc['full_name'],
keyword=svg_desc['keyword'],
description=svg_desc['description'],
data_link=svg_desc['data_link'],
component_library=library
)
except LibraryComponent.DoesNotExist:
component = LibraryComponent(
name=svg_desc['name'],
svg_path=os.path.join(
library_svg_folder, component_svg)[6:],
thumbnail_path=thumbnail_path,
symbol_prefix=svg_desc['symbol_prefix'],
full_name=svg_desc['full_name'],
keyword=svg_desc['keyword'],
description=svg_desc['description'],
data_link=svg_desc['data_link'],
component_library=library
)
component.save()

# Seed Alternate Components
for component_svg in glob.glob(library_svg_folder + '/*[B-Z].svg'):
component_svg = os.path.split(component_svg)[-1]
svg_desc = component_details[component_svg[:-4]]
try:
alternate_component = ComponentAlternate.objects.get(
part=svg_desc['part'], dmg=svg_desc['dmg'],
full_name=svg_desc['full_name'],
svg_path=os.path.join(
library_svg_folder, component_svg)[6:],
parent_component=LibraryComponent.objects.get(
name=svg_desc['name'],
component_library=library
)
)
except ComponentAlternate.DoesNotExist:
alternate_component = ComponentAlternate(
part=svg_desc['part'], dmg=svg_desc['dmg'],
full_name=svg_desc['full_name'],
svg_path=os.path.join(
library_svg_folder, component_svg)[6:],
parent_component=LibraryComponent.objects.get(
name=svg_desc['name'],
component_library=library
)
)
alternate_component.save()
107 changes: 10 additions & 97 deletions esim-cloud-backend/libAPI/management/commands/load_default_libs.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@
from libAPI.helper.main import generate_svg_and_save_to_folder
import os
import glob

from django.core.management import call_command
from django.contrib.admin import options
from django.contrib.auth import get_user_model
from django.core.files.uploadedfile import InMemoryUploadedFile
from django.core.management.base import BaseCommand

from libAPI.models import Library, LibraryComponent, \
ComponentAlternate, LibrarySet

from libAPI.lib_utils import save_libs
from libAPI.models import LibrarySet
import os
import logging
logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -50,91 +42,12 @@ def handle(self, *args, **options):
name="esim-default"
)
library_set.save()
seed_libraries(self, options['location'], library_set)

logger.info(f"Reading libraries from {options['location']}")

def seed_libraries(self, location, library_set):
logger.info(f"Reading libraries from {location}")
if not os.path.isdir(os.path.join(location, 'default')):
os.mkdir(os.path.join(location, 'default'))
for file in os.listdir(location):
if '.lib' in file:
self.stdout.write(f'Processing {file}')
lib_location = os.path.join(location, file)
lib_output_location = os.path.join(
location, 'default', 'symbol_svgs'
)
component_details = generate_svg_and_save_to_folder(
lib_location,
lib_output_location
)
library = Library.objects.filter(
library_name=file, library_set=library_set).first()
if not library:
library = Library(
library_name=file,
library_set=library_set
)
library.save()
logger.info('Created Library Object')
library_svg_folder = os.path.join(lib_output_location, file[:-4])
thumbnails = glob.glob(library_svg_folder+'/*_thumbnail.svg')

# Seed Primary component
for component_svg in glob.glob(library_svg_folder+'/*-1-A.svg'):
thumbnail_path = component_svg[:-4]+'_thumbnail.svg'
if thumbnail_path not in thumbnails:
raise FileNotFoundError(f'Thumbnail Does not exist for {component_svg}') # noqa

# Get Component name
component_svg = os.path.split(component_svg)[-1]

# Get Corresponding Details
svg_desc = component_details[component_svg[:-4]]

# Seed DB
try:
component = LibraryComponent.objects.get(
name=svg_desc['name'],
svg_path=os.path.join(
library_svg_folder, component_svg)[6:],
thumbnail_path=thumbnail_path,
symbol_prefix=svg_desc['symbol_prefix'],
full_name=svg_desc['full_name'],
keyword=svg_desc['keyword'],
description=svg_desc['description'],
data_link=svg_desc['data_link'],
component_library=library
)
except LibraryComponent.DoesNotExist:
component = LibraryComponent(
name=svg_desc['name'],
svg_path=os.path.join(
library_svg_folder, component_svg)[6:],
thumbnail_path=thumbnail_path,
symbol_prefix=svg_desc['symbol_prefix'],
full_name=svg_desc['full_name'],
keyword=svg_desc['keyword'],
description=svg_desc['description'],
data_link=svg_desc['data_link'],
component_library=library
)
component.save()
logger.info(f'Saved component {component_svg}')

# Seed Alternate Components
for component_svg in glob.glob(library_svg_folder+'/*[B-Z].svg'): # noqa , EdgeCase here
component_svg = os.path.split(component_svg)[-1]
svg_desc = component_details[component_svg[:-4]]
alternate_component = ComponentAlternate(
part=svg_desc['part'],
dmg=svg_desc['dmg'],
full_name=svg_desc['full_name'],
svg_path=os.path.join(
library_svg_folder, component_svg),
parent_component=LibraryComponent.objects.get(
name=svg_desc['name'],
)
)
alternate_component.save()
logger.info(f'Saved alternate component {component_svg}')
if not os.path.isdir(os.path.join(options['location'], 'default')):
os.mkdir(os.path.join(options['location'], 'default'))
save_libs(
os.listdir(options['location']),
options['location'], library_set
)
Loading

0 comments on commit b78227e

Please sign in to comment.