Skip to content

Commit

Permalink
Merge branch 'directory-fix' of https://github.com/4b11b4/KiField int…
Browse files Browse the repository at this point in the history
…o fixes
  • Loading branch information
Dave Vandenbout committed Feb 8, 2019
2 parents d89afce + 3b529d9 commit 2e85f35
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions kifield/kifield.py
Expand Up @@ -832,7 +832,7 @@ def insert_part_fields_into_wb(part_fields_dict, wb, recurse=False):
return wb


def insert_part_fields_into_xlsx(part_fields_dict, filename, recurse, group_components, backup):
def insert_part_fields_into_xlsx(part_fields_dict, filename, recurse, group_components, backup, prepend_dir):
'''Insert the fields in the extracted part dictionary into an XLSX spreadsheet.'''

logger.log(
Expand All @@ -856,7 +856,7 @@ def insert_part_fields_into_xlsx(part_fields_dict, filename, recurse, group_comp
wb.save(filename)


def insert_part_fields_into_csv(part_fields_dict, filename, recurse, group_components, backup):
def insert_part_fields_into_csv(part_fields_dict, filename, recurse, group_components, backup, prepend_dir):
'''Insert the fields in the extracted part dictionary into a CSV spreadsheet.'''

logger.log(DEBUG_OVERVIEW,
Expand All @@ -883,7 +883,7 @@ def insert_part_fields_into_csv(part_fields_dict, filename, recurse, group_compo
wb_to_csvfile(wb, filename, dialect)


def insert_part_fields_into_sch(part_fields_dict, filename, recurse, group_components, backup):
def insert_part_fields_into_sch(part_fields_dict, filename, recurse, group_components, backup, prepend_dir):
'''Insert the fields in the extracted part dictionary into a schematic.'''

logger.log(
Expand Down Expand Up @@ -1034,14 +1034,18 @@ def reorder_sch_fields(fields):
# If this schematic references other schematic sheets, then insert the part fields into those, too.
if recurse:
for sheet in sch.sheets:
# If filename includes a path, save this path to prepend below
if filename.count('/') > 0:
prepend_dir = filename.rsplit('/', 1)[0] + '/'
for field in sheet.fields:
if field['id'] == 'F1':
sheet_file = unquote(field['value'])
insert_part_fields_into_sch(part_fields_dict, sheet_file, recurse, group_components, backup)
# Prepend path for sheets which are nested more than once
sheet_file = prepend_dir + unquote(field['value'])
insert_part_fields_into_sch(part_fields_dict, sheet_file, recurse, group_components, backup, prepend_dir)
break


def insert_part_fields_into_lib(part_fields_dict, filename, recurse, group_components, backup):
def insert_part_fields_into_lib(part_fields_dict, filename, recurse, group_components, backup, prepend_dir):
'''Insert the fields in the extracted part dictionary into a library.'''

logger.log(
Expand Down Expand Up @@ -1127,7 +1131,7 @@ def insert_part_fields_into_lib(part_fields_dict, filename, recurse, group_compo
lib.save(filename)


def insert_part_fields_into_dcm(part_fields_dict, filename, recurse, group_components, backup):
def insert_part_fields_into_dcm(part_fields_dict, filename, recurse, group_components, backup, prepend_dir):
'''Insert the fields in the extracted part dictionary into a DCM file.'''

logger.log(
Expand Down Expand Up @@ -1157,7 +1161,7 @@ def insert_part_fields_into_dcm(part_fields_dict, filename, recurse, group_compo
dcm.save(filename)


def insert_part_fields(part_fields_dict, filenames, recurse, group_components, backup):
def insert_part_fields(part_fields_dict, filenames, recurse, group_components, backup, prepend_dir):
'''Insert part fields from a dictionary into a spreadsheet, part library, or schematic.'''

# No files backed-up yet, so clear list of file names.
Expand Down Expand Up @@ -1192,7 +1196,7 @@ def insert_part_fields(part_fields_dict, filenames, recurse, group_components, b

# Call the insertion function based on the file extension.
f_extension = os.path.splitext(f)[1].lower()
insertion_functions[f_extension](part_fields_dict, f, recurse, group_components, backup)
insertion_functions[f_extension](part_fields_dict, f, recurse, group_components, backup, prepend_dir)

except IOError:
logger.warn('Unable to write to file: {}.'.format(f))
Expand All @@ -1201,11 +1205,11 @@ def insert_part_fields(part_fields_dict, filenames, recurse, group_components, b
logger.warn('Unknown file type for field insertion: {}'.format(f))


def kifield(extract_filenames, insert_filenames, inc_field_names=None, exc_field_names=None, recurse=False, group_components=False, backup=True):
def kifield(extract_filenames, insert_filenames, inc_field_names=None, exc_field_names=None, recurse=False, group_components=False, backup=True, prepend_dir='./'):
'''Extract fields from a set of files and insert them into another set of files.'''

# Extract a dictionary of part field values from a set of files.
part_fields_dict = extract_part_fields(extract_filenames, inc_field_names, exc_field_names, recurse)

# Insert entries from the dictionary into these files.
insert_part_fields(part_fields_dict, insert_filenames, recurse, group_components, backup)
insert_part_fields(part_fields_dict, insert_filenames, recurse, group_components, backup, prepend_dir)

0 comments on commit 2e85f35

Please sign in to comment.