Skip to content

Commit

Permalink
Merge pull request #27 from thombashi/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
thombashi committed Jun 3, 2016
2 parents dfd7efb + acf84e3 commit cf49181
Show file tree
Hide file tree
Showing 16 changed files with 250 additions and 252 deletions.
6 changes: 3 additions & 3 deletions README.rst
@@ -1,5 +1,5 @@
SimpleSQLite
=============
============

.. image:: https://img.shields.io/pypi/pyversions/SimpleSQLite.svg
:target: https://pypi.python.org/pypi/SimpleSQLite
Expand Down Expand Up @@ -175,10 +175,10 @@ Insert list/tuple/namedtuple
For more information
--------------------

More examples are available at
http://simplesqlite.readthedocs.org/en/latest/pages/examples/index.html


Installation
============

Expand Down Expand Up @@ -214,7 +214,7 @@ Documentation
http://simplesqlite.readthedocs.org/en/latest/

Related project
==========================
===============

- sqlitebiter: CLI tool to create a SQLite database from CSV/JSON/Excel/Google-Sheets by using SimpleSQLite
- https://github.com/thombashi/sqlitebiter
Expand Down
139 changes: 43 additions & 96 deletions docs/make_readme.py
@@ -1,122 +1,69 @@
#!/usr/bin/env python
# encoding: utf-8

import os
import re
"""
.. codeauthor:: Tsuyoshi Hombashi <gogogo.vm@gmail.com>
"""

import sys

import readmemaker


VERSION = "0.4.3"
PROJECT_NAME = "SimpleSQLite"
OUTPUT_DIR = ".."
README_WORK_DIR = "."
DOC_PAGE_DIR = os.path.join(README_WORK_DIR, "pages")


def get_usage_file_path(filename):
return os.path.join(DOC_PAGE_DIR, "examples", filename)
def write_examples(maker):
maker.set_indent_level(0)
maker.write_chapter("Examples")

maker.inc_indent_level()
maker.write_chapter("Create a table")
maker.inc_indent_level()
maker.write_chapter("Create a table from data matrix")
maker.write_example_file("create_table_from_data_matrix.txt")

def replace_for_pypi(line):
line = line.replace(".. code-block::", ".. code::")
line = line.replace(".. code:: none", ".. code::")
maker.dec_indent_level()
maker.write_chapter("Insert records into a table")
maker.write_example_file("insert_record_example.txt")

return line
maker.write_chapter("For more information")
maker.write_line_list([
"More examples are available at ",
"http://%s.readthedocs.org/en/latest/pages/examples/index.html" % (
PROJECT_NAME.lower()),
])


def write_line_list(f, line_list):
f.write("\n".join([
replace_for_pypi(line)
for line in line_list
if re.search(":caption:", line) is None
]))
f.write("\n" * 2)
def main():
maker = readmemaker.ReadmeMaker(PROJECT_NAME, OUTPUT_DIR)

maker.write_introduction_file("badges.txt")

def write_usage_file(f, filename):
with open(get_usage_file_path(filename)) as f_usage_file:
write_line_list(
f, [line.rstrip()for line in f_usage_file.readlines()])
maker.inc_indent_level()
maker.write_chapter("Summary")
maker.write_introduction_file("summary.txt")
maker.write_introduction_file("feature.txt")

write_examples(maker)

def write_examples(f):
write_line_list(f, [
"Examples",
"========",
"",
"Create a table",
"--------------",
"",
"Create a table from data matrix",
"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~",
])
maker.write_file(
maker.doc_page_root_dir_path.joinpath("installation.rst"))

write_usage_file(f, "create_table_from_data_matrix.txt")

write_line_list(f, [
"Insert records into a table",
"---------------------------",
maker.set_indent_level(0)
maker.write_chapter("Documentation")
maker.write_line_list([
"http://%s.readthedocs.org/en/latest/" % (PROJECT_NAME.lower()),
])

write_usage_file(f, "insert_record_example.txt")

write_line_list(f, [
"For more information",
"--------------------",
"More examples are available at ",
"http://simplesqlite.readthedocs.org/en/latest/pages/examples/index.html",
"",
maker.write_chapter("Related project")
maker.write_line_list([
"- sqlitebiter: CLI tool to create a SQLite database from CSV/JSON/Excel/Google-Sheets by using SimpleSQLite",
" - https://github.com/thombashi/sqlitebiter"
])


def main():
with open(os.path.join(OUTPUT_DIR, "README.rst"), "w") as f:
write_line_list(f, [
"SimpleSQLite",
"=============",
"",
] + [
line.rstrip() for line in
open(os.path.join(
DOC_PAGE_DIR, "introduction", "badges.txt")).readlines()
])

write_line_list(f, [
"Summary",
"-------",
"",
] + [
line.rstrip() for line in
open(os.path.join(
DOC_PAGE_DIR, "introduction", "summary.txt")).readlines()
])

write_line_list(f, [
line.rstrip() for line in
open(os.path.join(
DOC_PAGE_DIR, "introduction", "feature.txt")).readlines()
])

write_examples(f)

write_line_list(f, [
line.rstrip() for line in
open(os.path.join(DOC_PAGE_DIR, "installation.rst")).readlines()
])

write_line_list(f, [
"Documentation",
"=============",
"",
"http://simplesqlite.readthedocs.org/en/latest/"
])

write_line_list(f, [
"Related project",
"==========================",
"",
"- sqlitebiter: CLI tool to create a SQLite database from CSV/JSON/Excel/Google-Sheets by using SimpleSQLite",
" - https://github.com/thombashi/sqlitebiter"
])
return 0


if __name__ == '__main__':
Expand Down
3 changes: 1 addition & 2 deletions docs/pages/reference/converter.rst
@@ -1,7 +1,6 @@
Convertor classes
Converter classes
----------------------------


RecordConvertor class
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down
14 changes: 14 additions & 0 deletions docs/pages/reference/error.rst
@@ -0,0 +1,14 @@
Errors
----------------------------

.. autoexception:: simplesqlite.NullDatabaseConnectionError
:show-inheritance:

.. autoexception:: simplesqlite.TableNotFoundError
:show-inheritance:

.. autoexception:: simplesqlite.AttributeNotFoundError
:show-inheritance:

.. autoexception:: simplesqlite.SqlSyntaxError
:show-inheritance:
6 changes: 6 additions & 0 deletions docs/pages/reference/function.rst
@@ -0,0 +1,6 @@
Functions
----------------------------

.. autofunction:: simplesqlite.append_table

.. autofunction:: simplesqlite.connect_sqlite_db_mem
3 changes: 2 additions & 1 deletion docs/pages/reference/index.rst
Expand Up @@ -4,9 +4,10 @@ Reference
.. toctree::
:maxdepth: 3

module
simplesqlite
sqlquery
loader
converter
function
error

7 changes: 0 additions & 7 deletions docs/pages/reference/module.rst

This file was deleted.

2 changes: 1 addition & 1 deletion setup.py
Expand Up @@ -19,7 +19,7 @@

setuptools.setup(
name="SimpleSQLite",
version="0.3.2",
version="0.3.3",
url="https://github.com/thombashi/SimpleSQLite",
bugtrack_url="https://github.com/thombashi/SimpleSQLite/issues",

Expand Down
115 changes: 7 additions & 108 deletions simplesqlite/__init__.py
Expand Up @@ -4,116 +4,15 @@
.. codeauthor:: Tsuyoshi Hombashi <gogogo.vm@gmail.com>
"""


from __future__ import absolute_import

import dataproperty

from .core import SimpleSQLite
import simplesqlite.loader


MEMORY_DB_NAME = ":memory:"


def validate_table_name(name):
"""
:param str name: Table name to validate.
:raises ValueError: |raises_validate_table_name|
"""

import re

if dataproperty.is_empty_string(name):
raise ValueError("table name is empty")

if re.search("^table$", name, re.IGNORECASE) is not None:
raise ValueError("invalid table name: " + name)


def append_table(con_src, con_dst, table_name):
"""
Append the table from source to destination.
:param SimpleSQLite con_src: Source of the database.
:param SimpleSQLite con_dst: Destination of the database.
:param str table_name: Table name to copy.
:return: |True| if success.
:rtype: bool
:raises simplesqlite.TableNotFoundError:
|raises_verify_table_existence|
:raises ValueError: If attribute of the table is different from each other.
.. seealso::
:py:meth:`simplesqlite.SimpleSQLite.create_table_with_data`
"""

con_src.verify_table_existence(table_name)
con_dst.validate_access_permission(["w", "a"])

if con_dst.has_table(table_name):
src_attr_list = con_src.get_attribute_name_list(table_name)
dst_attr_list = con_dst.get_attribute_name_list(table_name)
if src_attr_list != dst_attr_list:
raise ValueError("""
source and destination attribute is different from each other
src: %s
dst: %s
""" % (str(src_attr_list), str(dst_attr_list)))

result = con_src.select(select="*", table_name=table_name)
if result is None:
return False
value_matrix = result.fetchall()

con_dst.create_table_with_data(
table_name,
con_src.get_attribute_name_list(table_name),
value_matrix)

return True


def connect_sqlite_db_mem():
"""
:return: Instance of a in memory database.
:rtype: SimpleSQLite
:Examples:
.. code:: python
>>> import simplesqlite
>>> con = simplesqlite.connect_sqlite_db_mem()
>>> con.database_path
':memory:'
"""

return SimpleSQLite(MEMORY_DB_NAME, "w")


class NullDatabaseConnectionError(Exception):
"""
Raised when executing an operation of
:py:class:`~simplesqlite.SimpleSQLite` instance without connection to
a SQLite database file.
"""

pass


class TableNotFoundError(Exception):
"""
Raised when accessed the table that not exists in the database.
"""

pass


class AttributeNotFoundError(Exception):
"""
Raised when accessed the attribute that not exists in the table.
"""

pass
from .core import SimpleSQLite
from ._error import NullDatabaseConnectionError
from ._error import TableNotFoundError
from ._error import AttributeNotFoundError
from ._error import SqlSyntaxError
from ._func import append_table
from ._func import connect_sqlite_db_mem

0 comments on commit cf49181

Please sign in to comment.