-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
26 changed files
with
2,987 additions
and
2,498 deletions.
There are no files selected for viewing
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 |
---|---|---|
|
@@ -72,3 +72,5 @@ pyrebrickable-api | |
|
||
#pytest | ||
.pytest_cache/ | ||
|
||
pyrebrickable-data/rebrickable\.db |
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
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 |
---|---|---|
@@ -1,10 +1,20 @@ | ||
API Documentation | ||
API documentation | ||
================= | ||
|
||
|
||
There are two sub-APIs, the Lego and Users API, the documentation auto-generated from the swagger spec is here: | ||
|
||
.. toctree:: | ||
:maxdepth: 2 | ||
:maxdepth: 1 | ||
:caption: Contents: | ||
|
||
Lego API <../pyrebrickable-api/docs/LegoApi.md> | ||
Users API <../pyrebrickable-api/docs/UsersApi.md> | ||
Lego API <../pyrebrickable-api/docs/LegoApi> | ||
Users API <../pyrebrickable-api/docs/UsersApi> | ||
|
||
|
||
Indices and tables | ||
================== | ||
|
||
* :ref:`genindex` | ||
* :ref:`modindex` | ||
* :ref:`search` |
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,48 @@ | ||
pyrebrickable_data documentation | ||
================================ | ||
|
||
Installation | ||
------------ | ||
|
||
As part of pyrebrickable | ||
|
||
pip install pyrebrickable | ||
|
||
Or standalone | ||
|
||
pip install pyrebrickable-data | ||
|
||
|
||
Download and Import | ||
------------------- | ||
|
||
Before anything, you need to download and import the database CSV dumps into your own local database | ||
The local database will be created in your user data directory (somewhere in ~/.local/share, or %APPDATA% for Windows) | ||
The database dumps will be saved there as well. Use: | ||
|
||
python -m rebrickable_data.download | ||
python -m rebrickable_data.import | ||
|
||
This can take some time (a few minutes) | ||
|
||
Usage | ||
----- | ||
|
||
After you have downloaded and imported the data, you can use the data through the SQLAlchemy interface: | ||
|
||
from rebrickable_data.database import Session | ||
from rebrickable_data.models import Part | ||
|
||
# all the parts | ||
Session.query(Part).all() | ||
|
||
etc. | ||
|
||
|
||
|
||
Indices and tables | ||
================== | ||
|
||
* :ref:`genindex` | ||
* :ref:`modindex` | ||
* :ref:`search` |
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
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,11 @@ | ||
import os | ||
|
||
from sqlalchemy import create_engine | ||
from sqlalchemy.orm import sessionmaker | ||
|
||
from rebrickable_data.utils import data_dir | ||
|
||
db_path = os.path.join(data_dir, 'rebrickable.db') | ||
db_url = 'sqlite:///%s' % db_path | ||
engine = create_engine(db_url) | ||
Session = sessionmaker(bind=engine) |
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,25 @@ | ||
import os | ||
|
||
import requests | ||
|
||
from rebrickable_data.utils import data_dir, data_files | ||
|
||
url_format = 'https://m.rebrickable.com/media/downloads/%s.csv' | ||
|
||
|
||
def download_data_files(): | ||
for data_file in data_files: | ||
csv_path = os.path.join(data_dir, data_file + '.csv') | ||
url = url_format % data_file | ||
if not os.path.exists(csv_path): | ||
response = requests.get(url) | ||
if response.ok: | ||
with open(csv_path, 'wb') as csv_file: | ||
csv_file.write(response.content) | ||
print('OK, downloaded %s' % data_file) | ||
else: | ||
print('could not get remote data at %s' % url) | ||
|
||
|
||
if __name__ == '__main__': | ||
download_data_files() |
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,68 @@ | ||
import csv | ||
import os | ||
|
||
from sqlalchemy import inspect, Table | ||
|
||
from rebrickable_data.models import Base | ||
from rebrickable_data.utils import data_dir, data_files | ||
from rebrickable_data.database import db_url, engine, Session | ||
|
||
|
||
def get_class_by_tablename(tablename): | ||
"""Return class reference mapped to table. | ||
:param tablename: String with name of table. | ||
:return: Class reference or None. | ||
""" | ||
for c in Base._decl_class_registry.values(): | ||
if hasattr(c, '__tablename__') and c.__tablename__ == tablename: | ||
return c | ||
return Base.metadata.tables.get(tablename) | ||
|
||
|
||
def import_main(): | ||
print('') | ||
metadata = Base.metadata | ||
|
||
metadata.create_all(engine) | ||
|
||
# create a Session | ||
session = Session() | ||
|
||
for data_file in data_files: | ||
print('Importing %s ... ' % data_file, end='') | ||
|
||
model_type = get_class_by_tablename(data_file) | ||
|
||
assert model_type is not None | ||
|
||
if session.query(model_type).first(): | ||
print('There is already data in %s, not importing.' % data_file) | ||
continue | ||
|
||
csv_path = os.path.join(data_dir, data_file+'.csv') | ||
with open(csv_path, 'r') as csv_file: | ||
reader = csv.DictReader(csv_file) | ||
|
||
objects = list(reader) | ||
|
||
print(' %i rows ...' % len(objects), end='') | ||
|
||
for k in reader.fieldnames: | ||
if k.startswith('is_'): | ||
for row in objects: | ||
row[k] = row[k] == 't' | ||
|
||
if isinstance(model_type, Table): | ||
ins = model_type.insert(objects) | ||
conn = engine.connect() | ||
conn.execute(ins) | ||
else: | ||
session.bulk_insert_mappings(model_type, objects) | ||
|
||
session.commit() | ||
print('ok') | ||
|
||
|
||
if __name__ == '__main__': | ||
import_main() |
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,9 @@ | ||
from sqlalchemy.ext.declarative import declarative_base | ||
|
||
Base = declarative_base() | ||
|
||
from rebrickable_data.models.colors import * | ||
from rebrickable_data.models.inventories import * | ||
from rebrickable_data.models.parts import * | ||
from rebrickable_data.models.sets import * | ||
from rebrickable_data.models.themes import * |
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,11 @@ | ||
from sqlalchemy import Integer, Column, String, Boolean | ||
|
||
from rebrickable_data.models import Base | ||
|
||
|
||
class Color(Base): | ||
__tablename__ = 'colors' | ||
id = Column(Integer, primary_key=True) | ||
name = Column(String) | ||
rgb = Column(String) | ||
is_trans = Column(Boolean) |
Oops, something went wrong.