Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jacoblukewood authored Feb 6, 2022
1 parent 183f726 commit 3509198
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 0 deletions.
46 changes: 46 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,48 @@
# cseconvert

cseconvert is a cross platform Python utility script that extracts/converts SamacSys Library Loader/Component Search Engine files for use in PCB editor software.

## Requirements

* Python 2.7
* 'requests' package
* Internet access to https://componentsearchengine.com/
* A Component Search Engine account (free to register at https://componentsearchengine.com/register)

## Installation

Use the package manager [pip](https://pip.pypa.io/en/stable/) to install requests if not already installed

```sh
pip install requests
```

Make executable
```sh
chmod a+rx cseconvert
```

Move to your bin folder (method varies by operating system) (optional)
```sh
sudo mv cseconvert /usr/local/bin
```

## Usage

Extract to the current directory
```sh
cseconvert input_zip_file.zip
```

Extract to a specified directory
```sh
cseconvert input_zip_file.zip ~/Users/jacoblukewood/target_directory
```

## Contributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

## License
[MIT](https://choosealicense.com/licenses/mit/)
45 changes: 45 additions & 0 deletions cseconvert
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/usr/bin/env python2.7

"""Extracts/Converts SamacSys Library Loader/Component Search Engine files for use in PCB editor software.
Pass the .zip file to extract/convert as the first argument.
The default output is to a new directory the same directory as the input file. An alternate directory may be supplied as a second argument.
See https://github.com/jacoblukewood/cseconvert"""

import sys # standard library
import zipfile
from io import BytesIO

import requests # 3rd party packages

# Component Search Engine account details (register at https://componentsearchengine.com/register)
EMAIL = ''
PASSWORD = ''


if len(sys.argv) < 2:
print("""usage: cseconvert input_zip_file
cseconvert input_zip_file ... target_directory""")
exit()


if not EMAIL or not PASSWORD:
print("""Script not configured!
Enter your Component Search Engine account details on line 14 and 15 inside the source of this script.
You may register an account at https://componentsearchengine.com/register""")
exit()

zip_file = zipfile.ZipFile(sys.argv[1]) # get ZIP file path inputted
zip_files_list = zip_file.namelist() # get list of files in zip_file
epwfile = [x for x in zip_files_list if x[-4:] == ".epw"][0] # Check the file extention of the ZIP contents to find the .epw file

f = zip_file.open(epwfile) # extract the EPW file to memory
part_id = f.readline() # get the part ID which is stored on line 0
url = "https://componentsearchengine.com/ga/model.php?partID=" + str(part_id) # generate URL

raw_cse_response = requests.get(url, auth=(EMAIL, PASSWORD)) # send request and get raw response
compressed_cse_response = zipfile.ZipFile(BytesIO(raw_cse_response.content)) # set file structure to ZIP

# Extract the ZIP
for file in compressed_cse_response.namelist():
if '/' in file:
compressed_cse_response.extract(file, sys.argv[2] if len(sys.argv) > 2 else sys.argv[1][:-4]) # extract to same directory as input unless a second argument is supplied

0 comments on commit 3509198

Please sign in to comment.