-
Notifications
You must be signed in to change notification settings - Fork 226
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ADD] Load default libraries while ocntainer build
[FIX] Load from gallery
- Loading branch information
Showing
9 changed files
with
237 additions
and
253 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, path, library_set) | ||
for f in files: | ||
os.remove(path + '/' + f._name) | ||
|
||
|
||
def save_libs(files, path, out_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(out_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), | ||
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), | ||
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), | ||
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), | ||
parent_component=LibraryComponent.objects.get( | ||
name=svg_desc['name'], | ||
component_library=library | ||
) | ||
) | ||
alternate_component.save() |
36 changes: 36 additions & 0 deletions
36
esim-cloud-backend/libAPI/management/commands/createsuperuser_noinput.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
from django.contrib.auth import get_user_model | ||
from django.db.models import Q | ||
from django.core.management.base import BaseCommand | ||
import logging | ||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class Command(BaseCommand): | ||
help = "Create default admin user if not already present." | ||
|
||
def add_arguments(self, parser): | ||
parser.add_argument( | ||
'--username', type=str, | ||
help="username of admin account" | ||
) | ||
parser.add_argument( | ||
'--password', type=str, | ||
help="password of the admin account" | ||
) | ||
|
||
def handle(self, *args, **options): | ||
if options['username'] and options['password']: | ||
User = get_user_model() | ||
user = User.objects.filter(username=options['username']) | ||
if user.count() > 0: | ||
raise Exception(f"User with same username exists") | ||
user = User.objects.create_superuser( | ||
username=options['username'], | ||
email='', password=options['password'] | ||
) | ||
logger.info( | ||
f"Creating user {options['user']}" | ||
" with password {options['password']}") | ||
user.save() | ||
else: | ||
raise Exception("Username or Password not present") |
56 changes: 56 additions & 0 deletions
56
esim-cloud-backend/libAPI/management/commands/load_default_libs.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
from django.contrib.auth import get_user_model | ||
from django.core.management.base import BaseCommand | ||
from libAPI.lib_utils import save_libs | ||
from libAPI.models import LibrarySet | ||
import os | ||
import logging | ||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class Command(BaseCommand): | ||
help = "Load default libraries if not already present." | ||
|
||
def add_arguments(self, parser): | ||
parser.add_argument( | ||
'--username', | ||
help='input a user\'s username', type=str | ||
) | ||
parser.add_argument( | ||
'--location', type=self.dir_path, | ||
help="Directory containing kicad library files" | ||
) | ||
|
||
def dir_path(self, path): | ||
if os.path.isdir(path): | ||
return path | ||
else: | ||
raise Exception(f"{path} is not a valid path") | ||
|
||
def handle(self, *args, **options): | ||
User = get_user_model() | ||
if options['username']: | ||
user = User.objects.get(username=options['username']) | ||
else: | ||
raise Exception("Enter a superuser to associate libs") | ||
library_set = LibrarySet.objects.filter( | ||
user=user, default=True | ||
).first() | ||
if not library_set: | ||
library_set = LibrarySet( | ||
user=user, | ||
default=True, | ||
name="esim-default" | ||
) | ||
library_set.save() | ||
|
||
logger.info(f"Reading libraries from {options['location']}") | ||
|
||
if not os.path.isdir( | ||
os.path.join(options['location'], 'esim-default')): | ||
os.mkdir(os.path.join(options['location'], 'esim-default')) | ||
save_libs( | ||
os.listdir(options['location']), | ||
options['location'], | ||
os.path.join(options['location'], 'esim-default'), | ||
library_set | ||
) |
106 changes: 0 additions & 106 deletions
106
esim-cloud-backend/libAPI/management/commands/seed_libs.py
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.