ppss-pyramidutils


Nameppss-pyramidutils JSON
Version 1.6.1.3 PyPI version JSON
download
home_pageNone
SummarySimple utils to handle data from ini files in Pyramid for python 2.7 & 3
upload_time2024-05-02 09:21:02
maintainerNone
docs_urlNone
authorpdepmcp
requires_pythonNone
licenseNone
keywords pyramid module utils accelerator
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Overview

A collection of simple utils for common operations
This includes configuration item from .ini file, a filemanger for hadling file uploads, some utilities for SQLAlchemy models and a CSV reader/writer unicode-safe for python 2.7


### Configuration from .ini 

The class ppss_pyramidutils.Utils offers convenience options to get data from the main ini file, allowing default values.
The easiest way to use it is to subclass it with your conf class and override the __myconf__ property:

```python
from ppss_pyramidutils import Utils as IniUtils

class MyClass(IniUtils):
  myconf = ['param1','param2']
```

Then you can read ini file calling the class method __config__:

```python
MyClass.config(settings)
```

This method accetps other optional parameters: __prefix__ and __defaultval__.

If __prefix__ is not passed, lowered case class name is used instead (i.e.: myclass).
Ths config method use  all values in __myconf__ property and read in the ini file the parameter:
__prefix__.__value__

In this example it reads myclass.param1 and myclass.param2.
If a key in missing in the ini file, the corresponding class property will assume the value __defaultval__.

__myconf__ can be a tuple/list instead of a string. In this case the first value is the string, the second value is the default value only for that key.

A full example can work as follow:

```python
#ini file
myprefix.param1 = 12
myprefix.param2 = A simple String

from ppss_pyramidutils import Utils as IniUtils

class MyClass(IniUtils):
  myconf = ['param1','param2',('param3',"a missing value with a default"),"param4"]

MyClass.config(settings,"myprefix",defaultval="default")

MyClass.param1
# '12'
MyClass.param2
# 'A simple String'
MyClass.param3
# 'a missing value with a default'
MyClass.param4
# 'param4'

```

Because they are just class values, you can simply include your class anywhere in your code and access the class property required.
This will allow to read and set default for each value in a centralized place and to access it whenever and wherever needed.


### filemanger (for upload and other purposes)

__ppss_pyramidutils.FileManager__ extends __ppss_pyramidutils.Uitls__ setting __myconf__ attribute myconf = ['savepath','tmppath']
You can include the class in the ini of the pyramid app and call the __config__ method against it to properly config the class.

FileManager offer the following class methods:
* __saveToTmp(cls,requestfile)__ -> __file_path__: takes as argument the file object from the form, replace some chars in the filename and save it to a temp location (specified through ini file). This method returns the file path of the temporary file.
* __moveToDestination(cls,source,filename,subfolder="")__ -> __file_path__: this method takes a file path (tipically the return value of pthe revious saveToTmp call) and moves it in target folder (as of ini file), with the given __filename__. It can put it in a subfolder if __subfolder__ is specified. This will create the folder if required. Returns the complete path of the file.
* __deleteFile(cls,file)__ -> None : delete a file with the path __file__

It also has two commodity class methods:
* __sanitizeFilename(cls,filename, whitelist=_valid_filename_chars, replace=' ')__ -> __sanitizedfilename__: replaces all occurency of each char of __replace__ string with a "_", that removes all not allowed char (allowed chat by default are: -, \_, \., \(, \), __string.ascii_letters__ and __string.digits__ ). The method returns the sanitized file name. It is called automatically by saveToTmp to prevent attempts of injections in file system.
* __slugify(cls,filename)__ -> __sluggifiedfilename__: This method tries to convert an arbitrary file name in a sluggied string. 

### CSV reader/writer for python 2.7

Python 2.7 CSV reader and writer fail to address many unicode problems. 
You can simply use __ppss_pyramidutils.UnicodeReader__ and __ppss_pyramidutils.UnicodeWriter__ for this purpose.

Both __UnicodeReader__ and __UnicodeWriter__ .\_\_init\_\___ methods accept this parameters:
* __f__: the already opened file to read.
* __dialect__=csv.excel: the csv dialect to use. 
* __encoding__="utf-8-sig": encoding to use.

All other keyword arguments are passed on to the CSV reader (check the module in standard Python 2.7 documentation)

For conveninece and use as importer/export of CSV formatted data, two more classes are defined.

__Importer__ class can be initialized with this parameters:
* __fn__: File name of the source. It will be opened and passed to 
* __mapping__=None: allow to remap column names. If present the mapping param must be a dictionary-like object, containing all CSV column names as key, and the mapped names as values
* __delimiter__=",": the delimiter for the CSV input
* __headertransform__=None: a callable that receive each column name and may transform it. Usually used to do some sanitization on the names (ie: lower and remove/substitute forbiden chars)

Creating the __Importer__ object actually triggers the file opening and reading. To get the resulting rows as a list of dictionaries, you can use the method __getRows__.


__Exporter__ class takes this parameters when initialized:
* __evcollection__ : list-like object, where each item is a dictionary-like (see getter param) object.
* __titles__ : ordered list-like object, containing column names
* __parserows__ = True: if set, allow pre-processing of the input (__evcollection__), using __getter__ and __datetimeformat__ paramesters. 
* __delimiter__  = ',': the delimiter for the CSV output
* __getter__ = None: a callable to override the dictionary like way to get values. If set the getter will be called for each column with the item and name of the column (ie: val = getter(ev,k)). Only used if __parserows__ is True.
* __datetimeformat__ = "%Y-%m-%d" : formatter for datetime.datetime and datetime.date objects. Only used if __parserows__ is True.

This method set the property __retfile__ to a __tempfile.NamedTemporaryFile__ (set to writemode in Python 2)

the method __writeAll__ accept only a __delimiter__ param and actualy writes all rows in the __retfile__ property (by default a __tempfile.NamedTemporaryFile__ instance)


### Modelbase utilities


__ppss_pyramidutils.ModelCommonParent__ attach some commodity methods to derived SQLAlchemy classes, as class methods, like:

* all(cls,DBSession): returns all elements of the given class
* byId(cls,id,DBSession): returns the object with __id__ = id. COC: the class must have a id column.
* byField(cls,field,value,DBSession): like byId, on an arbitraty __field__
* byFields(cls,fields,DBSession): accept a list of filters as tuples of (columnname, value) and returns all items that matches
* delete(cls,element,DBSession): delete the element
* deleteAll(cls,elements,DBSession): delete all elements in elements list-like parameter.







__v1.6.1.3
fix "quote all" in exporter

__v1.6.1
added skip rows to jsonwalker

__v1.6.0
added cryptoutils and jsonutils

__v1.5.4.0
added custom header row to importer
__v1.5.3.2
imported names in base init
__v1.5.3.1
fix conf reader

__v1.5.2__
* New background thread in loop interface
* ini reader updated and refactored
__v1.5__
* added/refactored functions to have dbsessions for off-request activities
__v1.5__
* added utility for background jobs
__v1.4.4.5__
* fixed encoding of file in csv importer (now forced to utf-8)
* create tmp dir if not exists
__v1.4.4.4__
* addedd order by and order direction to modelbase commodity methods
__v1.4.4.3__
* fix in utf8csv change six unicode method to str method for py3 compatibility
__v1.4.4.2__
* fix in modelbase __str__ method for py3 compatibility
__1.4.3.1__
* added check on int/float before encoding for UTF-8 writer



            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "ppss-pyramidutils",
    "maintainer": null,
    "docs_url": null,
    "requires_python": null,
    "maintainer_email": null,
    "keywords": "pyramid module utils accelerator",
    "author": "pdepmcp",
    "author_email": "pdepmcp@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/25/cc/e29c5c81e32bbe31f6758e324c257ed08f0093404c6dfc9f0d0c99e46ccb/ppss_pyramidutils-1.6.1.3.tar.gz",
    "platform": null,
    "description": "# Overview\n\nA collection of simple utils for common operations\nThis includes configuration item from .ini file, a filemanger for hadling file uploads, some utilities for SQLAlchemy models and a CSV reader/writer unicode-safe for python 2.7\n\n\n### Configuration from .ini \n\nThe class ppss_pyramidutils.Utils offers convenience options to get data from the main ini file, allowing default values.\nThe easiest way to use it is to subclass it with your conf class and override the __myconf__ property:\n\n```python\nfrom ppss_pyramidutils import Utils as IniUtils\n\nclass MyClass(IniUtils):\n  myconf = ['param1','param2']\n```\n\nThen you can read ini file calling the class method __config__:\n\n```python\nMyClass.config(settings)\n```\n\nThis method accetps other optional parameters: __prefix__ and __defaultval__.\n\nIf __prefix__ is not passed, lowered case class name is used instead (i.e.: myclass).\nThs config method use  all values in __myconf__ property and read in the ini file the parameter:\n__prefix__.__value__\n\nIn this example it reads myclass.param1 and myclass.param2.\nIf a key in missing in the ini file, the corresponding class property will assume the value __defaultval__.\n\n__myconf__ can be a tuple/list instead of a string. In this case the first value is the string, the second value is the default value only for that key.\n\nA full example can work as follow:\n\n```python\n#ini file\nmyprefix.param1 = 12\nmyprefix.param2 = A simple String\n\nfrom ppss_pyramidutils import Utils as IniUtils\n\nclass MyClass(IniUtils):\n  myconf = ['param1','param2',('param3',\"a missing value with a default\"),\"param4\"]\n\nMyClass.config(settings,\"myprefix\",defaultval=\"default\")\n\nMyClass.param1\n# '12'\nMyClass.param2\n# 'A simple String'\nMyClass.param3\n# 'a missing value with a default'\nMyClass.param4\n# 'param4'\n\n```\n\nBecause they are just class values, you can simply include your class anywhere in your code and access the class property required.\nThis will allow to read and set default for each value in a centralized place and to access it whenever and wherever needed.\n\n\n### filemanger (for upload and other purposes)\n\n__ppss_pyramidutils.FileManager__ extends __ppss_pyramidutils.Uitls__ setting __myconf__ attribute myconf = ['savepath','tmppath']\nYou can include the class in the ini of the pyramid app and call the __config__ method against it to properly config the class.\n\nFileManager offer the following class methods:\n* __saveToTmp(cls,requestfile)__ -> __file_path__: takes as argument the file object from the form, replace some chars in the filename and save it to a temp location (specified through ini file). This method returns the file path of the temporary file.\n* __moveToDestination(cls,source,filename,subfolder=\"\")__ -> __file_path__: this method takes a file path (tipically the return value of pthe revious saveToTmp call) and moves it in target folder (as of ini file), with the given __filename__. It can put it in a subfolder if __subfolder__ is specified. This will create the folder if required. Returns the complete path of the file.\n* __deleteFile(cls,file)__ -> None : delete a file with the path __file__\n\nIt also has two commodity class methods:\n* __sanitizeFilename(cls,filename, whitelist=_valid_filename_chars, replace=' ')__ -> __sanitizedfilename__: replaces all occurency of each char of __replace__ string with a \"_\", that removes all not allowed char (allowed chat by default are: -, \\_, \\., \\(, \\), __string.ascii_letters__ and __string.digits__ ). The method returns the sanitized file name. It is called automatically by saveToTmp to prevent attempts of injections in file system.\n* __slugify(cls,filename)__ -> __sluggifiedfilename__: This method tries to convert an arbitrary file name in a sluggied string. \n\n### CSV reader/writer for python 2.7\n\nPython 2.7 CSV reader and writer fail to address many unicode problems. \nYou can simply use __ppss_pyramidutils.UnicodeReader__ and __ppss_pyramidutils.UnicodeWriter__ for this purpose.\n\nBoth __UnicodeReader__ and __UnicodeWriter__ .\\_\\_init\\_\\___ methods accept this parameters:\n* __f__: the already opened file to read.\n* __dialect__=csv.excel: the csv dialect to use. \n* __encoding__=\"utf-8-sig\": encoding to use.\n\nAll other keyword arguments are passed on to the CSV reader (check the module in standard Python 2.7 documentation)\n\nFor conveninece and use as importer/export of CSV formatted data, two more classes are defined.\n\n__Importer__ class can be initialized with this parameters:\n* __fn__: File name of the source. It will be opened and passed to \n* __mapping__=None: allow to remap column names. If present the mapping param must be a dictionary-like object, containing all CSV column names as key, and the mapped names as values\n* __delimiter__=\",\": the delimiter for the CSV input\n* __headertransform__=None: a callable that receive each column name and may transform it. Usually used to do some sanitization on the names (ie: lower and remove/substitute forbiden chars)\n\nCreating the __Importer__ object actually triggers the file opening and reading. To get the resulting rows as a list of dictionaries, you can use the method __getRows__.\n\n\n__Exporter__ class takes this parameters when initialized:\n* __evcollection__ : list-like object, where each item is a dictionary-like (see getter param) object.\n* __titles__ : ordered list-like object, containing column names\n* __parserows__ = True: if set, allow pre-processing of the input (__evcollection__), using __getter__ and __datetimeformat__ paramesters. \n* __delimiter__  = ',': the delimiter for the CSV output\n* __getter__ = None: a callable to override the dictionary like way to get values. If set the getter will be called for each column with the item and name of the column (ie: val = getter(ev,k)). Only used if __parserows__ is True.\n* __datetimeformat__ = \"%Y-%m-%d\" : formatter for datetime.datetime and datetime.date objects. Only used if __parserows__ is True.\n\nThis method set the property __retfile__ to a __tempfile.NamedTemporaryFile__ (set to writemode in Python 2)\n\nthe method __writeAll__ accept only a __delimiter__ param and actualy writes all rows in the __retfile__ property (by default a __tempfile.NamedTemporaryFile__ instance)\n\n\n### Modelbase utilities\n\n\n__ppss_pyramidutils.ModelCommonParent__ attach some commodity methods to derived SQLAlchemy classes, as class methods, like:\n\n* all(cls,DBSession): returns all elements of the given class\n* byId(cls,id,DBSession): returns the object with __id__ = id. COC: the class must have a id column.\n* byField(cls,field,value,DBSession): like byId, on an arbitraty __field__\n* byFields(cls,fields,DBSession): accept a list of filters as tuples of (columnname, value) and returns all items that matches\n* delete(cls,element,DBSession): delete the element\n* deleteAll(cls,elements,DBSession): delete all elements in elements list-like parameter.\n\n\n\n\n\n\n\n__v1.6.1.3\nfix \"quote all\" in exporter\n\n__v1.6.1\nadded skip rows to jsonwalker\n\n__v1.6.0\nadded cryptoutils and jsonutils\n\n__v1.5.4.0\nadded custom header row to importer\n__v1.5.3.2\nimported names in base init\n__v1.5.3.1\nfix conf reader\n\n__v1.5.2__\n* New background thread in loop interface\n* ini reader updated and refactored\n__v1.5__\n* added/refactored functions to have dbsessions for off-request activities\n__v1.5__\n* added utility for background jobs\n__v1.4.4.5__\n* fixed encoding of file in csv importer (now forced to utf-8)\n* create tmp dir if not exists\n__v1.4.4.4__\n* addedd order by and order direction to modelbase commodity methods\n__v1.4.4.3__\n* fix in utf8csv change six unicode method to str method for py3 compatibility\n__v1.4.4.2__\n* fix in modelbase __str__ method for py3 compatibility\n__1.4.3.1__\n* added check on int/float before encoding for UTF-8 writer\n\n\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "Simple utils to handle data from ini files in Pyramid for python 2.7 & 3",
    "version": "1.6.1.3",
    "project_urls": null,
    "split_keywords": [
        "pyramid",
        "module",
        "utils",
        "accelerator"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "25cce29c5c81e32bbe31f6758e324c257ed08f0093404c6dfc9f0d0c99e46ccb",
                "md5": "41cd58c1a28c73882a122f805c51848d",
                "sha256": "54a47a2c7533b08d5a6aaae94683dda5ad9d1c3e87f155ddb4ad7c068554322e"
            },
            "downloads": -1,
            "filename": "ppss_pyramidutils-1.6.1.3.tar.gz",
            "has_sig": false,
            "md5_digest": "41cd58c1a28c73882a122f805c51848d",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 16003,
            "upload_time": "2024-05-02T09:21:02",
            "upload_time_iso_8601": "2024-05-02T09:21:02.780929Z",
            "url": "https://files.pythonhosted.org/packages/25/cc/e29c5c81e32bbe31f6758e324c257ed08f0093404c6dfc9f0d0c99e46ccb/ppss_pyramidutils-1.6.1.3.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-05-02 09:21:02",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "lcname": "ppss-pyramidutils"
}
        
Elapsed time: 0.26157s